@@ -574,12 +574,12 @@ public static boolean formatCanBeFast(Token[] compiledPattern) {
574
574
575
575
switch (format ) {
576
576
case FORMAT_ENCODING :
577
- // We only care about UTF8 encoding
577
+ // Only handle UTF-8 for fast formats
578
578
if (token .getData () != UTF8Encoding .INSTANCE ) {
579
579
return false ;
580
580
}
581
581
break ;
582
- case FORMAT_OUTPUT :
582
+ case FORMAT_OUTPUT : // only %6N
583
583
RubyTimeOutputFormatter formatter = (RubyTimeOutputFormatter ) token .getData ();
584
584
585
585
// Check for the attributes present in the default case
@@ -595,14 +595,19 @@ public static boolean formatCanBeFast(Token[] compiledPattern) {
595
595
return false ;
596
596
}
597
597
break ;
598
+ case FORMAT_NANOSEC : // only %6N
599
+ if (i - 1 >= 0 && compiledPattern [i - 1 ].getFormat () == Format .FORMAT_OUTPUT ) {
600
+ break ;
601
+ } else {
602
+ return false ;
603
+ }
598
604
case FORMAT_STRING :
599
605
case FORMAT_DAY :
600
606
case FORMAT_HOUR :
601
607
case FORMAT_MINUTES :
602
608
case FORMAT_MONTH :
603
609
case FORMAT_SECONDS :
604
610
case FORMAT_YEAR_LONG :
605
- case FORMAT_NANOSEC :
606
611
break ;
607
612
default :
608
613
return false ;
@@ -646,22 +651,21 @@ public static Rope formatToRopeFast(Token[] compiledPattern, ZonedDateTime dt,
646
651
647
652
case FORMAT_YEAR_LONG : {
648
653
final int value = dt .getYear ();
649
-
650
654
assert value >= 1000 ;
651
655
assert value <= 9999 ;
652
656
653
657
appendRope = new LazyIntRope (value , UTF8Encoding .INSTANCE , 4 );
654
658
}
655
659
break ;
656
660
657
- case FORMAT_NANOSEC : {
661
+ case FORMAT_NANOSEC : { // always %6N, checked by formatCanBeFast()
658
662
final int nano = dt .getNano ();
659
663
final LazyIntRope microSecondRope = new LazyIntRope (nano / 1000 );
660
664
661
665
// This fast-path only handles the '%6N' format, so output will always be 6 characters long.
662
666
final int length = 6 ;
663
667
final int padding = length - microSecondRope .characterLength ();
664
- assert padding >= 0 ;
668
+ assert padding >= 0 : microSecondRope ;
665
669
666
670
// `padding` is guaranteed to be >= 0 because `nano` can be at most 9 digits long before the
667
671
// conversion to microseconds. The division further constrains the rope to be at most 6 digits long.
0 commit comments