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, 2019 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 
53 {
59 };
60 
61 wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext );
62 
63 wxString UnescapeString( const wxString& aSource );
64 
68 wxString PrettyPrintForMenu( const wxString& aString );
69 
83 int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize );
84 
93 int ReadDelimitedText( wxString* aDest, const char* aSource );
94 
104 std::string EscapedUTF8( wxString aString );
105 
109 wxString EscapedHTML( const wxString& aString );
110 
116 char* GetLine( FILE* aFile, char* Line, int* LineNum = NULL, int SizeLine = 255 );
117 
123 char* StrPurge( char* text );
124 
128 wxString DateAndTime();
129 
144 int StrNumCmp( const wxString& aString1, const wxString& aString2, bool aIgnoreCase = false );
145 
151 bool WildCompareString( const wxString& pattern,
152  const wxString& string_to_tst,
153  bool case_sensitive = true );
154 
162 int ValueStringCompare( wxString strFWord, wxString strSWord );
163 
170 int SplitString( wxString strToSplit,
171  wxString* strBeginning,
172  wxString* strDigits,
173  wxString* strEnd );
174 
181 int GetTrailingInt( const wxString& aStr );
182 
186 wxString GetIllegalFileNameWxChars();
187 
202 bool ReplaceIllegalFileNameChars( std::string* aName, int aReplaceChar = 0 );
203 bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar = 0 );
204 
205 #ifndef HAVE_STRTOKR
206 // common/strtok_r.c optionally:
207 extern "C" char* strtok_r( char* str, const char* delim, char** nextp );
208 #endif
209 
210 
216 {
217  bool operator() (const wxString& strA, const wxString& strB ) const
218  {
219  wxString::const_reverse_iterator sA = strA.rbegin();
220  wxString::const_reverse_iterator eA = strA.rend();
221 
222  wxString::const_reverse_iterator sB = strB.rbegin();
223  wxString::const_reverse_iterator eB = strB.rend();
224 
225  if( strA.empty() )
226  {
227  if( strB.empty() )
228  return false;
229 
230  // note: this rule implies that a null string is first in the sort order
231  return true;
232  }
233 
234  if( strB.empty() )
235  return false;
236 
237  while( sA != eA && sB != eB )
238  {
239  if( (*sA) == (*sB) )
240  {
241  ++sA;
242  ++sB;
243  continue;
244  }
245 
246  if( (*sA) < (*sB) )
247  return true;
248  else
249  return false;
250  }
251 
252  if( sB == eB )
253  return false;
254 
255  return true;
256  }
257 };
258 
268 static inline std::vector<std::string> split( const std::string& aStr, const std::string& aDelim )
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 }
292 
293 
295 inline void AccumulateDescription( wxString& aDesc, const wxString& aItem )
296 {
297  if( !aDesc.IsEmpty() )
298  aDesc << wxT( ", " );
299 
300  aDesc << aItem;
301 }
302 
303 
311 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
312 
319 void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = 1 );
320 
321 #endif // KICAD_STRING_H_
wxString GetIllegalFileNameWxChars()
Definition: string.cpp:729
std::string EscapedUTF8(wxString aString)
Function EscapedUTF8 returns an 8 bit UTF8 string given aString in unicode form.
Definition: string.cpp:304
ESCAPE_CONTEXT
Escape/Unescape routines to safely encode reserved-characters in various contexts.
Definition: kicad_string.h:52
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: string.cpp:796
A helper for sorting strings from the rear.
Definition: kicad_string.h:215
wxString DateAndTime()
Definition: string.cpp:400
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:152
int GetTrailingInt(const wxString &aStr)
Gets the trailing int, if any, from a string.
Definition: string.cpp:705
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:596
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase=false)
Compare two strings with alphanumerical content.
Definition: string.cpp:409
bool ReplaceIllegalFileNameChars(std::string *aName, int aReplaceChar=0)
Checks aName for illegal file name characters.
Definition: string.cpp:735
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed=1)
Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number.
Definition: string.cpp:819
bool operator()(const wxString &strA, const wxString &strB) const
Definition: kicad_string.h:217
void AccumulateDescription(wxString &aDesc, const wxString &aItem)
Utility to build comma separated lists in messages.
Definition: kicad_string.h:295
#define NULL
wxString EscapedHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
Definition: string.cpp:341
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:644
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:488
char * strtok_r(char *str, const char *delim, char **nextp)
char * GetLine(FILE *aFile, char *Line, int *LineNum=NULL, int SizeLine=255)
Read one line line from aFile.
Definition: string.cpp:384
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:255
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:268
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
Definition: string.cpp:365