Skip to content

Commit 8f194d5

Browse files
Parse String to UUID #1006_RevB
parse.kt ### Changes Made - **Added regex pre-validation** - **Restructured logic** so `UUID.fromString()` is only called when regex matches - **Exceptions are no longer default flow** - only thrown in rare edge cases where regex passes but UUID parsing fails - **Maintained safety** with try/catch around `UUID.fromString()` ### Testing - All existing test cases unchanged and continue to pass - Positive test: valid UUID strings → UUID objects - Negative test: invalid strings → remain String type
1 parent e41f337 commit 8f194d5

File tree

1 file changed

+12
-8
lines changed
  • core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api

1 file changed

+12
-8
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/parse.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ import java.time.LocalDate as JavaLocalDate
6363
import java.time.LocalDateTime as JavaLocalDateTime
6464
import java.time.LocalTime as JavaLocalTime
6565

66-
67-
6866
private val logger = KotlinLogging.logger { }
6967

7068
internal interface StringParser<T> {
@@ -494,15 +492,21 @@ internal object Parsers : GlobalParserOptions {
494492
posixParserToDoubleWithOptions,
495493
// Boolean
496494
stringParser<Boolean> { it.toBooleanOrNull() },
497-
//UUID
498-
stringParser<UUID> {str ->
499-
try{
500-
UUID.fromString(str)
501-
} catch(e: IllegalArgumentException){
495+
// UUID
496+
stringParser<UUID> { str ->
497+
498+
val uuidRegex = Regex("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")
499+
500+
if (uuidRegex.matches(str)) {
501+
try {
502+
UUID.fromString(str)
503+
} catch (e: IllegalArgumentException) {
504+
null
505+
}
506+
} else {
502507
null
503508
}
504509
},
505-
506510
// BigInteger
507511
stringParser<BigInteger> { it.toBigIntegerOrNull() },
508512
// BigDecimal

0 commit comments

Comments
 (0)