Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ private const val MAX_HOUR_DIGITS = 9
private const val MAX_MINUTE_SECOND_DIGITS = 2

/**
* Parses an `HH:MM:SS` time, splitting [value] on `:` and converting each part with [String.toInt].
* Parses an `HH:MM:SS` time, splitting [value] on `:` and converting each part with
* [String.toIntOrNull].
*
* The common shape (one to nine ASCII digits of hours, then one or two ASCII digits each of minutes
* and seconds) is read in place without allocating. Any other input goes through the general
* split-based path, so the accepted values and the thrown exceptions match it exactly.
*
* @param typeName the type named in the error message for a value without three parts.
* @param typeName the type named in the error message for a value without three parts, or with a
* non-numeric part.
*/
internal inline fun <T> parseHms(
value: String,
Expand All @@ -36,8 +38,13 @@ internal inline fun <T> parseHms(
}

val parts = value.split(":")
require(parts.size == 3) { "$typeName must be in the format HH:MM:SS, but was $value." }
return create(parts[0].toInt(), parts[1].toInt(), parts[2].toInt())
val formatError = { "$typeName must be in the format HH:MM:SS, but was $value." }
require(parts.size == 3, formatError)
val hours = parts[0].toIntOrNull()
val minutes = parts[1].toIntOrNull()
val seconds = parts[2].toIntOrNull()
require(hours != null && minutes != null && seconds != null, formatError)
return create(hours, minutes, seconds)
}

/** Returns the end of the run of at most [maxDigits] ASCII digits starting at [start]. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dev.sargunv.mobilitydata.utils.GtfsLocalTime
import dev.sargunv.mobilitydata.utils.ServiceTime
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonPrimitive
Expand Down Expand Up @@ -51,8 +52,13 @@ class HmsSerializersTest {
/** The parser before the fast path was added. */
private fun <T> reference(value: String, typeName: String, create: (Int, Int, Int) -> T): T {
val parts = value.split(":")
require(parts.size == 3) { "$typeName must be in the format HH:MM:SS, but was $value." }
return create(parts[0].toInt(), parts[1].toInt(), parts[2].toInt())
val formatError = { "$typeName must be in the format HH:MM:SS, but was $value." }
require(parts.size == 3, formatError)
val hours = parts[0].toIntOrNull()
val minutes = parts[1].toIntOrNull()
val seconds = parts[2].toIntOrNull()
require(hours != null && minutes != null && seconds != null, formatError)
return create(hours, minutes, seconds)
}

private fun <T> decode(serializer: KSerializer<T>, value: String): T =
Expand Down Expand Up @@ -94,4 +100,17 @@ class HmsSerializersTest {
assertEquals(ServiceTime(25, 35, 0), decode(ServiceTimeSerializer, "25:35:00"))
assertEquals(ServiceTime(7, 30, 0), decode(ServiceTimeSerializer, "007:30:00"))
}

@Test
fun parseHmsRejectsNonNumericPartsWithFormatMessage() {
val exception =
assertFailsWith<IllegalArgumentException> {
parseHms("ab:cd:ef", "ServiceTime", ::ServiceTime)
}
assertEquals(IllegalArgumentException::class, exception::class)
assertEquals(
"ServiceTime must be in the format HH:MM:SS, but was ab:cd:ef.",
exception.message,
)
}
}
Loading