-
-
Notifications
You must be signed in to change notification settings - Fork 938
Expand file tree
/
Copy pathStrptimeParser.java
More file actions
862 lines (762 loc) · 32.7 KB
/
StrptimeParser.java
File metadata and controls
862 lines (762 loc) · 32.7 KB
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
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v20.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.util;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.math.BigInteger;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jruby.lexer.StrptimeLexer;
/**
* This is Java implementation of ext/date/date_strptime.c in Ruby 2.3.1.
* see https://github.com/ruby/ruby/blob/394fa89c67722d35bdda89f10c7de5c304a5efb1/ext/date/date_strptime.c
*/
public class StrptimeParser {
// day_names
private static final String[] DAY_NAMES = new String[] {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
// month_names
private static final String[] MONTH_NAMES = new String[] {
"January", "February", "March", "April", "May", "June", "July", "August", "September",
"October", "November", "December", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
// merid_names
private static final String[] MERID_NAMES = new String[] {
"am", "pm", "a.m.", "p.m."
};
/**
* Ported Date::Format::Bag from lib/ruby/stdlib/date/format.rb in JRuby 9.1.5.0.
* see https://github.com/jruby/jruby/blob/036ce39f0476d4bd718e23e64caff36bb50b8dbc/lib/ruby/stdlib/date/format.rb
*/
public static class FormatBag {
private int mDay = Integer.MIN_VALUE;
private int wDay = Integer.MIN_VALUE;
private int cWDay = Integer.MIN_VALUE;
private int yDay = Integer.MIN_VALUE;
private int cWeek = Integer.MIN_VALUE;
private long cWYear = Long.MIN_VALUE;
private int min = Integer.MIN_VALUE;
private int mon = Integer.MIN_VALUE;
private int hour = Integer.MIN_VALUE;
private long year = Long.MIN_VALUE;
private int sec = Integer.MIN_VALUE;
private int wNum0 = Integer.MIN_VALUE;
private int wNum1 = Integer.MIN_VALUE;
private String zone = null;
private Number secFraction = null; // Rational
private int secFractionSize = Integer.MIN_VALUE;
private Number seconds = null; // Bignum or Rational
private int secondsSize = Integer.MIN_VALUE;
private int merid = Integer.MIN_VALUE;
private long cent = Long.MIN_VALUE;
//private boolean fail = false;
private String leftover = null;
public int getMDay() {
return mDay;
}
public int getWDay() {
return wDay;
}
public int getCWDay() {
return cWDay;
}
public int getYDay() {
return yDay;
}
public int getCWeek() {
return cWeek;
}
public long getCWYear() {
return cWYear;
}
public int getMin() {
return min;
}
public int getMon() {
return mon;
}
public int getHour() {
return hour;
}
public long getYear() {
return year;
}
public int getSec() {
return sec;
}
public int getWNum0() {
return wNum0;
}
public int getWNum1() {
return wNum1;
}
public String getZone() {
return zone;
}
public Number getSecFraction() {
return secFraction;
}
public int getSecFractionSize() {
return secFractionSize;
}
public Number getSeconds() {
return seconds;
}
public int getSecondsSize() {
return secondsSize;
}
public int getMerid() {
return merid;
}
public long getCent() {
return cent;
}
public String getLeftover() {
return leftover;
}
public static boolean has(int v) {
return v != Integer.MIN_VALUE;
}
public static boolean has(long v) {
return v != Long.MIN_VALUE;
}
public static boolean has(Number v) {
return v != null;
}
}
/**
* Ported from RubyDateFormatter#addToPattern in JRuby 9.1.5.0.
* see https://github.com/jruby/jruby/blob/036ce39f0476d4bd718e23e64caff36bb50b8dbc/core/src/main/java/org/jruby/util/RubyDateFormatter.java
*/
private void addToPattern(final List<StrptimeToken> compiledPattern, final String str) {
for (int i = 0; i < str.length(); i++) {
final char c = str.charAt(i);
if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')) {
compiledPattern.add(StrptimeToken.format(c));
} else {
compiledPattern.add(StrptimeToken.str(Character.toString(c)));
}
}
}
/**
* Ported from RubyDateFormatter#compilePattern in JRuby 9.1.5.0.
* see https://github.com/jruby/jruby/blob/036ce39f0476d4bd718e23e64caff36bb50b8dbc/core/src/main/java/org/jruby/util/RubyDateFormatter.java
*/
public List<StrptimeToken> compilePattern(final String pattern) {
final List<StrptimeToken> compiledPattern = new LinkedList<>();
final Reader reader = new StringReader(pattern); // TODO Use try-with-resource statement
StrptimeLexer lexer = new StrptimeLexer(reader);
StrptimeToken token;
try {
while ((token = lexer.yylex()) != null) {
if (token.getFormat() != StrptimeFormat.FORMAT_SPECIAL) {
compiledPattern.add(token);
} else {
char c = (Character) token.getData();
switch (c) {
case 'c':
addToPattern(compiledPattern, "a b e H:M:S Y");
break;
case 'D':
case 'x':
addToPattern(compiledPattern, "m/d/y");
break;
case 'F':
addToPattern(compiledPattern, "Y-m-d");
break;
case 'n':
compiledPattern.add(StrptimeToken.str("\n"));
break;
case 'R':
addToPattern(compiledPattern, "H:M");
break;
case 'r':
addToPattern(compiledPattern, "I:M:S p");
break;
case 'T':
case 'X':
addToPattern(compiledPattern, "H:M:S");
break;
case 't':
compiledPattern.add(StrptimeToken.str("\t"));
break;
case 'v':
addToPattern(compiledPattern, "e-b-Y");
break;
case 'Z':
// +HH:MM in 'date', never zone name
compiledPattern.add(StrptimeToken.zoneOffsetColons(1));
break;
case '+':
addToPattern(compiledPattern, "a b e H:M:S ");
// %Z: +HH:MM in 'date', never zone name
compiledPattern.add(StrptimeToken.zoneOffsetColons(1));
addToPattern(compiledPattern, " Y");
break;
default:
throw new Error("Unknown special char: " + c);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return compiledPattern;
}
public FormatBag parse(final List<StrptimeToken> compiledPattern, final String text) {
final FormatBag bag = new StringParser(text).parse(compiledPattern);
if (bag == null) {
return null;
}
if (FormatBag.has(bag.cent)) {
if (FormatBag.has(bag.cWYear)) {
bag.cWYear += bag.cent * 100;
}
if (FormatBag.has(bag.year)) {
bag.year += bag.cent * 100;
}
// delete bag._cent
bag.cent = Long.MIN_VALUE;
}
if (FormatBag.has(bag.merid)) {
if (FormatBag.has(bag.hour)) {
bag.hour %= 12;
bag.hour += bag.merid;
}
// delete bag._merid
bag.merid = Integer.MIN_VALUE;
}
return bag;
}
private static class StringParser {
private static final Pattern ZONE_PARSE_REGEX = Pattern.compile("\\A(" +
"(?:gmt|utc?)?[-+]\\d+(?:[,.:]\\d+(?::\\d+)?)?" +
"|(?-i:[[\\p{Alpha}].\\s]+)(?:standard|daylight)\\s+time\\b" +
"|(?-i:[[\\p{Alpha}]]+)(?:\\s+dst)?\\b" +
")", Pattern.CASE_INSENSITIVE);
private final String text;
private final FormatBag bag;
private int pos;
private boolean fail;
private StringParser(String text) {
this.text = text;
this.bag = new FormatBag();
this.pos = 0;
this.fail = false;
}
private FormatBag parse(final List<StrptimeToken> compiledPattern) {
for (int tokenIndex = 0; tokenIndex < compiledPattern.size(); tokenIndex++) {
final StrptimeToken token = compiledPattern.get(tokenIndex);
switch (token.getFormat()) {
case FORMAT_STRING: {
final String str = token.getData().toString();
for (int i = 0; i < str.length(); i++) {
final char c = str.charAt(i);
if (isSpace(c)) {
while (!isEndOfText(text, pos) && isSpace(text.charAt(pos))) {
pos++;
}
} else {
if (isEndOfText(text, pos) || c != text.charAt(pos)) {
fail = true;
}
pos++;
}
}
break;
}
case FORMAT_WEEK_LONG: // %A - The full weekday name (``Sunday'')
case FORMAT_WEEK_SHORT: { // %a - The abbreviated name (``Sun'')
final int dayIndex = findIndexInPatterns(DAY_NAMES);
if (dayIndex >= 0) {
bag.wDay = dayIndex % 7;
pos += DAY_NAMES[dayIndex].length();
} else {
fail = true;
}
break;
}
case FORMAT_MONTH_LONG: // %B - The full month name (``January'')
case FORMAT_MONTH_SHORT: { // %b, %h - The abbreviated month name (``Jan'')
final int monIndex = findIndexInPatterns(MONTH_NAMES);
if (monIndex >= 0) {
bag.mon = monIndex % 12 + 1;
pos += MONTH_NAMES[monIndex].length();
} else {
fail = true;
}
break;
}
case FORMAT_CENTURY: { // %C - year / 100 (round down. 20 in 2009)
if (isNumberPattern(compiledPattern, tokenIndex)) {
bag.cent = readDigits(2);
} else {
bag.cent = readDigitsMaxLong();
}
break;
}
case FORMAT_DAY: // %d, %Od - Day of the month, zero-padded (01..31)
case FORMAT_DAY_S: { // %e, %Oe - Day of the month, blank-padded ( 1..31)
final long day;
if (isBlank(text, pos)) {
pos += 1; // blank
day = readDigits(1);
} else {
day = readDigits(2);
}
if (!validRange(day, 1, 31)) {
fail = true;
}
bag.mDay = (int)day;
break;
}
case FORMAT_WEEKYEAR: { // %G - The week-based year
if (isNumberPattern(compiledPattern, tokenIndex)) {
bag.cWYear = readDigits(4);
} else {
bag.cWYear = readDigitsMaxLong();
}
break;
}
case FORMAT_WEEKYEAR_SHORT: { // %g - The last 2 digits of the week-based year (00..99)
final long v = readDigits(2);
if (!validRange(v, 0, 99)) {
fail = true;
}
bag.cWYear = v;
if (!bag.has(bag.cent)) {
bag.cent = v >= 69 ? 19 : 20;
}
break;
}
case FORMAT_HOUR: // %H, %OH - Hour of the day, 24-hour clock, zero-padded (00..23)
case FORMAT_HOUR_BLANK: { // %k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
final long hour;
if (isBlank(text, pos)) {
pos += 1; // blank
hour = readDigits(1);
} else {
hour = readDigits(2);
}
if (!validRange(hour, 0, 24)) {
fail = true;
}
bag.hour = (int)hour;
break;
}
case FORMAT_HOUR_M: // %I, %OI - Hour of the day, 12-hour clock, zero-padded (01..12)
case FORMAT_HOUR_S: { // %l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
final long hour;
if (isBlank(text, pos)) {
pos += 1; // blank
hour = readDigits(1);
} else {
hour = readDigits(2);
}
if (!validRange(hour, 1, 12)) {
fail = true;
}
bag.hour = (int)hour;
break;
}
case FORMAT_DAY_YEAR: { // %j - Day of the year (001..366)
final long day = readDigits(3);
if (!validRange(day, 1, 366)) {
fail = true;
}
bag.yDay = (int)day;
break;
}
case FORMAT_MILLISEC: // %L - Millisecond of the second (000..999)
case FORMAT_NANOSEC: { // %N - Fractional seconds digits, default is 9 digits (nanosecond)
boolean negative = false;
if (isSign(text, pos)) {
negative = text.charAt(pos) == '-';
pos++;
}
final Number v;
final int initPos = pos;
if (isNumberPattern(compiledPattern, tokenIndex)) {
if (token.getFormat() == StrptimeFormat.FORMAT_MILLISEC) {
v = BigInteger.valueOf(readDigits(3));
} else {
v = BigInteger.valueOf(readDigits(9));
}
} else {
v = readDigitsMax();
}
bag.secFraction = !negative ? v : negateInteger(v);
bag.secFractionSize = pos - initPos;
break;
}
case FORMAT_MINUTES: { // %M, %OM - Minute of the hour (00..59)
final long min = readDigits(2);
if (!validRange(min, 0, 59)) {
fail = true;
}
bag.min = (int)min;
break;
}
case FORMAT_MONTH: { // %m, %Om - Month of the year, zero-padded (01..12)
final long mon = readDigits(2);
if (!validRange(mon, 1, 12)) {
fail = true;
}
bag.mon = (int)mon;
break;
}
case FORMAT_MERIDIAN: // %P - Meridian indicator, lowercase (``am'' or ``pm'')
case FORMAT_MERIDIAN_LOWER_CASE: { // %p - Meridian indicator, uppercase (``AM'' or ``PM'')
final int meridIndex = findIndexInPatterns(MERID_NAMES);
if (meridIndex >= 0) {
bag.merid = meridIndex % 2 == 0 ? 0 : 12;
pos += MERID_NAMES[meridIndex].length();
} else {
fail = true;
}
break;
}
case FORMAT_MICROSEC_EPOCH: { // %Q - Number of microseconds since 1970-01-01 00:00:00 UTC.
boolean negative = false;
if (isMinus(text, pos)) {
negative = true;
pos++;
}
final Number sec = readDigitsMax();
bag.seconds = !negative ? sec : negateInteger(sec);
bag.secondsSize = 3;
break;
}
case FORMAT_SECONDS: { // %S - Second of the minute (00..59)
final long sec = readDigits(2);
if (!validRange(sec, 0, 60)) {
fail = true;
}
bag.sec = (int)sec;
break;
}
case FORMAT_EPOCH: { // %s - Number of seconds since 1970-01-01 00:00:00 UTC.
boolean negative = false;
if (isMinus(text, pos)) {
negative = true;
pos++;
}
final Number sec = readDigitsMax();
bag.seconds = !negative ? sec : negateInteger(sec);
break;
}
case FORMAT_WEEK_YEAR_S: // %U, %OU - Week number of the year. The week starts with Sunday. (00..53)
case FORMAT_WEEK_YEAR_M: { // %W, %OW - Week number of the year. The week starts with Monday. (00..53)
final long week = readDigits(2);
if (!validRange(week, 0, 53)) {
fail = true;
}
if (token.getFormat() == StrptimeFormat.FORMAT_WEEK_YEAR_S) {
bag.wNum0 = (int)week;
} else {
bag.wNum1 = (int)week;
}
break;
}
case FORMAT_DAY_WEEK2: { // %u, %Ou - Day of the week (Monday is 1, 1..7)
final long day = readDigits(1);
if (!validRange(day, 1, 7)) {
fail = true;
}
bag.cWDay = (int)day;
break;
}
case FORMAT_WEEK_WEEKYEAR: { // %V, %OV - Week number of the week-based year (01..53)
final long week = readDigits(2);
if (!validRange(week, 1, 53)) {
fail = true;
}
bag.cWeek = (int)week;
break;
}
case FORMAT_DAY_WEEK: { // %w - Day of the week (Sunday is 0, 0..6)
final long day = readDigits(1);
if (!validRange(day, 0, 6)) {
fail = true;
}
bag.wDay = (int)day;
break;
}
case FORMAT_YEAR_LONG: {
// %Y, %EY - Year with century (can be negative, 4 digits at least)
// -0001, 0000, 1995, 2009, 14292, etc.
boolean negative = false;
if (isSign(text, pos)) {
negative = text.charAt(pos) == '-';
pos++;
}
final long year;
if (isNumberPattern(compiledPattern, tokenIndex)) {
year = readDigits(4);
} else {
year = readDigitsMaxLong();
}
bag.year = !negative ? year : -1 * year;
break;
}
case FORMAT_YEAR_SHORT: { // %y, %Ey, %Oy - year % 100 (00..99)
final long year = readDigits(2);
if (!validRange(year, 0, 99)) {
fail = true;
}
bag.year = year;
if (!bag.has(bag.cent)) {
bag.cent = year >= 69 ? 19 : 20;
}
break;
}
case FORMAT_ZONE_ID: // %Z - Time zone abbreviation name
case FORMAT_COLON_ZONE_OFF: {
// %z - Time zone as hour and minute offset from UTC (e.g. +0900)
// %:z - hour and minute offset from UTC with a colon (e.g. +09:00)
// %::z - hour, minute and second offset from UTC (e.g. +09:00:00)
// %:::z - hour, minute and second offset from UTC
// (e.g. +09, +09:30, +09:30:30)
if (isEndOfText(text, pos)) {
fail = true;
break;
}
final Matcher m = ZONE_PARSE_REGEX.matcher(text.substring(pos));
if (m.find()) {
// zone
String zone = text.substring(pos, pos + m.end());
bag.zone = zone;
pos += zone.length();
} else {
fail = true;
}
break;
}
case FORMAT_SPECIAL:
{
throw new Error("FORMAT_SPECIAL is a special token only for the lexer.");
}
}
}
if (fail) {
return null;
}
if (text.length() > pos) {
bag.leftover = text.substring(pos, text.length());
}
return bag;
}
/**
* Ports read_digits from ext/date/date_strptime.c in MRI 2.3.1 under BSDL.
* see https://github.com/ruby/ruby/blob/394fa89c67722d35bdda89f10c7de5c304a5efb1/ext/date/date_strftime.c
*/
private long readDigits(final int len) {
char c;
long v = 0;
final int initPos = pos;
for (int i = 0; i < len; i++) {
if (isEndOfText(text, pos)) {
break;
}
c = text.charAt(pos);
if (!isDigit(c)) {
break;
} else {
v = v * 10 + toInt(c);
}
pos += 1;
}
if (pos == initPos) {
fail = true;
}
return v;
}
/**
* Ports READ_DIGITS_MAX from ext/date/date_strptime.c in MRI 2.3.1 under BSDL.
* see https://github.com/ruby/ruby/blob/394fa89c67722d35bdda89f10c7de5c304a5efb1/ext/date/date_strftime.c
*
* @return integer value (Long or BigInteger)
*/
private Number readDigitsMax() {
char c;
long v = 0; BigInteger vBig = null;
final int initPos = pos;
while (true) {
if (isEndOfText(text, pos)) break;
c = text.charAt(pos);
if (!isDigit(c)) break;
if (vBig == null) {
try {
// Using 10L to avoid binary incompat with Java 8 (jruby/jruby#5451)
long tmp = Math.multiplyExact(v, 10L);
tmp = Math.addExact(tmp, toInt(c));
v = tmp;
}
catch (ArithmeticException overflow) {
vBig = BigInteger.valueOf(v); continue;
}
}
else {
vBig = vBig.multiply(BigInteger.TEN);
vBig = vBig.add(BigInteger.valueOf(toInt(c)));
}
pos += 1;
}
if (pos == initPos) {
fail = true;
}
return vBig == null ? v : vBig;
}
private long readDigitsMaxLong() {
char c;
long v = 0L;
final int initPos = pos;
while (true) {
if (isEndOfText(text, pos)) break;
c = text.charAt(pos);
if (!isDigit(c)) break;
v = v * 10 + toInt(c);
pos += 1;
}
if (pos == initPos) {
fail = true;
}
return v;
}
/**
* Returns -1 if text doesn't match with patterns.
*/
private int findIndexInPatterns(final String[] patterns) {
if (isEndOfText(text, pos)) {
return -1;
}
for (int i = 0; i < patterns.length; i++) {
final String pattern = patterns[i];
final int len = pattern.length();
if (!isEndOfText(text, pos + len - 1)
&& pattern.equalsIgnoreCase(text.substring(pos, pos + len))) { // strncasecmp
return i;
}
}
return -1; // text doesn't match at any patterns.
}
/**
* Ports num_pattern_p from ext/date/date_strptime.c in MRI 2.3.1 under BSDL.
* see https://github.com/ruby/ruby/blob/394fa89c67722d35bdda89f10c7de5c304a5efb1/ext/date/date_strftime.c
*/
private static boolean isNumberPattern(final List<StrptimeToken> compiledPattern, final int i) {
if (compiledPattern.size() <= i + 1) {
return false;
} else {
final StrptimeToken nextToken = compiledPattern.get(i + 1);
final StrptimeFormat f = nextToken.getFormat();
if (f == StrptimeFormat.FORMAT_STRING && isDigit(((String) nextToken.getData()).charAt(0))) {
return true;
} else if (NUMBER_PATTERNS.contains(f)) {
return true;
} else {
return false;
}
}
}
// CDdeFGgHIjkLlMmNQRrSsTUuVvWwXxYy
private static final EnumSet<StrptimeFormat> NUMBER_PATTERNS =
EnumSet.copyOf(Arrays.asList(
StrptimeFormat.FORMAT_CENTURY, // 'C'
// D
StrptimeFormat.FORMAT_DAY, // 'd'
StrptimeFormat.FORMAT_DAY_S, // 'e'
// F
StrptimeFormat.FORMAT_WEEKYEAR, // 'G'
StrptimeFormat.FORMAT_WEEKYEAR_SHORT, // 'g'
StrptimeFormat.FORMAT_HOUR, // 'H'
StrptimeFormat.FORMAT_HOUR_M, // 'I'
StrptimeFormat.FORMAT_DAY_YEAR, // 'j'
StrptimeFormat.FORMAT_HOUR_BLANK, // 'k'
StrptimeFormat.FORMAT_MILLISEC, // 'L'
StrptimeFormat.FORMAT_HOUR_S, // 'l'
StrptimeFormat.FORMAT_MINUTES, // 'M'
StrptimeFormat.FORMAT_MONTH, // 'm'
StrptimeFormat.FORMAT_NANOSEC, // 'N'
// Q, R, r
StrptimeFormat.FORMAT_SECONDS, // 'S'
StrptimeFormat.FORMAT_EPOCH, // 's'
// T
StrptimeFormat.FORMAT_WEEK_YEAR_S, // 'U'
StrptimeFormat.FORMAT_DAY_WEEK2, // 'u'
StrptimeFormat.FORMAT_WEEK_WEEKYEAR, // 'V'
// v
StrptimeFormat.FORMAT_WEEK_YEAR_M, // 'W'
StrptimeFormat.FORMAT_DAY_WEEK, // 'w'
// X, x
StrptimeFormat.FORMAT_YEAR_LONG, // 'Y'
StrptimeFormat.FORMAT_YEAR_SHORT // 'y'
));
/**
* Ports valid_pattern_p in ext/date/date_strptime.c in MRI 2.3.1 under BSDL.
* see https://github.com/ruby/ruby/blob/394fa89c67722d35bdda89f10c7de5c304a5efb1/ext/date/date_strftime.c
*/
private static boolean validRange(long v, int lower, int upper) {
return lower <= v && v <= upper;
}
private static boolean isSpace(char c) {
return c == ' ' || c == '\t' || c == '\n' ||
c == '\u000b' || c == '\f' || c == '\r';
}
private static boolean isDigit(char c) {
return '0' <= c && c <= '9';
}
private static boolean isEndOfText(String text, int pos) {
return pos >= text.length();
}
private static boolean isSign(String text, int pos) {
return !isEndOfText(text, pos) && (text.charAt(pos) == '+' || text.charAt(pos) == '-');
}
private static boolean isMinus(String text, int pos) {
return !isEndOfText(text, pos) && text.charAt(pos) == '-';
}
private static boolean isBlank(String text, int pos) {
return !isEndOfText(text, pos) && text.charAt(pos) == ' ';
}
private static int toInt(char c) {
return c - '0';
}
private static Number negateInteger(final Number i) {
if (i instanceof BigInteger) {
return ((BigInteger) i).negate();
}
return -i.longValue();
}
}
}