Skip to content

Commit

Permalink
Fix RadixNode.matchAt
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Jan 28, 2022
1 parent 828ff4b commit e04b9c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/cats/parse/RadixNode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private[parse] final class RadixNode(
def matchAt(str: String, off: Int): Int =
matchAtOrNull(str, off) match {
case null => -1
case nonNull => nonNull.length
case nonNull => off + nonNull.length
}

final def matchAtOrNull(str: String, offset: Int): String =
Expand Down
12 changes: 12 additions & 0 deletions core/shared/src/test/scala/cats/parse/ParserTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2587,4 +2587,16 @@ class ParserTest extends munit.ScalaCheckSuite {
assertEquals((Parser.pure(1) ~ Parser.pure(3)).as(42), Parser.pure(42))
assertEquals((Parser.pure(1).soft ~ Parser.pure(3)).as(42), Parser.pure(42))
}

property("stringIn after skipping test") {
forAll { (first: String, oneOf0: List[String], content0: String) =>
val oneOf = oneOf0.filter(_.nonEmpty)
forAll(Gen.oneOf(content0 :: oneOf0)) { content =>
assertEquals(
Parser.stringIn(oneOf).parse(content).toOption,
(Parser.string0(first) *> Parser.stringIn(oneOf)).parse(first + content).toOption
)
}
}
}
}
3 changes: 2 additions & 1 deletion core/shared/src/test/scala/cats/parse/RadixNodeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ class RadixNodeTest extends munit.ScalaCheckSuite {
radix.matchAt(targ, off) match {
case x if x < 0 =>
assertEquals(radix.matchAtOrNull(targ, off), null)
case len =>
case off1 =>
val len = off1 - off
val left = radix.matchAtOrNull(targ, off)
assert(off + len <= targ.length, s"len = $len, off = $off")
assertEquals(left, targ.substring(off, off + len), s"len = $len, left = $left")
Expand Down

0 comments on commit e04b9c9

Please sign in to comment.