You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1507 lines
39 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
15 years ago
15 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
2 years ago
2 years ago
  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. * @file string_utils.cpp
  25. * @brief Some useful functions to handle strings.
  26. */
  27. #include <clocale>
  28. #include <cmath>
  29. #include <map>
  30. #include <core/map_helpers.h>
  31. #include <fmt/core.h>
  32. #include <macros.h>
  33. #include <richio.h> // StrPrintf
  34. #include <string_utils.h>
  35. #include <wx_filename.h>
  36. #include <fmt/chrono.h>
  37. #include <wx/log.h>
  38. #include <wx/regex.h>
  39. #include "locale_io.h"
  40. /**
  41. * Illegal file name characters used to ensure file names will be valid on all supported
  42. * platforms. This is the list of illegal file name characters for Windows which includes
  43. * the illegal file name characters for Linux and OSX.
  44. */
  45. static const char illegalFileNameChars[] = "\\/:\"<>|*?";
  46. // Checks if a full filename is valid, i.e. does not contains illegal chars
  47. bool IsFullFileNameValid( const wxString& aFullFilename )
  48. {
  49. // Test for forbidden chars in aFullFilename.
  50. // '\'and '/' are allowed here because aFullFilename can be a full path, and
  51. // ':' is allowed on Windows as second char in string.
  52. // So remove allowed separators from string to test
  53. wxString filtered_fullpath = aFullFilename;
  54. #ifdef __WINDOWS__
  55. // On MSW, the list returned by wxFileName::GetForbiddenChars() contains separators
  56. // '\'and '/'
  57. filtered_fullpath.Replace( "/", "_" );
  58. filtered_fullpath.Replace( "\\", "_" );
  59. // A disk identifier is allowed, and therefore remove its separator
  60. if( filtered_fullpath.Length() > 1 && filtered_fullpath[1] == ':' )
  61. filtered_fullpath[1] = ' ';
  62. #endif
  63. if( wxString::npos != filtered_fullpath.find_first_of( wxFileName::GetForbiddenChars() ) )
  64. return false;
  65. return true;
  66. }
  67. wxString ConvertToNewOverbarNotation( const wxString& aOldStr )
  68. {
  69. wxString newStr;
  70. bool inOverbar = false;
  71. // Don't get tripped up by the legacy empty-string token.
  72. if( aOldStr == wxT( "~" ) )
  73. return aOldStr;
  74. newStr.reserve( aOldStr.length() );
  75. for( wxString::const_iterator chIt = aOldStr.begin(); chIt != aOldStr.end(); ++chIt )
  76. {
  77. if( *chIt == '~' )
  78. {
  79. wxString::const_iterator lookahead = chIt + 1;
  80. if( lookahead != aOldStr.end() && *lookahead == '~' )
  81. {
  82. if( ++lookahead != aOldStr.end() && *lookahead == '{' )
  83. {
  84. // This way the subsequent opening curly brace will not start an
  85. // overbar.
  86. newStr << wxT( "~~{}" );
  87. continue;
  88. }
  89. // Two subsequent tildes mean a tilde.
  90. newStr << wxT( "~" );
  91. ++chIt;
  92. continue;
  93. }
  94. else if( lookahead != aOldStr.end() && *lookahead == '{' )
  95. {
  96. // Could mean the user wants "{" with an overbar, but more likely this
  97. // is a case of double notation conversion. Bail out.
  98. return aOldStr;
  99. }
  100. else
  101. {
  102. if( inOverbar )
  103. {
  104. newStr << wxT( "}" );
  105. inOverbar = false;
  106. }
  107. else
  108. {
  109. newStr << wxT( "~{" );
  110. inOverbar = true;
  111. }
  112. continue;
  113. }
  114. }
  115. else if( ( *chIt == ' ' || *chIt == '}' || *chIt == ')' ) && inOverbar )
  116. {
  117. // Spaces were used to terminate overbar as well
  118. newStr << wxT( "}" );
  119. inOverbar = false;
  120. }
  121. newStr << *chIt;
  122. }
  123. // Explicitly end the overbar even if there was no terminating '~' in the aOldStr.
  124. if( inOverbar )
  125. newStr << wxT( "}" );
  126. return newStr;
  127. }
  128. bool ConvertSmartQuotesAndDashes( wxString* aString )
  129. {
  130. bool retVal = false;
  131. for( wxString::iterator ii = aString->begin(); ii != aString->end(); ++ii )
  132. {
  133. if( *ii == L'\u00B4' || *ii == L'\u2018' || *ii == L'\u2019' )
  134. {
  135. *ii = '\'';
  136. retVal = true;
  137. }
  138. if( *ii == L'\u201C' || *ii == L'\u201D' )
  139. {
  140. *ii = '"';
  141. retVal = true;
  142. }
  143. if( *ii == L'\u2013' || *ii == L'\u2014' )
  144. {
  145. *ii = '-';
  146. retVal = true;
  147. }
  148. }
  149. return retVal;
  150. }
  151. wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext )
  152. {
  153. wxString converted;
  154. std::vector<bool> braceStack; // true == formatting construct
  155. converted.reserve( aSource.length() );
  156. for( wxUniChar c: aSource )
  157. {
  158. if( aContext == CTX_NETNAME )
  159. {
  160. if( c == '/' )
  161. converted += wxT( "{slash}" );
  162. else if( c == '\n' || c == '\r' )
  163. converted += wxEmptyString; // drop
  164. else
  165. converted += c;
  166. }
  167. else if( aContext == CTX_LIBID || aContext == CTX_LEGACY_LIBID )
  168. {
  169. // We no longer escape '/' in LIB_IDs, but we used to
  170. if( c == '/' && aContext == CTX_LEGACY_LIBID )
  171. converted += wxT( "{slash}" );
  172. else if( c == '\\' )
  173. converted += wxT( "{backslash}" );
  174. else if( c == '<' )
  175. converted += wxT( "{lt}" );
  176. else if( c == '>' )
  177. converted += wxT( "{gt}" );
  178. else if( c == ':' )
  179. converted += wxT( "{colon}" );
  180. else if( c == '\"' )
  181. converted += wxT( "{dblquote}" );
  182. else if( c == '\n' || c == '\r' )
  183. converted += wxEmptyString; // drop
  184. else
  185. converted += c;
  186. }
  187. else if( aContext == CTX_IPC )
  188. {
  189. if( c == '/' )
  190. converted += wxT( "{slash}" );
  191. else if( c == ',' )
  192. converted += wxT( "{comma}" );
  193. else if( c == '\"' )
  194. converted += wxT( "{dblquote}" );
  195. else
  196. converted += c;
  197. }
  198. else if( aContext == CTX_QUOTED_STR )
  199. {
  200. if( c == '\"' )
  201. converted += wxT( "{dblquote}" );
  202. else
  203. converted += c;
  204. }
  205. else if( aContext == CTX_JS_STR )
  206. {
  207. if( c >= 0x7F || c == '\'' || c == '\\' || c == '(' || c == ')' )
  208. {
  209. unsigned int code = c;
  210. char buffer[16];
  211. snprintf( buffer, sizeof(buffer), "\\u%4.4X", code );
  212. converted += buffer;
  213. }
  214. else
  215. {
  216. converted += c;
  217. }
  218. }
  219. else if( aContext == CTX_LINE )
  220. {
  221. if( c == '\n' || c == '\r' )
  222. converted += wxT( "{return}" );
  223. else
  224. converted += c;
  225. }
  226. else if( aContext == CTX_FILENAME )
  227. {
  228. if( c == '/' )
  229. converted += wxT( "{slash}" );
  230. else if( c == '\\' )
  231. converted += wxT( "{backslash}" );
  232. else if( c == '\"' )
  233. converted += wxT( "{dblquote}" );
  234. else if( c == '<' )
  235. converted += wxT( "{lt}" );
  236. else if( c == '>' )
  237. converted += wxT( "{gt}" );
  238. else if( c == '|' )
  239. converted += wxT( "{bar}" );
  240. else if( c == ':' )
  241. converted += wxT( "{colon}" );
  242. else if( c == '\t' )
  243. converted += wxT( "{tab}" );
  244. else if( c == '\n' || c == '\r' )
  245. converted += wxT( "{return}" );
  246. else
  247. converted += c;
  248. }
  249. else if( aContext == CTX_NO_SPACE )
  250. {
  251. if( c == ' ' )
  252. converted += wxT( "{space}" );
  253. else
  254. converted += c;
  255. }
  256. else if( aContext == CTX_CSV )
  257. {
  258. if( c == ',' )
  259. converted += wxT( "{comma}" );
  260. else if( c == '\n' || c == '\r' )
  261. converted += wxT( "{return}" );
  262. else
  263. converted += c;
  264. }
  265. else
  266. {
  267. converted += c;
  268. }
  269. }
  270. return converted;
  271. }
  272. wxString UnescapeString( const wxString& aSource )
  273. {
  274. size_t sourceLen = aSource.length();
  275. // smallest escape string is three characters, shortcut everything else
  276. if( sourceLen <= 2 )
  277. {
  278. return aSource;
  279. }
  280. wxString newbuf;
  281. newbuf.reserve( sourceLen );
  282. wxUniChar prev = 0;
  283. wxUniChar ch = 0;
  284. for( size_t i = 0; i < sourceLen; ++i )
  285. {
  286. prev = ch;
  287. ch = aSource[i];
  288. if( ch == '{' )
  289. {
  290. wxString token;
  291. int depth = 1;
  292. bool terminated = false;
  293. for( i = i + 1; i < sourceLen; ++i )
  294. {
  295. ch = aSource[i];
  296. if( ch == '{' )
  297. depth++;
  298. else if( ch == '}' )
  299. depth--;
  300. if( depth <= 0 )
  301. {
  302. terminated = true;
  303. break;
  304. }
  305. else
  306. {
  307. token << ch;
  308. }
  309. }
  310. if( !terminated )
  311. {
  312. newbuf << wxT( "{" ) << UnescapeString( token );
  313. }
  314. else if( prev == '$' || prev == '~' || prev == '^' || prev == '_' )
  315. {
  316. newbuf << wxT( "{" ) << UnescapeString( token ) << wxT( "}" );
  317. }
  318. else if( token == wxT( "dblquote" ) ) newbuf << wxT( "\"" );
  319. else if( token == wxT( "quote" ) ) newbuf << wxT( "'" );
  320. else if( token == wxT( "lt" ) ) newbuf << wxT( "<" );
  321. else if( token == wxT( "gt" ) ) newbuf << wxT( ">" );
  322. else if( token == wxT( "backslash" ) ) newbuf << wxT( "\\" );
  323. else if( token == wxT( "slash" ) ) newbuf << wxT( "/" );
  324. else if( token == wxT( "bar" ) ) newbuf << wxT( "|" );
  325. else if( token == wxT( "comma" ) ) newbuf << wxT( "," );
  326. else if( token == wxT( "colon" ) ) newbuf << wxT( ":" );
  327. else if( token == wxT( "space" ) ) newbuf << wxT( " " );
  328. else if( token == wxT( "dollar" ) ) newbuf << wxT( "$" );
  329. else if( token == wxT( "tab" ) ) newbuf << wxT( "\t" );
  330. else if( token == wxT( "return" ) ) newbuf << wxT( "\n" );
  331. else if( token == wxT( "brace" ) ) newbuf << wxT( "{" );
  332. else
  333. {
  334. newbuf << wxT( "{" ) << UnescapeString( token ) << wxT( "}" );
  335. }
  336. }
  337. else
  338. {
  339. newbuf << ch;
  340. }
  341. }
  342. return newbuf;
  343. }
  344. wxString TitleCaps( const wxString& aString )
  345. {
  346. wxArrayString words;
  347. wxString result;
  348. wxStringSplit( aString, words, ' ' );
  349. result.reserve( aString.length() );
  350. for( const wxString& word : words )
  351. {
  352. if( !result.IsEmpty() )
  353. result += wxT( " " );
  354. result += word.Capitalize();
  355. }
  356. return result;
  357. }
  358. wxString InitialCaps( const wxString& aString )
  359. {
  360. wxArrayString words;
  361. wxString result;
  362. wxStringSplit( aString, words, ' ' );
  363. result.reserve( aString.length() );
  364. for( const wxString& word : words )
  365. {
  366. if( result.IsEmpty() )
  367. result += word.Capitalize();
  368. else
  369. result += wxT( " " ) + word.Lower();
  370. }
  371. return result;
  372. }
  373. int ReadDelimitedText( wxString* aDest, const char* aSource )
  374. {
  375. std::string utf8; // utf8 but without escapes and quotes.
  376. bool inside = false;
  377. const char* start = aSource;
  378. char cc;
  379. while( (cc = *aSource++) != 0 )
  380. {
  381. if( cc == '"' )
  382. {
  383. if( inside )
  384. break; // 2nd double quote is end of delimited text
  385. inside = true; // first delimiter found, make note, do not copy
  386. }
  387. else if( inside )
  388. {
  389. if( cc == '\\' )
  390. {
  391. cc = *aSource++;
  392. if( !cc )
  393. break;
  394. // do no copy the escape byte if it is followed by \ or "
  395. if( cc != '"' && cc != '\\' )
  396. utf8 += '\\';
  397. utf8 += cc;
  398. }
  399. else
  400. {
  401. utf8 += cc;
  402. }
  403. }
  404. }
  405. *aDest = From_UTF8( utf8.c_str() );
  406. return aSource - start;
  407. }
  408. int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize )
  409. {
  410. if( aDestSize <= 0 )
  411. return 0;
  412. bool inside = false;
  413. const char* start = aSource;
  414. char* limit = aDest + aDestSize - 1;
  415. char cc;
  416. while( ( cc = *aSource++ ) != 0 && aDest < limit )
  417. {
  418. if( cc == '"' )
  419. {
  420. if( inside )
  421. break; // 2nd double quote is end of delimited text
  422. inside = true; // first delimiter found, make note, do not copy
  423. }
  424. else if( inside )
  425. {
  426. if( cc == '\\' )
  427. {
  428. cc = *aSource++;
  429. if( !cc )
  430. break;
  431. // do no copy the escape byte if it is followed by \ or "
  432. if( cc != '"' && cc != '\\' )
  433. *aDest++ = '\\';
  434. if( aDest < limit )
  435. *aDest++ = cc;
  436. }
  437. else
  438. {
  439. *aDest++ = cc;
  440. }
  441. }
  442. }
  443. *aDest = 0;
  444. return aSource - start;
  445. }
  446. std::string EscapedUTF8( const wxString& aString )
  447. {
  448. wxString str = aString;
  449. // No new-lines allowed in quoted strings
  450. str.Replace( wxT( "\r\n" ), wxT( "\r" ) );
  451. str.Replace( wxT( "\n" ), wxT( "\r" ) );
  452. std::string utf8 = TO_UTF8( aString );
  453. std::string ret;
  454. ret.reserve( utf8.length() + 2 );
  455. ret += '"';
  456. for( std::string::const_iterator it = utf8.begin(); it!=utf8.end(); ++it )
  457. {
  458. // this escaping strategy is designed to be compatible with ReadDelimitedText():
  459. if( *it == '"' )
  460. {
  461. ret += '\\';
  462. ret += '"';
  463. }
  464. else if( *it == '\\' )
  465. {
  466. ret += '\\'; // double it up
  467. ret += '\\';
  468. }
  469. else
  470. {
  471. ret += *it;
  472. }
  473. }
  474. ret += '"';
  475. return ret;
  476. }
  477. wxString EscapeHTML( const wxString& aString )
  478. {
  479. wxString converted;
  480. converted.reserve( aString.length() );
  481. for( wxUniChar c : aString )
  482. {
  483. if( c == '\"' )
  484. converted += wxT( "&quot;" );
  485. else if( c == '\'' )
  486. converted += wxT( "&apos;" );
  487. else if( c == '&' )
  488. converted += wxT( "&amp;" );
  489. else if( c == '<' )
  490. converted += wxT( "&lt;" );
  491. else if( c == '>' )
  492. converted += wxT( "&gt;" );
  493. else
  494. converted += c;
  495. }
  496. return converted;
  497. }
  498. wxString UnescapeHTML( const wxString& aString )
  499. {
  500. wxString converted = aString;
  501. // clang-format off
  502. static const std::map<wxString, wxString> c_replacements = {
  503. { wxS( "quot" ), wxS( "\"" ) },
  504. { wxS( "apos" ), wxS( "'" ) },
  505. { wxS( "amp" ), wxS( "&" ) },
  506. { wxS( "lt" ), wxS( "<" ) },
  507. { wxS( "gt" ), wxS( ">" ) }
  508. };
  509. // clang-format on
  510. // Construct regex
  511. wxString regexStr = "&(#(\\d*)|#x([a-zA-Z0-9]{4})";
  512. for( auto& [key, value] : c_replacements )
  513. regexStr << '|' << key;
  514. regexStr << ");";
  515. wxRegEx regex( regexStr );
  516. // Process matches
  517. size_t start = 0;
  518. size_t len = 0;
  519. wxString result;
  520. wxString str = converted;
  521. while( regex.Matches( str ) )
  522. {
  523. std::vector<wxString> matches;
  524. regex.GetMatch( &start, &len );
  525. result << str.Left( start );
  526. wxString code = regex.GetMatch( str, 1 );
  527. wxString codeDec = regex.GetMatch( str, 2 );
  528. wxString codeHex = regex.GetMatch( str, 3 );
  529. if( !codeDec.IsEmpty() || !codeHex.IsEmpty() )
  530. {
  531. unsigned long codeVal = 0;
  532. if( !codeDec.IsEmpty() )
  533. codeDec.ToCULong( &codeVal );
  534. else if( !codeHex.IsEmpty() )
  535. codeHex.ToCULong( &codeVal, 16 );
  536. if( codeVal != 0 )
  537. result << wxUniChar( codeVal );
  538. }
  539. else if( auto val = get_opt( c_replacements, code ) )
  540. {
  541. result << *val;
  542. }
  543. str = str.Mid( start + len );
  544. }
  545. result << str;
  546. return result;
  547. }
  548. wxString RemoveHTMLTags( const wxString& aInput )
  549. {
  550. wxString str = aInput;
  551. wxRegEx( wxS( "<[^>]*>" ) ).ReplaceAll( &str, wxEmptyString );
  552. return str;
  553. }
  554. wxString LinkifyHTML( wxString aStr )
  555. {
  556. static wxRegEx regex( wxS( "\\b(https?|ftp|file)://([-\\w+&@#/%?=~|!:,.;]*[^.,:;<>\\(\\)\\s\u00b6])" ),
  557. wxRE_ICASE );
  558. regex.ReplaceAll( &aStr, "<a href=\"\\0\">\\0</a>" );
  559. return aStr;
  560. }
  561. bool IsURL( wxString aStr )
  562. {
  563. static wxRegEx regex( wxS( "(https?|ftp|file)://([-\\w+&@#/%?=~|!:,.;]*[^.,:;<>\\s\u00b6])" ),
  564. wxRE_ICASE );
  565. regex.ReplaceAll( &aStr, "<a href=\"\\0\">\\0</a>" );
  566. return regex.Matches( aStr );
  567. }
  568. bool NoPrintableChars( const wxString& aString )
  569. {
  570. wxString tmp = aString;
  571. return tmp.Trim( true ).Trim( false ).IsEmpty();
  572. }
  573. int PrintableCharCount( const wxString& aString )
  574. {
  575. int char_count = 0;
  576. int overbarDepth = -1;
  577. int superSubDepth = -1;
  578. int braceNesting = 0;
  579. for( auto chIt = aString.begin(), end = aString.end(); chIt < end; ++chIt )
  580. {
  581. if( *chIt == '\t' )
  582. {
  583. // We don't format tabs in bitmap text (where this is currently used), so just
  584. // drop them from the count.
  585. continue;
  586. }
  587. else if( *chIt == '^' && superSubDepth == -1 )
  588. {
  589. auto lookahead = chIt;
  590. if( ++lookahead != end && *lookahead == '{' )
  591. {
  592. chIt = lookahead;
  593. superSubDepth = braceNesting;
  594. braceNesting++;
  595. continue;
  596. }
  597. }
  598. else if( *chIt == '_' && superSubDepth == -1 )
  599. {
  600. auto lookahead = chIt;
  601. if( ++lookahead != end && *lookahead == '{' )
  602. {
  603. chIt = lookahead;
  604. superSubDepth = braceNesting;
  605. braceNesting++;
  606. continue;
  607. }
  608. }
  609. else if( *chIt == '~' && overbarDepth == -1 )
  610. {
  611. auto lookahead = chIt;
  612. if( ++lookahead != end && *lookahead == '{' )
  613. {
  614. chIt = lookahead;
  615. overbarDepth = braceNesting;
  616. braceNesting++;
  617. continue;
  618. }
  619. }
  620. else if( *chIt == '{' )
  621. {
  622. braceNesting++;
  623. }
  624. else if( *chIt == '}' )
  625. {
  626. if( braceNesting > 0 )
  627. braceNesting--;
  628. if( braceNesting == superSubDepth )
  629. {
  630. superSubDepth = -1;
  631. continue;
  632. }
  633. if( braceNesting == overbarDepth )
  634. {
  635. overbarDepth = -1;
  636. continue;
  637. }
  638. }
  639. char_count++;
  640. }
  641. return char_count;
  642. }
  643. char* StrPurge( char* text )
  644. {
  645. static const char whitespace[] = " \t\n\r\f\v";
  646. if( text )
  647. {
  648. while( *text && strchr( whitespace, *text ) )
  649. ++text;
  650. char* cp = text + strlen( text ) - 1;
  651. while( cp >= text && strchr( whitespace, *cp ) )
  652. *cp-- = '\0';
  653. }
  654. return text;
  655. }
  656. char* GetLine( FILE* File, char* Line, int* LineNum, int SizeLine )
  657. {
  658. do {
  659. if( fgets( Line, SizeLine, File ) == nullptr )
  660. return nullptr;
  661. if( LineNum )
  662. *LineNum += 1;
  663. } while( Line[0] == '#' || Line[0] == '\n' || Line[0] == '\r' || Line[0] == 0 );
  664. strtok( Line, "\n\r" );
  665. return Line;
  666. }
  667. wxString GetISO8601CurrentDateTime()
  668. {
  669. // on msys2 variant mingw64, in fmt::format the %z format
  670. // (offset from UTC in the ISO 8601 format, e.g. -0430) does not work,
  671. // and is in fact %Z (locale-dependent time zone name or abbreviation) and breaks our date.
  672. // However, on msys2 variant ucrt64, it works (this is not the same code in fmt::format)
  673. #if defined(__MINGW32__) && !defined(_UCRT)
  674. return fmt::format( "{:%FT%T}", fmt::localtime( std::time( nullptr ) ) );
  675. #else
  676. return fmt::format( "{:%FT%T%z}", fmt::localtime( std::time( nullptr ) ) );
  677. #endif
  678. }
  679. int StrNumCmp( const wxString& aString1, const wxString& aString2, bool aIgnoreCase )
  680. {
  681. int nb1 = 0, nb2 = 0;
  682. auto str1 = aString1.begin();
  683. auto str2 = aString2.begin();
  684. while( str1 != aString1.end() && str2 != aString2.end() )
  685. {
  686. wxUniChar c1 = *str1;
  687. wxUniChar c2 = *str2;
  688. if( wxIsdigit( c1 ) && wxIsdigit( c2 ) ) // Both characters are digits, do numeric compare.
  689. {
  690. nb1 = 0;
  691. nb2 = 0;
  692. do
  693. {
  694. c1 = *str1;
  695. nb1 = nb1 * 10 + (int) c1 - '0';
  696. ++str1;
  697. } while( str1 != aString1.end() && wxIsdigit( *str1 ) );
  698. do
  699. {
  700. c2 = *str2;
  701. nb2 = nb2 * 10 + (int) c2 - '0';
  702. ++str2;
  703. } while( str2 != aString2.end() && wxIsdigit( *str2 ) );
  704. if( nb1 < nb2 )
  705. return -1;
  706. if( nb1 > nb2 )
  707. return 1;
  708. c1 = ( str1 != aString1.end() ) ? *str1 : wxUniChar( 0 );
  709. c2 = ( str2 != aString2.end() ) ? *str2 : wxUniChar( 0 );
  710. }
  711. // Any numerical comparisons to here are identical.
  712. if( aIgnoreCase )
  713. {
  714. if( c1 != c2 )
  715. {
  716. wxUniChar uc1 = wxToupper( c1 );
  717. wxUniChar uc2 = wxToupper( c2 );
  718. if( uc1 != uc2 )
  719. return uc1 < uc2 ? -1 : 1;
  720. }
  721. }
  722. else
  723. {
  724. if( c1 < c2 )
  725. return -1;
  726. if( c1 > c2 )
  727. return 1;
  728. }
  729. if( str1 != aString1.end() )
  730. ++str1;
  731. if( str2 != aString2.end() )
  732. ++str2;
  733. }
  734. if( str1 == aString1.end() && str2 != aString2.end() )
  735. {
  736. return -1; // Identical to here but aString1 is longer.
  737. }
  738. else if( str1 != aString1.end() && str2 == aString2.end() )
  739. {
  740. return 1; // Identical to here but aString2 is longer.
  741. }
  742. return 0;
  743. }
  744. bool WildCompareString( const wxString& pattern, const wxString& string_to_tst,
  745. bool case_sensitive )
  746. {
  747. const wxChar* cp = nullptr;
  748. const wxChar* mp = nullptr;
  749. const wxChar* wild = nullptr;
  750. const wxChar* str = nullptr;
  751. wxString _pattern, _string_to_tst;
  752. if( case_sensitive )
  753. {
  754. wild = pattern.GetData();
  755. str = string_to_tst.GetData();
  756. }
  757. else
  758. {
  759. _pattern = pattern;
  760. _pattern.MakeUpper();
  761. _string_to_tst = string_to_tst;
  762. _string_to_tst.MakeUpper();
  763. wild = _pattern.GetData();
  764. str = _string_to_tst.GetData();
  765. }
  766. while( ( *str ) && ( *wild != '*' ) )
  767. {
  768. if( ( *wild != *str ) && ( *wild != '?' ) )
  769. return false;
  770. wild++;
  771. str++;
  772. }
  773. while( *str )
  774. {
  775. if( *wild == '*' )
  776. {
  777. if( !*++wild )
  778. return true;
  779. mp = wild;
  780. cp = str + 1;
  781. }
  782. else if( ( *wild == *str ) || ( *wild == '?' ) )
  783. {
  784. wild++;
  785. str++;
  786. }
  787. else
  788. {
  789. wild = mp;
  790. str = cp++;
  791. }
  792. }
  793. while( *wild == '*' )
  794. {
  795. wild++;
  796. }
  797. return !*wild;
  798. }
  799. bool ApplyModifier( double& value, const wxString& aString )
  800. {
  801. /// Although the two 'μ's look the same, they are U+03BC and U+00B5
  802. static const wxString modifiers( wxT( "pnuµμmkKM" ) );
  803. if( !aString.length() )
  804. return false;
  805. wxChar modifier;
  806. wxString units;
  807. if( modifiers.Find( aString[ 0 ] ) >= 0 )
  808. {
  809. modifier = aString[ 0 ];
  810. units = aString.Mid( 1 ).Trim();
  811. }
  812. else
  813. {
  814. modifier = ' ';
  815. units = aString.Mid( 0 ).Trim();
  816. }
  817. if( units.length()
  818. && !units.IsSameAs( wxT( "F" ), false )
  819. && !units.IsSameAs( wxT( "hz" ), false )
  820. && !units.IsSameAs( wxT( "W" ), false )
  821. && !units.IsSameAs( wxT( "V" ), false )
  822. && !units.IsSameAs( wxT( "A" ), false )
  823. && !units.IsSameAs( wxT( "H" ), false ) )
  824. {
  825. return false;
  826. }
  827. if( modifier == 'p' )
  828. value *= 1.0e-12;
  829. if( modifier == 'n' )
  830. value *= 1.0e-9;
  831. else if( modifier == 'u' || modifier == wxS( "µ" )[0] || modifier == wxS( "μ" )[0] )
  832. value *= 1.0e-6;
  833. else if( modifier == 'm' )
  834. value *= 1.0e-3;
  835. else if( modifier == 'k' || modifier == 'K' )
  836. value *= 1.0e3;
  837. else if( modifier == 'M' )
  838. value *= 1.0e6;
  839. else if( modifier == 'G' )
  840. value *= 1.0e9;
  841. return true;
  842. }
  843. bool convertSeparators( wxString* value )
  844. {
  845. // Note: fetching the decimal separator from the current locale isn't a silver bullet because
  846. // it assumes the current computer's locale is the same as the locale the schematic was
  847. // authored in -- something that isn't true, for instance, when sharing designs through
  848. // DIYAudio.com.
  849. //
  850. // Some values are self-describing: multiple instances of a single separator character must be
  851. // thousands separators; a single instance of each character must be a thousands separator
  852. // followed by a decimal separator; etc.
  853. //
  854. // Only when presented with an ambiguous value do we fall back on the current locale.
  855. value->Replace( wxS( " " ), wxEmptyString );
  856. wxChar ambiguousSeparator = '?';
  857. wxChar thousandsSeparator = '?';
  858. bool thousandsSeparatorFound = false;
  859. wxChar decimalSeparator = '?';
  860. bool decimalSeparatorFound = false;
  861. int digits = 0;
  862. for( int ii = (int) value->length() - 1; ii >= 0; --ii )
  863. {
  864. wxChar c = value->GetChar( ii );
  865. if( c >= '0' && c <= '9' )
  866. {
  867. digits += 1;
  868. }
  869. else if( c == '.' || c == ',' )
  870. {
  871. if( decimalSeparator != '?' || thousandsSeparator != '?' )
  872. {
  873. // We've previously found a non-ambiguous separator...
  874. if( c == decimalSeparator )
  875. {
  876. if( thousandsSeparatorFound )
  877. return false; // decimal before thousands
  878. else if( decimalSeparatorFound )
  879. return false; // more than one decimal
  880. else
  881. decimalSeparatorFound = true;
  882. }
  883. else if( c == thousandsSeparator )
  884. {
  885. if( digits != 3 )
  886. return false; // thousands not followed by 3 digits
  887. else
  888. thousandsSeparatorFound = true;
  889. }
  890. }
  891. else if( ambiguousSeparator != '?' )
  892. {
  893. // We've previously found a separator, but we don't know for sure which...
  894. if( c == ambiguousSeparator )
  895. {
  896. // They both must be thousands separators
  897. thousandsSeparator = ambiguousSeparator;
  898. thousandsSeparatorFound = true;
  899. decimalSeparator = c == '.' ? ',' : '.';
  900. }
  901. else
  902. {
  903. // The first must have been a decimal, and this must be a thousands.
  904. decimalSeparator = ambiguousSeparator;
  905. decimalSeparatorFound = true;
  906. thousandsSeparator = c;
  907. thousandsSeparatorFound = true;
  908. }
  909. }
  910. else
  911. {
  912. // This is the first separator...
  913. // If it's preceded by a '0' (only), or if it's followed by some number of
  914. // digits not equal to 3, then it -must- be a decimal separator.
  915. //
  916. // In all other cases we don't really know what it is yet.
  917. if( ( ii == 1 && value->GetChar( 0 ) == '0' ) || digits != 3 )
  918. {
  919. decimalSeparator = c;
  920. decimalSeparatorFound = true;
  921. thousandsSeparator = c == '.' ? ',' : '.';
  922. }
  923. else
  924. {
  925. ambiguousSeparator = c;
  926. }
  927. }
  928. digits = 0;
  929. }
  930. else
  931. {
  932. digits = 0;
  933. }
  934. }
  935. // If we found nothing definitive then we have to look at the current locale
  936. if( decimalSeparator == '?' && thousandsSeparator == '?' )
  937. {
  938. const struct lconv* lc = localeconv();
  939. decimalSeparator = lc->decimal_point[0];
  940. thousandsSeparator = decimalSeparator == '.' ? ',' : '.';
  941. }
  942. // Convert to C-locale
  943. value->Replace( thousandsSeparator, wxEmptyString );
  944. value->Replace( decimalSeparator, '.' );
  945. return true;
  946. }
  947. int ValueStringCompare( const wxString& strFWord, const wxString& strSWord )
  948. {
  949. // Compare unescaped text
  950. wxString fWord = UnescapeString( strFWord );
  951. wxString sWord = UnescapeString( strSWord );
  952. // The different sections of the two strings
  953. wxString strFWordBeg, strFWordMid, strFWordEnd;
  954. wxString strSWordBeg, strSWordMid, strSWordEnd;
  955. // Split the two strings into separate parts
  956. SplitString( fWord, &strFWordBeg, &strFWordMid, &strFWordEnd );
  957. SplitString( sWord, &strSWordBeg, &strSWordMid, &strSWordEnd );
  958. // Compare the Beginning section of the strings
  959. int isEqual = strFWordBeg.CmpNoCase( strSWordBeg );
  960. if( isEqual > 0 )
  961. {
  962. return 1;
  963. }
  964. else if( isEqual < 0 )
  965. {
  966. return -1;
  967. }
  968. else
  969. {
  970. // If the first sections are equal compare their digits
  971. double lFirstNumber = 0;
  972. double lSecondNumber = 0;
  973. bool endingIsModifier = false;
  974. convertSeparators( &strFWordMid );
  975. convertSeparators( &strSWordMid );
  976. strFWordMid.ToCDouble( &lFirstNumber );
  977. strSWordMid.ToCDouble( &lSecondNumber );
  978. endingIsModifier |= ApplyModifier( lFirstNumber, strFWordEnd );
  979. endingIsModifier |= ApplyModifier( lSecondNumber, strSWordEnd );
  980. if( lFirstNumber > lSecondNumber )
  981. return 1;
  982. else if( lFirstNumber < lSecondNumber )
  983. return -1;
  984. // If the first two sections are equal and the endings are modifiers then compare them
  985. else if( !endingIsModifier )
  986. return strFWordEnd.CmpNoCase( strSWordEnd );
  987. // Ran out of things to compare; they must match
  988. else
  989. return 0;
  990. }
  991. }
  992. int SplitString( const wxString& strToSplit,
  993. wxString* strBeginning,
  994. wxString* strDigits,
  995. wxString* strEnd )
  996. {
  997. static const wxString separators( wxT( ".," ) );
  998. // Clear all the return strings
  999. strBeginning->Empty();
  1000. strDigits->Empty();
  1001. strEnd->Empty();
  1002. // There no need to do anything if the string is empty
  1003. if( strToSplit.length() == 0 )
  1004. return 0;
  1005. // Starting at the end of the string look for the first digit
  1006. int ii;
  1007. for( ii = (strToSplit.length() - 1); ii >= 0; ii-- )
  1008. {
  1009. if( wxIsdigit( strToSplit[ii] ) )
  1010. break;
  1011. }
  1012. // If there were no digits then just set the single string
  1013. if( ii < 0 )
  1014. {
  1015. *strBeginning = strToSplit;
  1016. }
  1017. else
  1018. {
  1019. // Since there is at least one digit this is the trailing string
  1020. *strEnd = strToSplit.substr( ii + 1 );
  1021. // Go to the end of the digits
  1022. int position = ii + 1;
  1023. for( ; ii >= 0; ii-- )
  1024. {
  1025. if( !wxIsdigit( strToSplit[ii] ) && separators.Find( strToSplit[ii] ) < 0 )
  1026. break;
  1027. }
  1028. // If all that was left was digits, then just set the digits string
  1029. if( ii < 0 )
  1030. *strDigits = strToSplit.substr( 0, position );
  1031. /* We were only looking for the last set of digits everything else is
  1032. * part of the preamble */
  1033. else
  1034. {
  1035. *strDigits = strToSplit.substr( ii + 1, position - ii - 1 );
  1036. *strBeginning = strToSplit.substr( 0, ii + 1 );
  1037. }
  1038. }
  1039. return 0;
  1040. }
  1041. int GetTrailingInt( const wxString& aStr )
  1042. {
  1043. int number = 0;
  1044. int base = 1;
  1045. // Trim and extract the trailing numeric part
  1046. int index = aStr.Len() - 1;
  1047. while( index >= 0 )
  1048. {
  1049. const char chr = aStr.GetChar( index );
  1050. if( chr < '0' || chr > '9' )
  1051. break;
  1052. number += ( chr - '0' ) * base;
  1053. base *= 10;
  1054. index--;
  1055. }
  1056. return number;
  1057. }
  1058. wxString GetIllegalFileNameWxChars()
  1059. {
  1060. return From_UTF8( illegalFileNameChars );
  1061. }
  1062. bool ReplaceIllegalFileNameChars( std::string* aName, int aReplaceChar )
  1063. {
  1064. bool changed = false;
  1065. std::string result;
  1066. result.reserve( aName->length() );
  1067. for( std::string::iterator it = aName->begin(); it != aName->end(); ++it )
  1068. {
  1069. if( strchr( illegalFileNameChars, *it ) )
  1070. {
  1071. if( aReplaceChar )
  1072. StrPrintf( &result, "%c", aReplaceChar );
  1073. else
  1074. StrPrintf( &result, "%%%02x", *it );
  1075. changed = true;
  1076. }
  1077. else
  1078. {
  1079. result += *it;
  1080. }
  1081. }
  1082. if( changed )
  1083. *aName = result;
  1084. return changed;
  1085. }
  1086. bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar )
  1087. {
  1088. bool changed = false;
  1089. wxString result;
  1090. result.reserve( aName.Length() );
  1091. wxString illWChars = GetIllegalFileNameWxChars();
  1092. for( wxString::iterator it = aName.begin(); it != aName.end(); ++it )
  1093. {
  1094. if( illWChars.Find( *it ) != wxNOT_FOUND )
  1095. {
  1096. if( aReplaceChar )
  1097. result += aReplaceChar;
  1098. else
  1099. result += wxString::Format( "%%%02x", *it );
  1100. changed = true;
  1101. }
  1102. else
  1103. {
  1104. result += *it;
  1105. }
  1106. }
  1107. if( changed )
  1108. aName = result;
  1109. return changed;
  1110. }
  1111. void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter )
  1112. {
  1113. wxString tmp;
  1114. for( unsigned ii = 0; ii < aText.Length(); ii++ )
  1115. {
  1116. if( aText[ii] == aSplitter )
  1117. {
  1118. aStrings.Add( tmp );
  1119. tmp.Clear();
  1120. }
  1121. else
  1122. {
  1123. tmp << aText[ii];
  1124. }
  1125. }
  1126. if( !tmp.IsEmpty() )
  1127. aStrings.Add( tmp );
  1128. }
  1129. void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed )
  1130. {
  1131. struct lconv* lc = localeconv();
  1132. char sep = lc->decimal_point[0];
  1133. unsigned sep_pos = aStringValue.Find( sep );
  1134. if( sep_pos > 0 )
  1135. {
  1136. // We want to keep at least aTrailingZeroAllowed digits after the separator
  1137. unsigned min_len = sep_pos + aTrailingZeroAllowed + 1;
  1138. while( aStringValue.Len() > min_len )
  1139. {
  1140. if( aStringValue.Last() == '0' )
  1141. aStringValue.RemoveLast();
  1142. else
  1143. break;
  1144. }
  1145. }
  1146. }
  1147. std::string FormatDouble2Str( double aValue )
  1148. {
  1149. std::string buf;
  1150. if( aValue != 0.0 && std::fabs( aValue ) <= 0.0001 )
  1151. {
  1152. buf = fmt::format( "{:.16f}", aValue );
  1153. // remove trailing zeros (and the decimal marker if needed)
  1154. while( !buf.empty() && buf[buf.size() - 1] == '0' )
  1155. {
  1156. buf.pop_back();
  1157. }
  1158. // if the value was really small
  1159. // we may have just stripped all the zeros after the decimal
  1160. if( buf[buf.size() - 1] == '.' )
  1161. {
  1162. buf.pop_back();
  1163. }
  1164. }
  1165. else
  1166. {
  1167. buf = fmt::format( "{:.10g}", aValue );
  1168. }
  1169. return buf;
  1170. }
  1171. std::string UIDouble2Str( double aValue )
  1172. {
  1173. char buf[50];
  1174. int len;
  1175. if( aValue != 0.0 && std::fabs( aValue ) <= 0.0001 )
  1176. {
  1177. // For these small values, %f works fine,
  1178. // and %g gives an exponent
  1179. len = snprintf( buf, sizeof( buf ), "%.16f", aValue );
  1180. while( --len > 0 && buf[len] == '0' )
  1181. buf[len] = '\0';
  1182. if( buf[len] == '.' || buf[len] == ',' )
  1183. buf[len] = '\0';
  1184. else
  1185. ++len;
  1186. }
  1187. else
  1188. {
  1189. // For these values, %g works fine, and sometimes %f
  1190. // gives a bad value (try aValue = 1.222222222222, with %.16f format!)
  1191. len = snprintf( buf, sizeof( buf ), "%.10g", aValue );
  1192. }
  1193. return std::string( buf, len );
  1194. }
  1195. wxString From_UTF8( const char* cstring )
  1196. {
  1197. // Convert an expected UTF8 encoded C string to a wxString
  1198. wxString line = wxString::FromUTF8( cstring );
  1199. if( line.IsEmpty() ) // happens when cstring is not a valid UTF8 sequence
  1200. {
  1201. line = wxConvCurrent->cMB2WC( cstring ); // try to use locale conversion
  1202. if( line.IsEmpty() )
  1203. line = wxString::From8BitData( cstring ); // try to use native string
  1204. }
  1205. return line;
  1206. }
  1207. wxString From_UTF8( const std::string& aString )
  1208. {
  1209. // Convert an expected UTF8 encoded std::string to a wxString
  1210. wxString line = wxString::FromUTF8( aString );
  1211. if( line.IsEmpty() ) // happens when aString is not a valid UTF8 sequence
  1212. {
  1213. line = wxConvCurrent->cMB2WC( aString.c_str() ); // try to use locale conversion
  1214. if( line.IsEmpty() )
  1215. line = wxString::From8BitData( aString.c_str() ); // try to use native string
  1216. }
  1217. return line;
  1218. }
  1219. wxString NormalizeFileUri( const wxString& aFileUri )
  1220. {
  1221. wxString uriPathAndFileName;
  1222. wxCHECK( aFileUri.StartsWith( wxS( "file://" ), &uriPathAndFileName ), aFileUri );
  1223. wxString tmp = uriPathAndFileName;
  1224. wxString retv = wxS( "file://" );
  1225. tmp.Replace( wxS( "\\" ), wxS( "/" ) );
  1226. tmp.Replace( wxS( ":" ), wxS( "" ) );
  1227. if( !tmp.IsEmpty() && tmp[0] != '/' )
  1228. tmp = wxS( "/" ) + tmp;
  1229. retv += tmp;
  1230. return retv;
  1231. }