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 The KiCad Developers, see AUTHORS.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, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef STRING_UTILS_H
21#define STRING_UTILS_H
22
23#include <algorithm>
24#include <string>
25#include <vector>
26
27#include <wx/string.h>
28#include <wx/filename.h>
29
30#include <kicommon.h>
31
32class PROJECT;
33
34void ConvertMarkdown2Html( const wxString& aMarkdownInput, wxString& aHtmlOutput );
35
39KICOMMON_API wxString ConvertToNewOverbarNotation( const wxString& aOldStr );
40
46KICOMMON_API bool ConvertSmartQuotesAndDashes( wxString* aString );
47
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
89KICOMMON_API wxString InitialCaps( const wxString& aString );
90
104KICOMMON_API int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize );
105
114KICOMMON_API int ReadDelimitedText( wxString* aDest, const char* aSource );
115
125KICOMMON_API std::string EscapedUTF8( const wxString& aString );
126
130KICOMMON_API wxString EscapeHTML( const wxString& aString );
131
135KICOMMON_API wxString UnescapeHTML( const wxString& aString );
136
143KICOMMON_API wxString RemoveHTMLTags( const wxString& aInput );
144
148KICOMMON_API wxString LinkifyHTML( wxString aStr );
149
153KICOMMON_API bool IsURL( wxString aStr );
154
160KICOMMON_API char* GetLine( FILE* aFile, char* Line, int* LineNum = nullptr, int SizeLine = 255 );
161
165KICOMMON_API bool NoPrintableChars( const wxString& aString );
166
171KICOMMON_API int PrintableCharCount( const wxString& aString );
172
178KICOMMON_API char* StrPurge( char* text );
179
184
199KICOMMON_API int StrNumCmp( const wxString& aString1, const wxString& aString2,
200 bool aIgnoreCase = false );
201
202
208
209
213template <typename T>
214inline void StrNumSort( T& aList, CASE_SENSITIVITY aCaseSensitivity )
215{
216 std::sort( aList.begin(), aList.end(),
217 [aCaseSensitivity]( const wxString& lhs, const wxString& rhs )
218 {
219 return StrNumCmp( lhs, rhs, aCaseSensitivity == CASE_SENSITIVITY::INSENSITIVE ) < 0;
220 } );
221}
222
223
229KICOMMON_API bool WildCompareString( const wxString& pattern,
230 const wxString& string_to_tst,
231 bool case_sensitive = true );
232
240KICOMMON_API int ValueStringCompare( const wxString& strFWord, const wxString& strSWord );
241
248KICOMMON_API int SplitString( const wxString& strToSplit,
249 wxString* strBeginning,
250 wxString* strDigits,
251 wxString* strEnd );
252
259KICOMMON_API int GetTrailingInt( const wxString& aStr );
260
265
271KICOMMON_API bool IsFullFileNameValid( const wxString& aFullFilename );
272
287KICOMMON_API bool ReplaceIllegalFileNameChars( std::string& aName, int aReplaceChar = 0 );
288KICOMMON_API bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar = 0 );
289
290
297{
298 bool operator() ( const wxString& strA, const wxString& strB ) const
299 {
300 wxString::const_reverse_iterator sA = strA.rbegin();
301 wxString::const_reverse_iterator eA = strA.rend();
302
303 wxString::const_reverse_iterator sB = strB.rbegin();
304 wxString::const_reverse_iterator eB = strB.rend();
305
306 if( strA.empty() )
307 {
308 if( strB.empty() )
309 return false;
310
311 // note: this rule implies that a null string is first in the sort order
312 return true;
313 }
314
315 if( strB.empty() )
316 return false;
317
318 while( sA != eA && sB != eB )
319 {
320 if( ( *sA ) == ( *sB ) )
321 {
322 ++sA;
323 ++sB;
324 continue;
325 }
326
327 if( ( *sA ) < ( *sB ) )
328 return true;
329 else
330 return false;
331 }
332
333 if( sB == eB )
334 return false;
335
336 return true;
337 }
338};
339
349static inline std::vector<std::string> split( const std::string& aStr, const std::string& aDelim )
350{
351 size_t pos = 0;
352 size_t last_pos = 0;
353 size_t len;
354
355 std::vector<std::string> tokens;
356
357 while( pos < aStr.size() )
358 {
359 pos = aStr.find_first_of( aDelim, last_pos );
360
361 if( pos == std::string::npos )
362 pos = aStr.size();
363
364 len = pos - last_pos;
365
366 tokens.push_back( aStr.substr( last_pos, len ) );
367
368 last_pos = pos + 1;
369 }
370
371 return tokens;
372}
373
375inline void AccumulateDescription( wxString& aDesc, const wxString& aItem )
376{
377 if( !aDesc.IsEmpty() )
378 aDesc << wxT( ", " );
379
380 aDesc << aItem;
381}
382
383
388template <typename T>
389inline void AccumulateDescriptions( wxString& aDesc, const T& aItemCollection )
390{
391 for( const auto& item : aItemCollection )
392 AccumulateDescription( aDesc, item );
393}
394
395
396template <typename T>
397inline wxString AccumulateDescriptions( const T& aItemCollection )
398{
399 wxString desc;
400 AccumulateDescriptions( desc, aItemCollection );
401 return desc;
402}
403
411KICOMMON_API void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
412
419KICOMMON_API void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = 1 );
420
430KICOMMON_API std::string UIDouble2Str( double aValue );
431
440KICOMMON_API std::string FormatDouble2Str( double aValue );
441
451#define TO_UTF8( wxstring ) ( (const char*) ( wxstring ).utf8_str() )
452
458KICOMMON_API wxString From_UTF8( const std::string& aString );
459KICOMMON_API wxString From_UTF8( const char* cstring );
460
475KICOMMON_API wxString NormalizeFileUri( const wxString& aFileUri );
476
489KICOMMON_API wxString ConvertPathToFileUri( const wxString& aPath, const PROJECT* aProject = nullptr );
490
506KICOMMON_API std::vector<wxString> ExpandStackedPinNotation( const wxString& aPinName,
507 bool* aValid = nullptr );
508
519KICOMMON_API int CountStackedPinNotation( const wxString& aPinName, bool* aValid = nullptr );
520
521
523
524KICOMMON_API int SortVariantNames( const wxString& aLhs, const wxString& aRhs );
525
526struct LOAD_MESSAGE;
527
536KICOMMON_API std::vector<LOAD_MESSAGE> ExtractLibraryLoadErrors( const wxString& aErrorString,
537 int aSeverity );
538
539#endif // STRING_UTILS_H
Container for project specific data.
Definition project.h:62
#define KICOMMON_API
Definition kicommon.h:27
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 std::vector< wxString > ExpandStackedPinNotation(const wxString &aPinName, bool *aValid=nullptr)
Expand stacked pin notation like [1,2,3], [1-4], [A1-A4], or [AA1-AA3,AB4,CD12-CD14] into individual ...
KICOMMON_API int CountStackedPinNotation(const wxString &aPinName, bool *aValid=nullptr)
Count the number of pins represented by stacked pin notation without allocating strings.
KICOMMON_API wxString InitialCaps(const wxString &aString)
Capitalize only the first word.
KICOMMON_API bool IsFullFileNameValid(const wxString &aFullFilename)
Checks if a full filename is valid, i.e.
KICOMMON_API int SortVariantNames(const wxString &aLhs, const wxString &aRhs)
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 std::vector< LOAD_MESSAGE > ExtractLibraryLoadErrors(const wxString &aErrorString, int aSeverity)
Parse library load error messages, extracting user-facing information while stripping internal code l...
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.
void StrNumSort(T &aList, CASE_SENSITIVITY aCaseSensitivity)
Sort a container of wxString objects, in place, using the StrNumCmp() function.
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.
CASE_SENSITIVITY
KICOMMON_API bool ReplaceIllegalFileNameChars(std::string &aName, int aReplaceChar=0)
Checks aName for illegal file name characters.
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.
@ CTX_FILENAME
@ CTX_QUOTED_STR
@ CTX_LINE
@ CTX_NO_SPACE
@ CTX_LIBID
@ CTX_NETNAME
@ CTX_CSV
@ CTX_IPC
@ CTX_LEGACY_LIBID
@ CTX_JS_STR
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.
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 bool IsURL(wxString aStr)
Performs a URL sniff-test on a string.
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 ConvertPathToFileUri(const wxString &aPath, const PROJECT *aProject=nullptr)
Convert a file path to a file:// URI.
KICOMMON_API wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
KICOMMON_API wxString GetDefaultVariantName()
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.
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 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...
void AccumulateDescriptions(wxString &aDesc, const T &aItemCollection)
Build a comma-separated list from a collection of wxStrings.
KICOMMON_API char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
KISTATUSBAR is a wxStatusBar suitable for Kicad manager.
Definition kistatusbar.h:50
A helper for sorting strings from the rear.
bool operator()(const wxString &strA, const wxString &strB) const