Comments (6)
You might want to take a look at:
Also note the repAs method that can build the string in a more efficient manner than you do here.
Lastly, you can parse a signed int32 without parsing the string and maybe failing, but it is more work: you either parse a - or not. If you don't see a - you either see 0, or 1 followed by 0 to 8 digits, etc...
This is like building a regular expression for a limited integer. It's probably worth implementing and adding to Numbers so people don't have to struggle with it.
That said, I do question if a parse error is what you want here vs an error message that says the number is too large. That can be done by using Numbers + filter (or flatMap but filter can be more efficient).
from cats-parse.
Suppose you have another parser you'd like to embed inside a cats parse parser, that would be one example.
A second example is supporting something we could support but don't (e.g. getting access to the whole String).
I don't think it is a great idea, but it is an escape hatch if someone gets in an unusual place.
from cats-parse.
Do you have examples of when this is necessary?
from cats-parse.
I've got a concrete example from something I'm working on at the moment.
Unless I'm missing a standard combinator, this is currently how to parse an integer:
final val integer: Parser[Int] =
Parser
.charIn('0' to '9')
.rep
.map { chars =>
chars
.foldLeft(new StringBuilder(chars.length))(_ append _)
.mkString
}
.flatMap { input =>
Either
.catchNonFatal(input.toInt)
.fold(
_ => Parser.failWith(s"Invalid number: $input"),
Parser.pure
)
}
from cats-parse.
I do this for unsigned ints:
def safeToInt(value: String): Option[Int] =
Try(value.toInt).toOption
val uintP: Parser[Int] = Numbers.digits.mapFilter(safeToInt)
from cats-parse.
Thanks for pointing me towards Numbers
. Aside from the direct stuff, it's got a lot of good examples.
In my case, a parse error is desirable because I'm using it to parse command line arguments, so an out of bounds number is going to be less common than a parse error caused by an extraneous quote.
from cats-parse.
Related Issues (20)
- A parser string interpolator
- Backtracking with context HOT 1
- Rep same char as surrounded by HOT 5
- idea for safe repetition on Parser0
- RadixNode and stringIn are a bit slow
- Is it possible to support Scala Native? HOT 6
- alternate design for voiding
- use java.util.BitSet on scalajs
- `p1.? ~ p2` != `(p1 ~ p2) | p2` for error reporting purposes HOT 9
- Remove isScalaJs/isScalaJvm on minor version update
- identifier parser HOT 2
- flakey test: X cats.parse.ParserTest.a.flatMap(b) composes as expected parser00
- add json string parsing
- add a withString combinator
- Should `Parser` have a typeclass? HOT 2
- set up github CI to run benchmarks on release
- investigate scalacheck failure 20230625T112756
- Link errors when crossbuilding for scalaJS or scalaNative HOT 3
- Recursion with `Parser0` HOT 2
- investigate scalacheck failure 20231113
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cats-parse.