Skip to content

Commit 3de6ba9

Browse files
committed
Address the review
1 parent b29e49d commit 3de6ba9

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

core/commonKotlin/src/Instant.kt

+10-6
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ internal fun parseIso(isoString: String): Instant {
283283
}
284284
val year = when {
285285
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")
287287
}
288288
i == yearStart + 10 && s[yearStart] >= '2' -> {
289289
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 {
311311
expect("'T' or 't'", i + 6) { it == 'T' || it == 't' }
312312
expect("':'", i + 9) { it == ':' }
313313
expect("':'", i + 12) { it == ':' }
314-
for (j in listOf(1, 2, 4, 5, 7, 8, 10, 11, 13, 14)) {
314+
for (j in asciiDigitPositionsInIsoStringAfterYear) {
315315
expect("an ASCII digit", i + j) { it in '0'..'9' }
316316
}
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')
318318
val month = twoDigitNumber(i + 1)
319319
val day = twoDigitNumber(i + 4)
320320
val hour = twoDigitNumber(i + 7)
@@ -350,11 +350,11 @@ internal fun parseIso(isoString: String): Instant {
350350
val offsetStrLength = s.length - i
351351
if (offsetStrLength % 3 != 0) { parseFailure("Invalid UTC offset string '${s.substring(i)}'") }
352352
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) {
354354
if (s.getOrNull(i + j) ?: break != ':')
355355
parseFailure("Expected ':' at index ${i + j}, got '${s[i + j]}'")
356356
}
357-
for (j in listOf(1, 2, 4, 5, 7, 8)) {
357+
for (j in asciiDigitsInIsoOffsetString) {
358358
if (s.getOrNull(i + j) ?: break !in '0'..'9')
359359
parseFailure("Expected a digit at index ${i + j}, got '${s[i + j]}'")
360360
}
@@ -382,6 +382,10 @@ internal fun parseIso(isoString: String): Instant {
382382
return UnboundedLocalDateTime(year, month, day, hour, minute, second, nanosecond).toInstant(offsetSeconds)
383383
}
384384

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+
385389
internal fun formatIso(instant: Instant): String = buildString {
386390
val ldt = UnboundedLocalDateTime.fromInstant(instant, 0)
387391
fun Appendable.appendTwoDigits(number: Int) {
@@ -422,7 +426,7 @@ internal fun formatIso(instant: Instant): String = buildString {
422426
while (ldt.nanosecond % POWERS_OF_TEN[zerosToStrip + 1] == 0) {
423427
++zerosToStrip
424428
}
425-
zerosToStrip -= (zerosToStrip.mod(3)) // rounding down to a multiple of 3
429+
zerosToStrip -= zerosToStrip % 3 // rounding down to a multiple of 3
426430
val numberToOutput = ldt.nanosecond / POWERS_OF_TEN[zerosToStrip]
427431
append((numberToOutput + POWERS_OF_TEN[9 - zerosToStrip]).toString().substring(1))
428432
}

0 commit comments

Comments
 (0)