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-2020 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 
46 bool ConvertSmartQuotesAndDashes( wxString* aString );
47 
52 {
58 };
59 
60 wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext );
61 
62 wxString UnescapeString( const wxString& aSource );
63 
67 wxString PrettyPrintForMenu( const wxString& aString );
68 
72 wxString TitleCaps( const wxString& aString );
73 
87 int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize );
88 
97 int ReadDelimitedText( wxString* aDest, const char* aSource );
98 
108 std::string EscapedUTF8( wxString aString );
109 
113 wxString EscapeHTML( const wxString& aString );
114 
120 char* GetLine( FILE* aFile, char* Line, int* LineNum = nullptr, int SizeLine = 255 );
121 
125 bool NoPrintableChars( wxString aString );
126 
132 char* StrPurge( char* text );
133 
137 wxString DateAndTime();
138 
153 int StrNumCmp( const wxString& aString1, const wxString& aString2, bool aIgnoreCase = false );
154 
160 bool WildCompareString( const wxString& pattern,
161  const wxString& string_to_tst,
162  bool case_sensitive = true );
163 
171 int ValueStringCompare( wxString strFWord, wxString strSWord );
172 
179 int SplitString( wxString strToSplit,
180  wxString* strBeginning,
181  wxString* strDigits,
182  wxString* strEnd );
183 
190 int GetTrailingInt( const wxString& aStr );
191 
195 wxString GetIllegalFileNameWxChars();
196 
211 bool ReplaceIllegalFileNameChars( std::string* aName, int aReplaceChar = 0 );
212 bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar = 0 );
213 
214 #ifndef HAVE_STRTOKR
215 // common/strtok_r.c optionally:
216 extern "C" char* strtok_r( char* str, const char* delim, char** nextp );
217 #endif
218 
219 
226 {
227  bool operator() (const wxString& strA, const wxString& strB ) const
228  {
229  wxString::const_reverse_iterator sA = strA.rbegin();
230  wxString::const_reverse_iterator eA = strA.rend();
231 
232  wxString::const_reverse_iterator sB = strB.rbegin();
233  wxString::const_reverse_iterator eB = strB.rend();
234 
235  if( strA.empty() )
236  {
237  if( strB.empty() )
238  return false;
239 
240  // note: this rule implies that a null string is first in the sort order
241  return true;
242  }
243 
244  if( strB.empty() )
245  return false;
246 
247  while( sA != eA && sB != eB )
248  {
249  if( (*sA) == (*sB) )
250  {
251  ++sA;
252  ++sB;
253  continue;
254  }
255 
256  if( (*sA) < (*sB) )
257  return true;
258  else
259  return false;
260  }
261 
262  if( sB == eB )
263  return false;
264 
265  return true;
266  }
267 };
268 
278 static inline std::vector<std::string> split( const std::string& aStr, const std::string& aDelim )
279 {
280  size_t pos = 0;
281  size_t last_pos = 0;
282  size_t len;
283 
284  std::vector<std::string> tokens;
285 
286  while( pos < aStr.size() )
287  {
288  pos = aStr.find_first_of( aDelim, last_pos );
289 
290  if( pos == std::string::npos )
291  pos = aStr.size();
292 
293  len = pos - last_pos;
294 
295  tokens.push_back( aStr.substr( last_pos, len ) );
296 
297  last_pos = pos + 1;
298  }
299 
300  return tokens;
301 }
302 
304 inline void AccumulateDescription( wxString& aDesc, const wxString& aItem )
305 {
306  if( !aDesc.IsEmpty() )
307  aDesc << wxT( ", " );
308 
309  aDesc << aItem;
310 }
311 
312 
320 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
321 
328 void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = 1 );
329 
338 std::string Double2Str( double aValue );
339 
344 wxString AngleToStringDegrees( double aAngle );
345 
346 #endif // KICAD_STRING_H_
wxString GetIllegalFileNameWxChars()
Definition: string.cpp:761
std::string EscapedUTF8(wxString aString)
Return an 8 bit UTF8 string given aString in Unicode form.
Definition: string.cpp:329
ESCAPE_CONTEXT
Escape/Unescape routines to safely encode reserved-characters in various contexts.
Definition: kicad_string.h:51
bool NoPrintableChars(wxString aString)
Return true if the string is empty or contains only whitespace.
Definition: string.cpp:390
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: string.cpp:828
char * GetLine(FILE *aFile, char *Line, int *LineNum=nullptr, int SizeLine=255)
Read one line line from aFile.
Definition: string.cpp:415
A helper for sorting strings from the rear.
Definition: kicad_string.h:225
wxString DateAndTime()
Definition: string.cpp:431
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:151
int GetTrailingInt(const wxString &aStr)
Gets the trailing int, if any, from a string.
Definition: string.cpp:737
int ValueStringCompare(wxString strFWord, wxString strSWord)
Compare strings like the strcmp function but handle numbers and modifiers within the string text corr...
Definition: string.cpp:628
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase=false)
Compare two strings with alphanumerical content.
Definition: string.cpp:440
bool ReplaceIllegalFileNameChars(std::string *aName, int aReplaceChar=0)
Checks aName for illegal file name characters.
Definition: string.cpp:767
std::string Double2Str(double aValue)
Prints a float number without using scientific notation and no trailing 0 We want to avoid scientific...
Definition: string.cpp:873
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed=1)
Remove trailing zeros from a string containing a converted float number.
Definition: string.cpp:851
bool operator()(const wxString &strA, const wxString &strB) const
Definition: kicad_string.h:227
void AccumulateDescription(wxString &aDesc, const wxString &aItem)
Utility to build comma separated lists in messages.
Definition: kicad_string.h:304
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
Definition: string.cpp:366
wxString AngleToStringDegrees(double aAngle)
A helper to convert the double aAngle (in internal unit) to a string in degrees.
Definition: string.cpp:903
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
These Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which ar...
Definition: string.cpp:78
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:676
bool ConvertSmartQuotesAndDashes(wxString *aString)
Converts curly quotes and em/en dashes to straight quotes and dashes.
Definition: string.cpp:44
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:519
char * strtok_r(char *str, const char *delim, char **nextp)
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:280
wxString TitleCaps(const wxString &aString)
Capitalize the first letter in each word.
Definition: string.cpp:216
static std::vector< std::string > split(const std::string &aStr, const std::string &aDelim)
Splits the input string into a vector of output strings.
Definition: kicad_string.h:278
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
Definition: string.cpp:396