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.

1159 lines
29 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
4 years ago
3 years ago
4 years ago
4 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-2022 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.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. /**
  34. * Illegal file name characters used to ensure file names will be valid on all supported
  35. * platforms. This is the list of illegal file name characters for Windows which includes
  36. * the illegal file name characters for Linux and OSX.
  37. */
  38. static const char illegalFileNameChars[] = "\\/:\"<>|";
  39. wxString ConvertToNewOverbarNotation( const wxString& aOldStr )
  40. {
  41. wxString newStr;
  42. bool inOverbar = false;
  43. // Don't get tripped up by the legacy empty-string token.
  44. if( aOldStr == wxT( "~" ) )
  45. return aOldStr;
  46. newStr.reserve( aOldStr.length() );
  47. for( wxString::const_iterator chIt = aOldStr.begin(); chIt != aOldStr.end(); ++chIt )
  48. {
  49. if( *chIt == '~' )
  50. {
  51. wxString::const_iterator lookahead = chIt + 1;
  52. if( lookahead != aOldStr.end() && *lookahead == '~' )
  53. {
  54. if( ++lookahead != aOldStr.end() && *lookahead == '{' )
  55. {
  56. // This way the subsequent opening curly brace will not start an
  57. // overbar.
  58. newStr << wxT( "~~{}" );
  59. continue;
  60. }
  61. // Two subsequent tildes mean a tilde.
  62. newStr << wxT( "~" );
  63. ++chIt;
  64. continue;
  65. }
  66. else if( lookahead != aOldStr.end() && *lookahead == '{' )
  67. {
  68. // Could mean the user wants "{" with an overbar, but more likely this
  69. // is a case of double notation conversion. Bail out.
  70. return aOldStr;
  71. }
  72. else
  73. {
  74. if( inOverbar )
  75. {
  76. newStr << wxT( "}" );
  77. inOverbar = false;
  78. }
  79. else
  80. {
  81. newStr << wxT( "~{" );
  82. inOverbar = true;
  83. }
  84. continue;
  85. }
  86. }
  87. else if( ( *chIt == ' ' || *chIt == '}' || *chIt == ')' ) && inOverbar )
  88. {
  89. // Spaces were used to terminate overbar as well
  90. newStr << wxT( "}" );
  91. inOverbar = false;
  92. }
  93. newStr << *chIt;
  94. }
  95. // Explicitly end the overbar even if there was no terminating '~' in the aOldStr.
  96. if( inOverbar )
  97. newStr << wxT( "}" );
  98. return newStr;
  99. }
  100. bool ConvertSmartQuotesAndDashes( wxString* aString )
  101. {
  102. bool retVal = false;
  103. for( wxString::iterator ii = aString->begin(); ii != aString->end(); ++ii )
  104. {
  105. if( *ii == L'\u00B4' || *ii == L'\u2018' || *ii == L'\u2019' )
  106. {
  107. *ii = '\'';
  108. retVal = true;
  109. }
  110. if( *ii == L'\u201C' || *ii == L'\u201D' )
  111. {
  112. *ii = '"';
  113. retVal = true;
  114. }
  115. if( *ii == L'\u2013' || *ii == L'\u2014' )
  116. {
  117. *ii = '-';
  118. retVal = true;
  119. }
  120. }
  121. return retVal;
  122. }
  123. wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext )
  124. {
  125. wxString converted;
  126. std::vector<bool> braceStack; // true == formatting construct
  127. converted.reserve( aSource.length() );
  128. for( wxUniChar c: aSource )
  129. {
  130. if( aContext == CTX_NETNAME )
  131. {
  132. if( c == '/' )
  133. converted += wxT( "{slash}" );
  134. else if( c == '\n' || c == '\r' )
  135. converted += wxEmptyString; // drop
  136. else
  137. converted += c;
  138. }
  139. else if( aContext == CTX_LIBID || aContext == CTX_LEGACY_LIBID )
  140. {
  141. // We no longer escape '/' in LIB_IDs, but we used to
  142. if( c == '/' && aContext == CTX_LEGACY_LIBID )
  143. converted += wxT( "{slash}" );
  144. else if( c == '\\' )
  145. converted += wxT( "{backslash}" );
  146. else if( c == '<' )
  147. converted += wxT( "{lt}" );
  148. else if( c == '>' )
  149. converted += wxT( "{gt}" );
  150. else if( c == ':' )
  151. converted += wxT( "{colon}" );
  152. else if( c == '\"' )
  153. converted += wxT( "{dblquote}" );
  154. else if( c == '\n' || c == '\r' )
  155. converted += wxEmptyString; // drop
  156. else
  157. converted += c;
  158. }
  159. else if( aContext == CTX_IPC )
  160. {
  161. if( c == '/' )
  162. converted += wxT( "{slash}" );
  163. else if( c == ',' )
  164. converted += wxT( "{comma}" );
  165. else if( c == '\"' )
  166. converted += wxT( "{dblquote}" );
  167. else
  168. converted += c;
  169. }
  170. else if( aContext == CTX_QUOTED_STR )
  171. {
  172. if( c == '\"' )
  173. converted += wxT( "{dblquote}" );
  174. else
  175. converted += c;
  176. }
  177. else if( aContext == CTX_JS_STR )
  178. {
  179. if( c >= 0x7F || c == '\'' || c == '\\' || c == '(' || c == ')' )
  180. {
  181. unsigned int code = c;
  182. char buffer[16];
  183. snprintf( buffer, sizeof(buffer), "\\\\u%4.4X", code );
  184. converted += buffer;
  185. }
  186. else
  187. {
  188. converted += c;
  189. }
  190. }
  191. else if( aContext == CTX_LINE )
  192. {
  193. if( c == '\n' || c == '\r' )
  194. converted += wxT( "{return}" );
  195. else
  196. converted += c;
  197. }
  198. else if( aContext == CTX_FILENAME )
  199. {
  200. if( c == '/' )
  201. converted += wxT( "{slash}" );
  202. else if( c == '\\' )
  203. converted += wxT( "{backslash}" );
  204. else if( c == '\"' )
  205. converted += wxT( "{dblquote}" );
  206. else if( c == '<' )
  207. converted += wxT( "{lt}" );
  208. else if( c == '>' )
  209. converted += wxT( "{gt}" );
  210. else if( c == '|' )
  211. converted += wxT( "{bar}" );
  212. else if( c == ':' )
  213. converted += wxT( "{colon}" );
  214. else if( c == '\t' )
  215. converted += wxT( "{tab}" );
  216. else if( c == '\n' || c == '\r' )
  217. converted += wxT( "{return}" );
  218. else
  219. converted += c;
  220. }
  221. else if( aContext == CTX_NO_SPACE )
  222. {
  223. if( c == ' ' )
  224. converted += wxT( "{space}" );
  225. else
  226. converted += c;
  227. }
  228. else if( aContext == CTX_CSV )
  229. {
  230. if( c == ',' )
  231. converted += wxT( "{comma}" );
  232. else if( c == '\n' || c == '\r' )
  233. converted += wxT( "{return}" );
  234. else
  235. converted += c;
  236. }
  237. else
  238. {
  239. converted += c;
  240. }
  241. }
  242. return converted;
  243. }
  244. wxString UnescapeString( const wxString& aSource )
  245. {
  246. size_t sourceLen = aSource.length();
  247. // smallest escape string is three characters, shortcut everything else
  248. if( sourceLen <= 2 )
  249. {
  250. return aSource;
  251. }
  252. wxString newbuf;
  253. newbuf.reserve( sourceLen );
  254. for( size_t i = 0; i < sourceLen; ++i )
  255. {
  256. wxUniChar ch = aSource[i];
  257. if( ( ch == '$' || ch == '~' || ch == '^' || ch == '_' )
  258. && i + 1 < sourceLen && aSource[i+1] == '{' )
  259. {
  260. for( ; i < sourceLen; ++i )
  261. {
  262. ch = aSource[i];
  263. newbuf += ch;
  264. if( ch == '}' )
  265. break;
  266. }
  267. }
  268. else if( ch == '{' )
  269. {
  270. wxString token;
  271. int depth = 1;
  272. for( i = i + 1; i < sourceLen; ++i )
  273. {
  274. ch = aSource[i];
  275. if( ch == '{' )
  276. depth++;
  277. else if( ch == '}' )
  278. depth--;
  279. if( depth <= 0 )
  280. break;
  281. else
  282. token.append( ch );
  283. }
  284. if( token == wxT( "dblquote" ) ) newbuf.append( wxT( "\"" ) );
  285. else if( token == wxT( "quote" ) ) newbuf.append( wxT( "'" ) );
  286. else if( token == wxT( "lt" ) ) newbuf.append( wxT( "<" ) );
  287. else if( token == wxT( "gt" ) ) newbuf.append( wxT( ">" ) );
  288. else if( token == wxT( "backslash" ) ) newbuf.append( wxT( "\\" ) );
  289. else if( token == wxT( "slash" ) ) newbuf.append( wxT( "/" ) );
  290. else if( token == wxT( "bar" ) ) newbuf.append( wxT( "|" ) );
  291. else if( token == wxT( "comma" ) ) newbuf.append( wxT( "," ) );
  292. else if( token == wxT( "colon" ) ) newbuf.append( wxT( ":" ) );
  293. else if( token == wxT( "space" ) ) newbuf.append( wxT( " " ) );
  294. else if( token == wxT( "dollar" ) ) newbuf.append( wxT( "$" ) );
  295. else if( token == wxT( "tab" ) ) newbuf.append( wxT( "\t" ) );
  296. else if( token == wxT( "return" ) ) newbuf.append( wxT( "\n" ) );
  297. else if( token == wxT( "brace" ) ) newbuf.append( wxT( "{" ) );
  298. else if( token.IsEmpty() ) newbuf.append( wxT( "{" ) );
  299. else
  300. {
  301. newbuf.append( wxT( "{" ) + UnescapeString( token ) + wxT( "}" ) );
  302. }
  303. }
  304. else
  305. {
  306. newbuf.append( ch );
  307. }
  308. }
  309. return newbuf;
  310. }
  311. wxString TitleCaps( const wxString& aString )
  312. {
  313. wxArrayString words;
  314. wxString result;
  315. wxStringSplit( aString, words, ' ' );
  316. result.reserve( aString.length() );
  317. for( const wxString& word : words )
  318. {
  319. if( !result.IsEmpty() )
  320. result += wxT( " " );
  321. result += word.Capitalize();
  322. }
  323. return result;
  324. }
  325. int ReadDelimitedText( wxString* aDest, const char* aSource )
  326. {
  327. std::string utf8; // utf8 but without escapes and quotes.
  328. bool inside = false;
  329. const char* start = aSource;
  330. char cc;
  331. while( (cc = *aSource++) != 0 )
  332. {
  333. if( cc == '"' )
  334. {
  335. if( inside )
  336. break; // 2nd double quote is end of delimited text
  337. inside = true; // first delimiter found, make note, do not copy
  338. }
  339. else if( inside )
  340. {
  341. if( cc == '\\' )
  342. {
  343. cc = *aSource++;
  344. if( !cc )
  345. break;
  346. // do no copy the escape byte if it is followed by \ or "
  347. if( cc != '"' && cc != '\\' )
  348. utf8 += '\\';
  349. utf8 += cc;
  350. }
  351. else
  352. {
  353. utf8 += cc;
  354. }
  355. }
  356. }
  357. *aDest = FROM_UTF8( utf8.c_str() );
  358. return aSource - start;
  359. }
  360. int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize )
  361. {
  362. if( aDestSize <= 0 )
  363. return 0;
  364. bool inside = false;
  365. const char* start = aSource;
  366. char* limit = aDest + aDestSize - 1;
  367. char cc;
  368. while( (cc = *aSource++) != 0 && aDest < limit )
  369. {
  370. if( cc == '"' )
  371. {
  372. if( inside )
  373. break; // 2nd double quote is end of delimited text
  374. inside = true; // first delimiter found, make note, do not copy
  375. }
  376. else if( inside )
  377. {
  378. if( cc == '\\' )
  379. {
  380. cc = *aSource++;
  381. if( !cc )
  382. break;
  383. // do no copy the escape byte if it is followed by \ or "
  384. if( cc != '"' && cc != '\\' )
  385. *aDest++ = '\\';
  386. if( aDest < limit )
  387. *aDest++ = cc;
  388. }
  389. else
  390. {
  391. *aDest++ = cc;
  392. }
  393. }
  394. }
  395. *aDest = 0;
  396. return aSource - start;
  397. }
  398. std::string EscapedUTF8( const wxString& aString )
  399. {
  400. wxString str = aString;
  401. // No new-lines allowed in quoted strings
  402. str.Replace( wxT( "\r\n" ), wxT( "\r" ) );
  403. str.Replace( wxT( "\n" ), wxT( "\r" ) );
  404. std::string utf8 = TO_UTF8( aString );
  405. std::string ret;
  406. ret.reserve( utf8.length() + 2 );
  407. ret += '"';
  408. for( std::string::const_iterator it = utf8.begin(); it!=utf8.end(); ++it )
  409. {
  410. // this escaping strategy is designed to be compatible with ReadDelimitedText():
  411. if( *it == '"' )
  412. {
  413. ret += '\\';
  414. ret += '"';
  415. }
  416. else if( *it == '\\' )
  417. {
  418. ret += '\\'; // double it up
  419. ret += '\\';
  420. }
  421. else
  422. {
  423. ret += *it;
  424. }
  425. }
  426. ret += '"';
  427. return ret;
  428. }
  429. wxString EscapeHTML( const wxString& aString )
  430. {
  431. wxString converted;
  432. converted.reserve( aString.length() );
  433. for( wxUniChar c : aString )
  434. {
  435. if( c == '\"' )
  436. converted += wxT( "&quot;" );
  437. else if( c == '\'' )
  438. converted += wxT( "&apos;" );
  439. else if( c == '&' )
  440. converted += wxT( "&amp;" );
  441. else if( c == '<' )
  442. converted += wxT( "&lt;" );
  443. else if( c == '>' )
  444. converted += wxT( "&gt;" );
  445. else
  446. converted += c;
  447. }
  448. return converted;
  449. }
  450. bool NoPrintableChars( const wxString& aString )
  451. {
  452. wxString tmp = aString;
  453. return tmp.Trim( true ).Trim( false ).IsEmpty();
  454. }
  455. /**
  456. * Return the number of printable (ie: non-formatting) chars. Used to approximate rendered
  457. * text size when speed is more important than accuracy.
  458. */
  459. int PrintableCharCount( const wxString& aString )
  460. {
  461. int char_count = 0;
  462. int overbarDepth = -1;
  463. int superSubDepth = -1;
  464. int braceNesting = 0;
  465. for( auto chIt = aString.begin(), end = aString.end(); chIt < end; ++chIt )
  466. {
  467. if( *chIt == '\t' )
  468. {
  469. // We don't format tabs in bitmap text (where this is currently used), so just
  470. // drop them from the count.
  471. continue;
  472. }
  473. else if( *chIt == '^' && superSubDepth == -1 )
  474. {
  475. auto lookahead = chIt;
  476. if( ++lookahead != end && *lookahead == '{' )
  477. {
  478. chIt = lookahead;
  479. superSubDepth = braceNesting;
  480. braceNesting++;
  481. continue;
  482. }
  483. }
  484. else if( *chIt == '_' && superSubDepth == -1 )
  485. {
  486. auto lookahead = chIt;
  487. if( ++lookahead != end && *lookahead == '{' )
  488. {
  489. chIt = lookahead;
  490. superSubDepth = braceNesting;
  491. braceNesting++;
  492. continue;
  493. }
  494. }
  495. else if( *chIt == '~' && overbarDepth == -1 )
  496. {
  497. auto lookahead = chIt;
  498. if( ++lookahead != end && *lookahead == '{' )
  499. {
  500. chIt = lookahead;
  501. overbarDepth = braceNesting;
  502. braceNesting++;
  503. continue;
  504. }
  505. }
  506. else if( *chIt == '{' )
  507. {
  508. braceNesting++;
  509. }
  510. else if( *chIt == '}' )
  511. {
  512. if( braceNesting > 0 )
  513. braceNesting--;
  514. if( braceNesting == superSubDepth )
  515. {
  516. superSubDepth = -1;
  517. continue;
  518. }
  519. if( braceNesting == overbarDepth )
  520. {
  521. overbarDepth = -1;
  522. continue;
  523. }
  524. }
  525. char_count++;
  526. }
  527. return char_count;
  528. }
  529. char* StrPurge( char* text )
  530. {
  531. static const char whitespace[] = " \t\n\r\f\v";
  532. if( text )
  533. {
  534. while( *text && strchr( whitespace, *text ) )
  535. ++text;
  536. char* cp = text + strlen( text ) - 1;
  537. while( cp >= text && strchr( whitespace, *cp ) )
  538. *cp-- = '\0';
  539. }
  540. return text;
  541. }
  542. char* GetLine( FILE* File, char* Line, int* LineNum, int SizeLine )
  543. {
  544. do {
  545. if( fgets( Line, SizeLine, File ) == nullptr )
  546. return nullptr;
  547. if( LineNum )
  548. *LineNum += 1;
  549. } while( Line[0] == '#' || Line[0] == '\n' || Line[0] == '\r' || Line[0] == 0 );
  550. strtok( Line, "\n\r" );
  551. return Line;
  552. }
  553. wxString DateAndTime()
  554. {
  555. wxDateTime datetime = wxDateTime::Now();
  556. datetime.SetCountry( wxDateTime::Country_Default );
  557. return datetime.Format( wxDefaultDateTimeFormat, wxDateTime::Local );
  558. }
  559. int StrNumCmp( const wxString& aString1, const wxString& aString2, bool aIgnoreCase )
  560. {
  561. int nb1 = 0, nb2 = 0;
  562. auto str1 = aString1.begin();
  563. auto str2 = aString2.begin();
  564. while( str1 != aString1.end() && str2 != aString2.end() )
  565. {
  566. wxUniChar c1 = *str1;
  567. wxUniChar c2 = *str2;
  568. if( wxIsdigit( c1 ) && wxIsdigit( c2 ) ) // Both characters are digits, do numeric compare.
  569. {
  570. nb1 = 0;
  571. nb2 = 0;
  572. do
  573. {
  574. c1 = *str1;
  575. nb1 = nb1 * 10 + (int) c1 - '0';
  576. ++str1;
  577. } while( str1 != aString1.end() && wxIsdigit( *str1 ) );
  578. do
  579. {
  580. c2 = *str2;
  581. nb2 = nb2 * 10 + (int) c2 - '0';
  582. ++str2;
  583. } while( str2 != aString2.end() && wxIsdigit( *str2 ) );
  584. if( nb1 < nb2 )
  585. return -1;
  586. if( nb1 > nb2 )
  587. return 1;
  588. c1 = ( str1 != aString1.end() ) ? *str1 : wxUniChar( 0 );
  589. c2 = ( str2 != aString2.end() ) ? *str2 : wxUniChar( 0 );
  590. }
  591. // Any numerical comparisons to here are identical.
  592. if( aIgnoreCase )
  593. {
  594. if( wxToupper( c1 ) < wxToupper( c2 ) )
  595. return -1;
  596. if( wxToupper( c1 ) > wxToupper( c2 ) )
  597. return 1;
  598. }
  599. else
  600. {
  601. if( c1 < c2 )
  602. return -1;
  603. if( c1 > c2 )
  604. return 1;
  605. }
  606. if( str1 != aString1.end() )
  607. ++str1;
  608. if( str2 != aString2.end() )
  609. ++str2;
  610. }
  611. if( str1 == aString1.end() && str2 != aString2.end() )
  612. {
  613. return -1; // Identical to here but aString1 is longer.
  614. }
  615. else if( str1 != aString1.end() && str2 == aString2.end() )
  616. {
  617. return 1; // Identical to here but aString2 is longer.
  618. }
  619. return 0;
  620. }
  621. bool WildCompareString( const wxString& pattern, const wxString& string_to_tst,
  622. bool case_sensitive )
  623. {
  624. const wxChar* cp = nullptr, * mp = nullptr;
  625. const wxChar* wild, * str;
  626. wxString _pattern, _string_to_tst;
  627. if( case_sensitive )
  628. {
  629. wild = pattern.GetData();
  630. str = string_to_tst.GetData();
  631. }
  632. else
  633. {
  634. _pattern = pattern;
  635. _pattern.MakeUpper();
  636. _string_to_tst = string_to_tst;
  637. _string_to_tst.MakeUpper();
  638. wild = _pattern.GetData();
  639. str = _string_to_tst.GetData();
  640. }
  641. while( ( *str ) && ( *wild != '*' ) )
  642. {
  643. if( ( *wild != *str ) && ( *wild != '?' ) )
  644. return false;
  645. wild++;
  646. str++;
  647. }
  648. while( *str )
  649. {
  650. if( *wild == '*' )
  651. {
  652. if( !*++wild )
  653. return 1;
  654. mp = wild;
  655. cp = str + 1;
  656. }
  657. else if( ( *wild == *str ) || ( *wild == '?' ) )
  658. {
  659. wild++;
  660. str++;
  661. }
  662. else
  663. {
  664. wild = mp;
  665. str = cp++;
  666. }
  667. }
  668. while( *wild == '*' )
  669. {
  670. wild++;
  671. }
  672. return !*wild;
  673. }
  674. bool ApplyModifier( double& value, const wxString& aString )
  675. {
  676. static const wxString modifiers( wxT( "pnumkKM" ) );
  677. if( !aString.length() )
  678. return false;
  679. wxChar modifier;
  680. wxString units;
  681. if( modifiers.Find( aString[ 0 ] ) >= 0 )
  682. {
  683. modifier = aString[ 0 ];
  684. units = aString.Mid( 1 ).Trim();
  685. }
  686. else
  687. {
  688. modifier = ' ';
  689. units = aString.Mid( 0 ).Trim();
  690. }
  691. if( units.length()
  692. && !units.CmpNoCase( wxT( "F" ) )
  693. && !units.CmpNoCase( wxT( "hz" ) )
  694. && !units.CmpNoCase( wxT( "W" ) )
  695. && !units.CmpNoCase( wxT( "V" ) )
  696. && !units.CmpNoCase( wxT( "H" ) ) )
  697. return false;
  698. if( modifier == 'p' )
  699. value *= 1.0e-12;
  700. if( modifier == 'n' )
  701. value *= 1.0e-9;
  702. else if( modifier == 'u' )
  703. value *= 1.0e-6;
  704. else if( modifier == 'm' )
  705. value *= 1.0e-3;
  706. else if( modifier == 'k' || modifier == 'K' )
  707. value *= 1.0e3;
  708. else if( modifier == 'M' )
  709. value *= 1.0e6;
  710. else if( modifier == 'G' )
  711. value *= 1.0e9;
  712. return true;
  713. }
  714. int ValueStringCompare( const wxString& strFWord, const wxString& strSWord )
  715. {
  716. // Compare unescaped text
  717. wxString fWord = UnescapeString( strFWord );
  718. wxString sWord = UnescapeString( strSWord );
  719. // The different sections of the two strings
  720. wxString strFWordBeg, strFWordMid, strFWordEnd;
  721. wxString strSWordBeg, strSWordMid, strSWordEnd;
  722. // Split the two strings into separate parts
  723. SplitString( fWord, &strFWordBeg, &strFWordMid, &strFWordEnd );
  724. SplitString( sWord, &strSWordBeg, &strSWordMid, &strSWordEnd );
  725. // Compare the Beginning section of the strings
  726. int isEqual = strFWordBeg.CmpNoCase( strSWordBeg );
  727. if( isEqual > 0 )
  728. {
  729. return 1;
  730. }
  731. else if( isEqual < 0 )
  732. {
  733. return -1;
  734. }
  735. else
  736. {
  737. // If the first sections are equal compare their digits
  738. double lFirstNumber = 0;
  739. double lSecondNumber = 0;
  740. bool endingIsModifier = false;
  741. strFWordMid.ToDouble( &lFirstNumber );
  742. strSWordMid.ToDouble( &lSecondNumber );
  743. endingIsModifier |= ApplyModifier( lFirstNumber, strFWordEnd );
  744. endingIsModifier |= ApplyModifier( lSecondNumber, strSWordEnd );
  745. if( lFirstNumber > lSecondNumber )
  746. return 1;
  747. else if( lFirstNumber < lSecondNumber )
  748. return -1;
  749. // If the first two sections are equal and the endings are modifiers then compare them
  750. else if( !endingIsModifier )
  751. return strFWordEnd.CmpNoCase( strSWordEnd );
  752. // Ran out of things to compare; they must match
  753. else
  754. return 0;
  755. }
  756. }
  757. int SplitString( const wxString& strToSplit,
  758. wxString* strBeginning,
  759. wxString* strDigits,
  760. wxString* strEnd )
  761. {
  762. static const wxString separators( wxT( ".," ) );
  763. // Clear all the return strings
  764. strBeginning->Empty();
  765. strDigits->Empty();
  766. strEnd->Empty();
  767. // There no need to do anything if the string is empty
  768. if( strToSplit.length() == 0 )
  769. return 0;
  770. // Starting at the end of the string look for the first digit
  771. int ii;
  772. for( ii = (strToSplit.length() - 1); ii >= 0; ii-- )
  773. {
  774. if( wxIsdigit( strToSplit[ii] ) )
  775. break;
  776. }
  777. // If there were no digits then just set the single string
  778. if( ii < 0 )
  779. {
  780. *strBeginning = strToSplit;
  781. }
  782. else
  783. {
  784. // Since there is at least one digit this is the trailing string
  785. *strEnd = strToSplit.substr( ii + 1 );
  786. // Go to the end of the digits
  787. int position = ii + 1;
  788. for( ; ii >= 0; ii-- )
  789. {
  790. if( !wxIsdigit( strToSplit[ii] ) && separators.Find( strToSplit[ii] ) < 0 )
  791. break;
  792. }
  793. // If all that was left was digits, then just set the digits string
  794. if( ii < 0 )
  795. *strDigits = strToSplit.substr( 0, position );
  796. /* We were only looking for the last set of digits everything else is
  797. * part of the preamble */
  798. else
  799. {
  800. *strDigits = strToSplit.substr( ii + 1, position - ii - 1 );
  801. *strBeginning = strToSplit.substr( 0, ii + 1 );
  802. }
  803. }
  804. return 0;
  805. }
  806. int GetTrailingInt( const wxString& aStr )
  807. {
  808. int number = 0;
  809. int base = 1;
  810. // Trim and extract the trailing numeric part
  811. int index = aStr.Len() - 1;
  812. while( index >= 0 )
  813. {
  814. const char chr = aStr.GetChar( index );
  815. if( chr < '0' || chr > '9' )
  816. break;
  817. number += ( chr - '0' ) * base;
  818. base *= 10;
  819. index--;
  820. }
  821. return number;
  822. }
  823. wxString GetIllegalFileNameWxChars()
  824. {
  825. return FROM_UTF8( illegalFileNameChars );
  826. }
  827. bool ReplaceIllegalFileNameChars( std::string* aName, int aReplaceChar )
  828. {
  829. bool changed = false;
  830. std::string result;
  831. result.reserve( aName->length() );
  832. for( std::string::iterator it = aName->begin(); it != aName->end(); ++it )
  833. {
  834. if( strchr( illegalFileNameChars, *it ) )
  835. {
  836. if( aReplaceChar )
  837. StrPrintf( &result, "%c", aReplaceChar );
  838. else
  839. StrPrintf( &result, "%%%02x", *it );
  840. changed = true;
  841. }
  842. else
  843. {
  844. result += *it;
  845. }
  846. }
  847. if( changed )
  848. *aName = result;
  849. return changed;
  850. }
  851. bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar )
  852. {
  853. bool changed = false;
  854. wxString result;
  855. result.reserve( aName.Length() );
  856. wxString illWChars = GetIllegalFileNameWxChars();
  857. for( wxString::iterator it = aName.begin(); it != aName.end(); ++it )
  858. {
  859. if( illWChars.Find( *it ) != wxNOT_FOUND )
  860. {
  861. if( aReplaceChar )
  862. result += aReplaceChar;
  863. else
  864. result += wxString::Format( "%%%02x", *it );
  865. changed = true;
  866. }
  867. else
  868. {
  869. result += *it;
  870. }
  871. }
  872. if( changed )
  873. aName = result;
  874. return changed;
  875. }
  876. void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter )
  877. {
  878. wxString tmp;
  879. for( unsigned ii = 0; ii < aText.Length(); ii++ )
  880. {
  881. if( aText[ii] == aSplitter )
  882. {
  883. aStrings.Add( tmp );
  884. tmp.Clear();
  885. }
  886. else
  887. {
  888. tmp << aText[ii];
  889. }
  890. }
  891. if( !tmp.IsEmpty() )
  892. aStrings.Add( tmp );
  893. }
  894. void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed )
  895. {
  896. struct lconv* lc = localeconv();
  897. char sep = lc->decimal_point[0];
  898. unsigned sep_pos = aStringValue.Find( sep );
  899. if( sep_pos > 0 )
  900. {
  901. // We want to keep at least aTrailingZeroAllowed digits after the separator
  902. unsigned min_len = sep_pos + aTrailingZeroAllowed + 1;
  903. while( aStringValue.Len() > min_len )
  904. {
  905. if( aStringValue.Last() == '0' )
  906. aStringValue.RemoveLast();
  907. else
  908. break;
  909. }
  910. }
  911. }
  912. std::string FormatDouble2Str( double aValue )
  913. {
  914. std::string buf;
  915. if( aValue != 0.0 && std::fabs( aValue ) <= 0.0001 )
  916. {
  917. buf = fmt::format( "{:.16f}", aValue );
  918. // remove trailing zeros (and the decimal marker if needed)
  919. while( !buf.empty() && buf[buf.size() - 1] == '0' )
  920. {
  921. buf.pop_back();
  922. }
  923. // if the value was really small
  924. // we may have just stripped all the zeros after the decimal
  925. if( buf[buf.size() - 1] == '.' )
  926. {
  927. buf.pop_back();
  928. }
  929. }
  930. else
  931. {
  932. buf = fmt::format( "{:.10g}", aValue );
  933. }
  934. return buf;
  935. }
  936. std::string UIDouble2Str( double aValue )
  937. {
  938. char buf[50];
  939. int len;
  940. if( aValue != 0.0 && std::fabs( aValue ) <= 0.0001 )
  941. {
  942. // For these small values, %f works fine,
  943. // and %g gives an exponent
  944. len = snprintf( buf, sizeof(buf), "%.16f", aValue );
  945. while( --len > 0 && buf[len] == '0' )
  946. buf[len] = '\0';
  947. if( buf[len] == '.' || buf[len] == ',' )
  948. buf[len] = '\0';
  949. else
  950. ++len;
  951. }
  952. else
  953. {
  954. // For these values, %g works fine, and sometimes %f
  955. // gives a bad value (try aValue = 1.222222222222, with %.16f format!)
  956. len = snprintf( buf, sizeof(buf), "%.10g", aValue );
  957. }
  958. return std::string( buf, len );
  959. }