KiCad PCB EDA Suite
string.cpp File Reference

Some useful functions to handle strings. More...

#include <fctsys.h>
#include <macros.h>
#include <richio.h>
#include <kicad_string.h>

Go to the source code of this file.

Functions

wxString EscapeString (const wxString &aSource)
 These Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are: (a) not legal in filenames (b) used as control characters in LIB_IDs (c) used to delineate hierarchical paths. More...
 
wxString UnescapeString (const wxString &aSource)
 
int ReadDelimitedText (wxString *aDest, const char *aSource)
 Copy bytes from aSource delimited string segment to aDest wxString. More...
 
int ReadDelimitedText (char *aDest, const char *aSource, int aDestSize)
 Copy bytes from aSource delimited string segment to aDest buffer. More...
 
std::string EscapedUTF8 (const wxString &aString)
 Function EscapedUTF8 returns an 8 bit UTF8 string given aString in unicode form. More...
 
wxString EscapedHTML (const wxString &aString)
 Return a new wxString escaped for embedding in HTML. More...
 
char * StrPurge (char *text)
 Remove leading and training spaces, tabs and end of line chars in text. More...
 
char * GetLine (FILE *File, char *Line, int *LineNum, int SizeLine)
 Read one line line from aFile. More...
 
wxString DateAndTime ()
 
int StrNumCmp (const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
 Compare two strings with alphanumerical content. More...
 
bool WildCompareString (const wxString &pattern, const wxString &string_to_tst, bool case_sensitive)
 Compare a string against wild card (* and ?) pattern using the usual rules. More...
 
bool ApplyModifier (double &value, const wxString &aString)
 
int ValueStringCompare (wxString strFWord, wxString strSWord)
 Compare strings like the strcmp function but handle numbers and modifiers within the string text correctly for sorting. More...
 
int SplitString (wxString strToSplit, wxString *strBeginning, wxString *strDigits, wxString *strEnd)
 Breaks a string into three parts: he alphabetic preamble, the numeric part, and any alphabetic ending. More...
 
int GetTrailingInt (const wxString &aStr)
 Gets the trailing int, if any, from a string. More...
 
wxString GetIllegalFileNameWxChars ()
 
bool ReplaceIllegalFileNameChars (std::string *aName, int aReplaceChar)
 Checks aName for illegal file name characters. More...
 
bool ReplaceIllegalFileNameChars (wxString &aName, int aReplaceChar)
 

Variables

static const char illegalFileNameChars [] = "\\/:\"<>|"
 Illegal file name characters used to insure file names will be valid on all supported platforms. More...
 

Detailed Description

Some useful functions to handle strings.

Definition in file string.cpp.

Function Documentation

◆ ApplyModifier()

bool ApplyModifier ( double &  value,
const wxString &  aString 
)

Definition at line 452 of file string.cpp.

453 {
454  static const wxString modifiers( wxT( "pnumkKM" ) );
455 
456  if( !aString.length() )
457  return false;
458 
459  wxChar modifier;
460  wxString units;
461 
462  if( modifiers.Find( aString[ 0 ] ) >= 0 )
463  {
464  modifier = aString[ 0 ];
465  units = aString.Mid( 1 ).Trim();
466  }
467  else
468  {
469  modifier = ' ';
470  units = aString.Mid( 0 ).Trim();
471  }
472 
473  if( units.length()
474  && !units.CmpNoCase( wxT( "F" ) )
475  && !units.CmpNoCase( wxT( "hz" ) )
476  && !units.CmpNoCase( wxT( "W" ) )
477  && !units.CmpNoCase( wxT( "V" ) )
478  && !units.CmpNoCase( wxT( "H" ) ) )
479  return false;
480 
481  if( modifier == 'p' )
482  value *= 1.0e-12;
483  if( modifier == 'n' )
484  value *= 1.0e-9;
485  else if( modifier == 'u' )
486  value *= 1.0e-6;
487  else if( modifier == 'm' )
488  value *= 1.0e-3;
489  else if( modifier == 'k' || modifier == 'K' )
490  value *= 1.0e3;
491  else if( modifier == 'M' )
492  value *= 1.0e6;
493  else if( modifier == 'G' )
494  value *= 1.0e9;
495 
496  return true;
497 }

Referenced by ValueStringCompare().

◆ DateAndTime()

wxString DateAndTime ( )
Returns
a string giving the current date and time.

Definition at line 304 of file string.cpp.

305 {
306  wxDateTime datetime = wxDateTime::Now();
307 
308  datetime.SetCountry( wxDateTime::Country_Default );
309  return datetime.Format( wxDefaultDateTimeFormat, wxDateTime::Local );
310 }

Referenced by PROJECT::ConfigSave(), PCB_EDIT_FRAME::DoGenFootprintsPositionFile(), PCB_EDIT_FRAME::DoGenFootprintsReport(), GENDRILL_WRITER_BASE::GenDrillReportFile(), NETLIST_EXPORTER_GENERIC::makeDesignHeader(), RecreateCmpFile(), WriteDiagnosticERC(), EXCELLON_WRITER::writeEXCELLONHeader(), NETLIST_EXPORTER_ORCADPCB2::WriteNetlist(), and NETLIST_EXPORTER_CADSTAR::WriteNetlist().

◆ EscapedHTML()

wxString EscapedHTML ( const wxString &  aString)

Return a new wxString escaped for embedding in HTML.

Definition at line 245 of file string.cpp.

246 {
247  wxString converted;
248 
249  for( wxUniChar c: aString )
250  {
251  if( c == '\"' )
252  converted += "&quot;";
253  else if( c == '\'' )
254  converted += "&apos;";
255  else if( c == '&' )
256  converted += "&amp;";
257  else if( c == '<' )
258  converted += "&lt;";
259  else if( c == '>' )
260  converted += "&gt;";
261  else
262  converted += c;
263  }
264 
265  return converted;
266 }

Referenced by FOOTPRINT_INFO_GENERATOR::GenerateHtml(), FOOTPRINT_INFO_GENERATOR::GetHtmlFieldRow(), FOOTPRINT_INFO_GENERATOR::SetHtmlAliasOf(), FOOTPRINT_INFO_GENERATOR::SetHtmlDesc(), FOOTPRINT_INFO_GENERATOR::SetHtmlKeywords(), and FOOTPRINT_INFO_GENERATOR::SetHtmlName().

◆ EscapedUTF8()

std::string EscapedUTF8 ( const wxString &  aString)

Function EscapedUTF8 returns an 8 bit UTF8 string given aString in unicode form.

Any double quoted or back slashes are prefixed with a '\' byte and the form of this UTF8 byte string is compatible with function ReadDelimitedText().

Parameters
aStringis the input string to convert.
Returns
std::string - the escaped input text, without the wrapping double quotes.

Definition at line 212 of file string.cpp.

213 {
214  std::string utf8 = TO_UTF8( aString );
215 
216  std::string ret;
217 
218  ret += '"';
219 
220  for( std::string::const_iterator it = utf8.begin(); it!=utf8.end(); ++it )
221  {
222  // this escaping strategy is designed to be compatible with ReadDelimitedText():
223  if( *it == '"' )
224  {
225  ret += '\\';
226  ret += '"';
227  }
228  else if( *it == '\\' )
229  {
230  ret += '\\'; // double it up
231  ret += '\\';
232  }
233  else
234  {
235  ret += *it;
236  }
237  }
238 
239  ret += '"';
240 
241  return ret;
242 }
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:47

References TO_UTF8.

Referenced by PCB_EDIT_FRAME::DoGenFootprintsReport(), SCH_LEGACY_PLUGIN::Format(), SCH_LEGACY_PLUGIN::saveField(), SCH_LEGACY_PLUGIN_CACHE::saveField(), LEGACY_PLUGIN::SaveModule3D(), and SCH_LEGACY_PLUGIN::saveSheet().

◆ EscapeString()

wxString EscapeString ( const wxString &  aSource)

These Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are: (a) not legal in filenames (b) used as control characters in LIB_IDs (c) used to delineate hierarchical paths.

Definition at line 43 of file string.cpp.

44 {
45 #if 1
46  return aSource;
47 #else
48  wxString converted;
49 
50  for( wxUniChar c: aSource )
51  {
52  if( c == '\"' )
53  converted += "&quot;";
54  else if( c == '\'' )
55  converted += "&apos;";
56  else if( c == '&' )
57  converted += "&amp;";
58  else if( c == '<' )
59  converted += "&lt;";
60  else if( c == '>' )
61  converted += "&gt;";
62  else if( c == '\\' )
63  converted += "&Backslash;";
64  else if( c == '/' )
65  converted += "&frasl;";
66  else if( c == '|' )
67  converted += "&verbar;";
68  else if( c == ':' )
69  converted += "&colon;";
70  else if( c == ' ' )
71  converted += "&nbsp;";
72  else if( c == '%' )
73  converted += "&percnt;";
74  else if( c == '$' )
75  converted += "&dollar;";
76  else if( c == '\t' )
77  converted += "&tab;";
78  else if( c == '\n' || c == '\r' )
79  converted += "&Newline;";
80  else
81  converted += c;
82  }
83 
84  return converted;
85 #endif
86 }

Referenced by FIELDS_EDITOR_GRID_DATA_MODEL::SetValue(), DIALOG_EDIT_ONE_FIELD::TransferDataFromWindow(), and FOOTPRINT_LIST_IMPL::WriteCacheToFile().

◆ GetIllegalFileNameWxChars()

wxString GetIllegalFileNameWxChars ( )
Returns
a wxString object containing the illegal file name characters for all platforms.

Definition at line 633 of file string.cpp.

634 {
636 }
static const char illegalFileNameChars[]
Illegal file name characters used to insure file names will be valid on all supported platforms.
Definition: string.cpp:40
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:53

References FROM_UTF8(), and illegalFileNameChars.

Referenced by ReplaceIllegalFileNameChars().

◆ GetLine()

char* GetLine ( FILE *  aFile,
char *  Line,
int *  LineNum = NULL,
int  SizeLine = 255 
)

Read one line line from aFile.

Returns
a pointer the first useful line read by eliminating blank lines and comments.

Definition at line 288 of file string.cpp.

289 {
290  do {
291  if( fgets( Line, SizeLine, File ) == NULL )
292  return NULL;
293 
294  if( LineNum )
295  *LineNum += 1;
296 
297  } while( Line[0] == '#' || Line[0] == '\n' || Line[0] == '\r' || Line[0] == 0 );
298 
299  strtok( Line, "\n\r" );
300  return Line;
301 }

Referenced by CVPCB_MAINFRAME::buildEquivalenceList().

◆ GetTrailingInt()

int GetTrailingInt ( const wxString &  aStr)

Gets the trailing int, if any, from a string.

Parameters
aStrthe string to check
Returns
the trailing int or 0 if none found

Definition at line 609 of file string.cpp.

610 {
611  int number = 0;
612  int base = 1;
613 
614  // Trim and extract the trailing numeric part
615  int index = aStr.Len() - 1;
616 
617  while( index >= 0 )
618  {
619  const char chr = aStr.GetChar( index );
620 
621  if( chr < '0' || chr > '9' )
622  break;
623 
624  number += ( chr - '0' ) * base;
625  base *= 10;
626  index--;
627  }
628 
629  return number;
630 }

Referenced by MODULE::GetNextPadName(), and MODULE::IncrementReference().

◆ ReadDelimitedText() [1/2]

int ReadDelimitedText ( wxString *  aDest,
const char *  aSource 
)

Copy bytes from aSource delimited string segment to aDest wxString.

Parameters
aDestis the destination wxString
aSourceis the source C string holding utf8 encoded bytes.
Returns
int - the number of bytes read from source, which may be more than the number copied, due to escaping of double quotes and the escape byte itself.

Definition at line 118 of file string.cpp.

119 {
120  std::string utf8; // utf8 but without escapes and quotes.
121  bool inside = false;
122  const char* start = aSource;
123  char cc;
124 
125  while( (cc = *aSource++) != 0 )
126  {
127  if( cc == '"' )
128  {
129  if( inside )
130  break; // 2nd double quote is end of delimited text
131 
132  inside = true; // first delimiter found, make note, do not copy
133  }
134 
135  else if( inside )
136  {
137  if( cc == '\\' )
138  {
139  cc = *aSource++;
140 
141  if( !cc )
142  break;
143 
144  // do no copy the escape byte if it is followed by \ or "
145  if( cc != '"' && cc != '\\' )
146  utf8 += '\\';
147 
148  utf8 += cc;
149  }
150  else
151  {
152  utf8 += cc;
153  }
154  }
155  }
156 
157  *aDest = FROM_UTF8( utf8.c_str() );
158 
159  return aSource - start;
160 }
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:53

References FROM_UTF8().

Referenced by LEGACY_PLUGIN::load3D(), LEGACY_PLUGIN::loadDIMENSION(), LEGACY_PLUGIN::loadMODULE_TEXT(), LEGACY_PLUGIN::loadNETCLASS(), LEGACY_PLUGIN::loadNETINFO_ITEM(), LEGACY_PLUGIN::loadPAD(), LEGACY_PLUGIN::loadPCB_TEXT(), LEGACY_PLUGIN::loadSHEET(), and LEGACY_PLUGIN::loadZONE_CONTAINER().

◆ ReadDelimitedText() [2/2]

int ReadDelimitedText ( char *  aDest,
const char *  aSource,
int  aDestSize 
)

Copy bytes from aSource delimited string segment to aDest buffer.

The extracted string will be null terminated even if truncation is necessary because aDestSize was not large enough.

Parameters
aDestis the destination byte buffer.
aSourceis the source bytes as a C string.
aDestSizeis the size of the destination byte buffer.
Returns
int - the number of bytes read from source, which may be more than the number copied, due to escaping of double quotes and the escape byte itself.
Deprecated:
should use the one which fetches a wxString, below.

Definition at line 163 of file string.cpp.

164 {
165  if( aDestSize <= 0 )
166  return 0;
167 
168  bool inside = false;
169  const char* start = aSource;
170  char* limit = aDest + aDestSize - 1;
171  char cc;
172 
173  while( (cc = *aSource++) != 0 && aDest < limit )
174  {
175  if( cc == '"' )
176  {
177  if( inside )
178  break; // 2nd double quote is end of delimited text
179 
180  inside = true; // first delimiter found, make note, do not copy
181  }
182 
183  else if( inside )
184  {
185  if( cc == '\\' )
186  {
187  cc = *aSource++;
188 
189  if( !cc )
190  break;
191 
192  // do no copy the escape byte if it is followed by \ or "
193  if( cc != '"' && cc != '\\' )
194  *aDest++ = '\\';
195 
196  if( aDest < limit )
197  *aDest++ = cc;
198  }
199  else
200  {
201  *aDest++ = cc;
202  }
203  }
204  }
205 
206  *aDest = 0;
207 
208  return aSource - start;
209 }

◆ ReplaceIllegalFileNameChars() [1/2]

bool ReplaceIllegalFileNameChars ( std::string *  aName,
int  aReplaceChar = 0 
)

Checks aName for illegal file name characters.

The Windows (DOS) file system forbidden characters already include the forbidden file name characters for both Posix and OSX systems. The characters \/?*|"<> are illegal and are replaced with xx where xx the hexadecimal equivalent of the replaced character. This replacement may not be as elegant as using an underscore ('_') or hyphen ('-') but it guarantees that there will be no naming conflicts when fixing footprint library names. however, if aReplaceChar is given, it will replace the illegal chars

Parameters
aNameis a point to a std::string object containing the footprint name to verify.
aReplaceChar(if not 0) is the replacement char.
Returns
true if any characters have been replaced in aName.

Definition at line 639 of file string.cpp.

640 {
641  bool changed = false;
642  std::string result;
643  result.reserve( aName->length() );
644 
645  for( std::string::iterator it = aName->begin(); it != aName->end(); ++it )
646  {
647  if( strchr( illegalFileNameChars, *it ) )
648  {
649  if( aReplaceChar )
650  StrPrintf( &result, "%c", aReplaceChar );
651  else
652  StrPrintf( &result, "%%%02x", *it );
653 
654  changed = true;
655  }
656  else
657  {
658  result += *it;
659  }
660  }
661 
662  if( changed )
663  *aName = result;
664 
665  return changed;
666 }
static const char illegalFileNameChars[]
Illegal file name characters used to insure file names will be valid on all supported platforms.
Definition: string.cpp:40
int StrPrintf(std::string *aResult, const char *aFormat,...)
Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a char...
Definition: richio.cpp:74

References illegalFileNameChars, and numEval::StrPrintf().

Referenced by EDEVICE::EDEVICE(), EELEMENT::EELEMENT(), LEGACY_PLUGIN::loadAllSections(), EAGLE_PLUGIN::loadLibrary(), LP_CACHE::LoadModules(), and SCH_EAGLE_PLUGIN::loadSheet().

◆ ReplaceIllegalFileNameChars() [2/2]

bool ReplaceIllegalFileNameChars ( wxString &  aName,
int  aReplaceChar 
)

Definition at line 669 of file string.cpp.

670 {
671  bool changed = false;
672  wxString result;
673  result.reserve( aName.Length() );
674  wxString illWChars = GetIllegalFileNameWxChars();
675 
676  for( wxString::iterator it = aName.begin(); it != aName.end(); ++it )
677  {
678  if( illWChars.Find( *it ) != wxNOT_FOUND )
679  {
680  if( aReplaceChar )
681  result += aReplaceChar;
682  else
683  result += wxString::Format( "%%%02x", *it );
684 
685  changed = true;
686  }
687  else
688  {
689  result += *it;
690  }
691  }
692 
693  if( changed )
694  aName = result;
695 
696  return changed;
697 }
wxString GetIllegalFileNameWxChars()
Definition: string.cpp:633
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205

References Format(), and GetIllegalFileNameWxChars().

◆ SplitString()

int SplitString ( wxString  strToSplit,
wxString *  strBeginning,
wxString *  strDigits,
wxString *  strEnd 
)

Breaks a string into three parts: he alphabetic preamble, the numeric part, and any alphabetic ending.

For example C10A is split to C 10 A

Definition at line 548 of file string.cpp.

552 {
553  static const wxString separators( wxT( ".," ) );
554 
555  // Clear all the return strings
556  strBeginning->Empty();
557  strDigits->Empty();
558  strEnd->Empty();
559 
560  // There no need to do anything if the string is empty
561  if( strToSplit.length() == 0 )
562  return 0;
563 
564  // Starting at the end of the string look for the first digit
565  int ii;
566 
567  for( ii = (strToSplit.length() - 1); ii >= 0; ii-- )
568  {
569  if( wxIsdigit( strToSplit[ii] ) )
570  break;
571  }
572 
573  // If there were no digits then just set the single string
574  if( ii < 0 )
575  {
576  *strBeginning = strToSplit;
577  }
578  else
579  {
580  // Since there is at least one digit this is the trailing string
581  *strEnd = strToSplit.substr( ii + 1 );
582 
583  // Go to the end of the digits
584  int position = ii + 1;
585 
586  for( ; ii >= 0; ii-- )
587  {
588  if( !wxIsdigit( strToSplit[ii] ) && separators.Find( strToSplit[ii] ) < 0 )
589  break;
590  }
591 
592  // If all that was left was digits, then just set the digits string
593  if( ii < 0 )
594  *strDigits = strToSplit.substr( 0, position );
595 
596  /* We were only looking for the last set of digits everything else is
597  * part of the preamble */
598  else
599  {
600  *strDigits = strToSplit.substr( ii + 1, position - ii - 1 );
601  *strBeginning = strToSplit.substr( 0, ii + 1 );
602  }
603  }
604 
605  return 0;
606 }

Referenced by UTIL::RefDesStringCompare(), and ValueStringCompare().

◆ StrNumCmp()

int StrNumCmp ( const wxString &  aString1,
const wxString &  aString2,
bool  aIgnoreCase = false 
)

Compare two strings with alphanumerical content.

This function is equivalent to strncmp() or strncasecmp() if aIgnoreCase is true except that strings containing numbers are compared by their integer value not by their ASCII code. In other words U10 would be greater than U2.

Parameters
aString1A wxString reference to the reference string.
aString2A wxString reference to the comparison string.
aIgnoreCaseUse true to make the comparison case insensitive.
Returns
An integer value of -1 if aString1 is less than aString2, 0 if aString1 is equal to aString2, or 1 if aString1 is greater than aString2.

Definition at line 313 of file string.cpp.

314 {
315  int nb1 = 0, nb2 = 0;
316 
317  auto str1 = aString1.begin();
318  auto str2 = aString2.begin();
319 
320  while( str1 != aString1.end() && str2 != aString2.end() )
321  {
322  wxUniChar c1 = *str1;
323  wxUniChar c2 = *str2;
324 
325  if( wxIsdigit( c1 ) && wxIsdigit( c2 ) ) // Both characters are digits, do numeric compare.
326  {
327  nb1 = 0;
328  nb2 = 0;
329 
330  do
331  {
332  c1 = *str1;
333  nb1 = nb1 * 10 + (int) c1 - '0';
334  ++str1;
335  } while( str1 != aString1.end() && wxIsdigit( *str1 ) );
336 
337  do
338  {
339  c2 = *str2;
340  nb2 = nb2 * 10 + (int) c2 - '0';
341  ++str2;
342  } while( str2 != aString2.end() && wxIsdigit( *str2 ) );
343 
344  if( nb1 < nb2 )
345  return -1;
346 
347  if( nb1 > nb2 )
348  return 1;
349 
350  c1 = ( str1 != aString1.end() ) ? *str1 : wxUniChar( 0 );
351  c2 = ( str2 != aString2.end() ) ? *str2 : wxUniChar( 0 );
352  }
353 
354  // Any numerical comparisons to here are identical.
355  if( aIgnoreCase )
356  {
357  if( wxToupper( c1 ) < wxToupper( c2 ) )
358  return -1;
359 
360  if( wxToupper( c1 ) > wxToupper( c2 ) )
361  return 1;
362  }
363  else
364  {
365  if( c1 < c2 )
366  return -1;
367 
368  if( c1 > c2 )
369  return 1;
370  }
371 
372  if( str1 != aString1.end() )
373  ++str1;
374 
375  if( str2 != aString2.end() )
376  ++str2;
377  }
378 
379  if( str1 == aString1.end() && str2 != aString2.end() )
380  {
381  return -1; // Identical to here but aString1 is longer.
382  }
383  else if( str1 != aString1.end() && str2 == aString2.end() )
384  {
385  return 1; // Identical to here but aString2 is longer.
386  }
387 
388  return 0;
389 }

Referenced by LIB_TREE_NODE::AssignIntrinsicRanks(), PIN_TABLE_DATA_MODEL::compare(), FP_TREE_MODEL_ADAPTER::getFootprints(), myCompareFunction(), operator<(), sortFPlist(), and FP_TREE_SYNCHRONIZING_ADAPTER::updateLibrary().

◆ StrPurge()

char* StrPurge ( char *  text)

Remove leading and training spaces, tabs and end of line chars in text.

Returns
a pointer on the first n char in text

Definition at line 269 of file string.cpp.

270 {
271  static const char whitespace[] = " \t\n\r\f\v";
272 
273  if( text )
274  {
275  while( *text && strchr( whitespace, *text ) )
276  ++text;
277 
278  char* cp = text + strlen( text ) - 1;
279 
280  while( cp >= text && strchr( whitespace, *cp ) )
281  *cp-- = '\0';
282  }
283 
284  return text;
285 }

Referenced by detect_file_type(), LEGACY_PLUGIN::loadAllSections(), EXCELLON_IMAGE::LoadFile(), GERBER_FILE_IMAGE::LoadGerberFile(), LEGACY_PLUGIN::loadMODULE(), LP_CACHE::LoadModules(), LEGACY_NETLIST_READER::LoadNetlist(), and LEGACY_PLUGIN::loadPAD().

◆ UnescapeString()

wxString UnescapeString ( const wxString &  aSource)

Definition at line 89 of file string.cpp.

90 {
91 #if 1
92  return aSource;
93 #else
94  wxString converted = aSource;
95 
96  converted.Replace( "&quot;", "\"" );
97  converted.Replace( "&apos;", "'" );
98  converted.Replace( "&lt;", "<" );
99  converted.Replace( "&gt;", ">" );
100  converted.Replace( "&Backslash;", "\\" );
101  converted.Replace( "&frasl;", "/" );
102  converted.Replace( "&verbar;", "|" );
103  converted.Replace( "&colon;", ":" );
104  converted.Replace( "&nbsp;", " " );
105  converted.Replace( "&percnt;", "%" );
106  converted.Replace( "&dollar;", "$" );
107  converted.Replace( "&tab;", "\t" );
108  converted.Replace( "&Newline;", "\n" );
109 
110  // must be done last
111  converted.Replace( "&amp;", "&" );
112 
113  return converted;
114 #endif
115 }

Referenced by FIELDS_EDITOR_GRID_DATA_MODEL::GetValue(), FOOTPRINT_LIST_IMPL::ReadCacheFromFile(), UTIL::RefDesStringCompare(), DIALOG_EDIT_ONE_FIELD::TransferDataToWindow(), and ValueStringCompare().

◆ ValueStringCompare()

int ValueStringCompare ( wxString  strFWord,
wxString  strSWord 
)

Compare strings like the strcmp function but handle numbers and modifiers within the string text correctly for sorting.

eg. 1mF > 55uF

Returns
-1 if first string is less than the second, 0 if the strings are equal, or 1 if the first string is greater than the second.

Definition at line 500 of file string.cpp.

501 {
502  // Compare unescaped text
503  strFWord = UnescapeString( strFWord );
504  strSWord = UnescapeString( strSWord );
505 
506  // The different sections of the two strings
507  wxString strFWordBeg, strFWordMid, strFWordEnd;
508  wxString strSWordBeg, strSWordMid, strSWordEnd;
509 
510  // Split the two strings into separate parts
511  SplitString( strFWord, &strFWordBeg, &strFWordMid, &strFWordEnd );
512  SplitString( strSWord, &strSWordBeg, &strSWordMid, &strSWordEnd );
513 
514  // Compare the Beginning section of the strings
515  int isEqual = strFWordBeg.CmpNoCase( strSWordBeg );
516 
517  if( isEqual > 0 )
518  return 1;
519  else if( isEqual < 0 )
520  return -1;
521  else
522  {
523  // If the first sections are equal compare their digits
524  double lFirstNumber = 0;
525  double lSecondNumber = 0;
526  bool endingIsModifier = false;
527 
528  strFWordMid.ToDouble( &lFirstNumber );
529  strSWordMid.ToDouble( &lSecondNumber );
530 
531  endingIsModifier |= ApplyModifier( lFirstNumber, strFWordEnd );
532  endingIsModifier |= ApplyModifier( lSecondNumber, strSWordEnd );
533 
534  if( lFirstNumber > lSecondNumber )
535  return 1;
536  else if( lFirstNumber < lSecondNumber )
537  return -1;
538  // If the first two sections are equal and the endings are modifiers then compare them
539  else if( !endingIsModifier )
540  return strFWordEnd.CmpNoCase( strSWordEnd );
541  // Ran out of things to compare; they must match
542  else
543  return 0;
544  }
545 }
int SplitString(wxString strToSplit, wxString *strBeginning, wxString *strDigits, wxString *strEnd)
Breaks a string into three parts: he alphabetic preamble, the numeric part, and any alphabetic ending...
Definition: string.cpp:548
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:89
bool ApplyModifier(double &value, const wxString &aString)
Definition: string.cpp:452

References ApplyModifier(), SplitString(), and UnescapeString().

Referenced by FIELDS_EDITOR_GRID_DATA_MODEL::cmp().

◆ WildCompareString()

bool WildCompareString ( const wxString &  pattern,
const wxString &  string_to_tst,
bool  case_sensitive = true 
)

Compare a string against wild card (* and ?) pattern using the usual rules.

Returns
true if pattern matched otherwise false.

Definition at line 392 of file string.cpp.

394 {
395  const wxChar* cp = NULL, * mp = NULL;
396  const wxChar* wild, * string;
397  wxString _pattern, _string_to_tst;
398 
399  if( case_sensitive )
400  {
401  wild = pattern.GetData();
402  string = string_to_tst.GetData();
403  }
404  else
405  {
406  _pattern = pattern;
407  _pattern.MakeUpper();
408  _string_to_tst = string_to_tst;
409  _string_to_tst.MakeUpper();
410  wild = _pattern.GetData();
411  string = _string_to_tst.GetData();
412  }
413 
414  while( ( *string ) && ( *wild != '*' ) )
415  {
416  if( ( *wild != *string ) && ( *wild != '?' ) )
417  return false;
418 
419  wild++; string++;
420  }
421 
422  while( *string )
423  {
424  if( *wild == '*' )
425  {
426  if( !*++wild )
427  return 1;
428  mp = wild;
429  cp = string + 1;
430  }
431  else if( ( *wild == *string ) || ( *wild == '?' ) )
432  {
433  wild++;
434  string++;
435  }
436  else
437  {
438  wild = mp;
439  string = cp++;
440  }
441  }
442 
443  while( *wild == '*' )
444  {
445  wild++;
446  }
447 
448  return !*wild;
449 }

Referenced by DIALOG_EXCHANGE_FOOTPRINTS::isMatch(), PCB_EDIT_FRAME::LockModule(), DIALOG_FIND::onButtonFindItemClick(), and DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS::visitItem().

Variable Documentation

◆ illegalFileNameChars

const char illegalFileNameChars[] = "\\/:\"<>|"
static

Illegal file name characters used to insure file names will be valid on all supported platforms.

This is the list of illegal file name characters for Windows which includes the illegal file name characters for Linux and OSX.

Definition at line 40 of file string.cpp.

Referenced by GetIllegalFileNameWxChars(), and ReplaceIllegalFileNameChars().