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, 2024 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 <string>
28#include <vector>
29#include <wx/string.h>
30#include <wx/filename.h>
31
32#include <kicommon.h>
33
34void ConvertMarkdown2Html( const wxString& aMarkdownInput, wxString& aHtmlOutput );
35
39KICOMMON_API wxString ConvertToNewOverbarNotation( const wxString& aOldStr );
40
46KICOMMON_API bool ConvertSmartQuotesAndDashes( wxString* aString );
47
52{
62 CTX_NO_SPACE // to replace spaces in names that do not accept spaces
63};
64
72KICOMMON_API wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext );
73
74KICOMMON_API wxString UnescapeString( const wxString& aSource );
75
79KICOMMON_API wxString PrettyPrintForMenu( const wxString& aString );
80
84KICOMMON_API wxString TitleCaps( const wxString& aString );
85
99KICOMMON_API int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize );
100
109KICOMMON_API int ReadDelimitedText( wxString* aDest, const char* aSource );
110
120KICOMMON_API std::string EscapedUTF8( const wxString& aString );
121
125KICOMMON_API wxString EscapeHTML( const wxString& aString );
126
130KICOMMON_API wxString UnescapeHTML( const wxString& aString );
131
138KICOMMON_API wxString RemoveHTMLTags( const wxString& aInput );
139
143KICOMMON_API wxString LinkifyHTML( wxString aStr );
144
150KICOMMON_API char* GetLine( FILE* aFile, char* Line, int* LineNum = nullptr, int SizeLine = 255 );
151
155KICOMMON_API bool NoPrintableChars( const wxString& aString );
156
161KICOMMON_API int PrintableCharCount( const wxString& aString );
162
168KICOMMON_API char* StrPurge( char* text );
169
174
189KICOMMON_API int StrNumCmp( const wxString& aString1, const wxString& aString2,
190 bool aIgnoreCase = false );
191
197KICOMMON_API bool WildCompareString( const wxString& pattern,
198 const wxString& string_to_tst,
199 bool case_sensitive = true );
200
208KICOMMON_API int ValueStringCompare( const wxString& strFWord, const wxString& strSWord );
209
216KICOMMON_API int SplitString( const wxString& strToSplit,
217 wxString* strBeginning,
218 wxString* strDigits,
219 wxString* strEnd );
220
227KICOMMON_API int GetTrailingInt( const wxString& aStr );
228
233
239KICOMMON_API bool IsFullFileNameValid( const wxString& aFullFilename );
240
255KICOMMON_API bool ReplaceIllegalFileNameChars( std::string* aName, int aReplaceChar = 0 );
256KICOMMON_API bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar = 0 );
257
258
265{
266 bool operator() ( const wxString& strA, const wxString& strB ) const
267 {
268 wxString::const_reverse_iterator sA = strA.rbegin();
269 wxString::const_reverse_iterator eA = strA.rend();
270
271 wxString::const_reverse_iterator sB = strB.rbegin();
272 wxString::const_reverse_iterator eB = strB.rend();
273
274 if( strA.empty() )
275 {
276 if( strB.empty() )
277 return false;
278
279 // note: this rule implies that a null string is first in the sort order
280 return true;
281 }
282
283 if( strB.empty() )
284 return false;
285
286 while( sA != eA && sB != eB )
287 {
288 if( ( *sA ) == ( *sB ) )
289 {
290 ++sA;
291 ++sB;
292 continue;
293 }
294
295 if( ( *sA ) < ( *sB ) )
296 return true;
297 else
298 return false;
299 }
300
301 if( sB == eB )
302 return false;
303
304 return true;
305 }
306};
307
317static inline std::vector<std::string> split( const std::string& aStr, const std::string& aDelim )
318{
319 size_t pos = 0;
320 size_t last_pos = 0;
321 size_t len;
322
323 std::vector<std::string> tokens;
324
325 while( pos < aStr.size() )
326 {
327 pos = aStr.find_first_of( aDelim, last_pos );
328
329 if( pos == std::string::npos )
330 pos = aStr.size();
331
332 len = pos - last_pos;
333
334 tokens.push_back( aStr.substr( last_pos, len ) );
335
336 last_pos = pos + 1;
337 }
338
339 return tokens;
340}
341
343inline void AccumulateDescription( wxString& aDesc, const wxString& aItem )
344{
345 if( !aDesc.IsEmpty() )
346 aDesc << wxT( ", " );
347
348 aDesc << aItem;
349}
350
358KICOMMON_API void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
359
366KICOMMON_API void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = 1 );
367
377KICOMMON_API std::string UIDouble2Str( double aValue );
378
387KICOMMON_API std::string FormatDouble2Str( double aValue );
388
398#define TO_UTF8( wxstring ) ( (const char*) ( wxstring ).utf8_str() )
399
405KICOMMON_API wxString From_UTF8( const std::string& aString );
406KICOMMON_API wxString From_UTF8( const char* cstring );
407
422KICOMMON_API wxString NormalizeFileUri( const wxString& aFileUri );
423
424
425#endif // STRING_UTILS_H
#define KICOMMON_API
Definition: kicommon.h:28
KICOMMON_API 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.
KICOMMON_API wxString PrettyPrintForMenu(const wxString &aString)
Remove markup (such as overbar or subscript) that we can't render to menu items.
KICOMMON_API bool IsFullFileNameValid(const wxString &aFullFilename)
Checks if a full filename is valid, i.e.
KICOMMON_API std::string UIDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 We want to avoid scientific ...
KICOMMON_API wxString From_UTF8(const std::string &aString)
Convert an expected UTF8 encoded std::string to a wxString.
KICOMMON_API wxString TitleCaps(const wxString &aString)
Capitalize the first letter in each word.
KICOMMON_API bool ConvertSmartQuotesAndDashes(wxString *aString)
Convert curly quotes and em/en dashes to straight quotes and dashes.
KICOMMON_API void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed=1)
Remove trailing zeros from a string containing a converted float number.
KICOMMON_API wxString GetIllegalFileNameWxChars()
KICOMMON_API wxString NormalizeFileUri(const wxString &aFileUri)
Normalize file path aFileUri to URI convention.
KICOMMON_API 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.
KICOMMON_API wxString RemoveHTMLTags(const wxString &aInput)
Removes HTML tags from a string.
KICOMMON_API wxString GetISO8601CurrentDateTime()
KICOMMON_API int ReadDelimitedText(char *aDest, const char *aSource, int aDestSize)
Copy bytes from aSource delimited string segment to aDest buffer.
ESCAPE_CONTEXT
Escape/Unescape routines to safely encode reserved-characters in various contexts.
Definition: string_utils.h:52
@ CTX_FILENAME
Definition: string_utils.h:61
@ CTX_QUOTED_STR
Definition: string_utils.h:57
@ CTX_LINE
Definition: string_utils.h:59
@ CTX_NO_SPACE
Definition: string_utils.h:62
@ CTX_LIBID
Definition: string_utils.h:54
@ CTX_NETNAME
Definition: string_utils.h:53
@ CTX_CSV
Definition: string_utils.h:60
@ CTX_IPC
Definition: string_utils.h:56
@ CTX_LEGACY_LIBID
Definition: string_utils.h:55
@ CTX_JS_STR
Definition: string_utils.h:58
KICOMMON_API std::string EscapedUTF8(const wxString &aString)
Return an 8 bit UTF8 string given aString in Unicode form.
KICOMMON_API int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase=false)
Compare two strings with alphanumerical content.
KICOMMON_API wxString UnescapeString(const wxString &aSource)
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:317
KICOMMON_API wxString ConvertToNewOverbarNotation(const wxString &aOldStr)
Convert the old ~...~ overbar notation to the new ~{...} one.
KICOMMON_API void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
KICOMMON_API wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
KICOMMON_API char * GetLine(FILE *aFile, char *Line, int *LineNum=nullptr, int SizeLine=255)
Read one line line from aFile.
KICOMMON_API int GetTrailingInt(const wxString &aStr)
Gets the trailing int, if any, from a string.
KICOMMON_API wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
void ConvertMarkdown2Html(const wxString &aMarkdownInput, wxString &aHtmlOutput)
KICOMMON_API wxString LinkifyHTML(wxString aStr)
Wraps links in HTML tags.
void AccumulateDescription(wxString &aDesc, const wxString &aItem)
Utility to build comma separated lists in messages.
Definition: string_utils.h:343
KICOMMON_API int PrintableCharCount(const wxString &aString)
Return the number of printable (ie: non-formatting) chars.
KICOMMON_API bool NoPrintableChars(const wxString &aString)
Return true if the string is empty or contains only whitespace.
KICOMMON_API std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
KICOMMON_API bool ReplaceIllegalFileNameChars(std::string *aName, int aReplaceChar=0)
Checks aName for illegal file name characters.
KICOMMON_API wxString UnescapeHTML(const wxString &aString)
Return a new wxString unescaped from HTML format.
KICOMMON_API int ValueStringCompare(const wxString &strFWord, const wxString &strSWord)
Compare strings like the strcmp function but handle numbers and modifiers within the string text corr...
KICOMMON_API 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:265
bool operator()(const wxString &strA, const wxString &strB) const
Definition: string_utils.h:266