@@ -283,7 +283,7 @@ internal fun parseIso(isoString: String): Instant {
283
283
}
284
284
val year = when {
285
285
i > yearStart + 10 -> {
286
- parseFailure(" Expected at most 10 digits for the year number, got ${i - yearStart} " )
286
+ parseFailure(" Expected at most 10 digits for the year number, got ${i - yearStart} digits " )
287
287
}
288
288
i == yearStart + 10 && s[yearStart] >= ' 2' -> {
289
289
parseFailure(" Expected at most 9 digits for the year number or year 1000000000, got ${i - yearStart} " )
@@ -311,10 +311,10 @@ internal fun parseIso(isoString: String): Instant {
311
311
expect(" 'T' or 't'" , i + 6 ) { it == ' T' || it == ' t' }
312
312
expect(" ':'" , i + 9 ) { it == ' :' }
313
313
expect(" ':'" , i + 12 ) { it == ' :' }
314
- for (j in listOf ( 1 , 2 , 4 , 5 , 7 , 8 , 10 , 11 , 13 , 14 ) ) {
314
+ for (j in asciiDigitPositionsInIsoStringAfterYear ) {
315
315
expect(" an ASCII digit" , i + j) { it in ' 0' .. ' 9' }
316
316
}
317
- fun twoDigitNumber (index : Int ) = s[index].code * 10 + s[index + 1 ].code - ' 0' .code * 11
317
+ fun twoDigitNumber (index : Int ) = ( s[index] - ' 0 ' ) * 10 + ( s[index + 1 ] - ' 0' )
318
318
val month = twoDigitNumber(i + 1 )
319
319
val day = twoDigitNumber(i + 4 )
320
320
val hour = twoDigitNumber(i + 7 )
@@ -350,11 +350,11 @@ internal fun parseIso(isoString: String): Instant {
350
350
val offsetStrLength = s.length - i
351
351
if (offsetStrLength % 3 != 0 ) { parseFailure(" Invalid UTC offset string '${s.substring(i)} '" ) }
352
352
if (offsetStrLength > 9 ) { parseFailure(" The UTC offset string '${s.substring(i)} ' is too long" ) }
353
- for (j in listOf ( 3 , 6 ) ) {
353
+ for (j in colonsInIsoOffsetString ) {
354
354
if (s.getOrNull(i + j) ? : break != ' :' )
355
355
parseFailure(" Expected ':' at index ${i + j} , got '${s[i + j]} '" )
356
356
}
357
- for (j in listOf ( 1 , 2 , 4 , 5 , 7 , 8 ) ) {
357
+ for (j in asciiDigitsInIsoOffsetString ) {
358
358
if (s.getOrNull(i + j) ? : break !in ' 0' .. ' 9' )
359
359
parseFailure(" Expected a digit at index ${i + j} , got '${s[i + j]} '" )
360
360
}
@@ -382,6 +382,10 @@ internal fun parseIso(isoString: String): Instant {
382
382
return UnboundedLocalDateTime (year, month, day, hour, minute, second, nanosecond).toInstant(offsetSeconds)
383
383
}
384
384
385
+ private val asciiDigitPositionsInIsoStringAfterYear by lazy { listOf (1 , 2 , 4 , 5 , 7 , 8 , 10 , 11 , 13 , 14 ) }
386
+ private val colonsInIsoOffsetString by lazy { listOf (3 , 6 ) }
387
+ private val asciiDigitsInIsoOffsetString by lazy { listOf (1 , 2 , 4 , 5 , 7 , 8 ) }
388
+
385
389
internal fun formatIso (instant : Instant ): String = buildString {
386
390
val ldt = UnboundedLocalDateTime .fromInstant(instant, 0 )
387
391
fun Appendable.appendTwoDigits (number : Int ) {
@@ -422,7 +426,7 @@ internal fun formatIso(instant: Instant): String = buildString {
422
426
while (ldt.nanosecond % POWERS_OF_TEN [zerosToStrip + 1 ] == 0 ) {
423
427
++ zerosToStrip
424
428
}
425
- zerosToStrip - = ( zerosToStrip.mod( 3 )) // rounding down to a multiple of 3
429
+ zerosToStrip - = zerosToStrip % 3 // rounding down to a multiple of 3
426
430
val numberToOutput = ldt.nanosecond / POWERS_OF_TEN [zerosToStrip]
427
431
append((numberToOutput + POWERS_OF_TEN [9 - zerosToStrip]).toString().substring(1 ))
428
432
}
0 commit comments