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
4 changes: 3 additions & 1 deletion core/shared/src/main/scala/monocle/std/String.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ trait StringOptics extends PlatformSpecificStringOptics {
// we reject cases where String will be an invalid Prism according 2nd Prism law
// * String starts with +
// * String starts with 0 and has multiple digits
// * String starts with -0 (negative zero forms never round-trip)
def inputBreaksPrismLaws(input: String): Boolean =
s.isEmpty || s.startsWith("+") || (s.startsWith("0") && s.length > 1)
s.isEmpty || s.startsWith("+") || (s.startsWith("0") && s.length > 1) ||
(s.length > 1 && s.charAt(0) == '-' && s.charAt(1) == '0')

if (inputBreaksPrismLaws(s)) None
else
Expand Down
13 changes: 13 additions & 0 deletions test/shared/src/test/scala/monocle/std/StringsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,17 @@ class StringsSpec extends MonocleSuite {
// checkAll("String to URI", PrismTests(stringToURI))

checkAll("plated String", TraversalTests(plate[String]))

// see #1582: "-0" et al. parsed to 0L but reverseGet round-tripped to "0"
test("stringToLong rejects negative-zero forms") {
assertEquals(stringToLong.getOption("-0"), None)
assertEquals(stringToLong.getOption("-00"), None)
assertEquals(stringToLong.getOption("-01"), None)
}

test("stringToLong still accepts ordinary negatives and zero") {
assertEquals(stringToLong.getOption("0"), Some(0L))
assertEquals(stringToLong.getOption("-1"), Some(-1L))
assertEquals(stringToLong.getOption("-10"), Some(-10L))
}
}
Loading