Skip to content

Commit cca90c5

Browse files
authored
fix(std): reject "-0" forms in stringToLong (#1582) (#1585)
1 parent 045cde0 commit cca90c5

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

core/shared/src/main/scala/monocle/std/String.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ trait StringOptics extends PlatformSpecificStringOptics {
3636
// we reject cases where String will be an invalid Prism according 2nd Prism law
3737
// * String starts with +
3838
// * String starts with 0 and has multiple digits
39+
// * String starts with -0 (negative zero forms never round-trip)
3940
def inputBreaksPrismLaws(input: String): Boolean =
40-
s.isEmpty || s.startsWith("+") || (s.startsWith("0") && s.length > 1)
41+
s.isEmpty || s.startsWith("+") || (s.startsWith("0") && s.length > 1) ||
42+
(s.length > 1 && s.charAt(0) == '-' && s.charAt(1) == '0')
4143

4244
if (inputBreaksPrismLaws(s)) None
4345
else

test/shared/src/test/scala/monocle/std/StringsSpec.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,17 @@ class StringsSpec extends MonocleSuite {
2525
// checkAll("String to URI", PrismTests(stringToURI))
2626

2727
checkAll("plated String", TraversalTests(plate[String]))
28+
29+
// see #1582: "-0" et al. parsed to 0L but reverseGet round-tripped to "0"
30+
test("stringToLong rejects negative-zero forms") {
31+
assertEquals(stringToLong.getOption("-0"), None)
32+
assertEquals(stringToLong.getOption("-00"), None)
33+
assertEquals(stringToLong.getOption("-01"), None)
34+
}
35+
36+
test("stringToLong still accepts ordinary negatives and zero") {
37+
assertEquals(stringToLong.getOption("0"), Some(0L))
38+
assertEquals(stringToLong.getOption("-1"), Some(-1L))
39+
assertEquals(stringToLong.getOption("-10"), Some(-10L))
40+
}
2841
}

0 commit comments

Comments
 (0)