KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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
35void ConvertMarkdown2Html( const wxString& aMarkdownInput, wxString& aHtmlOutput );
36
40wxString ConvertToNewOverbarNotation( const wxString& aOldStr );
41
47bool ConvertSmartQuotesAndDashes( wxString* aString );
48
53{
63 CTX_NO_SPACE // to replace spaces in names that do not accept spaces
64};
65
73wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext );
74
75wxString UnescapeString( const wxString& aSource );
76
80wxString PrettyPrintForMenu( const wxString& aString );
81
85wxString TitleCaps( const wxString& aString );
86
100int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize );
101
110int ReadDelimitedText( wxString* aDest, const char* aSource );
111
121std::string EscapedUTF8( const wxString& aString );
122
126wxString EscapeHTML( const wxString& aString );
127
133char* GetLine( FILE* aFile, char* Line, int* LineNum = nullptr, int SizeLine = 255 );
134
138bool NoPrintableChars( const wxString& aString );
139
144int PrintableCharCount( const wxString& aString );
145
151char* StrPurge( char* text );
152
156wxString DateAndTime();
157
172int StrNumCmp( const wxString& aString1, const wxString& aString2, bool aIgnoreCase = false );
173
179bool WildCompareString( const wxString& pattern,
180 const wxString& string_to_tst,
181 bool case_sensitive = true );
182
190int ValueStringCompare( const wxString& strFWord, const wxString& strSWord );
191
198int SplitString( const wxString& strToSplit,
199 wxString* strBeginning,
200 wxString* strDigits,
201 wxString* strEnd );
202
209int GetTrailingInt( const wxString& aStr );
210
215
230bool ReplaceIllegalFileNameChars( std::string* aName, int aReplaceChar = 0 );
231bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar = 0 );
232
233#ifndef HAVE_STRTOKR
234// common/strtok_r.c optionally:
235extern "C" char* strtok_r( char* str, const char* delim, char** nextp );
236#endif
237
238
245{
246 bool operator() ( const wxString& strA, const wxString& strB ) const
247 {
248 wxString::const_reverse_iterator sA = strA.rbegin();
249 wxString::const_reverse_iterator eA = strA.rend();
250
251 wxString::const_reverse_iterator sB = strB.rbegin();
252 wxString::const_reverse_iterator eB = strB.rend();
253
254 if( strA.empty() )
255 {
256 if( strB.empty() )
257 return false;
258
259 // note: this rule implies that a null string is first in the sort order
260 return true;
261 }
262
263 if( strB.empty() )
264 return false;
265
266 while( sA != eA && sB != eB )
267 {
268 if( ( *sA ) == ( *sB ) )
269 {
270 ++sA;
271 ++sB;
272 continue;
273 }
274
275 if( ( *sA ) < ( *sB ) )
276 return true;
277 else
278 return false;
279 }
280
281 if( sB == eB )
282 return false;
283
284 return true;
285 }
286};
287
297static inline std::vector<std::string> split( const std::string& aStr, const std::string& aDelim )
298{
299 size_t pos = 0;
300 size_t last_pos = 0;
301 size_t len;
302
303 std::vector<std::string> tokens;
304
305 while( pos < aStr.size() )
306 {
307 pos = aStr.find_first_of( aDelim, last_pos );
308
309 if( pos == std::string::npos )
310 pos = aStr.size();
311
312 len = pos - last_pos;
313
314 tokens.push_back( aStr.substr( last_pos, len ) );
315
316 last_pos = pos + 1;
317 }
318
319 return tokens;
320}
321
323inline void AccumulateDescription( wxString& aDesc, const wxString& aItem )
324{
325 if( !aDesc.IsEmpty() )
326 aDesc << wxT( ", " );
327
328 aDesc << aItem;
329}
330
338void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
339
346void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = 1 );
347
357std::string UIDouble2Str( double aValue );
358
367std::string FormatDouble2Str( double aValue );
368
369#endif // STRING_UTILS_H
wxString DateAndTime()
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
wxString ConvertToNewOverbarNotation(const wxString &aOldStr)
Convert the old ~...~ overbar notation to the new ~{...} one.
int GetTrailingInt(const wxString &aStr)
Gets the trailing int, if any, from a string.
wxString UnescapeString(const wxString &aSource)
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 GetIllegalFileNameWxChars()
bool ConvertSmartQuotesAndDashes(wxString *aString)
Convert curly quotes and em/en dashes to straight quotes and dashes.
char * GetLine(FILE *aFile, char *Line, int *LineNum=nullptr, int SizeLine=255)
Read one line line from aFile.
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase=false)
Compare two strings with alphanumerical content.
char * strtok_r(char *str, const char *delim, char **nextp)
int ReadDelimitedText(char *aDest, const char *aSource, int aDestSize)
Copy bytes from aSource delimited string segment to aDest buffer.
wxString TitleCaps(const wxString &aString)
Capitalize the first letter in each word.
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed=1)
Remove trailing zeros from a string containing a converted float number.
std::string UIDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 We want to avoid scientific ...
ESCAPE_CONTEXT
Escape/Unescape routines to safely encode reserved-characters in various contexts.
Definition: string_utils.h:53
@ CTX_FILENAME
Definition: string_utils.h:62
@ CTX_QUOTED_STR
Definition: string_utils.h:58
@ CTX_LINE
Definition: string_utils.h:60
@ CTX_NO_SPACE
Definition: string_utils.h:63
@ CTX_LIBID
Definition: string_utils.h:55
@ CTX_NETNAME
Definition: string_utils.h:54
@ CTX_CSV
Definition: string_utils.h:61
@ CTX_IPC
Definition: string_utils.h:57
@ CTX_LEGACY_LIBID
Definition: string_utils.h:56
@ CTX_JS_STR
Definition: string_utils.h:59
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:297
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
int PrintableCharCount(const wxString &aString)
Return the number of printable (ie: non-formatting) chars.
std::string EscapedUTF8(const wxString &aString)
Return an 8 bit UTF8 string given aString in Unicode form.
wxString PrettyPrintForMenu(const wxString &aString)
Remove markup (such as overbar or subscript) that we can't render to menu items.
void ConvertMarkdown2Html(const wxString &aMarkdownInput, wxString &aHtmlOutput)
void AccumulateDescription(wxString &aDesc, const wxString &aItem)
Utility to build comma separated lists in messages.
Definition: string_utils.h:323
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
int ValueStringCompare(const wxString &strFWord, const wxString &strSWord)
Compare strings like the strcmp function but handle numbers and modifiers within the string text corr...
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.
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
bool NoPrintableChars(const wxString &aString)
Return true if the string is empty or contains only whitespace.
bool ReplaceIllegalFileNameChars(std::string *aName, int aReplaceChar=0)
Checks aName for illegal file name characters.
A helper for sorting strings from the rear.
Definition: string_utils.h:245
bool operator()(const wxString &strA, const wxString &strB) const
Definition: string_utils.h:246