KiCad PCB EDA Suite
string_utils.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 
24 #ifndef STRING_UTILS_H
25 #define STRING_UTILS_H
26 
27 #include "config.h"
28 
29 #include <string>
30 #include <vector>
31 #include <wx/string.h>
32 #include <wx/filename.h>
33 
34 
35 void ConvertMarkdown2Html( const wxString& aMarkdownInput, wxString& aHtmlOutput );
36 
40 wxString ConvertToNewOverbarNotation( const wxString& aOldStr );
41 
47 bool ConvertSmartQuotesAndDashes( wxString* aString );
48 
53 {
59  CTX_NO_SPACE // to replace spaces in names that do not accept spaces
60 };
61 
69 wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext );
70 
71 wxString UnescapeString( const wxString& aSource );
72 
76 wxString PrettyPrintForMenu( const wxString& aString );
77 
81 wxString TitleCaps( const wxString& aString );
82 
96 int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize );
97 
106 int ReadDelimitedText( wxString* aDest, const char* aSource );
107 
117 std::string EscapedUTF8( const wxString& aString );
118 
122 wxString EscapeHTML( const wxString& aString );
123 
129 char* GetLine( FILE* aFile, char* Line, int* LineNum = nullptr, int SizeLine = 255 );
130 
134 bool NoPrintableChars( const wxString& aString );
135 
140 int PrintableCharCount( const wxString& aString );
141 
147 char* StrPurge( char* text );
148 
152 wxString DateAndTime();
153 
168 int StrNumCmp( const wxString& aString1, const wxString& aString2, bool aIgnoreCase = false );
169 
175 bool WildCompareString( const wxString& pattern,
176  const wxString& string_to_tst,
177  bool case_sensitive = true );
178 
186 int ValueStringCompare( const wxString& strFWord, const wxString& strSWord );
187 
194 int SplitString( const wxString& strToSplit,
195  wxString* strBeginning,
196  wxString* strDigits,
197  wxString* strEnd );
198 
205 int GetTrailingInt( const wxString& aStr );
206 
210 wxString GetIllegalFileNameWxChars();
211 
226 bool ReplaceIllegalFileNameChars( std::string* aName, int aReplaceChar = 0 );
227 bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar = 0 );
228 
229 #ifndef HAVE_STRTOKR
230 // common/strtok_r.c optionally:
231 extern "C" char* strtok_r( char* str, const char* delim, char** nextp );
232 #endif
233 
234 
241 {
242  bool operator() ( const wxString& strA, const wxString& strB ) const
243  {
244  wxString::const_reverse_iterator sA = strA.rbegin();
245  wxString::const_reverse_iterator eA = strA.rend();
246 
247  wxString::const_reverse_iterator sB = strB.rbegin();
248  wxString::const_reverse_iterator eB = strB.rend();
249 
250  if( strA.empty() )
251  {
252  if( strB.empty() )
253  return false;
254 
255  // note: this rule implies that a null string is first in the sort order
256  return true;
257  }
258 
259  if( strB.empty() )
260  return false;
261 
262  while( sA != eA && sB != eB )
263  {
264  if( ( *sA ) == ( *sB ) )
265  {
266  ++sA;
267  ++sB;
268  continue;
269  }
270 
271  if( ( *sA ) < ( *sB ) )
272  return true;
273  else
274  return false;
275  }
276 
277  if( sB == eB )
278  return false;
279 
280  return true;
281  }
282 };
283 
293 static inline std::vector<std::string> split( const std::string& aStr, const std::string& aDelim )
294 {
295  size_t pos = 0;
296  size_t last_pos = 0;
297  size_t len;
298 
299  std::vector<std::string> tokens;
300 
301  while( pos < aStr.size() )
302  {
303  pos = aStr.find_first_of( aDelim, last_pos );
304 
305  if( pos == std::string::npos )
306  pos = aStr.size();
307 
308  len = pos - last_pos;
309 
310  tokens.push_back( aStr.substr( last_pos, len ) );
311 
312  last_pos = pos + 1;
313  }
314 
315  return tokens;
316 }
317 
319 inline void AccumulateDescription( wxString& aDesc, const wxString& aItem )
320 {
321  if( !aDesc.IsEmpty() )
322  aDesc << wxT( ", " );
323 
324  aDesc << aItem;
325 }
326 
334 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
335 
342 void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = 1 );
343 
353 std::string Double2Str( double aValue );
354 
358 wxString AngleToStringDegrees( double aAngle );
359 
360 #endif // STRING_UTILS_H
wxString UnescapeString(const wxString &aSource)
char * strtok_r(char *str, const char *delim, char **nextp)
wxString TitleCaps(const wxString &aString)
Capitalize the first letter in each word.
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
A helper for sorting strings from the rear.
Definition: string_utils.h:240
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.
wxString AngleToStringDegrees(double aAngle)
A helper to convert the double aAngle (in internal unit) to a string in degrees.
char * GetLine(FILE *aFile, char *Line, int *LineNum=nullptr, int SizeLine=255)
Read one line line from aFile.
wxString ConvertToNewOverbarNotation(const wxString &aOldStr)
Convert the old ~...~ overbar notation to the new ~{...} one.
bool operator()(const wxString &strA, const wxString &strB) const
Definition: string_utils.h:242
std::string EscapedUTF8(const wxString &aString)
Return an 8 bit UTF8 string given aString in Unicode form.
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
int ValueStringCompare(const wxString &strFWord, const wxString &strSWord)
Compare strings like the strcmp function but handle numbers and modifiers within the string text corr...
void ConvertMarkdown2Html(const wxString &aMarkdownInput, wxString &aHtmlOutput)
int PrintableCharCount(const wxString &aString)
Return the number of printable (ie: non-formatting) chars.
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
wxString DateAndTime()
ESCAPE_CONTEXT
Escape/Unescape routines to safely encode reserved-characters in various contexts.
Definition: string_utils.h:52
wxString GetIllegalFileNameWxChars()
bool ReplaceIllegalFileNameChars(std::string *aName, int aReplaceChar=0)
Checks aName for illegal file name characters.
void AccumulateDescription(wxString &aDesc, const wxString &aItem)
Utility to build comma separated lists in messages.
Definition: string_utils.h:319
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase=false)
Compare two strings with alphanumerical content.
std::string Double2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 We want to avoid scientific ...
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.
wxString PrettyPrintForMenu(const wxString &aString)
Remove markup (such as overbar or subscript) that we can't render to menu items.
int GetTrailingInt(const wxString &aStr)
Gets the trailing int, if any, from a string.
int ReadDelimitedText(char *aDest, const char *aSource, int aDestSize)
Copy bytes from aSource delimited string segment to aDest buffer.
bool NoPrintableChars(const wxString &aString)
Return true if the string is empty or contains only whitespace.
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed=1)
Remove trailing zeros from a string containing a converted float number.
bool ConvertSmartQuotesAndDashes(wxString *aString)
Convert curly quotes and em/en dashes to straight quotes and dashes.
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: string_utils.h:293