This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
strlib.js
1186 lines (1120 loc) · 45.7 KB
/
strlib.js
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
/**
* @fileoverview String-related helper functions
* @author <a href="mailto:[email protected]">Jeff Parsons</a> (@jeffpar)
* @copyright © 2012-2020 Jeff Parsons
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*
* PCjs 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 3
* of the License, or (at your option) any later version.
*
* PCjs 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 PCjs. If not,
* see <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every modified copy of this work
* and to display that copyright notice when the software starts running; see COPYRIGHT in
* <https://www.pcjs.org/modules/shared/lib/defines.js>.
*
* Some PCjs files also attempt to load external resource files, such as character-image files,
* ROM files, and disk image files. Those external resource files are not considered part of PCjs
* for purposes of the GNU General Public License, and the author does not claim any copyright
* as to their contents.
*/
"use strict";
class Str {
/**
* isValidInt(s, base)
*
* The built-in parseInt() function has the annoying feature of returning a partial value (ie,
* up to the point where it encounters an invalid character); eg, parseInt("foo", 16) returns 0xf.
*
* So it's best to use our own Str.parseInt() function, which will in turn use this function to
* validate the entire string.
*
* @param {string} s is the string representation of some number
* @param {number} [base] is the radix to use (default is 10); only 2, 8, 10 and 16 are supported
* @return {boolean} true if valid, false if invalid (or the specified base isn't supported)
*/
static isValidInt(s, base)
{
if (!base || base == 10) return s.match(/^-?[0-9]+$/) !== null;
if (base == 16) return s.match(/^-?[0-9a-f]+$/i) !== null;
if (base == 8) return s.match(/^-?[0-7]+$/) !== null;
if (base == 2) return s.match(/^-?[01]+$/) !== null;
return false;
}
/**
* parseInt(s, base)
*
* This is a wrapper around the built-in parseInt() function. Our wrapper recognizes certain prefixes
* ('$' or "0x" for hex, '#' or "0o" for octal) and suffixes ('.' for decimal, 'h' for hex, 'y' for
* binary), and then calls isValidInt() to ensure we don't convert strings that contain partial values;
* see isValidInt() for details.
*
* The use of multiple prefix/suffix combinations is undefined (although for the record, we process
* prefixes first). We do NOT support the "0b" prefix to indicate binary UNLESS one or more commas are
* also present (because "0b" is also a valid hex sequence), and we do NOT support a single leading zero
* to indicate octal (because such a number could also be decimal or hex). Any number of commas are
* allowed; we remove them all before calling the built-in parseInt().
*
* More recently, we've added support for "^D", "^O", and "^B" prefixes to accommodate the base overrides
* that the PDP-10's MACRO-10 assembly language supports (decimal, octal, and binary, respectively).
* If this support turns out to adversely affect other debuggers, then it will have to be "conditionalized".
* Similarly, we've added support for "K", "M", and "G" MACRO-10-style suffixes that add 3, 6, or 9 zeros
* to the value to be parsed, respectively.
*
* @param {string} s is the string representation of some number
* @param {number} [base] is the radix to use (default is 10); can be overridden by prefixes/suffixes
* @return {number|undefined} corresponding value, or undefined if invalid
*/
static parseInt(s, base)
{
let value;
if (s) {
if (!base) base = 10;
let ch, chPrefix, chSuffix;
let fCommas = (s.indexOf(',') > 0);
if (fCommas) s = s.replace(/,/g, '');
ch = chPrefix = s.charAt(0);
if (chPrefix == '#') {
base = 8;
chPrefix = '';
}
else if (chPrefix == '$') {
base = 16;
chPrefix = '';
}
if (ch != chPrefix) {
s = s.substr(1);
}
else {
ch = chPrefix = s.substr(0, 2);
if (chPrefix == '0b' && fCommas || chPrefix == '^B') {
base = 2;
chPrefix = '';
}
else if (chPrefix == '0o' || chPrefix == '^O') {
base = 8;
chPrefix = '';
}
else if (chPrefix == '^D') {
base = 10;
chPrefix = '';
}
else if (chPrefix == '0x') {
base = 16;
chPrefix = '';
}
if (ch != chPrefix) s = s.substr(2);
}
ch = chSuffix = s.slice(-1);
if (chSuffix == 'Y' || chSuffix == 'y') {
base = 2;
chSuffix = '';
}
else if (chSuffix == '.') {
base = 10;
chSuffix = '';
}
else if (chSuffix == 'H' || chSuffix == 'h') {
base = 16;
chSuffix = '';
}
else if (chSuffix == 'K') {
chSuffix = '000';
}
else if (chSuffix == 'M') {
chSuffix = '000000';
}
else if (chSuffix == 'G') {
chSuffix = '000000000';
}
if (ch != chSuffix) s = s.slice(0, -1) + chSuffix;
/*
* This adds support for the MACRO-10 binary shifting (Bn) suffix, which must be stripped from the
* number before parsing, and then applied to the value after parsing. If n is omitted, 35 is assumed,
* which is a net shift of zero. If n < 35, then a left shift of (35 - n) is required; if n > 35, then
* a right shift of -(35 - n) is required.
*/
let v, shift = 0;
if (base <= 10) {
let match = s.match(/(-?[0-9]+)B([0-9]*)/);
if (match) {
s = match[1];
shift = 35 - ((match[2] || 35) & 0xff);
}
}
if (Str.isValidInt(s, base) && !isNaN(v = parseInt(s, base))) {
/*
* With the need to support larger (eg, 36-bit) integers, truncating to 32 bits is no longer helpful.
*
* value = v|0;
*/
if (shift) {
/*
* Since binary shifting is a logical operation, and since shifting by division only works properly
* with positive numbers, we must convert a negative value to a positive value, by computing the two's
* complement.
*/
if (v < 0) v += Math.pow(2, 36);
if (shift > 0) {
v *= Math.pow(2, shift);
} else {
v = Math.trunc(v / Math.pow(2, -shift));
}
}
value = v;
}
}
return value;
}
/**
* toBase(n, radix, cch, sPrefix, nGrouping)
*
* Displays the given number as an unsigned integer using the specified radix and number of digits.
*
* @param {number|*} n
* @param {number} radix (ie, the base)
* @param {number} cch (the desired number of digits)
* @param {string} [sPrefix] (default is none)
* @param {number} [nGrouping]
* @return {string}
*/
static toBase(n, radix, cch, sPrefix = "", nGrouping = 0)
{
/*
* We can't rely entirely on isNaN(), because isNaN(null) returns false, and we can't rely
* entirely on typeof either, because typeof Nan returns "number". Sigh.
*
* Alternatively, we could mask and shift n regardless of whether it's null/undefined/NaN,
* since JavaScript coerces such operands to zero, but I think there's "value" in seeing those
* values displayed differently.
*/
let s = "";
if (isNaN(n) || typeof n != "number") {
n = null;
} else {
/*
* Callers that produced an input by dividing by a power of two rather than shifting (in order
* to access more than 32 bits) may produce a fractional result, which ordinarily we would simply
* ignore, but if the integer portion is zero and the sign is negative, we should probably treat
* this value as a sign-extension.
*/
if (n < 0 && n > -1) n = -1;
/*
* Negative values should be two's complemented according to the number of digits; for example,
* 12 octal digits implies an upper limit 8^12.
*/
if (n < 0) {
n += Math.pow(radix, cch);
}
if (n >= Math.pow(radix, cch)) {
cch = Math.ceil(Math.log(n) / Math.log(radix));
}
}
let g = nGrouping || -1;
while (cch-- > 0) {
if (!g) {
s = ',' + s;
g = nGrouping;
}
if (n == null) {
s = '?' + s;
} else {
let d = n % radix;
d += (d >= 0 && d <= 9? 0x30 : 0x41 - 10);
s = String.fromCharCode(d) + s;
n = Math.trunc(n / radix);
}
g--;
}
return sPrefix + s;
}
/**
* toBin(n, cch, nGrouping)
*
* Converts an integer to binary, with the specified number of digits (up to a maximum of 36).
*
* @param {number|*} n (supports integers up to 36 bits now)
* @param {number} [cch] is the desired number of binary digits (0 or undefined for default of either 8, 18, or 36)
* @param {number} [nGrouping]
* @return {string} the binary representation of n
*/
static toBin(n, cch, nGrouping)
{
if (!cch) {
// cch = Math.ceil(Math.log(Math.abs(n) + 1) / Math.LN2) || 1;
let v = Math.abs(n);
if (v <= 0b11111111) {
cch = 8;
} else if (v <= 0b111111111111111111) {
cch = 18;
} else {
cch = 36;
}
} else if (cch > 36) cch = 36;
return Str.toBase(n, 2, cch, "", nGrouping);
}
/**
* toBinBytes(n, cb, fPrefix)
*
* Converts an integer to binary, with the specified number of bytes (up to the default of 4).
*
* @param {number|null|undefined} n (interpreted as a 32-bit value)
* @param {number} [cb] is the desired number of binary bytes (4 is both the default and the maximum)
* @param {boolean} [fPrefix]
* @return {string} the binary representation of n
*/
static toBinBytes(n, cb, fPrefix)
{
let s = "";
if (!cb || cb > 4) cb = 4;
for (let i = 0; i < cb; i++) {
if (s) s = ',' + s;
s = Str.toBin(n & 0xff, 8) + s;
n >>= 8;
}
return (fPrefix? "0b" : "") + s;
}
/**
* toOct(n, cch, fPrefix)
*
* Converts an integer to octal, with the specified number of digits (default of 6; max of 12)
*
* You might be tempted to use the built-in n.toString(8) instead, but it doesn't zero-pad and it
* doesn't properly convert negative values. Moreover, if n is undefined, n.toString() will throw
* an exception, whereas this function will return '?' characters.
*
* @param {number|*} n (supports integers up to 36 bits now)
* @param {number} [cch] is the desired number of octal digits (0 or undefined for default of either 6, 8, or 12)
* @param {boolean} [fPrefix]
* @return {string} the octal representation of n
*/
static toOct(n, cch, fPrefix)
{
if (!cch) {
// cch = Math.ceil(Math.log(Math.abs(n) + 1) / Math.log(8)) || 1;
let v = Math.abs(n);
if (v <= 0o777777) {
cch = 6;
} else if (v <= 0o77777777) {
cch = 8;
} else {
cch = 12;
}
} else if (cch > 12) cch = 12;
return Str.toBase(n, 8, cch, fPrefix? "0o" : "");
}
/**
* toDec(n, cch)
*
* Converts an integer to decimal, with the specified number of digits (default of 5; max of 11)
*
* You might be tempted to use the built-in n.toString(10) instead, but it doesn't zero-pad and it
* doesn't properly convert negative values. Moreover, if n is undefined, n.toString() will throw
* an exception, whereas this function will return '?' characters.
*
* @param {number|*} n (supports integers up to 36 bits now)
* @param {number} [cch] is the desired number of decimal digits (0 or undefined for default of either 5 or 11)
* @return {string} the decimal representation of n
*/
static toDec(n, cch)
{
if (!cch) {
// cch = Math.ceil(Math.log(Math.abs(n) + 1) / Math.LN10) || 1;
let v = Math.abs(n);
if (v <= 99999) {
cch = 5;
} else {
cch = 11;
}
} else if (cch > 11) cch = 11;
return Str.toBase(n, 10, cch);
}
/**
* toHex(n, cch, fPrefix)
*
* Converts an integer to hex, with the specified number of digits (default of 4 or 8, max of 9).
*
* You might be tempted to use the built-in n.toString(16) instead, but it doesn't zero-pad and it
* doesn't properly convert negative values; for example, if n is -2147483647, then n.toString(16)
* will return "-7fffffff" instead of "80000001". Moreover, if n is undefined, n.toString() will
* throw an exception, whereas this function will return '?' characters.
*
* NOTE: The following work-around (adapted from code found on StackOverflow) would be another solution,
* taking care of negative values, zero-padding, and upper-casing, but not null/undefined/NaN values:
*
* s = (n < 0? n + 0x100000000 : n).toString(16);
* s = "00000000".substr(0, 8 - s.length) + s;
* s = s.substr(0, cch).toUpperCase();
*
* @param {number|*} n (supports integers up to 36 bits now)
* @param {number} [cch] is the desired number of hex digits (0 or undefined for default of either 4, 8, or 9)
* @param {boolean} [fPrefix]
* @return {string} the hex representation of n
*/
static toHex(n, cch, fPrefix)
{
if (!cch) {
// cch = Math.ceil(Math.log(Math.abs(n) + 1) / Math.log(16)) || 1;
let v = Math.abs(n);
if (v <= 0xffff) {
cch = 4;
} else if (v <= 0xffffffff) {
cch = 8;
} else {
cch = 9;
}
} else if (cch > 9) cch = 9;
return Str.toBase(n, 16, cch, fPrefix? "0x" : "");
}
/**
* toHexByte(b)
*
* Alias for Str.toHex(b, 2, true)
*
* @param {number|null|undefined} b is a byte value
* @return {string} the hex representation of b
*/
static toHexByte(b)
{
return Str.toHex(b, 2, true);
}
/**
* toHexWord(w)
*
* Alias for Str.toHex(w, 4, true)
*
* @param {number|null|undefined} w is a word (16-bit) value
* @return {string} the hex representation of w
*/
static toHexWord(w)
{
return Str.toHex(w, 4, true);
}
/**
* toHexLong(l)
*
* Alias for Str.toHex(l, 8, true)
*
* @param {number|null|undefined} l is a dword (32-bit) value
* @return {string} the hex representation of w
*/
static toHexLong(l)
{
return Str.toHex(l, 8, true);
}
/**
* getBaseName(sFileName, fStripExt)
*
* This is a poor-man's version of Node's path.basename(), which Node-only components should use instead.
*
* Note that if fStripExt is true, this strips ANY extension, whereas path.basename() strips the extension only
* if it matches the second parameter (eg, path.basename("/foo/bar/baz/asdf/quux.html", ".html") returns "quux").
*
* @param {string} sFileName
* @param {boolean} [fStripExt]
* @return {string}
*/
static getBaseName(sFileName, fStripExt)
{
let sBaseName = sFileName;
let i = sFileName.lastIndexOf('/');
if (i >= 0) sBaseName = sFileName.substr(i + 1);
/*
* This next bit is a kludge to clean up names that are part of a URL that includes unsightly query parameters.
*/
i = sBaseName.indexOf('&');
if (i > 0) sBaseName = sBaseName.substr(0, i);
if (fStripExt) {
i = sBaseName.lastIndexOf(".");
if (i > 0) {
sBaseName = sBaseName.substring(0, i);
}
}
return sBaseName;
}
/**
* getExtension(sFileName)
*
* This is a poor-man's version of Node's path.extname(), which Node-only components should use instead.
*
* Note that we EXCLUDE the period from the returned extension, whereas path.extname() includes it.
*
* @param {string} sFileName
* @return {string} the filename's extension (in lower-case and EXCLUDING the "."), or an empty string
*/
static getExtension(sFileName)
{
let sExtension = "";
let i = sFileName.lastIndexOf(".");
if (i >= 0) {
sExtension = sFileName.substr(i + 1).toLowerCase();
}
return sExtension;
}
/**
* endsWith(s, sSuffix)
*
* @param {string} s
* @param {string} sSuffix
* @return {boolean} true if s ends with sSuffix, false if not
*/
static endsWith(s, sSuffix)
{
return s.indexOf(sSuffix, s.length - sSuffix.length) !== -1;
}
/**
* escapeHTML(sHTML)
*
* @param {string} sHTML
* @return {string} with special characters "escaped" as HTML entities, similar to PHP's htmlspecialchars()
*/
static escapeHTML(sHTML)
{
/*
* Most recently, '$' was added to the list to help avoid problems when callers use the resulting string
* as a replacement string for JavaScript's string replace() function, which treats '$' specially. Technically,
* that's on the callers of replace(), not us, but this doesn't seem harmful, and it's definitely helpful.
*/
return sHTML.replace(/[&<>"'$]/g, function(m)
{
return Str.HTMLEscapeMap[m];
});
}
/**
* replace(sSearch, sReplace, s)
*
* The JavaScript replace() function ALWAYS interprets "$" specially in replacement strings, even when
* the search string is NOT a RegExp; specifically:
*
* $$ Inserts a "$"
* $& Inserts the matched substring
* $` Inserts the portion of the string that precedes the matched substring
* $' Inserts the portion of the string that follows the matched substring
* $n Where n is a positive integer less than 100, inserts the nth parenthesized sub-match string,
* provided the first argument was a RegExp object
*
* So, if a replacement string containing dollar signs passes through a series of replace() calls, untold
* problems could result. Hence, this function, which simply uses the replacement string as-is.
*
* Similar to the JavaScript replace() method (when sSearch is a string), this replaces only ONE occurrence
* (ie, the FIRST occurrence); it might be nice to add options to replace the LAST occurrence and/or ALL
* occurrences, but we'll revisit that later.
*
* @param {string} sSearch
* @param {string} sReplace
* @param {string} s
* @return {string}
*/
static replace(sSearch, sReplace, s)
{
let i = s.indexOf(sSearch);
if (i >= 0) {
s = s.substr(0, i) + sReplace + s.substr(i + sSearch.length);
}
return s;
}
/**
* replaceAll(sSearch, sReplace, s)
*
* @param {string} sSearch
* @param {string} sReplace
* @param {string} s
* @return {string}
*/
static replaceAll(sSearch, sReplace, s)
{
let a = {};
a[sSearch] = sReplace;
return Str.replaceArray(a, s);
}
/**
* replaceArray(a, s)
*
* @param {Object} a
* @param {string} s
* @return {string}
*/
static replaceArray(a, s)
{
let sMatch = "";
for (let k in a) {
/*
* As noted in:
*
* http://www.regexguru.com/2008/04/escape-characters-only-when-necessary/
*
* inside character classes, only backslash, caret, hyphen and the closing bracket need to be
* escaped. And in fact, if you ensure that the closing bracket is first, the caret is not first,
* and the hyphen is last, you can avoid escaping those as well.
*/
k = k.replace(/([\\[\]*{}().+?|$])/g, "\\$1");
sMatch += (sMatch? '|' : '') + k;
}
return s.replace(new RegExp('(' + sMatch + ')', "g"), function(m)
{
return a[m];
});
}
/**
* pad(s, cch, fPadLeft)
*
* NOTE: the maximum amount of padding currently supported is 40 spaces.
*
* @param {string} s is a string
* @param {number} cch is desired length
* @param {boolean} [fPadLeft] (default is padding on the right)
* @return {string} the original string (s) with spaces padding it to the specified length
*/
static pad(s, cch, fPadLeft)
{
let sPadding = " ";
return fPadLeft? (sPadding + s).slice(-cch) : (s + sPadding).slice(0, cch);
}
/**
* parseDate(date)
* parseDate(date, time)
* parseDate(year, month, day, hour, minute, second)
*
* Produces a UTC date when ONLY a date (no time) is provided; otherwise, it combines the date and
* and time, producing a date that is either UTC or local, depending on the presence (or lack) of time
* zone information. Finally, if numeric inputs are provided, then Date.UTC() is called to generate
* a UTC time.
*
* In general, you should use this instead of new Date(s), because the Date constructor implicitly calls
* Date.parse(s), which behaves inconsistently. For example, ISO date-only strings (e.g. "1970-01-01")
* generate a UTC time, but non-ISO date-only strings (eg, "10/1/1945" or "October 1, 1945") generate a
* local time.
*
* @param {...} args
* @return {Date} (UTC unless a time string with a non-GMT timezone is explicitly provided)
*/
static parseDate(...args)
{
let date;
if (args[0] === undefined) {
date = new Date(Date.now());
}
else if (typeof args[0] === "string") {
date = new Date(args[0] + ' ' + (args[1] || "00:00:00 GMT"));
}
else if (args[1] === undefined) {
date = new Date(args[0]);
} else {
date = new Date(Date.UTC(...args));
}
return date;
}
/**
* isValidDate(date)
*
* @param {Date} date
* @return {boolean}
*/
static isValidDate(date)
{
return !isNaN(date.getTime());
}
/**
* sprintf(format, ...args)
*
* Copied from the CCjs project (https://github.com/jeffpar/ccjs/blob/master/lib/stdio.js) and extended.
* Far from complete, let alone sprintf-compatible, but it's adequate for the handful of sprintf-style format
* specifiers that I use.
*
* TODO: The %c and %s specifiers support a negative width for left-justified output, but the numeric specifiers
* (eg, %d and %x) do not; they support only positive widths and right-justified output. That's one of the more
* glaring omissions at the moment.
*
* @param {string} format
* @param {...} args
* @return {string}
*/
static sprintf(format, ...args)
{
/*
* This isn't just a nice optimization; it's also important if the caller is simply trying
* to printf() a string that may also contain '%' and doesn't want or expect any formatting.
*/
if (!args || !args.length) {
return format;
}
let buffer = "";
let aParts = format.split(/%([-+ 0#]*)([0-9]*|\*)(\.[0-9]+|)([hlL]?)([A-Za-z%])/);
let iArg = 0, iPart;
for (iPart = 0; iPart < aParts.length - 6; iPart += 6) {
buffer += aParts[iPart];
let arg, type = aParts[iPart+5];
/*
* Check for unrecognized types immediately, so we don't inadvertently pop any arguments;
* the first 12 ("ACDFHIMNSTWY") are for our non-standard Date extensions (see below).
*
* For reference purposes, the standard ANSI C set of format types is: "dioxXucsfeEgGpn%".
*/
let iType = "ACDFHIMNSTWYbdfjcsoXx%".indexOf(type);
if (iType < 0) {
buffer += '%' + aParts[iPart+1] + aParts[iPart+2] + aParts[iPart+3] + aParts[iPart+4] + type;
continue;
}
if (iArg < args.length) {
arg = args[iArg];
if (type != '%') iArg++;
} else {
arg = args[args.length-1];
}
let flags = aParts[iPart+1];
let hash = flags.indexOf('#') >= 0;
let zeroPad = flags.indexOf('0') >= 0;
let width = aParts[iPart+2];
if (width == '*') {
width = arg;
if (iArg < args.length) {
arg = args[iArg++];
} else {
arg = args[args.length-1];
}
} else {
width = +width || 0;
}
let precision = aParts[iPart+3];
precision = precision? +precision.substr(1) : -1;
// let length = aParts[iPart+4]; // eg, 'h', 'l' or 'L' (all currently ignored)
let ach = null, s, radix = 0, prefix = "";
/*
* The following non-standard sprintf() format codes provide handy alternatives to the
* PHP date() format codes that we used to use with the old datelib.formatDate() function:
*
* a: lowercase ante meridiem and post meridiem (am or pm) %A
* d: day of the month, 2 digits with leading zeros (01, 02, ..., 31) %02D
* D: 3-letter day of the week ("Sun", "Mon", ..., "Sat") %.3W
* F: month ("January", "February", ..., "December") %F
* g: hour in 12-hour format, without leading zeros (1, 2, ..., 12) %I
* h: hour in 24-hour format, without leading zeros (0, 1, ..., 23) %H
* H: hour in 24-hour format, with leading zeros (00, 01, ..., 23) %02H
* i: minutes, with leading zeros (00, 01, ..., 59) %02N
* j: day of the month, without leading zeros (1, 2, ..., 31) %D
* l: day of the week ("Sunday", "Monday", ..., "Saturday") %W
* m: month, with leading zeros (01, 02, ..., 12) %02M
* M: 3-letter month ("Jan", "Feb", ..., "Dec") %.3F
* n: month, without leading zeros (1, 2, ..., 12) %M
* s: seconds, with leading zeros (00, 01, ..., 59) %02S
* y: 2-digit year (eg, 14) %0.2Y
* Y: 4-digit year (eg, 2014) %Y
*
* We also support a few custom format codes:
*
* %C: calendar output (equivalent to: %W, %F %D, %Y)
* %T: timestamp output (equivalent to: %Y-%02M-%02D %02H:%02N:%02S)
*
* Use the optional '#' flag with any of the above '%' format codes to produce UTC results
* (eg, '%#I' instead of '%I').
*
* The %A, %F, and %W types act as strings (which support the '-' left justification flag, as well as
* the width and precision options), and the rest act as integers (which support the '0' padding flag
* and the width option). Also, while %Y does act as an integer, it also supports truncation using the
* precision option (normally, integers do not); this enables a variable number of digits for the year.
*
* So old code like this:
*
* printf("%s\n", formatDate("l, F j, Y", date));
*
* can now be written like this:
*
* printf("%W, %F %D, %Y\n", date, date, date, date);
*
* or even more succinctly, as:
*
* printf("%C\n", date);
*
* In fact, even the previous example can be written more succinctly as:
*
* printf("%W, %F %D, %Y\n", date);
*
* because unlike the C runtime, we reuse the final parameter once the format string has exhausted all parameters.
*/
let ch, date = /** @type {Date} */ (iType < 12 && typeof arg != "object"? Str.parseDate(arg) : arg), dateUndefined;
switch(type) {
case 'C':
ch = hash? '#' : '';
buffer += (Str.isValidDate(date)? Str.sprintf(Str.sprintf("%%%sW, %%%sF %%%sD, %%%sY", ch), date) : dateUndefined);
continue;
case 'D':
arg = hash? date.getUTCDate() : date.getDate();
type = 'd';
break;
case 'A':
case 'H':
case 'I':
arg = hash? date.getUTCHours() : date.getHours();
if (type == 'A') {
arg = (arg < 12 ? "am" : "pm");
type = 's';
}
else {
if (type == 'I') {
arg = (!arg? 12 : (arg > 12 ? arg - 12 : arg));
}
type = 'd';
}
break;
case 'F':
case 'M':
arg = hash? date.getUTCMonth() : date.getMonth();
if (type == 'F') {
arg = Str.NamesOfMonths[arg];
type = 's';
} else {
arg++;
type = 'd';
}
break;
case 'N':
arg = hash? date.getUTCMinutes() : date.getMinutes();
type = 'd';
break;
case 'S':
arg = hash? date.getUTCSeconds() : date.getSeconds();
type = 'd'
break;
case 'T':
ch = hash? '#' : '';
buffer += (Str.isValidDate(date)? Str.sprintf(Str.sprintf("%%%sY-%%%s02M-%%%s02D %%%s02H:%%%s02N:%%%s02S", ch), date) : dateUndefined);
continue;
case 'W':
arg = Str.NamesOfDays[hash? date.getUTCDay() : date.getDay()];
type = 's';
break;
case 'Y':
arg = hash? date.getUTCFullYear() : date.getFullYear();
if (precision > 0) {
arg = arg % (Math.pow(10, precision));
precision = -1;
}
type = 'd';
break;
}
switch(type) {
case 'b':
/*
* This is a non-standard format specifier that seems handy.
*/
buffer += (arg? "true" : "false");
break;
case 'd':
/*
* I could use "arg |= 0", but there may be some value to supporting integers > 32 bits,
* so I use Math.trunc() instead. Bit-wise operators also mask a lot of evils, by converting
* complete nonsense into zero, so while I'm ordinarily a fan, that's not desirable here.
*
* Other (hidden) advantages of Math.trunc(): it automatically converts strings, it honors
* numeric prefixes (the traditional "0x" for hex and the newer "0o" for octal), and it returns
* NaN if the ENTIRE string cannot be converted.
*
* parseInt(), which would seem to be the more logical choice here, doesn't understand "0o",
* doesn't return NaN if non-digits are embedded in the string, and doesn't behave consistently
* across all browsers when parsing older octal values with a leading "0"; Math.trunc() doesn't
* recognize those octal values either, but I'm OK with that, as long as it CONSISTENTLY doesn't
* recognize them.
*
* That last problem is why some recommend you ALWAYS pass a radix to parseInt(), but that
* forces you to parse the string first and determine the proper radix; otherwise, you end up
* with NEW inconsistencies. For example, if radix is 10 and the string is "0x10", the result
* is zero, since parseInt() happily stops parsing when it reaches the first non-radix 10 digit.
*/
arg = Math.trunc(arg);
/*
* Before falling into the decimal floating-point code, we take this opportunity to convert
* the precision value, if any, to the minimum number of digits to print. Which basically means
* setting zeroPad to true and width to precision, and then unsetting precision.
*
* TODO: This isn't quite accurate. For example, printf("%6.3d", 3) should print " 003", not
* "000003". But once again, this isn't a common enough case to worry about.
*/
if (precision >= 0) {
zeroPad = true;
if (width < precision) width = precision;
precision = -1;
}
/* falls through */
case 'f':
s = arg + "";
if (precision >= 0) {
s = arg.toFixed(precision);
}
if (s.length < width) {
if (zeroPad) {
if (arg < 0) {
width--;
s = s.substr(1);
}
s = ("0000000000" + s).slice(-width);
if (arg < 0) s = '-' + s;
} else {
s = (" " + s).slice(-width);
}
}
buffer += s;
break;
case 'j':
/*
* 'j' is one of our non-standard extensions to the sprintf() interface; it signals that
* the caller is providing an Object that should be rendered as JSON. If a width is included
* (eg, "%2j"), it's used as an indentation value; otherwise, no whitespace is added.
*/
buffer += JSON.stringify(arg, null, width || undefined);
break;
case 'c':
arg = typeof arg == "string"? arg[0] : String.fromCharCode(arg);
/* falls through */
case 's':
/*
* 's' includes some non-standard benefits, such as coercing non-strings to strings first;
* we know undefined and null values don't have a toString() method, but hopefully everything
* else does.
*/
if (arg != undefined) {
if (typeof arg != "string") {
arg = arg.toString();
}
if (precision >= 0) {
arg = arg.substr(0, precision);
}
while (arg.length < width) {
if (flags.indexOf('-') >= 0) {
arg += ' ';
} else {
arg = ' ' + arg;
}
}
}
buffer += arg;
break;
case 'o':
radix = 8;
if (hash) prefix = "0";
/* falls through */
case 'X':
ach = Str.HexUpperCase;
// if (hash) prefix = "0X"; // I don't like that %#X uppercases both the prefix and the value
/* falls through */
case 'x':
s = "";
if (!radix) radix = 16;
if (!prefix && hash) prefix = "0x";
if (!ach) ach = Str.HexLowerCase;
/*
* For all the same reasons articulated above (for type 'd'), we pass the arg through Math.trunc(),
* and we honor precision, if any, as the minimum number of digits to print.
*/
arg = Math.trunc(arg);
if (precision >= 0) {
zeroPad = true;
if (width < precision) width = precision;
precision = -1;
}
if (zeroPad && !width) {
/*
* Here we replicate a bit of logic from toHex(), which selects a width based on the value, and
* is triggered by the format specification "%0x", where zero-padding is requested without a width.
*/
let v = Math.abs(arg);
if (v <= 0xff) {
width = 2;
} else if (v <= 0xffff) {
width = 4;
} else if (v <= 0xffffffff) {
width = 8;
} else {
width = 9;
}
width += prefix.length;
}
width -= prefix.length;
do {
let d = arg & (radix - 1);
arg >>>= (radix == 16? 4 : 3);
if (zeroPad || !s || d || arg) {
s = ach[d] + s;
} else {
if (prefix) {
s = prefix + s;
prefix = "";