Skip to content

Commit c4a6661

Browse files
Yu-zhbobzhang
authored andcommitted
fix some corner cases
1 parent b502e6d commit c4a6661

File tree

6 files changed

+37
-5
lines changed

6 files changed

+37
-5
lines changed

strconv/decimal.mbt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,9 @@ test "parse_decimal with large numbers and truncation" {
688688
content="10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
689689
)
690690
}
691+
692+
test "corner cases" {
693+
inspect!(parse_decimal?(".123"), content="Ok(0.123)")
694+
inspect!(parse_decimal?("."), content="Err(invalid syntax)")
695+
inspect!(parse_decimal?("-"), content="Err(invalid syntax)")
696+
}

strconv/double_test.mbt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@ test "try_fast_path overflow when mantissa is too large" {
3636
// The function successfully falls back to slow path
3737
inspect!(result, content="9.007199254740991e+38")
3838
}
39+
40+
test "corner cases" {
41+
inspect!(@strconv.parse_double?(".123"), content="Ok(0.123)")
42+
inspect!(@strconv.parse_double?("."), content="Err(invalid syntax)")
43+
inspect!(@strconv.parse_double?("-"), content="Err(invalid syntax)")
44+
}

strconv/int.mbt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ pub fn parse_int64(str : String, base~ : Int = 0) -> Int64!StrConvError {
7070

7171
// calculate overflow threshold
7272
let overflow_threshold = overflow_threshold(num_base, neg)
73-
73+
let has_digit = match rest {
74+
['0'..='9' | 'a'..='z' | 'A'..='Z', ..]
75+
| ['_', '0'..='9' | 'a'..='z' | 'A'..='Z', ..] => true
76+
_ => false
77+
}
78+
guard has_digit else { syntax_err!() }
7479
// convert
7580
loop rest, 0L, allow_underscore {
7681
['_'], _, _ =>

strconv/int_test.mbt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,12 @@ test "@moonbitlang/core/strconv.parse_int64/boundary_cases" {
115115
test "panic @moonbitlang/core/strconv.parse_int64/invalid_base" {
116116
ignore(@moonbitlang/core/strconv.parse_int64!("12345x"))
117117
}
118+
119+
test "edge cases" {
120+
// Invalid: Missing digits after hex prefix
121+
inspect!(@strconv.parse_int64?("0x"), content="Err(invalid syntax)")
122+
inspect!(@strconv.parse_int64?("0x_"), content="Err(invalid syntax)")
123+
124+
// Underscore is allowed between the prefix and the first digit
125+
inspect!(@strconv.parse_int64?("0x_DEADBEEF"), content="Ok(3735928559)")
126+
}

strconv/uint.mbt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ pub fn parse_uint64(str : String, base~ : Int = 0) -> UInt64!StrConvError {
3737
16 => UINT64_MAX / 16 + 1
3838
_ => UINT64_MAX / num_base.to_uint64() + 1
3939
}
40+
let has_digit = match rest {
41+
['0'..='9' | 'a'..='z' | 'A'..='Z', ..]
42+
| ['_', '0'..='9' | 'a'..='z' | 'A'..='Z', ..] => true
43+
_ => false
44+
}
45+
guard has_digit else { syntax_err!() }
4046
loop rest, 0UL, allow_underscore {
4147
['_'], _, _ =>
4248
// the last character cannot be underscore

strconv/uint_test.mbt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ test "@strconv.parse_uint64/hex_and_edge_cases" {
8484

8585
test "edge cases" {
8686
// Invalid: Missing digits after hex prefix
87-
// inspect!(@strconv.parse_uint64?("0x"), content="Err(invalid syntax)")
88-
89-
// Invalid underscore placements in hexadecimal strings
90-
// inspect!(@strconv.parse_uint64?("0x_DEADBEEF"), content="Err(invalid syntax)")
87+
inspect!(@strconv.parse_uint64?("0x"), content="Err(invalid syntax)")
88+
inspect!(@strconv.parse_uint64?("0x_"), content="Err(invalid syntax)")
9189

90+
// Underscore is allowed between the prefix and the first digit
91+
inspect!(@strconv.parse_uint64?("0x_DEADBEEF"), content="Ok(3735928559)")
9292
}

0 commit comments

Comments
 (0)