KiCad PCB EDA Suite
kicad_string.h File Reference
#include "config.h"
#include <string>
#include <vector>
#include <wx/string.h>
#include <wx/filename.h>

Go to the source code of this file.

Classes

struct  rsort_wxString
 A helper for sorting strings from the rear. More...
 

Enumerations

enum  ESCAPE_CONTEXT {
  CTX_NETNAME, CTX_LIBID, CTX_QUOTED_STR, CTX_LINE,
  CTX_FILENAME
}
 Escape/Unescape routines to safely encode reserved-characters in various contexts. More...
 

Functions

bool ConvertSmartQuotesAndDashes (wxString *aString)
 Converts curly quotes and em/en dashes to straight quotes and dashes. More...
 
wxString EscapeString (const wxString &aSource, ESCAPE_CONTEXT aContext)
 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)
 
wxString PrettyPrintForMenu (const wxString &aString)
 Remove markup (such as overbar or subscript) that we can't render to menu items. More...
 
int ReadDelimitedText (char *aDest, const char *aSource, int aDestSize)
 Copy bytes from aSource delimited string segment to aDest buffer. More...
 
int ReadDelimitedText (wxString *aDest, const char *aSource)
 Copy bytes from aSource delimited string segment to aDest wxString. More...
 
std::string EscapedUTF8 (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 * GetLine (FILE *aFile, char *Line, int *LineNum=NULL, int SizeLine=255)
 Read one line line from aFile. More...
 
char * StrPurge (char *text)
 Remove leading and training spaces, tabs and end of line chars in text. More...
 
wxString DateAndTime ()
 
int StrNumCmp (const wxString &aString1, const wxString &aString2, bool aIgnoreCase=false)
 Compare two strings with alphanumerical content. More...
 
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. More...
 
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=0)
 Checks aName for illegal file name characters. More...
 
bool ReplaceIllegalFileNameChars (wxString &aName, int aReplaceChar=0)
 
char * strtok_r (char *str, const char *delim, char **nextp)
 
static std::vector< std::string > split (const std::string &aStr, const std::string &aDelim)
 Splits the input string into a vector of output strings. More...
 

Detailed Description

See also
common.h, string.cpp

Definition in file kicad_string.h.

Enumeration Type Documentation

◆ ESCAPE_CONTEXT

Escape/Unescape routines to safely encode reserved-characters in various contexts.

Enumerator
CTX_NETNAME 
CTX_LIBID 
CTX_QUOTED_STR 
CTX_LINE 
CTX_FILENAME 

Definition at line 52 of file kicad_string.h.

Function Documentation

◆ ConvertSmartQuotesAndDashes()

bool ConvertSmartQuotesAndDashes ( wxString *  aString)

Converts curly quotes and em/en dashes to straight quotes and dashes.

Parameters
aString
Returns
true if any characters required conversion.

Definition at line 43 of file string.cpp.

44 {
45  bool retVal = false;
46 
47  for( wxString::iterator ii = aString->begin(); ii != aString->end(); ++ii )
48  {
49  if( *ii == L'\u0060' || *ii == L'\u00B4' || *ii == L'\u2018' || *ii == L'\u2019' )
50  {
51  *ii = '\'';
52  retVal = true;
53  }
54  if( *ii == L'\u201C' || *ii == L'\u201D' )
55  {
56  *ii = '"';
57  retVal = true;
58  }
59  if( *ii == L'\u2013' || *ii == L'\u2014' )
60  {
61  *ii = '-';
62  retVal = true;
63  }
64  }
65 
66  return retVal;
67 }

Referenced by WX_HTML_REPORT_PANEL::onBtnSaveToFile(), SCINTILLA_TRICKS::onCharHook(), and PANEL_SETUP_RULES::TransferDataToWindow().

◆ DateAndTime()

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

Definition at line 400 of file string.cpp.

401 {
402  wxDateTime datetime = wxDateTime::Now();
403 
404  datetime.SetCountry( wxDateTime::Country_Default );
405  return datetime.Format( wxDefaultDateTimeFormat, wxDateTime::Local );
406 }

Referenced by GENDRILL_WRITER_BASE::GenDrillReportFile(), PLACE_FILE_EXPORTER::GenPositionData(), PLACE_FILE_EXPORTER::GenReportData(), NETLIST_EXPORTER_GENERIC::makeDesignHeader(), RecreateCmpFile(), EXCELLON_WRITER::writeEXCELLONHeader(), PCB_CALCULATOR_DATAFILE::WriteHeader(), NETLIST_EXPORTER_ORCADPCB2::WriteNetlist(), NETLIST_EXPORTER_CADSTAR::WriteNetlist(), and DIALOG_ERC::writeReport().

◆ EscapedHTML()

wxString EscapedHTML ( const wxString &  aString)

Return a new wxString escaped for embedding in HTML.

Definition at line 341 of file string.cpp.

342 {
343  wxString converted;
344 
345  for( wxUniChar c: aString )
346  {
347  if( c == '\"' )
348  converted += "&quot;";
349  else if( c == '\'' )
350  converted += "&apos;";
351  else if( c == '&' )
352  converted += "&amp;";
353  else if( c == '<' )
354  converted += "&lt;";
355  else if( c == '>' )
356  converted += "&gt;";
357  else
358  converted += c;
359  }
360 
361  return converted;
362 }

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

◆ EscapedUTF8()

std::string EscapedUTF8 ( 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 304 of file string.cpp.

305 {
306  // No new-lines allowed in quoted strings
307  aString.Replace( "\r\n", "\r" );
308  aString.Replace( "\n", "\r" );
309 
310  std::string utf8 = TO_UTF8( aString );
311 
312  std::string ret;
313 
314  ret += '"';
315 
316  for( std::string::const_iterator it = utf8.begin(); it!=utf8.end(); ++it )
317  {
318  // this escaping strategy is designed to be compatible with ReadDelimitedText():
319  if( *it == '"' )
320  {
321  ret += '\\';
322  ret += '"';
323  }
324  else if( *it == '\\' )
325  {
326  ret += '\\'; // double it up
327  ret += '\\';
328  }
329  else
330  {
331  ret += *it;
332  }
333  }
334 
335  ret += '"';
336 
337  return ret;
338 }
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:100

References TO_UTF8.

Referenced by SCH_LEGACY_PLUGIN::Format(), PLACE_FILE_EXPORTER::GenReportData(), SCH_LEGACY_PLUGIN::saveField(), SCH_LEGACY_PLUGIN_CACHE::saveField(), LEGACY_PLUGIN::SaveModule3D(), SCH_SEXPR_PLUGIN::saveSheet(), and SCH_LEGACY_PLUGIN::saveSheet().

◆ EscapeString()

wxString EscapeString ( const wxString &  aSource,
ESCAPE_CONTEXT  aContext 
)

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 77 of file string.cpp.

78 {
79  wxString converted;
80 
81  for( wxUniChar c: aSource )
82  {
83  if( aContext == CTX_NETNAME )
84  {
85  if( c == '/' )
86  converted += "{slash}";
87  else if( c == '\n' || c == '\r' )
88  converted += ""; // drop
89  else
90  converted += c;
91  }
92  else if( aContext == CTX_LIBID )
93  {
94  if( c == '{' )
95  converted += "{brace}";
96  else if( c == ':' )
97  converted += "{colon}";
98  else if( c == '\n' || c == '\r' )
99  converted += ""; // drop
100  else
101  converted += c;
102  }
103  else if( aContext == CTX_QUOTED_STR )
104  {
105  if( c == '{' )
106  converted += "{brace}";
107  else if( c == '\"' )
108  converted += "{dblquote}";
109  else
110  converted += c;
111  }
112  else if( aContext == CTX_LINE )
113  {
114  if( c == '\n' || c == '\r' )
115  converted += "{return}";
116  else
117  converted += c;
118  }
119  else if( aContext == CTX_FILENAME )
120  {
121  if( c == '{' )
122  converted += "{brace}";
123  else if( c == '/' )
124  converted += "{slash}";
125  else if( c == '\\' )
126  converted += "{backslash}";
127  else if( c == '\"' )
128  converted += "{dblquote}";
129  else if( c == '<' )
130  converted += "{lt}";
131  else if( c == '>' )
132  converted += "{gt}";
133  else if( c == '|' )
134  converted += "{bar}";
135  else if( c == ':' )
136  converted += "{colon}";
137  else if( c == '\t' )
138  converted += "{tab}";
139  else if( c == '\n' || c == '\r' )
140  converted += "{return}";
141  else
142  converted += c;
143  }
144  else
145  converted += c;
146  }
147 
148  return converted;
149 }

References CTX_FILENAME, CTX_LIBID, CTX_LINE, CTX_NETNAME, and CTX_QUOTED_STR.

Referenced by SCH_EDIT_TOOL::ChangeTextType(), CONNECTION_GRAPH::ercCheckBusToNetConflicts(), CONNECTION_GRAPH::ercCheckLabels(), CONNECTION_SUBGRAPH::GetNameForDriver(), DIALOG_SELECT_NET_FROM_LIST::onRenameNet(), BACK_ANNOTATE::processNetNameChange(), DIALOG_EDIT_SHEET_PIN::TransferDataFromWindow(), DIALOG_LABEL_EDITOR::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 729 of file string.cpp.

730 {
732 }
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:114

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 384 of file string.cpp.

385 {
386  do {
387  if( fgets( Line, SizeLine, File ) == NULL )
388  return NULL;
389 
390  if( LineNum )
391  *LineNum += 1;
392 
393  } while( Line[0] == '#' || Line[0] == '\n' || Line[0] == '\r' || Line[0] == 0 );
394 
395  strtok( Line, "\n\r" );
396  return Line;
397 }
#define NULL

References NULL.

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 705 of file string.cpp.

706 {
707  int number = 0;
708  int base = 1;
709 
710  // Trim and extract the trailing numeric part
711  int index = aStr.Len() - 1;
712 
713  while( index >= 0 )
714  {
715  const char chr = aStr.GetChar( index );
716 
717  if( chr < '0' || chr > '9' )
718  break;
719 
720  number += ( chr - '0' ) * base;
721  base *= 10;
722  index--;
723  }
724 
725  return number;
726 }

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

◆ PrettyPrintForMenu()

wxString PrettyPrintForMenu ( const wxString &  aString)

Remove markup (such as overbar or subscript) that we can't render to menu items.

◆ ReadDelimitedText() [1/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 255 of file string.cpp.

256 {
257  if( aDestSize <= 0 )
258  return 0;
259 
260  bool inside = false;
261  const char* start = aSource;
262  char* limit = aDest + aDestSize - 1;
263  char cc;
264 
265  while( (cc = *aSource++) != 0 && aDest < limit )
266  {
267  if( cc == '"' )
268  {
269  if( inside )
270  break; // 2nd double quote is end of delimited text
271 
272  inside = true; // first delimiter found, make note, do not copy
273  }
274 
275  else if( inside )
276  {
277  if( cc == '\\' )
278  {
279  cc = *aSource++;
280 
281  if( !cc )
282  break;
283 
284  // do no copy the escape byte if it is followed by \ or "
285  if( cc != '"' && cc != '\\' )
286  *aDest++ = '\\';
287 
288  if( aDest < limit )
289  *aDest++ = cc;
290  }
291  else
292  {
293  *aDest++ = cc;
294  }
295  }
296  }
297 
298  *aDest = 0;
299 
300  return aSource - start;
301 }

◆ ReadDelimitedText() [2/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 210 of file string.cpp.

211 {
212  std::string utf8; // utf8 but without escapes and quotes.
213  bool inside = false;
214  const char* start = aSource;
215  char cc;
216 
217  while( (cc = *aSource++) != 0 )
218  {
219  if( cc == '"' )
220  {
221  if( inside )
222  break; // 2nd double quote is end of delimited text
223 
224  inside = true; // first delimiter found, make note, do not copy
225  }
226 
227  else if( inside )
228  {
229  if( cc == '\\' )
230  {
231  cc = *aSource++;
232 
233  if( !cc )
234  break;
235 
236  // do no copy the escape byte if it is followed by \ or "
237  if( cc != '"' && cc != '\\' )
238  utf8 += '\\';
239 
240  utf8 += cc;
241  }
242  else
243  {
244  utf8 += cc;
245  }
246  }
247  }
248 
249  *aDest = FROM_UTF8( utf8.c_str() );
250 
251  return aSource - start;
252 }
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:114

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().

◆ 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 735 of file string.cpp.

736 {
737  bool changed = false;
738  std::string result;
739  result.reserve( aName->length() );
740 
741  for( std::string::iterator it = aName->begin(); it != aName->end(); ++it )
742  {
743  if( strchr( illegalFileNameChars, *it ) )
744  {
745  if( aReplaceChar )
746  StrPrintf( &result, "%c", aReplaceChar );
747  else
748  StrPrintf( &result, "%%%02x", *it );
749 
750  changed = true;
751  }
752  else
753  {
754  result += *it;
755  }
756  }
757 
758  if( changed )
759  *aName = result;
760 
761  return changed;
762 }
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 *result, const char *format,...)
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 StrPrintf().

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

◆ ReplaceIllegalFileNameChars() [2/2]

bool ReplaceIllegalFileNameChars ( wxString &  aName,
int  aReplaceChar = 0 
)

Definition at line 765 of file string.cpp.

766 {
767  bool changed = false;
768  wxString result;
769  result.reserve( aName.Length() );
770  wxString illWChars = GetIllegalFileNameWxChars();
771 
772  for( wxString::iterator it = aName.begin(); it != aName.end(); ++it )
773  {
774  if( illWChars.Find( *it ) != wxNOT_FOUND )
775  {
776  if( aReplaceChar )
777  result += aReplaceChar;
778  else
779  result += wxString::Format( "%%%02x", *it );
780 
781  changed = true;
782  }
783  else
784  {
785  result += *it;
786  }
787  }
788 
789  if( changed )
790  aName = result;
791 
792  return changed;
793 }
wxString GetIllegalFileNameWxChars()
Definition: string.cpp:729
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:201

References Format(), and GetIllegalFileNameWxChars().

◆ split()

static std::vector<std::string> split ( const std::string &  aStr,
const std::string &  aDelim 
)
inlinestatic

Splits the input string into a vector of output strings.

Parameters
aStr- Input string with 0 or more delimiters
aDelim- The string of delimiter. Multiple characters here denote alternate delimiters
Note
Multiple delimiters are considered to be separate records with empty strings
Returns
a vector of strings

Definition at line 268 of file kicad_string.h.

269 {
270  size_t pos = 0;
271  size_t last_pos = 0;
272  size_t len;
273 
274  std::vector<std::string> tokens;
275 
276  while( pos < aStr.size() )
277  {
278  pos = aStr.find_first_of( aDelim, last_pos );
279 
280  if( pos == std::string::npos )
281  pos = aStr.size();
282 
283  len = pos - last_pos;
284 
285  tokens.push_back( aStr.substr( last_pos, len ) );
286 
287  last_pos = pos + 1;
288  }
289 
290  return tokens;
291 }

Referenced by CompareToBucket::CompareToBucket(), HLBVH_SAH_Evaluator::HLBVH_SAH_Evaluator(), SCH_EDIT_FRAME::KiwayMailIn(), PCB_EDIT_FRAME::KiwayMailIn(), and PNS::NODE::rebuildJoint().

◆ 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 644 of file string.cpp.

648 {
649  static const wxString separators( wxT( ".," ) );
650 
651  // Clear all the return strings
652  strBeginning->Empty();
653  strDigits->Empty();
654  strEnd->Empty();
655 
656  // There no need to do anything if the string is empty
657  if( strToSplit.length() == 0 )
658  return 0;
659 
660  // Starting at the end of the string look for the first digit
661  int ii;
662 
663  for( ii = (strToSplit.length() - 1); ii >= 0; ii-- )
664  {
665  if( wxIsdigit( strToSplit[ii] ) )
666  break;
667  }
668 
669  // If there were no digits then just set the single string
670  if( ii < 0 )
671  {
672  *strBeginning = strToSplit;
673  }
674  else
675  {
676  // Since there is at least one digit this is the trailing string
677  *strEnd = strToSplit.substr( ii + 1 );
678 
679  // Go to the end of the digits
680  int position = ii + 1;
681 
682  for( ; ii >= 0; ii-- )
683  {
684  if( !wxIsdigit( strToSplit[ii] ) && separators.Find( strToSplit[ii] ) < 0 )
685  break;
686  }
687 
688  // If all that was left was digits, then just set the digits string
689  if( ii < 0 )
690  *strDigits = strToSplit.substr( 0, position );
691 
692  /* We were only looking for the last set of digits everything else is
693  * part of the preamble */
694  else
695  {
696  *strDigits = strToSplit.substr( ii + 1, position - ii - 1 );
697  *strBeginning = strToSplit.substr( 0, ii + 1 );
698  }
699  }
700 
701  return 0;
702 }

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 409 of file string.cpp.

410 {
411  int nb1 = 0, nb2 = 0;
412 
413  auto str1 = aString1.begin();
414  auto str2 = aString2.begin();
415 
416  while( str1 != aString1.end() && str2 != aString2.end() )
417  {
418  wxUniChar c1 = *str1;
419  wxUniChar c2 = *str2;
420 
421  if( wxIsdigit( c1 ) && wxIsdigit( c2 ) ) // Both characters are digits, do numeric compare.
422  {
423  nb1 = 0;
424  nb2 = 0;
425 
426  do
427  {
428  c1 = *str1;
429  nb1 = nb1 * 10 + (int) c1 - '0';
430  ++str1;
431  } while( str1 != aString1.end() && wxIsdigit( *str1 ) );
432 
433  do
434  {
435  c2 = *str2;
436  nb2 = nb2 * 10 + (int) c2 - '0';
437  ++str2;
438  } while( str2 != aString2.end() && wxIsdigit( *str2 ) );
439 
440  if( nb1 < nb2 )
441  return -1;
442 
443  if( nb1 > nb2 )
444  return 1;
445 
446  c1 = ( str1 != aString1.end() ) ? *str1 : wxUniChar( 0 );
447  c2 = ( str2 != aString2.end() ) ? *str2 : wxUniChar( 0 );
448  }
449 
450  // Any numerical comparisons to here are identical.
451  if( aIgnoreCase )
452  {
453  if( wxToupper( c1 ) < wxToupper( c2 ) )
454  return -1;
455 
456  if( wxToupper( c1 ) > wxToupper( c2 ) )
457  return 1;
458  }
459  else
460  {
461  if( c1 < c2 )
462  return -1;
463 
464  if( c1 > c2 )
465  return 1;
466  }
467 
468  if( str1 != aString1.end() )
469  ++str1;
470 
471  if( str2 != aString2.end() )
472  ++str2;
473  }
474 
475  if( str1 == aString1.end() && str2 != aString2.end() )
476  {
477  return -1; // Identical to here but aString1 is longer.
478  }
479  else if( str1 != aString1.end() && str2 == aString2.end() )
480  {
481  return 1; // Identical to here but aString2 is longer.
482  }
483 
484  return 0;
485 }

Referenced by LIB_TREE_NODE::AssignIntrinsicRanks(), BOOST_AUTO_TEST_CASE(), SCH_PIN_TABLE_DATA_MODEL::compare(), PIN_TABLE_DATA_MODEL::compare(), PLACE_FILE_EXPORTER::GenReportData(), FP_TREE_MODEL_ADAPTER::getFootprints(), myCompareFunction(), MODULE::cmp_pads::operator()(), 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 365 of file string.cpp.

366 {
367  static const char whitespace[] = " \t\n\r\f\v";
368 
369  if( text )
370  {
371  while( *text && strchr( whitespace, *text ) )
372  ++text;
373 
374  char* cp = text + strlen( text ) - 1;
375 
376  while( cp >= text && strchr( whitespace, *cp ) )
377  *cp-- = '\0';
378  }
379 
380  return text;
381 }

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().

◆ strtok_r()

◆ UnescapeString()

wxString UnescapeString ( const wxString &  aSource)

Definition at line 152 of file string.cpp.

153 {
154  wxString newbuf;
155  size_t sourceLen = aSource.length();
156 
157  for( size_t i = 0; i < sourceLen; ++i )
158  {
159  if( ( aSource[i] == '$' || aSource[i] == '^' || aSource[i] == '_' )
160  && i + 1 < sourceLen && aSource[i+1] == '{' )
161  {
162  for( ; i < sourceLen; ++i )
163  {
164  newbuf += aSource[i];
165 
166  if( aSource[i] == '}' )
167  break;
168  }
169  }
170  else if( aSource[i] == '{' )
171  {
172  wxString token;
173 
174  for( i = i + 1; i < sourceLen; ++i )
175  {
176  if( aSource[i] == '}' )
177  break;
178  else
179  token.append( aSource[i] );
180  }
181 
182  if( token == wxS( "dblquote" ) ) newbuf.append( wxS( "\"" ) );
183  else if( token == wxS( "quote" ) ) newbuf.append( wxS( "'" ) );
184  else if( token == wxS( "lt" ) ) newbuf.append( wxS( "<" ) );
185  else if( token == wxS( "gt" ) ) newbuf.append( wxS( ">" ) );
186  else if( token == wxS( "backslash" ) ) newbuf.append( wxS( "\\" ) );
187  else if( token == wxS( "slash" ) ) newbuf.append( wxS( "/" ) );
188  else if( token == wxS( "bar" ) ) newbuf.append( wxS( "|" ) );
189  else if( token == wxS( "colon" ) ) newbuf.append( wxS( ":" ) );
190  else if( token == wxS( "space" ) ) newbuf.append( wxS( " " ) );
191  else if( token == wxS( "dollar" ) ) newbuf.append( wxS( "$" ) );
192  else if( token == wxS( "tab" ) ) newbuf.append( wxS( "\t" ) );
193  else if( token == wxS( "return" ) ) newbuf.append( wxS( "\n" ) );
194  else if( token == wxS( "brace" ) ) newbuf.append( wxS( "{" ) );
195  else
196  {
197  newbuf.append( "{" + token + "}" );
198  }
199  }
200  else
201  {
202  newbuf.append( aSource[i] );
203  }
204  }
205 
206  return newbuf;
207 }

Referenced by SCH_CONNECTION::AppendInfoToMsgPanel(), SCH_EDIT_TOOL::ChangeTextType(), NETLIST_EXPORTER_PSPICE_SIM::ComponentToVector(), SCH_CONNECTION::ConfigureFromLabel(), BOARD_NETLIST_UPDATER::deleteSinglePadNets(), GRID_CELL_ESCAPED_TEXT_RENDERER::Draw(), KIGFX::PCB_PAINTER::draw(), EDA_TEXT::EDA_TEXT(), SCH_EDIT_FRAME::ExecuteRemoteCommand(), NETLIST_EXPORTER_PSPICE::Format(), DIALOG_SELECT_NET_FROM_LIST::formatNetName(), GRID_CELL_ESCAPED_TEXT_RENDERER::GetBestSize(), ZONE_CONTAINER::GetMsgPanelInfo(), GERBER_DRAW_ITEM::GetMsgPanelInfo(), D_PAD::GetMsgPanelInfo(), TRACK::GetMsgPanelInfoBase_Common(), BOARD_CONNECTED_ITEM::GetNetnameMsg(), GERBVIEW_CONTROL::HighlightControl(), SCH_CONNECTION::IsBusLabel(), SCH_CONNECTION::MightBeBusLabel(), DIALOG_SELECT_NET_FROM_LIST::netFilterMatches(), DIALOG_SELECT_NET_FROM_LIST::onRenameNet(), FOOTPRINT_LIST_IMPL::ReadCacheFromFile(), NET_SELECTOR_COMBOPOPUP::rebuildList(), UTIL::RefDesStringCompare(), EDA_TEXT::Replace(), NET_SETTINGS::ResolveNetClassAssignments(), EDA_TEXT::SetText(), BOARD::SortedNetnamesList(), DIALOG_SIGNAL_LIST::TransferDataToWindow(), DIALOG_EDIT_SHEET_PIN::TransferDataToWindow(), DIALOG_LABEL_EDITOR::TransferDataToWindow(), HIGHLIGHT_MENU::update(), BOARD_NETLIST_UPDATER::updateComponentPadConnections(), BOARD_NETLIST_UPDATER::updateCopperZoneNets(), SCH_EDIT_FRAME::UpdateNetHighlightStatus(), GERBVIEW_FRAME::updateNetnameListSelectBox(), 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 596 of file string.cpp.

597 {
598  // Compare unescaped text
599  strFWord = UnescapeString( strFWord );
600  strSWord = UnescapeString( strSWord );
601 
602  // The different sections of the two strings
603  wxString strFWordBeg, strFWordMid, strFWordEnd;
604  wxString strSWordBeg, strSWordMid, strSWordEnd;
605 
606  // Split the two strings into separate parts
607  SplitString( strFWord, &strFWordBeg, &strFWordMid, &strFWordEnd );
608  SplitString( strSWord, &strSWordBeg, &strSWordMid, &strSWordEnd );
609 
610  // Compare the Beginning section of the strings
611  int isEqual = strFWordBeg.CmpNoCase( strSWordBeg );
612 
613  if( isEqual > 0 )
614  return 1;
615  else if( isEqual < 0 )
616  return -1;
617  else
618  {
619  // If the first sections are equal compare their digits
620  double lFirstNumber = 0;
621  double lSecondNumber = 0;
622  bool endingIsModifier = false;
623 
624  strFWordMid.ToDouble( &lFirstNumber );
625  strSWordMid.ToDouble( &lSecondNumber );
626 
627  endingIsModifier |= ApplyModifier( lFirstNumber, strFWordEnd );
628  endingIsModifier |= ApplyModifier( lSecondNumber, strSWordEnd );
629 
630  if( lFirstNumber > lSecondNumber )
631  return 1;
632  else if( lFirstNumber < lSecondNumber )
633  return -1;
634  // If the first two sections are equal and the endings are modifiers then compare them
635  else if( !endingIsModifier )
636  return strFWordEnd.CmpNoCase( strSWordEnd );
637  // Ran out of things to compare; they must match
638  else
639  return 0;
640  }
641 }
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:644
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:152
bool ApplyModifier(double &value, const wxString &aString)
Definition: string.cpp:548

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 488 of file string.cpp.

490 {
491  const wxChar* cp = NULL, * mp = NULL;
492  const wxChar* wild, * string;
493  wxString _pattern, _string_to_tst;
494 
495  if( case_sensitive )
496  {
497  wild = pattern.GetData();
498  string = string_to_tst.GetData();
499  }
500  else
501  {
502  _pattern = pattern;
503  _pattern.MakeUpper();
504  _string_to_tst = string_to_tst;
505  _string_to_tst.MakeUpper();
506  wild = _pattern.GetData();
507  string = _string_to_tst.GetData();
508  }
509 
510  while( ( *string ) && ( *wild != '*' ) )
511  {
512  if( ( *wild != *string ) && ( *wild != '?' ) )
513  return false;
514 
515  wild++; string++;
516  }
517 
518  while( *string )
519  {
520  if( *wild == '*' )
521  {
522  if( !*++wild )
523  return 1;
524  mp = wild;
525  cp = string + 1;
526  }
527  else if( ( *wild == *string ) || ( *wild == '?' ) )
528  {
529  wild++;
530  string++;
531  }
532  else
533  {
534  wild = mp;
535  string = cp++;
536  }
537  }
538 
539  while( *wild == '*' )
540  {
541  wild++;
542  }
543 
544  return !*wild;
545 }
#define NULL

References NULL.

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