Skip to content

Commit

Permalink
Optimize charWhere for Sets (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek authored Oct 29, 2021
1 parent 5306e42 commit 70f3fa0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion core/shared/src/main/scala/cats/parse/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,13 @@ object Parser {
/** parse one character that matches a given function
*/
def charWhere(fn: Char => Boolean): Parser[Char] =
charIn(Impl.allChars.filter(fn))
fn match {
case s: Set[Char] =>
// Set extends function, but it is also iterable
charIn(s)
case _ =>
charIn(Impl.allChars.filter(fn))
}

/** Parse a string while the given function is true
*/
Expand Down
6 changes: 6 additions & 0 deletions core/shared/src/test/scala/cats/parse/ParserTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2383,4 +2383,10 @@ class ParserTest extends munit.ScalaCheckSuite {
}
}
}

property("a parser from a set of chars is the same with charWhere/charIn") {
forAll { (input: String, chars: Set[Char]) =>
assertEquals(Parser.charWhere(chars).parse(input), Parser.charIn(chars).parse(input))
}
}
}

0 comments on commit 70f3fa0

Please sign in to comment.