KiCad PCB EDA Suite
kicad_string.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2004-2021 KiCad Developers, see change_log.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
30 #ifndef KICAD_STRING_H_
31 #define KICAD_STRING_H_
32 
33 #include "config.h"
34 
35 #include <string>
36 #include <vector>
37 #include <wx/string.h>
38 #include <wx/filename.h>
39 
40 
44 wxString ConvertToNewOverbarNotation( const wxString& aOldStr );
45 
51 bool ConvertSmartQuotesAndDashes( wxString* aString );
52 
57 {
63 };
64 
72 wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext );
73 
74 wxString UnescapeString( const wxString& aSource );
75 
79 wxString PrettyPrintForMenu( const wxString& aString );
80 
84 wxString TitleCaps( const wxString& aString );
85 
99 int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize );
100 
109 int ReadDelimitedText( wxString* aDest, const char* aSource );
110 
120 std::string EscapedUTF8( const wxString& aString );
121 
125 wxString EscapeHTML( const wxString& aString );
126 
132 char* GetLine( FILE* aFile, char* Line, int* LineNum = nullptr, int SizeLine = 255 );
133 
137 bool NoPrintableChars( const wxString& aString );
138 
144 char* StrPurge( char* text );
145 
149 wxString DateAndTime();
150 
165 int StrNumCmp( const wxString& aString1, const wxString& aString2, bool aIgnoreCase = false );
166 
172 bool WildCompareString( const wxString& pattern,
173  const wxString& string_to_tst,
174  bool case_sensitive = true );
175 
183 int ValueStringCompare( const wxString& strFWord, const wxString& strSWord );
184 
191 int SplitString( const wxString& strToSplit,
192  wxString* strBeginning,
193  wxString* strDigits,
194  wxString* strEnd );
195 
202 int GetTrailingInt( const wxString& aStr );
203 
207 wxString GetIllegalFileNameWxChars();
208 
223 bool ReplaceIllegalFileNameChars( std::string* aName, int aReplaceChar = 0 );
224 bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar = 0 );
225 
226 #ifndef HAVE_STRTOKR
227 // common/strtok_r.c optionally:
228 extern "C" char* strtok_r( char* str, const char* delim, char** nextp );
229 #endif
230 
231 
238 {
239  bool operator() ( const wxString& strA, const wxString& strB ) const
240  {
241  wxString::const_reverse_iterator sA = strA.rbegin();
242  wxString::const_reverse_iterator eA = strA.rend();
243 
244  wxString::const_reverse_iterator sB = strB.rbegin();
245  wxString::const_reverse_iterator eB = strB.rend();
246 
247  if( strA.empty() )
248  {
249  if( strB.empty() )
250  return false;
251 
252  // note: this rule implies that a null string is first in the sort order
253  return true;
254  }
255 
256  if( strB.empty() )
257  return false;
258 
259  while( sA != eA && sB != eB )
260  {
261  if( ( *sA ) == ( *sB ) )
262  {
263  ++sA;
264  ++sB;
265  continue;
266  }
267 
268  if( ( *sA ) < ( *sB ) )
269  return true;
270  else
271  return false;
272  }
273 
274  if( sB == eB )
275  return false;
276 
277  return true;
278  }
279 };
280 
290 static inline std::vector<std::string> split( const std::string& aStr, const std::string& aDelim )
291 {
292  size_t pos = 0;
293  size_t last_pos = 0;
294  size_t len;
295 
296  std::vector<std::string> tokens;
297 
298  while( pos < aStr.size() )
299  {
300  pos = aStr.find_first_of( aDelim, last_pos );
301 
302  if( pos == std::string::npos )
303  pos = aStr.size();
304 
305  len = pos - last_pos;
306 
307  tokens.push_back( aStr.substr( last_pos, len ) );
308 
309  last_pos = pos + 1;
310  }
311 
312  return tokens;
313 }
314 
316 inline void AccumulateDescription( wxString& aDesc, const wxString& aItem )
317 {
318  if( !aDesc.IsEmpty() )
319  aDesc << wxT( ", " );
320 
321  aDesc << aItem;
322 }
323 
331 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
332 
339 void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = 1 );
340 
350 std::string Double2Str( double aValue );
351 
355 wxString AngleToStringDegrees( double aAngle );
356 
357 #endif // KICAD_STRING_H_
wxString GetIllegalFileNameWxChars()
Definition: string.cpp:843
ESCAPE_CONTEXT
Escape/Unescape routines to safely encode reserved-characters in various contexts.
Definition: kicad_string.h:56
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: string.cpp:910
char * GetLine(FILE *aFile, char *Line, int *LineNum=nullptr, int SizeLine=255)
Read one line line from aFile.
Definition: string.cpp:493
A helper for sorting strings from the rear.
Definition: kicad_string.h:237
wxString DateAndTime()
Definition: string.cpp:509
std::string EscapedUTF8(const wxString &aString)
Return an 8 bit UTF8 string given aString in Unicode form.
Definition: string.cpp:403
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:222
int GetTrailingInt(const wxString &aStr)
Gets the trailing int, if any, from a string.
Definition: string.cpp:819
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase=false)
Compare two strings with alphanumerical content.
Definition: string.cpp:518
wxString ConvertToNewOverbarNotation(const wxString &aOldStr)
Convert the old ~...~ overbar notation to the new ~{...} one.
Definition: string.cpp:44
int ValueStringCompare(const wxString &strFWord, const wxString &strSWord)
Compare strings like the strcmp function but handle numbers and modifiers within the string text corr...
Definition: string.cpp:706
bool ReplaceIllegalFileNameChars(std::string *aName, int aReplaceChar=0)
Checks aName for illegal file name characters.
Definition: string.cpp:849
std::string Double2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 We want to avoid scientific ...
Definition: string.cpp:955
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed=1)
Remove trailing zeros from a string containing a converted float number.
Definition: string.cpp:933
bool operator()(const wxString &strA, const wxString &strB) const
Definition: kicad_string.h:239
void AccumulateDescription(wxString &aDesc, const wxString &aItem)
Utility to build comma separated lists in messages.
Definition: kicad_string.h:316
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
Definition: string.cpp:442
bool NoPrintableChars(const wxString &aString)
Return true if the string is empty or contains only whitespace.
Definition: string.cpp:466
wxString AngleToStringDegrees(double aAngle)
A helper to convert the double aAngle (in internal unit) to a string in degrees.
Definition: string.cpp:985
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
Definition: string.cpp:141
bool ConvertSmartQuotesAndDashes(wxString *aString)
Convert curly quotes and em/en dashes to straight quotes and dashes.
Definition: string.cpp:114
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.
Definition: string.cpp:597
char * strtok_r(char *str, const char *delim, char **nextp)
int SplitString(const wxString &strToSplit, wxString *strBeginning, wxString *strDigits, wxString *strEnd)
Break a string into three parts: he alphabetic preamble, the numeric part, and any alphabetic ending.
Definition: string.cpp:758
wxString PrettyPrintForMenu(const wxString &aString)
Remove markup (such as overbar or subscript) that we can't render to menu items.
int ReadDelimitedText(char *aDest, const char *aSource, int aDestSize)
Copy bytes from aSource delimited string segment to aDest buffer.
Definition: string.cpp:354
wxString TitleCaps(const wxString &aString)
Capitalize the first letter in each word.
Definition: string.cpp:290
static std::vector< std::string > split(const std::string &aStr, const std::string &aDelim)
Split the input string into a vector of output strings.
Definition: kicad_string.h:290
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
Definition: string.cpp:474