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, 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 <algorithm>
28#include <string>
29#include <vector>
30
31#include <wx/string.h>
32#include <wx/filename.h>
33
34#include <kicommon.h>
35
36class PROJECT;
37
38void ConvertMarkdown2Html( const wxString& aMarkdownInput, wxString& aHtmlOutput );
39
43KICOMMON_API wxString ConvertToNewOverbarNotation( const wxString& aOldStr );
44
50KICOMMON_API bool ConvertSmartQuotesAndDashes( wxString* aString );
51
68
76KICOMMON_API wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext );
77
78KICOMMON_API wxString UnescapeString( const wxString& aSource );
79
83KICOMMON_API wxString PrettyPrintForMenu( const wxString& aString );
84
88KICOMMON_API wxString TitleCaps( const wxString& aString );
89
93KICOMMON_API wxString InitialCaps( const wxString& aString );
94
108KICOMMON_API int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize );
109
118KICOMMON_API int ReadDelimitedText( wxString* aDest, const char* aSource );
119
129KICOMMON_API std::string EscapedUTF8( const wxString& aString );
130
134KICOMMON_API wxString EscapeHTML( const wxString& aString );
135
139KICOMMON_API wxString UnescapeHTML( const wxString& aString );
140
147KICOMMON_API wxString RemoveHTMLTags( const wxString& aInput );
148
152KICOMMON_API wxString LinkifyHTML( wxString aStr );
153
157KICOMMON_API bool IsURL( wxString aStr );
158
164KICOMMON_API char* GetLine( FILE* aFile, char* Line, int* LineNum = nullptr, int SizeLine = 255 );
165
169KICOMMON_API bool NoPrintableChars( const wxString& aString );
170
175KICOMMON_API int PrintableCharCount( const wxString& aString );
176
182KICOMMON_API char* StrPurge( char* text );
183
188
203KICOMMON_API int StrNumCmp( const wxString& aString1, const wxString& aString2,
204 bool aIgnoreCase = false );
205
206
212
213
217template <typename T>
218inline void StrNumSort( T& aList, CASE_SENSITIVITY aCaseSensitivity )
219{
220 std::sort( aList.begin(), aList.end(),
221 [aCaseSensitivity]( const wxString& lhs, const wxString& rhs )
222 {
223 return StrNumCmp( lhs, rhs, aCaseSensitivity == CASE_SENSITIVITY::INSENSITIVE ) < 0;
224 } );
225}
226
227
233KICOMMON_API bool WildCompareString( const wxString& pattern,
234 const wxString& string_to_tst,
235 bool case_sensitive = true );
236
244KICOMMON_API int ValueStringCompare( const wxString& strFWord, const wxString& strSWord );
245
252KICOMMON_API int SplitString( const wxString& strToSplit,
253 wxString* strBeginning,
254 wxString* strDigits,
255 wxString* strEnd );
256
263KICOMMON_API int GetTrailingInt( const wxString& aStr );
264
269
275KICOMMON_API bool IsFullFileNameValid( const wxString& aFullFilename );
276
291KICOMMON_API bool ReplaceIllegalFileNameChars( std::string& aName, int aReplaceChar = 0 );
292KICOMMON_API bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar = 0 );
293
294
301{
302 bool operator() ( const wxString& strA, const wxString& strB ) const
303 {
304 wxString::const_reverse_iterator sA = strA.rbegin();
305 wxString::const_reverse_iterator eA = strA.rend();
306
307 wxString::const_reverse_iterator sB = strB.rbegin();
308 wxString::const_reverse_iterator eB = strB.rend();
309
310 if( strA.empty() )
311 {
312 if( strB.empty() )
313 return false;
314
315 // note: this rule implies that a null string is first in the sort order
316 return true;
317 }
318
319 if( strB.empty() )
320 return false;
321
322 while( sA != eA && sB != eB )
323 {
324 if( ( *sA ) == ( *sB ) )
325 {
326 ++sA;
327 ++sB;
328 continue;
329 }
330
331 if( ( *sA ) < ( *sB ) )
332 return true;
333 else
334 return false;
335 }
336
337 if( sB == eB )
338 return false;
339
340 return true;
341 }
342};
343
353static inline std::vector<std::string> split( const std::string& aStr, const std::string& aDelim )
354{
355 size_t pos = 0;
356 size_t last_pos = 0;
357 size_t len;
358
359 std::vector<std::string> tokens;
360
361 while( pos < aStr.size() )
362 {
363 pos = aStr.find_first_of( aDelim, last_pos );
364
365 if( pos == std::string::npos )
366 pos = aStr.size();
367
368 len = pos - last_pos;
369
370 tokens.push_back( aStr.substr( last_pos, len ) );
371
372 last_pos = pos + 1;
373 }
374
375 return tokens;
376}
377
379inline void AccumulateDescription( wxString& aDesc, const wxString& aItem )
380{
381 if( !aDesc.IsEmpty() )
382 aDesc << wxT( ", " );
383
384 aDesc << aItem;
385}
386
387
392template <typename T>
393inline void AccumulateDescriptions( wxString& aDesc, const T& aItemCollection )
394{
395 for( const auto& item : aItemCollection )
396 AccumulateDescription( aDesc, item );
397}
398
399
400template <typename T>
401inline wxString AccumulateDescriptions( const T& aItemCollection )
402{
403 wxString desc;
404 AccumulateDescriptions( desc, aItemCollection );
405 return desc;
406}
407
415KICOMMON_API void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
416
423KICOMMON_API void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = 1 );
424
434KICOMMON_API std::string UIDouble2Str( double aValue );
435
444KICOMMON_API std::string FormatDouble2Str( double aValue );
445
455#define TO_UTF8( wxstring ) ( (const char*) ( wxstring ).utf8_str() )
456
462KICOMMON_API wxString From_UTF8( const std::string& aString );
463KICOMMON_API wxString From_UTF8( const char* cstring );
464
479KICOMMON_API wxString NormalizeFileUri( const wxString& aFileUri );
480
493KICOMMON_API wxString ConvertPathToFileUri( const wxString& aPath, const PROJECT* aProject = nullptr );
494
510KICOMMON_API std::vector<wxString> ExpandStackedPinNotation( const wxString& aPinName,
511 bool* aValid = nullptr );
512
523KICOMMON_API int CountStackedPinNotation( const wxString& aPinName, bool* aValid = nullptr );
524
525
527
528KICOMMON_API int SortVariantNames( const wxString& aLhs, const wxString& aRhs );
529
530struct LOAD_MESSAGE;
531
540KICOMMON_API std::vector<LOAD_MESSAGE> ExtractLibraryLoadErrors( const wxString& aErrorString,
541 int aSeverity );
542
543#endif // STRING_UTILS_H
Container for project specific data.
Definition project.h:66
#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:54
A helper for sorting strings from the rear.
bool operator()(const wxString &strA, const wxString &strB) const