KiCad PCB EDA Suite
Loading...
Searching...
No Matches
string_utils.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
24
25#include <clocale>
26#include <cmath>
27#include <map>
28#include <core/map_helpers.h>
29#include <fmt/core.h>
30#include <macros.h>
31#include <string_utils.h>
32#include <widgets/kistatusbar.h>
33#include <wx_filename.h>
34#include <fmt/chrono.h>
35#include <wx/log.h>
36#include <wx/regex.h>
37#include <wx/tokenzr.h>
39#include "locale_io.h"
40#include <wx/event.h>
41#include <wx/uri.h>
42#include <project.h>
43#include <common.h>
44
45
51static constexpr std::string_view illegalFileNameChars = "\\/:\"<>|*?";
52
53static const wxChar defaultVariantName[] = wxT( "< Default >" );
54
55
56// Checks if a full filename is valid, i.e. does not contains illegal chars
57bool IsFullFileNameValid( const wxString& aFullFilename )
58{
59
60 // Test for forbidden chars in aFullFilename.
61 // '\'and '/' are allowed here because aFullFilename can be a full path, and
62 // ':' is allowed on Windows as second char in string.
63 // So remove allowed separators from string to test
64 wxString filtered_fullpath = aFullFilename;
65
66#ifdef __WINDOWS__
67 // On MSW, the list returned by wxFileName::GetForbiddenChars() contains separators
68 // '\'and '/'
69 filtered_fullpath.Replace( "/", "_" );
70 filtered_fullpath.Replace( "\\", "_" );
71
72 // A disk identifier is allowed, and therefore remove its separator
73 if( filtered_fullpath.Length() > 1 && filtered_fullpath[1] == ':' )
74 filtered_fullpath[1] = ' ';
75#endif
76
77 if( wxString::npos != filtered_fullpath.find_first_of( wxFileName::GetForbiddenChars() ) )
78 return false;
79
80 return true;
81}
82
83
84wxString ConvertToNewOverbarNotation( const wxString& aOldStr )
85{
86 wxString newStr;
87 bool inOverbar = false;
88
89 // Don't get tripped up by the legacy empty-string token.
90 if( aOldStr == wxT( "~" ) )
91 return aOldStr;
92
93 newStr.reserve( aOldStr.length() );
94
95 for( wxString::const_iterator chIt = aOldStr.begin(); chIt != aOldStr.end(); ++chIt )
96 {
97 if( *chIt == '~' )
98 {
99 wxString::const_iterator lookahead = chIt + 1;
100
101 if( lookahead != aOldStr.end() && *lookahead == '~' )
102 {
103 if( ++lookahead != aOldStr.end() && *lookahead == '{' )
104 {
105 // This way the subsequent opening curly brace will not start an
106 // overbar.
107 newStr << wxT( "~~{}" );
108 continue;
109 }
110
111 // Two subsequent tildes mean a tilde.
112 newStr << wxT( "~" );
113 ++chIt;
114 continue;
115 }
116 else if( lookahead != aOldStr.end() && *lookahead == '{' )
117 {
118 // Could mean the user wants "{" with an overbar, but more likely this
119 // is a case of double notation conversion. Bail out.
120 return aOldStr;
121 }
122 else
123 {
124 if( inOverbar )
125 {
126 newStr << wxT( "}" );
127 inOverbar = false;
128 }
129 else
130 {
131 newStr << wxT( "~{" );
132 inOverbar = true;
133 }
134
135 continue;
136 }
137 }
138 else if( ( *chIt == ' ' || *chIt == '}' || *chIt == ')' ) && inOverbar )
139 {
140 // Spaces were used to terminate overbar as well
141 newStr << wxT( "}" );
142 inOverbar = false;
143 }
144
145 newStr << *chIt;
146 }
147
148 // Explicitly end the overbar even if there was no terminating '~' in the aOldStr.
149 if( inOverbar )
150 newStr << wxT( "}" );
151
152 return newStr;
153}
154
155
156bool ConvertSmartQuotesAndDashes( wxString* aString )
157{
158 bool retVal = false;
159
160 for( wxString::iterator ii = aString->begin(); ii != aString->end(); ++ii )
161 {
162 if( *ii == L'\u2018' || *ii == L'\u2019' )
163 {
164 *ii = '\'';
165 retVal = true;
166 }
167 if( *ii == L'\u201C' || *ii == L'\u201D' )
168 {
169 *ii = '"';
170 retVal = true;
171 }
172 if( *ii == L'\u2013' || *ii == L'\u2014' )
173 {
174 *ii = '-';
175 retVal = true;
176 }
177 }
178
179 return retVal;
180}
181
182
183wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext )
184{
185 wxString converted;
186 std::vector<bool> braceStack; // true == formatting construct
187
188 converted.reserve( aSource.length() );
189
190 for( wxUniChar c: aSource )
191 {
192 if( aContext == CTX_NETNAME )
193 {
194 if( c == '/' )
195 converted += wxT( "{slash}" );
196 else if( c == '\n' || c == '\r' )
197 converted += wxEmptyString; // drop
198 else
199 converted += c;
200 }
201 else if( aContext == CTX_LIBID || aContext == CTX_LEGACY_LIBID )
202 {
203 // We no longer escape '/' in LIB_IDs, but we used to
204 if( c == '/' && aContext == CTX_LEGACY_LIBID )
205 converted += wxT( "{slash}" );
206 else if( c == '\\' )
207 converted += wxT( "{backslash}" );
208 else if( c == '<' )
209 converted += wxT( "{lt}" );
210 else if( c == '>' )
211 converted += wxT( "{gt}" );
212 else if( c == ':' )
213 converted += wxT( "{colon}" );
214 else if( c == '\"' )
215 converted += wxT( "{dblquote}" );
216 else if( c == '\n' || c == '\r' )
217 converted += wxEmptyString; // drop
218 else
219 converted += c;
220 }
221 else if( aContext == CTX_IPC )
222 {
223 if( c == '/' )
224 converted += wxT( "{slash}" );
225 else if( c == ',' )
226 converted += wxT( "{comma}" );
227 else if( c == '\"' )
228 converted += wxT( "{dblquote}" );
229 else
230 converted += c;
231 }
232 else if( aContext == CTX_QUOTED_STR )
233 {
234 if( c == '\"' )
235 converted += wxT( "{dblquote}" );
236 else
237 converted += c;
238 }
239 else if( aContext == CTX_JS_STR )
240 {
241 if( c >= 0x7F || c == '\'' || c == '"' || c == '\\' || c == '(' || c == ')' )
242 {
243 unsigned int code = c;
244 char buffer[16];
245 snprintf( buffer, sizeof(buffer), "\\u%4.4X", code );
246 converted += buffer;
247 }
248 else
249 {
250 converted += c;
251 }
252 }
253 else if( aContext == CTX_LINE )
254 {
255 if( c == '\n' || c == '\r' )
256 converted += wxT( "{return}" );
257 else
258 converted += c;
259 }
260 else if( aContext == CTX_FILENAME )
261 {
262 if( c == '/' )
263 converted += wxT( "{slash}" );
264 else if( c == '\\' )
265 converted += wxT( "{backslash}" );
266 else if( c == '\"' )
267 converted += wxT( "{dblquote}" );
268 else if( c == '<' )
269 converted += wxT( "{lt}" );
270 else if( c == '>' )
271 converted += wxT( "{gt}" );
272 else if( c == '|' )
273 converted += wxT( "{bar}" );
274 else if( c == ':' )
275 converted += wxT( "{colon}" );
276 else if( c == '\t' )
277 converted += wxT( "{tab}" );
278 else if( c == '\n' || c == '\r' )
279 converted += wxT( "{return}" );
280 else
281 converted += c;
282 }
283 else if( aContext == CTX_NO_SPACE )
284 {
285 if( c == ' ' )
286 converted += wxT( "{space}" );
287 else
288 converted += c;
289 }
290 else if( aContext == CTX_CSV )
291 {
292 if( c == ',' )
293 converted += wxT( "{comma}" );
294 else if( c == '\n' || c == '\r' )
295 converted += wxT( "{return}" );
296 else
297 converted += c;
298 }
299 else
300 {
301 converted += c;
302 }
303 }
304
305 return converted;
306}
307
308
309wxString UnescapeString( const wxString& aSource )
310{
311 size_t sourceLen = aSource.length();
312
313 // smallest escape string is three characters, shortcut everything else
314 if( sourceLen <= 2 )
315 {
316 return aSource;
317 }
318
319 wxString newbuf;
320 newbuf.reserve( sourceLen );
321
322 wxUniChar prev = 0;
323 wxUniChar ch = 0;
324
325 for( size_t i = 0; i < sourceLen; ++i )
326 {
327 prev = ch;
328 ch = aSource[i];
329
330 if( ch == '{' )
331 {
332 wxString token;
333 int depth = 1;
334 bool terminated = false;
335
336 for( i = i + 1; i < sourceLen; ++i )
337 {
338 ch = aSource[i];
339
340 if( ch == '{' )
341 depth++;
342 else if( ch == '}' )
343 depth--;
344
345 if( depth <= 0 )
346 {
347 terminated = true;
348 break;
349 }
350 else
351 {
352 token << ch;
353 }
354 }
355
356 if( !terminated )
357 {
358 newbuf << wxT( "{" ) << UnescapeString( token );
359 }
360 else if( prev == '$' || prev == '~' || prev == '^' || prev == '_' )
361 {
362 newbuf << wxT( "{" ) << UnescapeString( token ) << wxT( "}" );
363 }
364 else if( token == wxT( "dblquote" ) ) newbuf << wxT( "\"" );
365 else if( token == wxT( "quote" ) ) newbuf << wxT( "'" );
366 else if( token == wxT( "lt" ) ) newbuf << wxT( "<" );
367 else if( token == wxT( "gt" ) ) newbuf << wxT( ">" );
368 else if( token == wxT( "backslash" ) ) newbuf << wxT( "\\" );
369 else if( token == wxT( "slash" ) ) newbuf << wxT( "/" );
370 else if( token == wxT( "bar" ) ) newbuf << wxT( "|" );
371 else if( token == wxT( "comma" ) ) newbuf << wxT( "," );
372 else if( token == wxT( "colon" ) ) newbuf << wxT( ":" );
373 else if( token == wxT( "space" ) ) newbuf << wxT( " " );
374 else if( token == wxT( "dollar" ) ) newbuf << wxT( "$" );
375 else if( token == wxT( "tab" ) ) newbuf << wxT( "\t" );
376 else if( token == wxT( "return" ) ) newbuf << wxT( "\n" );
377 else if( token == wxT( "brace" ) ) newbuf << wxT( "{" );
378 else
379 {
380 newbuf << wxT( "{" ) << UnescapeString( token ) << wxT( "}" );
381 }
382 }
383 else
384 {
385 newbuf << ch;
386 }
387 }
388
389 return newbuf;
390}
391
392
393wxString TitleCaps( const wxString& aString )
394{
395 wxArrayString words;
396 wxString result;
397
398 wxStringSplit( aString, words, ' ' );
399
400 result.reserve( aString.length() );
401
402 for( const wxString& word : words )
403 {
404 if( !result.IsEmpty() )
405 result += wxT( " " );
406
407 result += word.Capitalize();
408 }
409
410 return result;
411}
412
413
414wxString InitialCaps( const wxString& aString )
415{
416 wxArrayString words;
417 wxString result;
418
419 wxStringSplit( aString, words, ' ' );
420
421 result.reserve( aString.length() );
422
423 for( const wxString& word : words )
424 {
425 if( result.IsEmpty() )
426 result += word.Capitalize();
427 else
428 result += wxT( " " ) + word.Lower();
429 }
430
431 return result;
432}
433
434
435int ReadDelimitedText( wxString* aDest, const char* aSource )
436{
437 std::string utf8; // utf8 but without escapes and quotes.
438 bool inside = false;
439 const char* start = aSource;
440 char cc;
441
442 while( (cc = *aSource++) != 0 )
443 {
444 if( cc == '"' )
445 {
446 if( inside )
447 break; // 2nd double quote is end of delimited text
448
449 inside = true; // first delimiter found, make note, do not copy
450 }
451
452 else if( inside )
453 {
454 if( cc == '\\' )
455 {
456 cc = *aSource++;
457
458 if( !cc )
459 break;
460
461 // do no copy the escape byte if it is followed by \ or "
462 if( cc != '"' && cc != '\\' )
463 utf8 += '\\';
464
465 utf8 += cc;
466 }
467 else
468 {
469 utf8 += cc;
470 }
471 }
472 }
473
474 *aDest = From_UTF8( utf8.c_str() );
475
476 return aSource - start;
477}
478
479
480int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize )
481{
482 if( aDestSize <= 0 )
483 return 0;
484
485 bool inside = false;
486 const char* start = aSource;
487 char* limit = aDest + aDestSize - 1;
488 char cc;
489
490 while( ( cc = *aSource++ ) != 0 && aDest < limit )
491 {
492 if( cc == '"' )
493 {
494 if( inside )
495 break; // 2nd double quote is end of delimited text
496
497 inside = true; // first delimiter found, make note, do not copy
498 }
499 else if( inside )
500 {
501 if( cc == '\\' )
502 {
503 cc = *aSource++;
504
505 if( !cc )
506 break;
507
508 // do no copy the escape byte if it is followed by \ or "
509 if( cc != '"' && cc != '\\' )
510 *aDest++ = '\\';
511
512 if( aDest < limit )
513 *aDest++ = cc;
514 }
515 else
516 {
517 *aDest++ = cc;
518 }
519 }
520 }
521
522 *aDest = 0;
523
524 return aSource - start;
525}
526
527
528std::string EscapedUTF8( const wxString& aString )
529{
530 wxString str = aString;
531
532 // No new-lines allowed in quoted strings
533 str.Replace( wxT( "\r\n" ), wxT( "\r" ) );
534 str.Replace( wxT( "\n" ), wxT( "\r" ) );
535
536 std::string utf8 = TO_UTF8( aString );
537
538 std::string ret;
539
540 ret.reserve( utf8.length() + 2 );
541
542 ret += '"';
543
544 for( std::string::const_iterator it = utf8.begin(); it!=utf8.end(); ++it )
545 {
546 // this escaping strategy is designed to be compatible with ReadDelimitedText():
547 if( *it == '"' )
548 {
549 ret += '\\';
550 ret += '"';
551 }
552 else if( *it == '\\' )
553 {
554 ret += '\\'; // double it up
555 ret += '\\';
556 }
557 else
558 {
559 ret += *it;
560 }
561 }
562
563 ret += '"';
564
565 return ret;
566}
567
568
569wxString EscapeHTML( const wxString& aString )
570{
571 wxString converted;
572
573 converted.reserve( aString.length() );
574
575 for( wxUniChar c : aString )
576 {
577 if( c == '\"' )
578 converted += wxT( "&quot;" );
579 else if( c == '\'' )
580 converted += wxT( "&apos;" );
581 else if( c == '&' )
582 converted += wxT( "&amp;" );
583 else if( c == '<' )
584 converted += wxT( "&lt;" );
585 else if( c == '>' )
586 converted += wxT( "&gt;" );
587 else
588 converted += c;
589 }
590
591 return converted;
592}
593
594
595wxString UnescapeHTML( const wxString& aString )
596{
597 // clang-format off
598 static const std::map<wxString, wxString> c_replacements = {
599 { wxS( "quot" ), wxS( "\"" ) },
600 { wxS( "apos" ), wxS( "'" ) },
601 { wxS( "amp" ), wxS( "&" ) },
602 { wxS( "lt" ), wxS( "<" ) },
603 { wxS( "gt" ), wxS( ">" ) }
604 };
605 // clang-format on
606
607 // Construct regex
608 wxString regexStr = "&(#(\\d*)|#x([a-zA-Z0-9]{4})";
609
610 for( auto& [key, value] : c_replacements )
611 regexStr << '|' << key;
612
613 regexStr << ");";
614
615 wxRegEx regex( regexStr );
616
617 // Process matches
618 size_t start = 0;
619 size_t len = 0;
620
621 wxString result;
622 wxString str = aString;
623
624 while( regex.Matches( str ) )
625 {
626 std::vector<wxString> matches;
627 regex.GetMatch( &start, &len );
628
629 result << str.Left( start );
630
631 wxString code = regex.GetMatch( str, 1 );
632 wxString codeDec = regex.GetMatch( str, 2 );
633 wxString codeHex = regex.GetMatch( str, 3 );
634
635 if( !codeDec.IsEmpty() || !codeHex.IsEmpty() )
636 {
637 unsigned long codeVal = 0;
638
639 if( !codeDec.IsEmpty() )
640 codeDec.ToCULong( &codeVal );
641 else if( !codeHex.IsEmpty() )
642 codeHex.ToCULong( &codeVal, 16 );
643
644 if( codeVal != 0 )
645 result << wxUniChar( codeVal );
646 }
647 else if( auto val = get_opt( c_replacements, code ) )
648 {
649 result << *val;
650 }
651
652 str = str.Mid( start + len );
653 }
654
655 result << str;
656
657 return result;
658}
659
660
661wxString RemoveHTMLTags( const wxString& aInput )
662{
663 wxString str = aInput;
664 wxRegEx( wxS( "<[^>]*>" ) ).ReplaceAll( &str, wxEmptyString );
665
666 return str;
667}
668
669
670wxString LinkifyHTML( wxString aStr )
671{
672 static wxRegEx regex( wxS( "\\b(https?|ftp|file)://([-\\w+&@#/%?=~|!:,.;]*[^.,:;<>\\(\\)\\s\u00b6])" ),
673 wxRE_ICASE );
674
675 regex.ReplaceAll( &aStr, "<a href=\"\\0\">\\0</a>" );
676
677 return aStr;
678}
679
680
681bool IsURL( wxString aStr )
682{
683 static wxRegEx regex( wxS( "(https?|ftp|file)://([-\\w+&@#/%?=~|!:,.;]*[^.,:;<>\\s\u00b6])" ),
684 wxRE_ICASE );
685
686 regex.ReplaceAll( &aStr, "<a href=\"\\0\">\\0</a>" );
687
688 return regex.Matches( aStr );
689}
690
691
692bool NoPrintableChars( const wxString& aString )
693{
694 wxString tmp = aString;
695
696 return tmp.Trim( true ).Trim( false ).IsEmpty();
697}
698
699
700int PrintableCharCount( const wxString& aString )
701{
702 int char_count = 0;
703 int overbarDepth = -1;
704 int superSubDepth = -1;
705 int braceNesting = 0;
706
707 for( auto chIt = aString.begin(), end = aString.end(); chIt < end; ++chIt )
708 {
709 if( *chIt == '\t' )
710 {
711 // We don't format tabs in bitmap text (where this is currently used), so just
712 // drop them from the count.
713 continue;
714 }
715 else if( *chIt == '^' && superSubDepth == -1 )
716 {
717 auto lookahead = chIt;
718
719 if( ++lookahead != end && *lookahead == '{' )
720 {
721 chIt = lookahead;
722 superSubDepth = braceNesting;
723 braceNesting++;
724 continue;
725 }
726 }
727 else if( *chIt == '_' && superSubDepth == -1 )
728 {
729 auto lookahead = chIt;
730
731 if( ++lookahead != end && *lookahead == '{' )
732 {
733 chIt = lookahead;
734 superSubDepth = braceNesting;
735 braceNesting++;
736 continue;
737 }
738 }
739 else if( *chIt == '~' && overbarDepth == -1 )
740 {
741 auto lookahead = chIt;
742
743 if( ++lookahead != end && *lookahead == '{' )
744 {
745 chIt = lookahead;
746 overbarDepth = braceNesting;
747 braceNesting++;
748 continue;
749 }
750 }
751 else if( *chIt == '{' )
752 {
753 braceNesting++;
754 }
755 else if( *chIt == '}' )
756 {
757 if( braceNesting > 0 )
758 braceNesting--;
759
760 if( braceNesting == superSubDepth )
761 {
762 superSubDepth = -1;
763 continue;
764 }
765
766 if( braceNesting == overbarDepth )
767 {
768 overbarDepth = -1;
769 continue;
770 }
771 }
772
773 char_count++;
774 }
775
776 return char_count;
777}
778
779
780char* StrPurge( char* text )
781{
782 static const char whitespace[] = " \t\n\r\f\v";
783
784 if( text )
785 {
786 while( *text && strchr( whitespace, *text ) )
787 ++text;
788
789 char* cp = text + strlen( text ) - 1;
790
791 while( cp >= text && strchr( whitespace, *cp ) )
792 *cp-- = '\0';
793 }
794
795 return text;
796}
797
798
799char* GetLine( FILE* File, char* Line, int* LineNum, int SizeLine )
800{
801 do {
802 if( fgets( Line, SizeLine, File ) == nullptr )
803 return nullptr;
804
805 if( LineNum )
806 *LineNum += 1;
807
808 } while( Line[0] == '#' || Line[0] == '\n' || Line[0] == '\r' || Line[0] == 0 );
809
810 strtok( Line, "\n\r" );
811 return Line;
812}
813
814
816{
817 return wxDateTime::Now().FormatISOCombined( 'T' );
818}
819
820
821int StrNumCmp( const wxString& aString1, const wxString& aString2, bool aIgnoreCase )
822{
823 int nb1 = 0, nb2 = 0;
824
825 auto str1 = aString1.begin();
826 auto str2 = aString2.begin();
827
828 const auto str1End = aString1.end();
829 const auto str2End = aString2.end();
830
831 while( str1 != str1End && str2 != str2End )
832 {
833 wxUniChar c1 = *str1;
834 wxUniChar c2 = *str2;
835
836 if( wxIsdigit( c1 ) && wxIsdigit( c2 ) ) // Both characters are digits, do numeric compare.
837 {
838 nb1 = 0;
839 nb2 = 0;
840
841 do
842 {
843 c1 = *str1;
844 nb1 = nb1 * 10 + (int) c1 - '0';
845 ++str1;
846 } while( str1 != str1End && wxIsdigit( *str1 ) );
847
848 do
849 {
850 c2 = *str2;
851 nb2 = nb2 * 10 + (int) c2 - '0';
852 ++str2;
853 } while( str2 != str2End && wxIsdigit( *str2 ) );
854
855 if( nb1 < nb2 )
856 return -1;
857
858 if( nb1 > nb2 )
859 return 1;
860
861 c1 = ( str1 != str1End ) ? *str1 : wxUniChar( 0 );
862 c2 = ( str2 != str2End ) ? *str2 : wxUniChar( 0 );
863 }
864
865 // Any numerical comparisons to here are identical.
866 if( aIgnoreCase )
867 {
868 if( c1 != c2 )
869 {
870 wxUniChar uc1 = wxToupper( c1 );
871 wxUniChar uc2 = wxToupper( c2 );
872
873 if( uc1 != uc2 )
874 return uc1 < uc2 ? -1 : 1;
875 }
876 }
877 else
878 {
879 if( c1 < c2 )
880 return -1;
881
882 if( c1 > c2 )
883 return 1;
884 }
885
886 if( str1 != str1End )
887 ++str1;
888
889 if( str2 != str2End )
890 ++str2;
891 }
892
893 if( str1 == str1End && str2 != str2End )
894 {
895 return -1; // Identical to here but aString1 is longer.
896 }
897 else if( str1 != str1End && str2 == str2End )
898 {
899 return 1; // Identical to here but aString2 is longer.
900 }
901
902 return 0;
903}
904
905
906bool WildCompareString( const wxString& pattern, const wxString& string_to_tst,
907 bool case_sensitive )
908{
909 const wxChar* cp = nullptr;
910 const wxChar* mp = nullptr;
911 const wxChar* wild = nullptr;
912 const wxChar* str = nullptr;
913 wxString _pattern, _string_to_tst;
914
915 if( case_sensitive )
916 {
917 wild = pattern.GetData();
918 str = string_to_tst.GetData();
919 }
920 else
921 {
922 _pattern = pattern;
923 _pattern.MakeUpper();
924 _string_to_tst = string_to_tst;
925 _string_to_tst.MakeUpper();
926 wild = _pattern.GetData();
927 str = _string_to_tst.GetData();
928 }
929
930 while( ( *str ) && ( *wild != '*' ) )
931 {
932 if( ( *wild != *str ) && ( *wild != '?' ) )
933 return false;
934
935 wild++;
936 str++;
937 }
938
939 while( *str )
940 {
941 if( *wild == '*' )
942 {
943 if( !*++wild )
944 return true;
945
946 mp = wild;
947 cp = str + 1;
948 }
949 else if( ( *wild == *str ) || ( *wild == '?' ) )
950 {
951 wild++;
952 str++;
953 }
954 else
955 {
956 wild = mp;
957 str = cp++;
958 }
959 }
960
961 while( *wild == '*' )
962 {
963 wild++;
964 }
965
966 return !*wild;
967}
968
969
970bool ApplyModifier( double& value, const wxString& aString )
971{
973 static const wxString modifiers( wxT( "afpnuµμmLRFkKMGTPE" ) );
974
975 if( !aString.length() )
976 return false;
977
978 wxChar modifier;
979 wxString units;
980
981 if( modifiers.Find( aString[ 0 ] ) >= 0 )
982 {
983 modifier = aString[ 0 ];
984 units = aString.Mid( 1 ).Trim();
985 }
986 else
987 {
988 modifier = ' ';
989 units = aString.Mid( 0 ).Trim();
990 }
991
992 if( units.length()
993 && !units.IsSameAs( wxT( "F" ), false )
994 && !units.IsSameAs( wxT( "hz" ), false )
995 && !units.IsSameAs( wxT( "W" ), false )
996 && !units.IsSameAs( wxT( "V" ), false )
997 && !units.IsSameAs( wxT( "A" ), false )
998 && !units.IsSameAs( wxT( "H" ), false ) )
999 {
1000 return false;
1001 }
1002
1003 // Note: most of these are SI, but some (L, R, F) are IEC 60062.
1004 if( modifier == 'a' )
1005 value *= 1.0e-18;
1006 else if( modifier == 'f' )
1007 value *= 1.0e-15;
1008 if( modifier == 'p' )
1009 value *= 1.0e-12;
1010 if( modifier == 'n' )
1011 value *= 1.0e-9;
1012 else if( modifier == 'u' || modifier == wxS( "µ" )[0] || modifier == wxS( "μ" )[0] )
1013 value *= 1.0e-6;
1014 else if( modifier == 'm' || modifier == 'L' )
1015 value *= 1.0e-3;
1016 else if( modifier == 'R' || modifier == 'F' )
1017 ; // unity scalar
1018 else if( modifier == 'k' || modifier == 'K' )
1019 value *= 1.0e3;
1020 else if( modifier == 'M' )
1021 value *= 1.0e6;
1022 else if( modifier == 'G' )
1023 value *= 1.0e9;
1024 else if( modifier == 'T' )
1025 value *= 1.0e12;
1026 else if( modifier == 'P' )
1027 value *= 1.0e15;
1028 else if( modifier == 'E' )
1029 value *= 1.0e18;
1030
1031 return true;
1032}
1033
1034
1035bool convertSeparators( wxString* value )
1036{
1037 // Note: fetching the decimal separator from the current locale isn't a silver bullet because
1038 // it assumes the current computer's locale is the same as the locale the schematic was
1039 // authored in -- something that isn't true, for instance, when sharing designs through
1040 // DIYAudio.com.
1041 //
1042 // Some values are self-describing: multiple instances of a single separator character must be
1043 // thousands separators; a single instance of each character must be a thousands separator
1044 // followed by a decimal separator; etc.
1045 //
1046 // Only when presented with an ambiguous value do we fall back on the current locale.
1047
1048 value->Replace( wxS( " " ), wxEmptyString );
1049
1050 wxChar ambiguousSeparator = '?';
1051 wxChar thousandsSeparator = '?';
1052 bool thousandsSeparatorFound = false;
1053 wxChar decimalSeparator = '?';
1054 bool decimalSeparatorFound = false;
1055 int digits = 0;
1056
1057 for( int ii = (int) value->length() - 1; ii >= 0; --ii )
1058 {
1059 wxChar c = value->GetChar( ii );
1060
1061 if( c >= '0' && c <= '9' )
1062 {
1063 digits += 1;
1064 }
1065 else if( c == '.' || c == ',' )
1066 {
1067 if( decimalSeparator != '?' || thousandsSeparator != '?' )
1068 {
1069 // We've previously found a non-ambiguous separator...
1070
1071 if( c == decimalSeparator )
1072 {
1073 if( thousandsSeparatorFound )
1074 return false; // decimal before thousands
1075 else if( decimalSeparatorFound )
1076 return false; // more than one decimal
1077 else
1078 decimalSeparatorFound = true;
1079 }
1080 else if( c == thousandsSeparator )
1081 {
1082 if( digits != 3 )
1083 return false; // thousands not followed by 3 digits
1084 else
1085 thousandsSeparatorFound = true;
1086 }
1087 }
1088 else if( ambiguousSeparator != '?' )
1089 {
1090 // We've previously found a separator, but we don't know for sure which...
1091
1092 if( c == ambiguousSeparator )
1093 {
1094 // They both must be thousands separators
1095 thousandsSeparator = ambiguousSeparator;
1096 thousandsSeparatorFound = true;
1097 decimalSeparator = c == '.' ? ',' : '.';
1098 }
1099 else
1100 {
1101 // The first must have been a decimal, and this must be a thousands.
1102 decimalSeparator = ambiguousSeparator;
1103 decimalSeparatorFound = true;
1104 thousandsSeparator = c;
1105 thousandsSeparatorFound = true;
1106 }
1107 }
1108 else
1109 {
1110 // This is the first separator...
1111
1112 // If it's preceded by a '0' (only), or if it's followed by some number of
1113 // digits not equal to 3, then it -must- be a decimal separator.
1114 //
1115 // In all other cases we don't really know what it is yet.
1116
1117 if( ( ii == 1 && value->GetChar( 0 ) == '0' ) || digits != 3 )
1118 {
1119 decimalSeparator = c;
1120 decimalSeparatorFound = true;
1121 thousandsSeparator = c == '.' ? ',' : '.';
1122 }
1123 else
1124 {
1125 ambiguousSeparator = c;
1126 }
1127 }
1128
1129 digits = 0;
1130 }
1131 else
1132 {
1133 digits = 0;
1134 }
1135 }
1136
1137 // If we found nothing definitive then we have to look at the current locale
1138 if( decimalSeparator == '?' && thousandsSeparator == '?' )
1139 {
1140 const struct lconv* lc = localeconv();
1141
1142 decimalSeparator = lc->decimal_point[0];
1143 thousandsSeparator = decimalSeparator == '.' ? ',' : '.';
1144 }
1145
1146 // Convert to C-locale
1147 value->Replace( thousandsSeparator, wxEmptyString );
1148 value->Replace( decimalSeparator, '.' );
1149
1150 return true;
1151}
1152
1153
1154int ValueStringCompare( const wxString& strFWord, const wxString& strSWord )
1155{
1156 // Compare unescaped text
1157 wxString fWord = UnescapeString( strFWord );
1158 wxString sWord = UnescapeString( strSWord );
1159
1160 // The different sections of the two strings
1161 wxString strFWordBeg, strFWordMid, strFWordEnd;
1162 wxString strSWordBeg, strSWordMid, strSWordEnd;
1163
1164 // Split the two strings into separate parts
1165 SplitString( fWord, &strFWordBeg, &strFWordMid, &strFWordEnd );
1166 SplitString( sWord, &strSWordBeg, &strSWordMid, &strSWordEnd );
1167
1168 // Compare the Beginning section of the strings
1169 int isEqual = strFWordBeg.CmpNoCase( strSWordBeg );
1170
1171 if( isEqual > 0 )
1172 {
1173 return 1;
1174 }
1175 else if( isEqual < 0 )
1176 {
1177 return -1;
1178 }
1179 else
1180 {
1181 // If the first sections are equal compare their digits
1182 double lFirstNumber = 0;
1183 double lSecondNumber = 0;
1184 bool endingIsModifier = false;
1185
1186 convertSeparators( &strFWordMid );
1187 convertSeparators( &strSWordMid );
1188
1189 strFWordMid.ToCDouble( &lFirstNumber );
1190 strSWordMid.ToCDouble( &lSecondNumber );
1191
1192 endingIsModifier |= ApplyModifier( lFirstNumber, strFWordEnd );
1193 endingIsModifier |= ApplyModifier( lSecondNumber, strSWordEnd );
1194
1195 if( lFirstNumber > lSecondNumber )
1196 return 1;
1197 else if( lFirstNumber < lSecondNumber )
1198 return -1;
1199 // If the first two sections are equal and the endings are modifiers then compare them
1200 else if( !endingIsModifier )
1201 return strFWordEnd.CmpNoCase( strSWordEnd );
1202 // Ran out of things to compare; they must match
1203 else
1204 return 0;
1205 }
1206}
1207
1208
1209int SplitString( const wxString& strToSplit,
1210 wxString* strBeginning,
1211 wxString* strDigits,
1212 wxString* strEnd )
1213{
1214 static const wxString separators( wxT( ".," ) );
1215 wxUniChar infix = 0;
1216
1217 // Clear all the return strings
1218 strBeginning->Empty();
1219 strDigits->Empty();
1220 strEnd->Empty();
1221
1222 // There no need to do anything if the string is empty
1223 if( strToSplit.length() == 0 )
1224 return 0;
1225
1226 // Starting at the end of the string look for the first digit
1227 int ii;
1228
1229 for( ii = (strToSplit.length() - 1); ii >= 0; ii-- )
1230 {
1231 if( wxIsdigit( strToSplit[ii] ) )
1232 break;
1233 }
1234
1235 // If there were no digits then just set the single string
1236 if( ii < 0 )
1237 {
1238 *strBeginning = strToSplit;
1239 }
1240 else
1241 {
1242 // Since there is at least one digit this is the trailing string
1243 *strEnd = strToSplit.substr( ii + 1 );
1244
1245 // Go to the end of the digits
1246 int position = ii + 1;
1247
1248 for( ; ii >= 0; ii-- )
1249 {
1250 double scale;
1251 wxUniChar c = strToSplit[ii];
1252
1253 if( wxIsdigit( c ) )
1254 {
1255 continue;
1256 }
1257 if( infix == 0 && NUMERIC_EVALUATOR::IsOldSchoolDecimalSeparator( c, &scale ) )
1258 {
1259 infix = c;
1260 continue;
1261 }
1262 else if( separators.Find( strToSplit[ii] ) >= 0 )
1263 {
1264 continue;
1265 }
1266 else
1267 {
1268 break;
1269 }
1270 }
1271
1272 // If all that was left was digits, then just set the digits string
1273 if( ii < 0 )
1274 {
1275 *strDigits = strToSplit.substr( 0, position );
1276 }
1277 // Otherwise everything else is part of the preamble
1278 else
1279 {
1280 *strDigits = strToSplit.substr( ii + 1, position - ii - 1 );
1281 *strBeginning = strToSplit.substr( 0, ii + 1 );
1282 }
1283
1284 if( infix > 0 )
1285 {
1286 strDigits->Replace( infix, '.' );
1287 *strEnd = infix + *strEnd;
1288 }
1289 }
1290
1291 return 0;
1292}
1293
1294
1295int GetTrailingInt( const wxString& aStr )
1296{
1297 int number = 0;
1298 int base = 1;
1299
1300 // Trim and extract the trailing numeric part
1301 int index = aStr.Len() - 1;
1302
1303 while( index >= 0 )
1304 {
1305 const char chr = aStr.GetChar( index );
1306
1307 if( chr < '0' || chr > '9' )
1308 break;
1309
1310 number += ( chr - '0' ) * base;
1311 base *= 10;
1312 index--;
1313 }
1314
1315 return number;
1316}
1317
1318
1320{
1321 return wxString::FromUTF8( illegalFileNameChars.data(), illegalFileNameChars.length() );
1322}
1323
1324
1325bool ReplaceIllegalFileNameChars( std::string& aName, int aReplaceChar )
1326{
1327 size_t first_illegal_pos = aName.find_first_of( illegalFileNameChars );
1328
1329 if( first_illegal_pos == std::string::npos )
1330 {
1331 return false;
1332 }
1333
1334 std::string result;
1335 // result will be at least equal to original, add 16 in case of hex replacements
1336 result.reserve( aName.length() + 16 );
1337 // append the valid part
1338 result.append( aName, 0, first_illegal_pos );
1339
1340 for( size_t i = first_illegal_pos; i < aName.length(); ++i )
1341 {
1342 char c = aName[i];
1343
1344 // Check if this specific char is illegal
1345 if( illegalFileNameChars.find( c ) != std::string_view::npos )
1346 {
1347 if( aReplaceChar )
1348 {
1349 result.push_back( aReplaceChar );
1350 }
1351 else
1352 {
1353 fmt::format_to( std::back_inserter( result ), "%{:02x}", static_cast<unsigned char>( c ) );
1354 }
1355 }
1356 else
1357 {
1358 result.push_back( c );
1359 }
1360 }
1361
1362 aName = std::move( result );
1363 return true;
1364}
1365
1366
1367bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar )
1368{
1369 bool changed = false;
1370 wxString result;
1371 result.reserve( aName.Length() );
1372 wxString illWChars = GetIllegalFileNameWxChars();
1373
1374 for( wxString::iterator it = aName.begin(); it != aName.end(); ++it )
1375 {
1376 if( illWChars.Find( *it ) != wxNOT_FOUND )
1377 {
1378 if( aReplaceChar )
1379 result += aReplaceChar;
1380 else
1381 result += wxString::Format( "%%%02x", *it );
1382
1383 changed = true;
1384 }
1385 else
1386 {
1387 result += *it;
1388 }
1389 }
1390
1391 if( changed )
1392 aName = std::move( result );
1393
1394 return changed;
1395}
1396
1397
1398void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter )
1399{
1400 wxString tmp;
1401
1402 for( unsigned ii = 0; ii < aText.Length(); ii++ )
1403 {
1404 if( aText[ii] == aSplitter )
1405 {
1406 aStrings.Add( tmp );
1407 tmp.Clear();
1408 }
1409 else
1410 {
1411 tmp << aText[ii];
1412 }
1413 }
1414
1415 if( !tmp.IsEmpty() )
1416 aStrings.Add( tmp );
1417}
1418
1419
1420void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed )
1421{
1422 struct lconv* lc = localeconv();
1423 char sep = lc->decimal_point[0];
1424 unsigned sep_pos = aStringValue.Find( sep );
1425
1426 if( sep_pos > 0 )
1427 {
1428 // We want to keep at least aTrailingZeroAllowed digits after the separator
1429 unsigned min_len = sep_pos + aTrailingZeroAllowed + 1;
1430
1431 while( aStringValue.Len() > min_len )
1432 {
1433 if( aStringValue.Last() == '0' )
1434 aStringValue.RemoveLast();
1435 else
1436 break;
1437 }
1438 }
1439}
1440
1441
1442std::string FormatDouble2Str( double aValue )
1443{
1444 std::string buf;
1445
1446 if( aValue != 0.0 && std::fabs( aValue ) <= 0.0001 )
1447 {
1448 buf = fmt::format( "{:.16f}", aValue );
1449
1450 // remove trailing zeros (and the decimal marker if needed)
1451 while( !buf.empty() && buf[buf.size() - 1] == '0' )
1452 {
1453 buf.pop_back();
1454 }
1455
1456 // if the value was really small
1457 // we may have just stripped all the zeros after the decimal
1458 if( buf[buf.size() - 1] == '.' )
1459 {
1460 buf.pop_back();
1461 }
1462 }
1463 else
1464 {
1465 buf = fmt::format( "{:.10g}", aValue );
1466 }
1467
1468 return buf;
1469}
1470
1471
1472std::string UIDouble2Str( double aValue )
1473{
1474 char buf[50];
1475 int len;
1476
1477 if( aValue != 0.0 && std::fabs( aValue ) <= 0.0001 )
1478 {
1479 // For these small values, %f works fine,
1480 // and %g gives an exponent
1481 len = snprintf( buf, sizeof( buf ), "%.16f", aValue );
1482
1483 while( --len > 0 && buf[len] == '0' )
1484 buf[len] = '\0';
1485
1486 if( buf[len] == '.' || buf[len] == ',' )
1487 buf[len] = '\0';
1488 else
1489 ++len;
1490 }
1491 else
1492 {
1493 // For these values, %g works fine, and sometimes %f
1494 // gives a bad value (try aValue = 1.222222222222, with %.16f format!)
1495 len = snprintf( buf, sizeof( buf ), "%.10g", aValue );
1496 }
1497
1498 return std::string( buf, len );
1499}
1500
1501
1502wxString From_UTF8( const char* cstring )
1503{
1504 // Convert an expected UTF8 encoded C string to a wxString
1505 wxString line = wxString::FromUTF8( cstring );
1506
1507 if( line.IsEmpty() ) // happens when cstring is not a valid UTF8 sequence
1508 {
1509 line = wxConvCurrent->cMB2WC( cstring ); // try to use locale conversion
1510
1511 if( line.IsEmpty() )
1512 line = wxString::From8BitData( cstring ); // try to use native string
1513 }
1514
1515 return line;
1516}
1517
1518
1519wxString From_UTF8( const std::string& aString )
1520{
1521 // Convert an expected UTF8 encoded std::string to a wxString
1522 wxString line = wxString::FromUTF8( aString );
1523
1524 if( line.IsEmpty() ) // happens when aString is not a valid UTF8 sequence
1525 {
1526 line = wxConvCurrent->cMB2WC( aString.c_str() ); // try to use locale conversion
1527
1528 if( line.IsEmpty() )
1529 line = wxString::From8BitData( aString.c_str() ); // try to use native string
1530 }
1531
1532 return line;
1533}
1534
1535
1536wxString NormalizeFileUri( const wxString& aFileUri )
1537{
1538 wxString uriPathAndFileName;
1539
1540 wxCHECK( aFileUri.StartsWith( wxS( "file://" ), &uriPathAndFileName ), aFileUri );
1541
1542 wxString tmp = uriPathAndFileName;
1543 wxString retv = wxS( "file://" );
1544
1545 tmp.Replace( wxS( "\\" ), wxS( "/" ) );
1546 tmp.Replace( wxS( ":" ), wxS( "" ) );
1547
1548 if( !tmp.IsEmpty() && tmp[0] != '/' )
1549 tmp = wxS( "/" ) + tmp;
1550
1551 retv += tmp;
1552
1553 return retv;
1554}
1555
1556
1557wxString ConvertPathToFileUri( const wxString& aPath, const PROJECT* aProject )
1558{
1559 if( aPath.IsEmpty() || aPath == wxS( "~" ) )
1560 return aPath;
1561
1562 bool looksLikePath = aPath.StartsWith( wxS( "/" ) ) || aPath.StartsWith( wxS( "${" ) )
1563 || aPath.StartsWith( wxS( "./" ) ) || aPath.StartsWith( wxS( "../" ) );
1564
1565#ifdef __WINDOWS__
1566 looksLikePath = looksLikePath || ( aPath.Length() >= 2 && wxIsalpha( aPath[0] ) && aPath[1] == ':' )
1567 || aPath.StartsWith( wxS( "\\\\" ) ) || aPath.StartsWith( wxS( ".\\" ) )
1568 || aPath.StartsWith( wxS( "..\\" ) );
1569#endif
1570
1571 if( !looksLikePath )
1572 {
1573 wxURI uri( aPath );
1574
1575 if( uri.HasScheme() )
1576 return aPath;
1577
1578 return aPath; // Not a path, return unchanged
1579 }
1580
1581 // Resolve env vars
1582 wxString resolved = aPath;
1583
1584 if( aProject )
1585 resolved = ResolveUriByEnvVars( aPath, aProject );
1586
1587 wxFileName fname( resolved );
1588
1589 if( !fname.IsAbsolute() && aProject && !aProject->GetProjectPath().IsEmpty() )
1590 {
1591 fname.MakeAbsolute( aProject->GetProjectPath() );
1592 resolved = fname.GetFullPath();
1593 }
1594
1595 // Only convert if the file actually exists
1596 bool isUNC = resolved.StartsWith( wxS( "\\\\" ) );
1597
1598 if( !isUNC && !wxFileExists( resolved ) && !wxDirExists( resolved ) )
1599 return aPath;
1600
1601 if( aPath.StartsWith( wxS( "/" ) ) )
1602 return wxS( "file://" ) + aPath;
1603
1604 if( aPath.StartsWith( wxS( "${" ) ) )
1605 return wxS( "file://" ) + aPath;
1606
1607 if( aPath.StartsWith( wxS( "./" ) ) || aPath.StartsWith( wxS( "../" ) ) )
1608 return wxS( "file://" ) + aPath;
1609
1610#ifdef __WINDOWS__
1611 if( aPath.StartsWith( wxS( "\\\\" ) ) )
1612 {
1613 wxString path = aPath.Mid( 2 );
1614 path.Replace( wxS( "\\" ), wxS( "/" ) );
1615 return wxS( "file://" ) + path;
1616 }
1617
1618 if( aPath.Length() >= 2 && wxIsalpha( aPath[0] ) && aPath[1] == ':' )
1619 {
1620 wxString path = aPath;
1621 path.Replace( wxS( "\\" ), wxS( "/" ) );
1622 return wxS( "file:///" ) + path;
1623 }
1624
1625 if( aPath.StartsWith( wxS( ".\\" ) ) || aPath.StartsWith( wxS( "..\\" ) ) )
1626 {
1627 wxString path = aPath;
1628 path.Replace( wxS( "\\" ), wxS( "/" ) );
1629 return wxS( "file://" ) + path;
1630 }
1631#endif
1632
1633 return aPath;
1634}
1635
1636
1637namespace
1638{
1639 // Characters that carry structural meaning inside stacked pin notation and therefore must be
1640 // backslash-escaped when they appear literally inside an individual pin number.
1641 bool IsStackedPinSpecialChar( wxUniChar aChar )
1642 {
1643 return aChar == '[' || aChar == ']' || aChar == ',' || aChar == '-' || aChar == '\\';
1644 }
1645
1646
1647 // A backslash starts an escape sequence only when it precedes one of the structural characters.
1648 // Any other backslash is a literal character, which keeps legacy notation that happened to use
1649 // an un-escaped backslash (e.g. [A\B,C]) intact.
1650 bool IsEscapeAt( const wxString& aText, size_t aIndex )
1651 {
1652 return aText[aIndex] == '\\' && aIndex + 1 < aText.length()
1653 && IsStackedPinSpecialChar( aText[aIndex + 1] );
1654 }
1655
1656
1657 // Split the inner part of a stacked notation string on commas, honouring backslash escaping so
1658 // that a literal comma in a pin number (written "\,") does not start a new item. The returned
1659 // parts are still escaped; call UnescapeStackedPinItem() to recover the literal pin number.
1660 std::vector<wxString> SplitStackedPinItems( const wxString& aInner )
1661 {
1662 std::vector<wxString> parts;
1663 wxString current;
1664
1665 for( size_t i = 0; i < aInner.length(); ++i )
1666 {
1667 if( IsEscapeAt( aInner, i ) )
1668 {
1669 current << aInner[i] << aInner[i + 1];
1670 ++i;
1671 }
1672 else if( aInner[i] == ',' )
1673 {
1674 parts.push_back( current );
1675 current.clear();
1676 }
1677 else
1678 {
1679 current << aInner[i];
1680 }
1681 }
1682
1683 parts.push_back( current );
1684 return parts;
1685 }
1686
1687
1688 int FindUnescaped( const wxString& aText, wxUniChar aChar )
1689 {
1690 for( size_t i = 0; i < aText.length(); ++i )
1691 {
1692 if( IsEscapeAt( aText, i ) )
1693 ++i;
1694 else if( aText[i] == aChar )
1695 return static_cast<int>( i );
1696 }
1697
1698 return wxNOT_FOUND;
1699 }
1700
1701
1702 // A backslash that does not precede a structural character is a literal and is left untouched.
1703 wxString UnescapeStackedPinItem( const wxString& aItem )
1704 {
1705 wxString out;
1706
1707 for( size_t i = 0; i < aItem.length(); ++i )
1708 {
1709 if( IsEscapeAt( aItem, i ) )
1710 out << aItem[++i];
1711 else
1712 out << aItem[i];
1713 }
1714
1715 return out;
1716 }
1717
1718
1719 // Extract (prefix, numericValue) where numericValue = -1 if no numeric suffix
1720 std::pair<wxString, long> ParseAlphaNumericPin( const wxString& pinNum )
1721 {
1722 wxString prefix;
1723 long numValue = -1;
1724
1725 size_t numStart = pinNum.length();
1726 for( int i = static_cast<int>( pinNum.length() ) - 1; i >= 0; --i )
1727 {
1728 if( !wxIsdigit( pinNum[i] ) )
1729 {
1730 numStart = i + 1;
1731 break;
1732 }
1733 if( i == 0 )
1734 numStart = 0; // all digits
1735 }
1736
1737 if( numStart < pinNum.length() )
1738 {
1739 prefix = pinNum.Left( numStart );
1740 wxString numericPart = pinNum.Mid( numStart );
1741 numericPart.ToLong( &numValue );
1742 }
1743
1744 return { prefix, numValue };
1745 }
1746}
1747
1748wxString EscapeStackedPinItem( const wxString& aPinNumber )
1749{
1750 wxString escaped;
1751
1752 for( wxUniChar ch : aPinNumber )
1753 {
1754 if( IsStackedPinSpecialChar( ch ) )
1755 escaped << '\\';
1756
1757 escaped << ch;
1758 }
1759
1760 return escaped;
1761}
1762
1763
1764std::vector<wxString> SplitStackedPinDisplayItems( const wxString& aInner )
1765{
1766 std::vector<wxString> items;
1767
1768 for( const wxString& part : SplitStackedPinItems( aInner ) )
1769 items.push_back( UnescapeStackedPinItem( part ) );
1770
1771 return items;
1772}
1773
1774
1775std::vector<wxString> ExpandStackedPinNotation( const wxString& aPinName, bool* aValid )
1776{
1777 if( aValid )
1778 *aValid = true;
1779
1780 std::vector<wxString> expanded;
1781
1782 const bool hasOpenBracket = aPinName.Contains( wxT( "[" ) );
1783 const bool hasCloseBracket = aPinName.Contains( wxT( "]" ) );
1784
1785 if( hasOpenBracket || hasCloseBracket )
1786 {
1787 if( !aPinName.StartsWith( wxT( "[" ) ) || !aPinName.EndsWith( wxT( "]" ) ) )
1788 {
1789 if( aValid )
1790 *aValid = false;
1791 expanded.push_back( aPinName );
1792 return expanded;
1793 }
1794 }
1795
1796 if( !aPinName.StartsWith( wxT( "[" ) ) || !aPinName.EndsWith( wxT( "]" ) ) )
1797 {
1798 expanded.push_back( aPinName );
1799 return expanded;
1800 }
1801
1802 const wxString inner = aPinName.Mid( 1, aPinName.Length() - 2 );
1803
1804 for( wxString part : SplitStackedPinItems( inner ) )
1805 {
1806 part.Trim( true ).Trim( false );
1807
1808 if( part.empty() )
1809 continue;
1810
1811 // A range (e.g. A1-A4) is only recognized on an unescaped dash; an escaped dash is a
1812 // literal character inside a single pin number.
1813 int dashPos = FindUnescaped( part, '-' );
1814 if( dashPos != wxNOT_FOUND )
1815 {
1816 wxString startTxt = UnescapeStackedPinItem( part.Left( dashPos ) );
1817 wxString endTxt = UnescapeStackedPinItem( part.Mid( dashPos + 1 ) );
1818 startTxt.Trim( true ).Trim( false );
1819 endTxt.Trim( true ).Trim( false );
1820
1821 auto [startPrefix, startVal] = ParseAlphaNumericPin( startTxt );
1822 auto [endPrefix, endVal] = ParseAlphaNumericPin( endTxt );
1823
1824 if( startPrefix != endPrefix || startVal == -1 || endVal == -1 || startVal > endVal )
1825 {
1826 if( aValid )
1827 *aValid = false;
1828 expanded.clear();
1829 expanded.push_back( aPinName );
1830 return expanded;
1831 }
1832
1833 for( long ii = startVal; ii <= endVal; ++ii )
1834 {
1835 if( startPrefix.IsEmpty() )
1836 expanded.emplace_back( wxString::Format( wxT( "%ld" ), ii ) );
1837 else
1838 expanded.emplace_back( wxString::Format( wxT( "%s%ld" ), startPrefix, ii ) );
1839 }
1840 }
1841 else
1842 {
1843 expanded.push_back( UnescapeStackedPinItem( part ) );
1844 }
1845 }
1846
1847 if( expanded.empty() )
1848 {
1849 expanded.push_back( aPinName );
1850 if( aValid )
1851 *aValid = false;
1852 }
1853
1854 return expanded;
1855}
1856
1857
1858int CountStackedPinNotation( const wxString& aPinName, bool* aValid )
1859{
1860 size_t len = aPinName.length();
1861
1862 // An empty pin number is a single (valid) pin; guard before indexing below.
1863 if( len == 0 )
1864 {
1865 if( aValid )
1866 *aValid = true;
1867
1868 return 1;
1869 }
1870
1871 if( !aValid )
1872 {
1873 // Fastest path when we're not interested in validity
1874 if( len < 3 )
1875 return 1;
1876 }
1877 else
1878 {
1879 *aValid = true;
1880
1881 // Fast path: if no brackets, it's a single pin
1882 const bool hasOpenBracket = aPinName.Contains( wxT( "[" ) );
1883 const bool hasCloseBracket = aPinName.Contains( wxT( "]" ) );
1884
1885 if( hasOpenBracket || hasCloseBracket )
1886 {
1887 if( aPinName[0] != '[' || aPinName[len - 1] != ']' )
1888 {
1889 *aValid = false;
1890 return 1;
1891 }
1892 }
1893 }
1894
1895 if( aPinName[0] != '[' || aPinName[len - 1] != ']' )
1896 return 1;
1897
1898 const wxString inner = aPinName.Mid( 1, aPinName.Length() - 2 );
1899
1900 int count = 0;
1901
1902 for( wxString part : SplitStackedPinItems( inner ) )
1903 {
1904 part.Trim( true ).Trim( false );
1905
1906 if( part.empty() )
1907 continue;
1908
1909 int dashPos = FindUnescaped( part, '-' );
1910 if( dashPos != wxNOT_FOUND )
1911 {
1912 wxString startTxt = UnescapeStackedPinItem( part.Left( dashPos ) );
1913 wxString endTxt = UnescapeStackedPinItem( part.Mid( dashPos + 1 ) );
1914 startTxt.Trim( true ).Trim( false );
1915 endTxt.Trim( true ).Trim( false );
1916
1917 auto [startPrefix, startVal] = ParseAlphaNumericPin( startTxt );
1918 auto [endPrefix, endVal] = ParseAlphaNumericPin( endTxt );
1919
1920 if( startPrefix != endPrefix || startVal == -1 || endVal == -1 || startVal > endVal )
1921 {
1922 if( aValid )
1923 *aValid = false;
1924
1925 return 1;
1926 }
1927
1928 // Count pins in the range
1929 count += static_cast<int>( endVal - startVal + 1 );
1930 }
1931 else
1932 {
1933 // Single pin
1934 ++count;
1935 }
1936 }
1937
1938 if( count == 0 )
1939 {
1940 if( aValid )
1941 *aValid = false;
1942
1943 return 1;
1944 }
1945
1946 return count;
1947}
1948
1949
1951{
1952 return wxString( defaultVariantName );
1953}
1954
1955
1956int SortVariantNames( const wxString& aLhs, const wxString& aRhs )
1957{
1958 if( ( aLhs == defaultVariantName ) && ( aRhs != defaultVariantName ) )
1959 return -1;
1960
1961 if( ( aLhs != defaultVariantName ) && ( aRhs == defaultVariantName ) )
1962 return 1;
1963
1964 return StrNumCmp( aLhs, aRhs );
1965}
1966
1967
1968std::vector<LOAD_MESSAGE> ExtractLibraryLoadErrors( const wxString& aErrorString, int aSeverity )
1969{
1970 std::vector<LOAD_MESSAGE> messages;
1971
1972 if( aErrorString.IsEmpty() )
1973 return messages;
1974
1975 // Errors are separated by newlines. We want to keep:
1976 // - Lines starting with "Library '" (library-level errors)
1977 // - Lines containing "Expecting" (file error location)
1978 // And strip:
1979 // - Lines starting with "from " (internal code location info)
1980 wxStringTokenizer tokenizer( aErrorString, wxS( "\n" ), wxTOKEN_STRTOK );
1981
1982 while( tokenizer.HasMoreTokens() )
1983 {
1984 wxString line = tokenizer.GetNextToken();
1985
1986 // Skip internal code location lines (e.g., "from pcb_io_kicad_sexpr_parser.cpp : ...")
1987 if( line.StartsWith( wxS( "from " ) ) )
1988 continue;
1989
1990 if( line.StartsWith( wxS( "Library '" ) ) || line.Contains( wxS( "Expecting" ) ) )
1991 messages.push_back( { line, static_cast<SEVERITY>( aSeverity ) } );
1992 }
1993
1994 return messages;
1995}
1996
int index
static bool IsOldSchoolDecimalSeparator(wxUniChar ch, double *siScaler)
Container for project specific data.
Definition project.h:62
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition project.cpp:183
const wxString ResolveUriByEnvVars(const wxString &aUri, const PROJECT *aProject)
Replace any environment and/or text variables in URIs.
Definition common.cpp:717
The common library.
This file contains miscellaneous commonly used macros and functions.
std::optional< V > get_opt(const std::map< wxString, V > &aMap, const wxString &aKey)
Definition map_helpers.h:30
SEVERITY
const int scale
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
wxString RemoveHTMLTags(const wxString &aInput)
Removes HTML tags from a string.
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
bool WildCompareString(const wxString &pattern, const wxString &string_to_tst, bool case_sensitive)
Compare a string against wild card (* and ?) pattern using the usual rules.
wxString GetDefaultVariantName()
std::vector< wxString > ExpandStackedPinNotation(const wxString &aPinName, bool *aValid)
Expand stacked pin notation like [1,2,3], [1-4], [A1-A4], or [AA1-AA3,AB4,CD12-CD14] into individual ...
wxString ConvertToNewOverbarNotation(const wxString &aOldStr)
Convert the old ~...~ overbar notation to the new ~{...} one.
int GetTrailingInt(const wxString &aStr)
Gets the trailing int, if any, from a string.
wxString UnescapeString(const wxString &aSource)
wxString LinkifyHTML(wxString aStr)
Wraps links in HTML tags.
bool convertSeparators(wxString *value)
wxString GetIllegalFileNameWxChars()
wxString From_UTF8(const char *cstring)
std::vector< LOAD_MESSAGE > ExtractLibraryLoadErrors(const wxString &aErrorString, int aSeverity)
Parse library load error messages, extracting user-facing information while stripping internal code l...
int SortVariantNames(const wxString &aLhs, const wxString &aRhs)
bool ConvertSmartQuotesAndDashes(wxString *aString)
Convert curly quotes and em/en dashes to straight quotes and dashes.
static const wxChar defaultVariantName[]
wxString ConvertPathToFileUri(const wxString &aPath, const PROJECT *aProject)
Convert a file path to a file:// URI.
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
int ReadDelimitedText(wxString *aDest, const char *aSource)
Copy bytes from aSource delimited string segment to aDest wxString.
wxString TitleCaps(const wxString &aString)
Capitalize the first letter in each word.
std::string UIDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 We want to avoid scientific ...
static constexpr std::string_view illegalFileNameChars
Illegal file name characters used to ensure file names will be valid on all supported platforms.
std::vector< wxString > SplitStackedPinDisplayItems(const wxString &aInner)
Split the inner part of a stacked notation string (the text between the brackets) into its individual...
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
bool ReplaceIllegalFileNameChars(std::string &aName, int aReplaceChar)
Checks aName for illegal file name characters.
wxString EscapeStackedPinItem(const wxString &aPinNumber)
Escape the characters that carry structural meaning inside stacked pin notation ('[',...
int PrintableCharCount(const wxString &aString)
Return the number of printable (ie: non-formatting) chars.
std::string EscapedUTF8(const wxString &aString)
Return an 8 bit UTF8 string given aString in Unicode form.
char * GetLine(FILE *File, char *Line, int *LineNum, int SizeLine)
Read one line line from aFile.
bool ApplyModifier(double &value, const wxString &aString)
wxString InitialCaps(const wxString &aString)
Capitalize only the first word.
wxString NormalizeFileUri(const wxString &aFileUri)
Normalize file path aFileUri to URI convention.
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
wxString GetISO8601CurrentDateTime()
int ValueStringCompare(const wxString &strFWord, const wxString &strSWord)
Compare strings like the strcmp function but handle numbers and modifiers within the string text corr...
int SplitString(const wxString &strToSplit, wxString *strBeginning, wxString *strDigits, wxString *strEnd)
Break a string into three parts: he alphabetic preamble, the numeric part, and any alphabetic ending.
bool IsFullFileNameValid(const wxString &aFullFilename)
Checks if a full filename is valid, i.e.
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Remove trailing zeros from a string containing a converted float number.
int CountStackedPinNotation(const wxString &aPinName, bool *aValid)
Count the number of pins represented by stacked pin notation.
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
bool NoPrintableChars(const wxString &aString)
Return true if the string is empty or contains only whitespace.
wxString UnescapeHTML(const wxString &aString)
Return a new wxString unescaped from HTML format.
bool IsURL(wxString aStr)
Performs a URL sniff-test on a string.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
ESCAPE_CONTEXT
Escape/Unescape routines to safely encode reserved-characters in various contexts.
@ CTX_FILENAME
@ CTX_QUOTED_STR
@ CTX_LINE
@ CTX_NO_SPACE
@ CTX_LIBID
@ CTX_NETNAME
@ CTX_CSV
@ CTX_IPC
@ CTX_LEGACY_LIBID
@ CTX_JS_STR
std::string path
VECTOR2I end
wxString result
Test unit parsing edge cases and error handling.