38#include <fmt/chrono.h>
60 wxString filtered_fullpath = aFullFilename;
65 filtered_fullpath.Replace(
"/",
"_" );
66 filtered_fullpath.Replace(
"\\",
"_" );
69 if( filtered_fullpath.Length() > 1 && filtered_fullpath[1] ==
':' )
70 filtered_fullpath[1] =
' ';
73 if( wxString::npos != filtered_fullpath.find_first_of( wxFileName::GetForbiddenChars() ) )
83 bool inOverbar =
false;
86 if( aOldStr == wxT(
"~" ) )
89 newStr.reserve( aOldStr.length() );
91 for( wxString::const_iterator chIt = aOldStr.begin(); chIt != aOldStr.end(); ++chIt )
95 wxString::const_iterator lookahead = chIt + 1;
97 if( lookahead != aOldStr.end() && *lookahead ==
'~' )
99 if( ++lookahead != aOldStr.end() && *lookahead ==
'{' )
103 newStr << wxT(
"~~{}" );
108 newStr << wxT(
"~" );
112 else if( lookahead != aOldStr.end() && *lookahead ==
'{' )
122 newStr << wxT(
"}" );
127 newStr << wxT(
"~{" );
134 else if( ( *chIt ==
' ' || *chIt ==
'}' || *chIt ==
')' ) && inOverbar )
137 newStr << wxT(
"}" );
146 newStr << wxT(
"}" );
156 for( wxString::iterator ii = aString->begin(); ii != aString->end(); ++ii )
158 if( *ii == L
'\u00B4' || *ii == L
'\u2018' || *ii == L
'\u2019' )
163 if( *ii == L
'\u201C' || *ii == L
'\u201D' )
168 if( *ii == L
'\u2013' || *ii == L
'\u2014' )
182 std::vector<bool> braceStack;
184 converted.reserve( aSource.length() );
186 for( wxUniChar c: aSource )
191 converted += wxT(
"{slash}" );
192 else if( c ==
'\n' || c ==
'\r' )
193 converted += wxEmptyString;
201 converted += wxT(
"{slash}" );
203 converted += wxT(
"{backslash}" );
205 converted += wxT(
"{lt}" );
207 converted += wxT(
"{gt}" );
209 converted += wxT(
"{colon}" );
211 converted += wxT(
"{dblquote}" );
212 else if( c ==
'\n' || c ==
'\r' )
213 converted += wxEmptyString;
220 converted += wxT(
"{slash}" );
222 converted += wxT(
"{comma}" );
224 converted += wxT(
"{dblquote}" );
231 converted += wxT(
"{dblquote}" );
237 if( c >= 0x7F || c ==
'\'' || c ==
'\\' || c ==
'(' || c ==
')' )
239 unsigned int code = c;
241 snprintf( buffer,
sizeof(buffer),
"\\u%4.4X", code );
251 if( c ==
'\n' || c ==
'\r' )
252 converted += wxT(
"{return}" );
259 converted += wxT(
"{slash}" );
261 converted += wxT(
"{backslash}" );
263 converted += wxT(
"{dblquote}" );
265 converted += wxT(
"{lt}" );
267 converted += wxT(
"{gt}" );
269 converted += wxT(
"{bar}" );
271 converted += wxT(
"{colon}" );
273 converted += wxT(
"{tab}" );
274 else if( c ==
'\n' || c ==
'\r' )
275 converted += wxT(
"{return}" );
282 converted += wxT(
"{space}" );
289 converted += wxT(
"{comma}" );
290 else if( c ==
'\n' || c ==
'\r' )
291 converted += wxT(
"{return}" );
307 size_t sourceLen = aSource.length();
316 newbuf.reserve( sourceLen );
321 for(
size_t i = 0; i < sourceLen; ++i )
330 bool terminated =
false;
332 for( i = i + 1; i < sourceLen; ++i )
356 else if( prev ==
'$' || prev ==
'~' || prev ==
'^' || prev ==
'_' )
360 else if( token == wxT(
"dblquote" ) ) newbuf << wxT(
"\"" );
361 else if( token == wxT(
"quote" ) ) newbuf << wxT(
"'" );
362 else if( token == wxT(
"lt" ) ) newbuf << wxT(
"<" );
363 else if( token == wxT(
"gt" ) ) newbuf << wxT(
">" );
364 else if( token == wxT(
"backslash" ) ) newbuf << wxT(
"\\" );
365 else if( token == wxT(
"slash" ) ) newbuf << wxT(
"/" );
366 else if( token == wxT(
"bar" ) ) newbuf << wxT(
"|" );
367 else if( token == wxT(
"comma" ) ) newbuf << wxT(
"," );
368 else if( token == wxT(
"colon" ) ) newbuf << wxT(
":" );
369 else if( token == wxT(
"space" ) ) newbuf << wxT(
" " );
370 else if( token == wxT(
"dollar" ) ) newbuf << wxT(
"$" );
371 else if( token == wxT(
"tab" ) ) newbuf << wxT(
"\t" );
372 else if( token == wxT(
"return" ) ) newbuf << wxT(
"\n" );
373 else if( token == wxT(
"brace" ) ) newbuf << wxT(
"{" );
396 result.reserve( aString.length() );
398 for(
const wxString& word : words )
400 if( !result.IsEmpty() )
401 result += wxT(
" " );
403 result += word.Capitalize();
414 const char* start = aSource;
417 while( (cc = *aSource++) != 0 )
437 if( cc !=
'"' && cc !=
'\\' )
451 return aSource - start;
461 const char* start = aSource;
462 char* limit = aDest + aDestSize - 1;
465 while( (cc = *aSource++) != 0 && aDest < limit )
485 if( cc !=
'"' && cc !=
'\\' )
500 return aSource - start;
506 wxString str = aString;
509 str.Replace( wxT(
"\r\n" ), wxT(
"\r" ) );
510 str.Replace( wxT(
"\n" ), wxT(
"\r" ) );
512 std::string utf8 =
TO_UTF8( aString );
516 ret.reserve( utf8.length() + 2 );
520 for( std::string::const_iterator it = utf8.begin(); it!=utf8.end(); ++it )
528 else if( *it ==
'\\' )
549 converted.reserve( aString.length() );
551 for( wxUniChar c : aString )
554 converted += wxT(
""" );
556 converted += wxT(
"'" );
558 converted += wxT(
"&" );
560 converted += wxT(
"<" );
562 converted += wxT(
">" );
573 wxString converted = aString;
576 static const std::map<wxString, wxString> c_replacements = {
577 { wxS(
"quot" ), wxS(
"\"" ) },
578 { wxS(
"apos" ), wxS(
"'" ) },
579 { wxS(
"amp" ), wxS(
"&" ) },
580 { wxS(
"lt" ), wxS(
"<" ) },
581 { wxS(
"gt" ), wxS(
">" ) }
586 wxString regexStr =
"&(#(\\d*)|#x([a-zA-Z0-9]{4})";
588 for(
auto& [key, value] : c_replacements )
589 regexStr <<
'|' << key;
593 wxRegEx regex( regexStr );
600 wxString str = converted;
602 while( regex.Matches( str ) )
604 std::vector<wxString> matches;
605 regex.GetMatch( &start, &len );
607 result << str.Left( start );
609 wxString code = regex.GetMatch( str, 1 );
610 wxString codeDec = regex.GetMatch( str, 2 );
611 wxString codeHex = regex.GetMatch( str, 3 );
613 if( !codeDec.IsEmpty() || !codeHex.IsEmpty() )
615 unsigned long codeVal = 0;
617 if( !codeDec.IsEmpty() )
618 codeDec.ToCULong( &codeVal );
619 else if( !codeHex.IsEmpty() )
620 codeHex.ToCULong( &codeVal, 16 );
623 result << wxUniChar( codeVal );
625 else if(
auto val =
get_opt( c_replacements, code ) )
630 str = str.Mid( start + len );
641 wxString str = aInput;
642 wxRegEx( wxS(
"<[^>]*>" ) ).ReplaceAll( &str, wxEmptyString );
650 wxRegEx regex( wxS(
"\\b(https?|ftp|file)://([-\\w+&@#/%?=~|!:,.;]*[^.,:;<>\\s\u00b6])" ),
653 regex.ReplaceAll( &aStr,
"<a href=\"\\0\">\\0</a>" );
661 wxString tmp = aString;
663 return tmp.Trim(
true ).Trim(
false ).IsEmpty();
674 int overbarDepth = -1;
675 int superSubDepth = -1;
676 int braceNesting = 0;
678 for(
auto chIt = aString.begin(), end = aString.end(); chIt < end; ++chIt )
686 else if( *chIt ==
'^' && superSubDepth == -1 )
688 auto lookahead = chIt;
690 if( ++lookahead != end && *lookahead ==
'{' )
693 superSubDepth = braceNesting;
698 else if( *chIt ==
'_' && superSubDepth == -1 )
700 auto lookahead = chIt;
702 if( ++lookahead != end && *lookahead ==
'{' )
705 superSubDepth = braceNesting;
710 else if( *chIt ==
'~' && overbarDepth == -1 )
712 auto lookahead = chIt;
714 if( ++lookahead != end && *lookahead ==
'{' )
717 overbarDepth = braceNesting;
722 else if( *chIt ==
'{' )
726 else if( *chIt ==
'}' )
728 if( braceNesting > 0 )
731 if( braceNesting == superSubDepth )
737 if( braceNesting == overbarDepth )
753 static const char whitespace[] =
" \t\n\r\f\v";
757 while( *
text && strchr( whitespace, *
text ) )
760 char* cp =
text + strlen(
text ) - 1;
762 while( cp >=
text && strchr( whitespace, *cp ) )
770char*
GetLine( FILE* File,
char* Line,
int* LineNum,
int SizeLine )
773 if( fgets( Line, SizeLine, File ) ==
nullptr )
779 }
while( Line[0] ==
'#' || Line[0] ==
'\n' || Line[0] ==
'\r' || Line[0] == 0 );
781 strtok( Line,
"\n\r" );
792#if defined(__MINGW32__) && !defined(_UCRT)
793 return fmt::format(
"{:%FT%T}", fmt::localtime( std::time(
nullptr ) ) );
795 return fmt::format(
"{:%FT%T%z}", fmt::localtime( std::time(
nullptr ) ) );
800int StrNumCmp(
const wxString& aString1,
const wxString& aString2,
bool aIgnoreCase )
802 int nb1 = 0, nb2 = 0;
804 auto str1 = aString1.begin();
805 auto str2 = aString2.begin();
807 while( str1 != aString1.end() && str2 != aString2.end() )
809 wxUniChar c1 = *str1;
810 wxUniChar c2 = *str2;
812 if( wxIsdigit( c1 ) && wxIsdigit( c2 ) )
820 nb1 = nb1 * 10 + (int) c1 -
'0';
822 }
while( str1 != aString1.end() && wxIsdigit( *str1 ) );
827 nb2 = nb2 * 10 + (int) c2 -
'0';
829 }
while( str2 != aString2.end() && wxIsdigit( *str2 ) );
837 c1 = ( str1 != aString1.end() ) ? *str1 : wxUniChar( 0 );
838 c2 = ( str2 != aString2.end() ) ? *str2 : wxUniChar( 0 );
846 wxUniChar uc1 = wxToupper( c1 );
847 wxUniChar uc2 = wxToupper( c2 );
850 return uc1 < uc2 ? -1 : 1;
862 if( str1 != aString1.end() )
865 if( str2 != aString2.end() )
869 if( str1 == aString1.end() && str2 != aString2.end() )
873 else if( str1 != aString1.end() && str2 == aString2.end() )
883 bool case_sensitive )
885 const wxChar* cp =
nullptr;
886 const wxChar* mp =
nullptr;
887 const wxChar* wild =
nullptr;
888 const wxChar* str =
nullptr;
889 wxString _pattern, _string_to_tst;
893 wild = pattern.GetData();
894 str = string_to_tst.GetData();
899 _pattern.MakeUpper();
900 _string_to_tst = string_to_tst;
901 _string_to_tst.MakeUpper();
902 wild = _pattern.GetData();
903 str = _string_to_tst.GetData();
906 while( ( *str ) && ( *wild !=
'*' ) )
908 if( ( *wild != *str ) && ( *wild !=
'?' ) )
925 else if( ( *wild == *str ) || ( *wild ==
'?' ) )
937 while( *wild ==
'*' )
949 static const wxString modifiers( wxT(
"pnuµμmkKM" ) );
951 if( !aString.length() )
957 if( modifiers.Find( aString[ 0 ] ) >= 0 )
959 modifier = aString[ 0 ];
960 units = aString.Mid( 1 ).Trim();
965 units = aString.Mid( 0 ).Trim();
969 && !units.IsSameAs( wxT(
"F" ),
false )
970 && !units.IsSameAs( wxT(
"hz" ),
false )
971 && !units.IsSameAs( wxT(
"W" ),
false )
972 && !units.IsSameAs( wxT(
"V" ),
false )
973 && !units.IsSameAs( wxT(
"A" ),
false )
974 && !units.IsSameAs( wxT(
"H" ),
false ) )
979 if( modifier ==
'p' )
981 if( modifier ==
'n' )
983 else if( modifier ==
'u' || modifier == wxS(
"µ" )[0] || modifier == wxS(
"μ" )[0] )
985 else if( modifier ==
'm' )
987 else if( modifier ==
'k' || modifier ==
'K' )
989 else if( modifier ==
'M' )
991 else if( modifier ==
'G' )
1011 value->Replace( wxS(
" " ), wxEmptyString );
1013 wxChar ambiguousSeparator =
'?';
1014 wxChar thousandsSeparator =
'?';
1015 bool thousandsSeparatorFound =
false;
1016 wxChar decimalSeparator =
'?';
1017 bool decimalSeparatorFound =
false;
1020 for(
int ii = (
int) value->length() - 1; ii >= 0; --ii )
1022 wxChar c = value->GetChar( ii );
1024 if( c >=
'0' && c <=
'9' )
1028 else if( c ==
'.' || c ==
',' )
1030 if( decimalSeparator !=
'?' || thousandsSeparator !=
'?' )
1034 if( c == decimalSeparator )
1036 if( thousandsSeparatorFound )
1038 else if( decimalSeparatorFound )
1041 decimalSeparatorFound =
true;
1043 else if( c == thousandsSeparator )
1048 thousandsSeparatorFound =
true;
1051 else if( ambiguousSeparator !=
'?' )
1055 if( c == ambiguousSeparator )
1058 thousandsSeparator = ambiguousSeparator;
1059 thousandsSeparatorFound =
true;
1060 decimalSeparator = c ==
'.' ?
',' :
'.';
1065 decimalSeparator = ambiguousSeparator;
1066 decimalSeparatorFound =
true;
1067 thousandsSeparator = c;
1068 thousandsSeparatorFound =
true;
1080 if( ( ii == 1 && value->GetChar( 0 ) ==
'0' ) || digits != 3 )
1082 decimalSeparator = c;
1083 decimalSeparatorFound =
true;
1084 thousandsSeparator = c ==
'.' ?
',' :
'.';
1088 ambiguousSeparator = c;
1101 if( decimalSeparator ==
'?' && thousandsSeparator ==
'?' )
1103 const struct lconv* lc = localeconv();
1105 decimalSeparator = lc->decimal_point[0];
1106 thousandsSeparator = decimalSeparator ==
'.' ?
',' :
'.';
1110 value->Replace( thousandsSeparator, wxEmptyString );
1111 value->Replace( decimalSeparator,
'.' );
1124 wxString strFWordBeg, strFWordMid, strFWordEnd;
1125 wxString strSWordBeg, strSWordMid, strSWordEnd;
1128 SplitString( fWord, &strFWordBeg, &strFWordMid, &strFWordEnd );
1129 SplitString( sWord, &strSWordBeg, &strSWordMid, &strSWordEnd );
1132 int isEqual = strFWordBeg.CmpNoCase( strSWordBeg );
1138 else if( isEqual < 0 )
1145 double lFirstNumber = 0;
1146 double lSecondNumber = 0;
1147 bool endingIsModifier =
false;
1154 strFWordMid.ToDouble( &lFirstNumber );
1155 strSWordMid.ToDouble( &lSecondNumber );
1157 endingIsModifier |=
ApplyModifier( lFirstNumber, strFWordEnd );
1158 endingIsModifier |=
ApplyModifier( lSecondNumber, strSWordEnd );
1160 if( lFirstNumber > lSecondNumber )
1162 else if( lFirstNumber < lSecondNumber )
1165 else if( !endingIsModifier )
1166 return strFWordEnd.CmpNoCase( strSWordEnd );
1175 wxString* strBeginning,
1176 wxString* strDigits,
1179 static const wxString separators( wxT(
".," ) );
1182 strBeginning->Empty();
1187 if( strToSplit.length() == 0 )
1193 for( ii = (strToSplit.length() - 1); ii >= 0; ii-- )
1195 if( wxIsdigit( strToSplit[ii] ) )
1202 *strBeginning = strToSplit;
1207 *strEnd = strToSplit.substr( ii + 1 );
1210 int position = ii + 1;
1212 for( ; ii >= 0; ii-- )
1214 if( !wxIsdigit( strToSplit[ii] ) && separators.Find( strToSplit[ii] ) < 0 )
1220 *strDigits = strToSplit.substr( 0, position );
1226 *strDigits = strToSplit.substr( ii + 1, position - ii - 1 );
1227 *strBeginning = strToSplit.substr( 0, ii + 1 );
1241 int index = aStr.Len() - 1;
1245 const char chr = aStr.GetChar( index );
1247 if( chr <
'0' || chr >
'9' )
1250 number += ( chr -
'0' ) * base;
1267 bool changed =
false;
1269 result.reserve( aName->length() );
1271 for( std::string::iterator it = aName->begin(); it != aName->end(); ++it )
1276 StrPrintf( &result,
"%c", aReplaceChar );
1297 bool changed =
false;
1299 result.reserve( aName.Length() );
1302 for( wxString::iterator it = aName.begin(); it != aName.end(); ++it )
1304 if( illWChars.Find( *it ) != wxNOT_FOUND )
1307 result += aReplaceChar;
1309 result += wxString::Format(
"%%%02x", *it );
1326void wxStringSplit(
const wxString& aText, wxArrayString& aStrings, wxChar aSplitter )
1330 for(
unsigned ii = 0; ii < aText.Length(); ii++ )
1332 if( aText[ii] == aSplitter )
1334 aStrings.Add( tmp );
1343 if( !tmp.IsEmpty() )
1344 aStrings.Add( tmp );
1350 struct lconv* lc = localeconv();
1351 char sep = lc->decimal_point[0];
1352 unsigned sep_pos = aStringValue.Find( sep );
1357 unsigned min_len = sep_pos + aTrailingZeroAllowed + 1;
1359 while( aStringValue.Len() > min_len )
1361 if( aStringValue.Last() ==
'0' )
1362 aStringValue.RemoveLast();
1374 if( aValue != 0.0 && std::fabs( aValue ) <= 0.0001 )
1376 buf = fmt::format(
"{:.16f}", aValue );
1379 while( !buf.empty() && buf[buf.size() - 1] ==
'0' )
1386 if( buf[buf.size() - 1] ==
'.' )
1393 buf = fmt::format(
"{:.10g}", aValue );
1405 if( aValue != 0.0 && std::fabs( aValue ) <= 0.0001 )
1409 len = snprintf( buf,
sizeof(buf),
"%.16f", aValue );
1411 while( --len > 0 && buf[len] ==
'0' )
1414 if( buf[len] ==
'.' || buf[len] ==
',' )
1423 len = snprintf( buf,
sizeof(buf),
"%.10g", aValue );
1426 return std::string( buf, len );
1433 wxString line = wxString::FromUTF8( cstring );
1435 if( line.IsEmpty() )
1437 line = wxConvCurrent->cMB2WC( cstring );
1439 if( line.IsEmpty() )
1440 line = wxString::From8BitData( cstring );
1450 wxString line = wxString::FromUTF8( aString );
1452 if( line.IsEmpty() )
1454 line = wxConvCurrent->cMB2WC( aString.c_str() );
1456 if( line.IsEmpty() )
1457 line = wxString::From8BitData( aString.c_str() );
1466 wxString uriPathAndFileName;
1468 wxCHECK( aFileUri.StartsWith( wxS(
"file://" ), &uriPathAndFileName ), aFileUri );
1470 wxString tmp = uriPathAndFileName;
1471 wxString retv = wxS(
"file://" );
1473 tmp.Replace( wxS(
"\\" ), wxS(
"/" ) );
1474 tmp.Replace( wxS(
":" ), wxS(
"" ) );
1476 if( !tmp.IsEmpty() && tmp[0] !=
'/' )
1477 tmp = wxS(
"/" ) + tmp;
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
This file contains miscellaneous commonly used macros and functions.
std::optional< V > get_opt(const std::map< wxString, V > &aMap, const wxString &aKey)
int StrPrintf(std::string *result, const char *format,...)
This is like sprintf() but the output is appended to a std::string instead of to a character array.
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
wxString RemoveHTMLTags(const wxString &aInput)
Removes HTML tags from a string.
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
bool WildCompareString(const wxString &pattern, const wxString &string_to_tst, bool case_sensitive)
Compare a string against wild card (* and ?) pattern using the usual rules.
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)
static const char illegalFileNameChars[]
Illegal file name characters used to ensure file names will be valid on all supported platforms.
wxString LinkifyHTML(wxString aStr)
Wraps links in HTML tags.
bool convertSeparators(wxString *value)
wxString GetIllegalFileNameWxChars()
wxString From_UTF8(const char *cstring)
bool ConvertSmartQuotesAndDashes(wxString *aString)
Convert curly quotes and em/en dashes to straight quotes and dashes.
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
int ReadDelimitedText(wxString *aDest, const char *aSource)
Copy bytes from aSource delimited string segment to aDest wxString.
bool ReplaceIllegalFileNameChars(std::string *aName, int aReplaceChar)
Checks aName for illegal file name characters.
wxString TitleCaps(const wxString &aString)
Capitalize the first letter in each word.
std::string UIDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 We want to avoid scientific ...
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.
char * GetLine(FILE *File, char *Line, int *LineNum, int SizeLine)
Read one line line from aFile.
bool ApplyModifier(double &value, const wxString &aString)
wxString NormalizeFileUri(const wxString &aFileUri)
Normalize file path aFileUri to URI convention.
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
wxString GetISO8601CurrentDateTime()
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.
bool IsFullFileNameValid(const wxString &aFullFilename)
Checks if a full filename is valid, i.e.
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Remove trailing zeros from a string containing a converted float number.
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.
wxString UnescapeHTML(const wxString &aString)
Return a new wxString unescaped from HTML format.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
ESCAPE_CONTEXT
Escape/Unescape routines to safely encode reserved-characters in various contexts.