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
54KICOMMON_API std::vector<wxString> SplitGraphemeClusters( const wxString& aText );
55
73
81KICOMMON_API wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext );
82
83KICOMMON_API wxString UnescapeString( const wxString& aSource );
84
88KICOMMON_API wxString PrettyPrintForMenu( const wxString& aString );
89
93KICOMMON_API wxString TitleCaps( const wxString& aString );
94
98KICOMMON_API wxString InitialCaps( const wxString& aString );
99
113KICOMMON_API int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize );
114
123KICOMMON_API int ReadDelimitedText( wxString* aDest, const char* aSource );
124
134KICOMMON_API std::string EscapedUTF8( const wxString& aString );
135
139KICOMMON_API wxString EscapeHTML( const wxString& aString );
140
144KICOMMON_API wxString UnescapeHTML( const wxString& aString );
145
152KICOMMON_API wxString RemoveHTMLTags( const wxString& aInput );
153
157KICOMMON_API wxString LinkifyHTML( wxString aStr );
158
162KICOMMON_API bool IsURL( wxString aStr );
163
169KICOMMON_API char* GetLine( FILE* aFile, char* Line, int* LineNum = nullptr, int SizeLine = 255 );
170
174KICOMMON_API bool NoPrintableChars( const wxString& aString );
175
180KICOMMON_API int PrintableCharCount( const wxString& aString );
181
187KICOMMON_API char* StrPurge( char* text );
188
193
208KICOMMON_API int StrNumCmp( const wxString& aString1, const wxString& aString2,
209 bool aIgnoreCase = false );
210
211
217
218
222template <typename T>
223inline void StrNumSort( T& aList, CASE_SENSITIVITY aCaseSensitivity )
224{
225 std::sort( aList.begin(), aList.end(),
226 [aCaseSensitivity]( const wxString& lhs, const wxString& rhs )
227 {
228 return StrNumCmp( lhs, rhs, aCaseSensitivity == CASE_SENSITIVITY::INSENSITIVE ) < 0;
229 } );
230}
231
232
238KICOMMON_API bool WildCompareString( const wxString& pattern,
239 const wxString& string_to_tst,
240 bool case_sensitive = true );
241
249KICOMMON_API int ValueStringCompare( const wxString& strFWord, const wxString& strSWord );
250
257KICOMMON_API int SplitString( const wxString& strToSplit,
258 wxString* strBeginning,
259 wxString* strDigits,
260 wxString* strEnd );
261
268KICOMMON_API int GetTrailingInt( const wxString& aStr );
269
274
280KICOMMON_API bool IsFullFileNameValid( const wxString& aFullFilename );
281
296KICOMMON_API bool ReplaceIllegalFileNameChars( std::string& aName, int aReplaceChar = 0 );
297KICOMMON_API bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar = 0 );
298
299
306{
307 bool operator() ( const wxString& strA, const wxString& strB ) const
308 {
309 wxString::const_reverse_iterator sA = strA.rbegin();
310 wxString::const_reverse_iterator eA = strA.rend();
311
312 wxString::const_reverse_iterator sB = strB.rbegin();
313 wxString::const_reverse_iterator eB = strB.rend();
314
315 if( strA.empty() )
316 {
317 if( strB.empty() )
318 return false;
319
320 // note: this rule implies that a null string is first in the sort order
321 return true;
322 }
323
324 if( strB.empty() )
325 return false;
326
327 while( sA != eA && sB != eB )
328 {
329 if( ( *sA ) == ( *sB ) )
330 {
331 ++sA;
332 ++sB;
333 continue;
334 }
335
336 if( ( *sA ) < ( *sB ) )
337 return true;
338 else
339 return false;
340 }
341
342 if( sB == eB )
343 return false;
344
345 return true;
346 }
347};
348
358static inline std::vector<std::string> split( const std::string& aStr, const std::string& aDelim )
359{
360 size_t pos = 0;
361 size_t last_pos = 0;
362 size_t len;
363
364 std::vector<std::string> tokens;
365
366 while( pos < aStr.size() )
367 {
368 pos = aStr.find_first_of( aDelim, last_pos );
369
370 if( pos == std::string::npos )
371 pos = aStr.size();
372
373 len = pos - last_pos;
374
375 tokens.push_back( aStr.substr( last_pos, len ) );
376
377 last_pos = pos + 1;
378 }
379
380 return tokens;
381}
382
384inline void AccumulateDescription( wxString& aDesc, const wxString& aItem )
385{
386 if( !aDesc.IsEmpty() )
387 aDesc << wxT( ", " );
388
389 aDesc << aItem;
390}
391
392
397template <typename T>
398inline void AccumulateDescriptions( wxString& aDesc, const T& aItemCollection )
399{
400 for( const auto& item : aItemCollection )
401 AccumulateDescription( aDesc, item );
402}
403
404
405template <typename T>
406inline wxString AccumulateDescriptions( const T& aItemCollection )
407{
408 wxString desc;
409 AccumulateDescriptions( desc, aItemCollection );
410 return desc;
411}
412
420KICOMMON_API void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
421
428KICOMMON_API void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = 1 );
429
439KICOMMON_API std::string UIDouble2Str( double aValue );
440
449KICOMMON_API std::string FormatDouble2Str( double aValue );
450
458KICOMMON_API wxString FormatTrimmedDecimal( double aValue, int aPrecision );
459
469#define TO_UTF8( wxstring ) ( (const char*) ( wxstring ).utf8_str() )
470
476KICOMMON_API wxString From_UTF8( const std::string& aString );
477KICOMMON_API wxString From_UTF8( const char* cstring );
478
493KICOMMON_API wxString NormalizeFileUri( const wxString& aFileUri );
494
507KICOMMON_API wxString ConvertPathToFileUri( const wxString& aPath, const PROJECT* aProject = nullptr );
508
524KICOMMON_API std::vector<wxString> ExpandStackedPinNotation( const wxString& aPinName,
525 bool* aValid = nullptr );
526
537KICOMMON_API wxString EscapeStackedPinItem( const wxString& aPinNumber );
538
548KICOMMON_API std::vector<wxString> SplitStackedPinDisplayItems( const wxString& aInner );
549
560KICOMMON_API int CountStackedPinNotation( const wxString& aPinName, bool* aValid = nullptr );
561
562
564KICOMMON_API bool IsAutoGeneratedNetName( const wxString& aNetName );
565
566
568KICOMMON_API wxString GetVariantFileToken( const wxString& aVariantName );
569KICOMMON_API wxString ExpandVariantOutputPath( const wxString& aPath, const wxString& aVariantName );
570
571KICOMMON_API int SortVariantNames( const wxString& aLhs, const wxString& aRhs );
572
573class IO_ERROR;
574
582KICOMMON_API wxString ExtractLibraryLoadException( const IO_ERROR& aException );
583
584#endif // STRING_UTILS_H
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Container for project specific data.
Definition project.h:63
#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.
KICOMMON_API wxString InitialCaps(const wxString &aString)
Capitalize only the first word.
KICOMMON_API wxString EscapeStackedPinItem(const wxString &aPinNumber)
Escape the characters that carry structural meaning inside stacked pin notation ('[',...
KICOMMON_API bool IsAutoGeneratedNetName(const wxString &aNetName)
Recognize the reserved prefixes used for generated pin-net names.
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 std::vector< wxString > SplitStackedPinDisplayItems(const wxString &aInner)
Split the inner part of a stacked notation string (the text between the brackets) into its individual...
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 wxString FormatTrimmedDecimal(double aValue, int aPrecision)
Format a value to aPrecision decimal digits and strip trailing zeros, keeping one digit after the dec...
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_REGEX
@ 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 ExtractLibraryLoadException(const IO_ERROR &aException)
Parse library load error messages, extracting user-facing information while stripping internal code l...
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 std::vector< wxString > SplitGraphemeClusters(const wxString &aText)
Split text into the units a reader sees as one character.
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 ExpandVariantOutputPath(const wxString &aPath, const wxString &aVariantName)
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 GetVariantFileToken(const wxString &aVariantName)
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.
A helper for sorting strings from the rear.
bool operator()(const wxString &strA, const wxString &strB) const