-
Notifications
You must be signed in to change notification settings - Fork 10
/
tlString.cc
1477 lines (1287 loc) · 28.6 KB
/
tlString.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
KLayout Layout Viewer
Copyright (C) 2013-2018 Matthias Koefferlein
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <limits>
#include <vector>
#include <sstream>
#include "tlString.h"
static std::locale c_locale ("C");
// -------------------------------------------------------------------------
// Utility: a strtod version that is independent of the locale
static std::string micron_format ("%.5f");
static std::string dbu_format ("%.2f");
void tl::set_micron_resolution (unsigned int ndigits)
{
micron_format = "%." + tl::to_string (ndigits) + "f";
}
void tl::set_db_resolution (unsigned int ndigits)
{
dbu_format = "%." + tl::to_string (ndigits) + "f";
}
std::string tl::micron_to_string (double d)
{
return tl::sprintf (micron_format.c_str (), d);
}
std::string tl::db_to_string (double d)
{
return tl::sprintf (dbu_format.c_str (), d);
}
// -------------------------------------------------------------------------
// Utility: a strtod version that is independent of the locale
static double local_strtod (const char *cp, const char *&cp_new)
{
const char *cp0 = cp;
// Extract sign
double s = 1.0;
if (*cp == '-') {
s = -1.0;
++cp;
/*
} else if (*cp == '+') {
++cp;
*/
}
// Extract upper digits
int exponent = 0;
double mant = 0.0;
while (isdigit (*cp)) {
mant = mant * 10.0 + double (*cp - '0');
++cp;
}
// Extract lower digits
if (*cp == '.') {
++cp;
while (isdigit (*cp)) {
mant = mant * 10.0 + double (*cp - '0');
++cp;
--exponent;
}
}
// Extract exponent (unless we're at the beginning)
if (cp != cp0 && (*cp == 'e' || *cp == 'E')) {
++cp;
bool epos = true;
if (*cp == '-') {
epos = false;
++cp;
} else if (*cp == '+') {
++cp;
}
int en = 0;
while (isdigit (*cp)) {
en = en * 10 + int (*cp - '0');
++cp;
}
if (! epos) {
en = -en;
}
exponent += en;
}
cp_new = cp;
return s * mant * pow(10.0, exponent);
}
// -------------------------------------------------------------------------
// Implementation
// TODO: no translation provided currently
std::string
tl::translate (const std::string &s)
{
return s;
}
// TODO: no conversion provided currently
// This assumes the file system encoding is UTF-8
std::string
tl::string_to_system (const std::string &s)
{
return s;
}
std::string
tl::to_string (double d, int prec)
{
std::ostringstream os;
os.imbue (c_locale);
os.precision (prec);
os.setf (std::ios_base::fmtflags (0), std::ios::basefield);
os.setf (std::ios_base::fmtflags (0), std::ios::floatfield);
os << d;
return os.str ();
}
std::string
tl::to_string (float d, int prec)
{
std::ostringstream os;
os.imbue (c_locale);
os.precision (prec);
os.setf (std::ios_base::fmtflags (0), std::ios::basefield);
os.setf (std::ios_base::fmtflags (0), std::ios::floatfield);
os << d;
return os.str ();
}
template <>
std::string
tl::to_string (const int &d)
{
std::ostringstream os;
os.imbue (c_locale);
os << d;
return os.str ();
}
template <>
std::string
tl::to_string (const unsigned int &d)
{
std::ostringstream os;
os.imbue (c_locale);
os << d;
return os.str ();
}
template <>
std::string
tl::to_string (const long &d)
{
std::ostringstream os;
os.imbue (c_locale);
os << d;
return os.str ();
}
template <>
std::string
tl::to_string (const long long &d)
{
std::ostringstream os;
os.imbue (c_locale);
os << d;
return os.str ();
}
template <>
std::string
tl::to_string (const unsigned long &d)
{
std::ostringstream os;
os.imbue (c_locale);
os << d;
return os.str ();
}
template <>
std::string
tl::to_string (const unsigned long long &d)
{
std::ostringstream os;
os.imbue (c_locale);
os << d;
return os.str ();
}
template <>
std::string
tl::to_string (char * const &cp)
{
return std::string (cp);
}
template <>
std::string
tl::to_string (const char * const &cp)
{
return std::string (cp);
}
template <>
std::string
tl::to_string (unsigned char * const &cp)
{
return std::string ((const char *) cp);
}
template <>
std::string
tl::to_string (const unsigned char * const &cp)
{
return std::string ((const char *) cp);
}
std::string
tl::to_string (const char *cp, int length)
{
return std::string (cp, 0, length);
}
std::string
tl::to_string (const unsigned char *cp, int length)
{
return std::string ((const char *) cp, 0, length);
}
template <>
std::string
tl::to_string (const bool &b)
{
return b ? "true" : "false";
}
int
tl::edit_distance (const std::string &a, const std::string &b)
{
std::vector<int> row0, row1;
row0.resize (a.size () + 1, 0);
row1.resize (a.size () + 1, 0);
for (int i = 0; i <= int (a.size ()); ++i) {
row0[i] = i;
}
for (int i = 0; i < int (b.size ()); ++i) {
row1[0] = i + 1;
for (int j = 0; j < int (a.size ()); ++j) {
int cost = (b[i] == a[j] ? 0 : 1);
row1[j + 1] = std::min (row0[j] + cost, std::min (row0[j + 1], row1[j]) + 1);
}
row0.swap (row1);
}
return row0 [a.size ()];
}
std::string
tl::to_quoted_string (const std::string &s)
{
std::string r;
r.reserve (s.size () + 2);
r += '\'';
for (const char *c = s.c_str (); *c; ++c) {
if (*c == '\'' || *c == '\\') {
r += '\\';
r += *c;
} else if (*c == '\n') {
r += "\\n";
} else if (*c == '\r') {
r += "\\r";
} else if (*c == '\t') {
r += "\\t";
} else if (! isprint (*c)) {
char b [20];
::sprintf (b, "\\%03o", int ((unsigned char) *c));
r += b;
} else {
r += *c;
}
}
r += '\'';
return r;
}
std::string
tl::escape_string (const std::string &s)
{
std::string r;
for (const char *c = s.c_str (); *c; ++c) {
if (*c == '\\') {
r += '\\';
r += *c;
} else if (*c == '\n') {
r += "\\n";
} else if (*c == '\r') {
r += "\\r";
} else if (*c == '\t') {
r += "\\t";
} else if (! isprint (*c)) {
char b [20];
::sprintf (b, "\\%03o", int ((unsigned char) *c));
r += b;
} else {
r += *c;
}
}
return r;
}
inline char unescape_char (const char * &cp)
{
if (isdigit (*cp)) {
int c = 0;
while (*cp && isdigit (*cp)) {
c = c * 8 + int (*cp - '0');
++cp;
}
--cp;
return char (c);
} else if (*cp == 'r') {
return '\r';
} else if (*cp == 'n') {
return '\n';
} else if (*cp == 't') {
return '\t';
} else {
return *cp;
}
}
std::string
tl::unescape_string (const std::string &value)
{
std::string r;
for (const char *cp = value.c_str (); *cp; ++cp) {
if (*cp == '\\' && cp[1]) {
++cp;
r += unescape_char (cp);
} else {
r += *cp;
}
}
return r;
}
std::string
tl::to_word_or_quoted_string (const std::string &s, const char *non_term)
{
// If the string does not contain non_term characters, we may simply keep it.
// Otherwise we need to quote it.
const char *cp = s.c_str ();
if (*cp && (isalpha (*cp) || strchr (non_term, *cp) != NULL)) {
++cp;
for ( ; *cp && (isalnum (*cp) || strchr (non_term, *cp) != NULL); ++cp) {
;
}
}
if (*cp || s.empty ()) {
return to_quoted_string (s);
} else {
return s;
}
}
void
tl::from_string (const std::string &s, const char * &result)
{
result = s.c_str ();
}
void
tl::from_string (const std::string &s, const unsigned char * &result)
{
result = (unsigned char *) s.c_str ();
}
void
tl::from_string (const std::string &s, double &v) throw (tl::Exception)
{
const char *cp = s.c_str ();
while (*cp && isspace (*cp)) {
++cp;
}
if (! *cp) {
throw tl::Exception (tl::translate ("Got empty string where a real number was expected"));
}
const char *cp_end = cp;
v = local_strtod (cp, cp_end);
while (*cp_end && isspace (*cp_end)) {
++cp_end;
}
}
template <class T>
void
convert_string_to_int (const std::string &s, T &v) throw (tl::Exception)
{
double x;
// HACK: this should be some real string-to-int conversion
tl::from_string (s, x);
if (x < std::numeric_limits <T>::min ()) {
throw tl::Exception (tl::translate ("Range underflow: ") + s);
}
if (x > std::numeric_limits <T>::max ()) {
throw tl::Exception (tl::translate ("Range overflow: ") + s);
}
v = T (x);
if (x != v) {
throw tl::Exception (tl::translate ("Number cannot be represented precisely: ") + s);
}
}
void
tl::from_string (const std::string &s, int &v) throw (tl::Exception)
{
convert_string_to_int (s, v);
}
void
tl::from_string (const std::string &s, long &v) throw (tl::Exception)
{
convert_string_to_int (s, v);
}
void
tl::from_string (const std::string &s, long long &v) throw (tl::Exception)
{
convert_string_to_int (s, v);
}
void
tl::from_string (const std::string &s, unsigned int &v) throw (tl::Exception)
{
convert_string_to_int (s, v);
}
void
tl::from_string (const std::string &s, unsigned long &v) throw (tl::Exception)
{
convert_string_to_int (s, v);
}
void
tl::from_string (const std::string &s, unsigned long long &v) throw (tl::Exception)
{
convert_string_to_int (s, v);
}
void
tl::from_string (const std::string &s, bool &b) throw (tl::Exception)
{
std::string t (tl::trim (s));
if (t == "true") {
b = true;
} else if (t == "false") {
b = false;
} else if (t == "1") {
b = true;
} else if (t == "0") {
b = false;
} else {
throw tl::Exception (tl::translate ("Invalid boolean value: ") + s);
}
}
std::string
tl::join (const std::vector <std::string> &vv, const std::string &s)
{
std::ostringstream r;
bool first = true;
for (std::vector <std::string>::const_iterator i = vv.begin (); i != vv.end (); ++i) {
if (first) {
r << s;
first = false;
}
r << *i;
}
return r.str ();
}
std::vector<std::string>
tl::split (const std::string &t, const std::string &s)
{
std::vector<std::string> r;
size_t p = 0;
for (size_t pp = 0; (pp = t.find (s, p)) != std::string::npos; p = pp + s.size ()) {
r.push_back (std::string (t, p, pp - p));
}
r.push_back (std::string (t, p));
return r;
}
std::string
tl::trim (const std::string &s)
{
const char *cp = s.c_str ();
while (isspace (*cp) && *cp) {
++cp;
}
const char *cq = s.c_str () + s.size ();
while (cq > cp && isspace (cq [-1])) {
--cq;
}
return std::string (cp, 0, cq - cp);
}
// -------------------------------------------------------------------
// tl::Extractor implementation
tl::Extractor::Extractor (const char *s)
: m_cp (s)
{
// .. nothing yet ..
}
tl::Extractor &
tl::Extractor::read (unsigned int &value)
{
if (! try_read (value)) {
error (tl::translate ("Expected an unsigned integer value"));
}
return *this;
}
tl::Extractor &
tl::Extractor::read (unsigned long &value)
{
if (! try_read (value)) {
error (tl::translate ("Expected an unsigned long integer value"));
}
return *this;
}
tl::Extractor &
tl::Extractor::read (unsigned long long &value)
{
if (! try_read (value)) {
error (tl::translate ("Expected an unsigned long integer value"));
}
return *this;
}
tl::Extractor &
tl::Extractor::read (double &value)
{
if (! try_read (value)) {
error (tl::translate ("Expected a real number"));
}
return *this;
}
tl::Extractor &
tl::Extractor::read (int &value)
{
if (! try_read (value)) {
error (tl::translate ("Expected a integer value"));
}
return *this;
}
tl::Extractor &
tl::Extractor::read (long &value)
{
if (! try_read (value)) {
error (tl::translate ("Expected a long integer value"));
}
return *this;
}
tl::Extractor &
tl::Extractor::read (long long &value)
{
if (! try_read (value)) {
error (tl::translate ("Expected a long integer value"));
}
return *this;
}
tl::Extractor &
tl::Extractor::read (bool &value)
{
if (! try_read (value)) {
error (tl::translate ("Expected a boolean value ('true', 'false')"));
}
return *this;
}
tl::Extractor &
tl::Extractor::read (std::string &value, const char *term)
{
if (! try_read (value, term)) {
error (tl::translate ("Expected a string"));
}
return *this;
}
tl::Extractor &
tl::Extractor::read_word (std::string &value, const char *non_term)
{
if (! try_read_word (value, non_term)) {
error (tl::translate ("Expected a word string"));
}
return *this;
}
tl::Extractor &
tl::Extractor::read_word_or_quoted (std::string &value, const char *non_term)
{
if (! try_read_word (value, non_term) && ! try_read_quoted (value)) {
error (tl::translate ("Expected a word or quoted string"));
}
return *this;
}
tl::Extractor &
tl::Extractor::read_quoted (std::string &value)
{
if (! try_read_quoted (value)) {
error (tl::translate ("Expected a quoted string"));
}
return *this;
}
bool
tl::Extractor::try_read (unsigned int &value)
{
if (! *skip ()) {
return false;
}
if (! isdigit (*m_cp)) {
return false;
}
value = 0;
while (isdigit (*m_cp)) {
if ((value * 10) / 10 != value) {
throw tl::Exception (tl::translate ("Range overflow on unsigned integer"));
}
value *= 10;
value += (*m_cp - '0');
++m_cp;
}
return true;
}
bool
tl::Extractor::try_read (int &value)
{
if (! *skip ()) {
return false;
}
bool minus = false;
if (*m_cp == '-') {
minus = true;
++m_cp;
} else if (*m_cp == '+') {
++m_cp;
}
if (! isdigit (*m_cp)) {
return false;
}
value = 0;
while (isdigit (*m_cp)) {
if ((value * 10) / 10 != value) {
throw tl::Exception (tl::translate ("Range overflow on integer"));
}
value *= 10;
value += (*m_cp - '0');
++m_cp;
}
if (minus) {
value = -value;
}
return true;
}
bool
tl::Extractor::try_read (unsigned long &value)
{
if (! *skip ()) {
return false;
}
if (! isdigit (*m_cp)) {
return false;
}
value = 0;
while (isdigit (*m_cp)) {
if ((value * 10) / 10 != value) {
throw tl::Exception (tl::translate ("Range overflow on unsigned long integer"));
}
value *= 10;
value += (*m_cp - '0');
++m_cp;
}
return true;
}
bool
tl::Extractor::try_read (unsigned long long &value)
{
if (! *skip ()) {
return false;
}
if (! isdigit (*m_cp)) {
return false;
}
value = 0;
while (isdigit (*m_cp)) {
if ((value * 10) / 10 != value) {
throw tl::Exception (tl::translate ("Range overflow on unsigned long long integer"));
}
value *= 10;
value += (*m_cp - '0');
++m_cp;
}
return true;
}
bool
tl::Extractor::try_read (long &value)
{
if (! *skip ()) {
return false;
}
bool minus = false;
if (*m_cp == '-') {
minus = true;
++m_cp;
} else if (*m_cp == '+') {
++m_cp;
}
if (! isdigit (*m_cp)) {
return false;
}
value = 0;
while (isdigit (*m_cp)) {
if ((value * 10) / 10 != value) {
throw tl::Exception (tl::translate ("Range overflow on long integer"));
}
value *= 10;
value += (*m_cp - '0');
++m_cp;
}
if (minus) {
value = -value;
}
return true;
}
bool
tl::Extractor::try_read (long long &value)
{
if (! *skip ()) {
return false;
}
bool minus = false;
if (*m_cp == '-') {
minus = true;
++m_cp;
} else if (*m_cp == '+') {
++m_cp;
}
if (! isdigit (*m_cp)) {
return false;
}
value = 0;
while (isdigit (*m_cp)) {
if ((value * 10) / 10 != value) {
throw tl::Exception (tl::translate ("Range overflow on long long integer"));
}
value *= 10;
value += (*m_cp - '0');
++m_cp;
}
if (minus) {
value = -value;
}
return true;
}
bool
tl::Extractor::try_read (double &value)
{
if (! *skip ()) {
return false;
}
const char *cp_end = m_cp;
value = local_strtod (m_cp, cp_end);
if (cp_end == m_cp) {
return false;
} else {
m_cp = cp_end;
return true;
}
}
bool
tl::Extractor::try_read (bool &value)
{
if (test ("0") || test ("false")) {
value = false;
return true;
}
if (test ("1") || test ("true")) {
value = true;
return true;
}
return false;
}
bool
tl::Extractor::try_read_word (std::string &string, const char *non_term)
{
if (! *skip ()) {
return false;
}
string.clear ();
while (*m_cp && (isalnum (*m_cp) || strchr (non_term, *m_cp) != NULL)) {
string += *m_cp;
++m_cp;
}
return ! string.empty ();
}
bool
tl::Extractor::try_read_word_or_quoted (std::string &string, const char *non_term)
{
return try_read_word (string, non_term) || try_read_quoted (string);
}
bool
tl::Extractor::try_read_quoted (std::string &string)
{
char q = *skip ();
if (q != '\'' && q != '\"') {
return false;
}
++m_cp;
string.clear ();
while (*m_cp && *m_cp != q) {
if (*m_cp == '\\' && m_cp[1]) {
++m_cp;
string += unescape_char (m_cp);
} else {
string += *m_cp;
}
++m_cp;
}
if (*m_cp == q) {
++m_cp;
}
return true;
}
bool
tl::Extractor::try_read (std::string &string, const char *term)
{
// if the terminating characters contain line feed for blank, we must not skip over them
if (strchr (term, '\n') || strchr (term, ' ')) {
while (isspace (*m_cp) && strchr (term, *m_cp) == 0 && *m_cp) {
++m_cp;
}
if (! *m_cp) {
return false;
}
} else if (! *skip ()) {
return false;
}
bool term_is_space = false;
for (const char *t = term; *t && ! term_is_space; ++t) {
term_is_space = isspace (*t);
}
string.clear ();
while (*m_cp && (term_is_space || ! isspace (*m_cp)) && strchr (term, *m_cp) == NULL) {
string += *m_cp;
++m_cp;
}
return true;
}
tl::Extractor &
tl::Extractor::expect_end ()
{
if (! at_end ()) {
error (tl::translate ("Expected end of text"));
}
return *this;
}
tl::Extractor &
tl::Extractor::expect_more ()
{
if (at_end ()) {
error (tl::translate ("Expected more text"));
}
return *this;
}
tl::Extractor &
tl::Extractor::expect (const char *token)
{
if (! test (token)) {
error (tl::sprintf (tl::translate ("Expected '%s'").c_str (), token));
}
return *this;
}
bool
tl::Extractor::test (const char *token)
{
skip ();
const char *cp = m_cp;
while (*cp && *token) {
if (*cp != *token) {
return false;
}
++cp;
++token;
}