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 
82 int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize );
83 
92 int ReadDelimitedText( wxString* aDest, const char* aSource );
93 
103 std::string EscapedUTF8( wxString aString );
104 
108 wxString EscapeHTML( const wxString& aString );
109 
115 char* GetLine( FILE* aFile, char* Line, int* LineNum = nullptr, int SizeLine = 255 );
116 
120 bool NoPrintableChars( wxString aString );
121 
127 char* StrPurge( char* text );
128 
132 wxString DateAndTime();
133 
148 int StrNumCmp( const wxString& aString1, const wxString& aString2, bool aIgnoreCase = false );
149 
155 bool WildCompareString( const wxString& pattern,
156  const wxString& string_to_tst,
157  bool case_sensitive = true );
158 
166 int ValueStringCompare( wxString strFWord, wxString strSWord );
167 
174 int SplitString( wxString strToSplit,
175  wxString* strBeginning,
176  wxString* strDigits,
177  wxString* strEnd );
178 
185 int GetTrailingInt( const wxString& aStr );
186 
190 wxString GetIllegalFileNameWxChars();
191 
206 bool ReplaceIllegalFileNameChars( std::string* aName, int aReplaceChar = 0 );
207 bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar = 0 );
208 
209 #ifndef HAVE_STRTOKR
210 // common/strtok_r.c optionally:
211 extern "C" char* strtok_r( char* str, const char* delim, char** nextp );
212 #endif
213 
214 
221 {
222  bool operator() (const wxString& strA, const wxString& strB ) const
223  {
224  wxString::const_reverse_iterator sA = strA.rbegin();
225  wxString::const_reverse_iterator eA = strA.rend();
226 
227  wxString::const_reverse_iterator sB = strB.rbegin();
228  wxString::const_reverse_iterator eB = strB.rend();
229 
230  if( strA.empty() )
231  {
232  if( strB.empty() )
233  return false;
234 
235  // note: this rule implies that a null string is first in the sort order
236  return true;
237  }
238 
239  if( strB.empty() )
240  return false;
241 
242  while( sA != eA && sB != eB )
243  {
244  if( (*sA) == (*sB) )
245  {
246  ++sA;
247  ++sB;
248  continue;
249  }
250 
251  if( (*sA) < (*sB) )
252  return true;
253  else
254  return false;
255  }
256 
257  if( sB == eB )
258  return false;
259 
260  return true;
261  }
262 };
263 
273 static inline std::vector<std::string> split( const std::string& aStr, const std::string& aDelim )
274 {
275  size_t pos = 0;
276  size_t last_pos = 0;
277  size_t len;
278 
279  std::vector<std::string> tokens;
280 
281  while( pos < aStr.size() )
282  {
283  pos = aStr.find_first_of( aDelim, last_pos );
284 
285  if( pos == std::string::npos )
286  pos = aStr.size();
287 
288  len = pos - last_pos;
289 
290  tokens.push_back( aStr.substr( last_pos, len ) );
291 
292  last_pos = pos + 1;
293  }
294 
295  return tokens;
296 }
297 
299 inline void AccumulateDescription( wxString& aDesc, const wxString& aItem )
300 {
301  if( !aDesc.IsEmpty() )
302  aDesc << wxT( ", " );
303 
304  aDesc << aItem;
305 }
306 
307 
315 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
316 
323 void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = 1 );
324 
325 #endif // KICAD_STRING_H_
wxString GetIllegalFileNameWxChars()
Definition: string.cpp:740
std::string EscapedUTF8(wxString aString)
Return an 8 bit UTF8 string given aString in Unicode form.
Definition: string.cpp:309
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:370
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: string.cpp:807
char * GetLine(FILE *aFile, char *Line, int *LineNum=nullptr, int SizeLine=255)
Read one line line from aFile.
Definition: string.cpp:395
A helper for sorting strings from the rear.
Definition: kicad_string.h:220
wxString DateAndTime()
Definition: string.cpp:411
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:150
int GetTrailingInt(const wxString &aStr)
Gets the trailing int, if any, from a string.
Definition: string.cpp:716
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:607
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase=false)
Compare two strings with alphanumerical content.
Definition: string.cpp:420
bool ReplaceIllegalFileNameChars(std::string *aName, int aReplaceChar=0)
Checks aName for illegal file name characters.
Definition: string.cpp:746
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed=1)
Remove trailing zeros from a string containing a converted float number.
Definition: string.cpp:830
bool operator()(const wxString &strA, const wxString &strB) const
Definition: kicad_string.h:222
void AccumulateDescription(wxString &aDesc, const wxString &aItem)
Utility to build comma separated lists in messages.
Definition: kicad_string.h:299
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
Definition: string.cpp:346
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:77
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:655
bool ConvertSmartQuotesAndDashes(wxString *aString)
Converts curly quotes and em/en dashes to straight quotes and dashes.
Definition: string.cpp:43
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:499
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:260
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:273
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
Definition: string.cpp:376