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.

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