Best way to parse ascii letters and numbers #224
-
I am implementing a parser for the package url (purl) spec and in looking at the type section, I need to deal with ASCII letters and numbers. What is the best way to construct the parser for
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's not clear what exactly it means by "not be percent encoded"; whether that's supposed to be "no percent-encoded characters in the name" or what. But basically, it seems like a direct translation is probably best: val start = oneOf('a' to 'z') | oneOf('A' to 'Z') | oneOf('.', '+', '-')
val letters = oneOf('0' to '9') | start (you could use val typePurl = (start ~> many(letters)).span.map(_.toLowerCase) That will give you a canonicalisation to lowercase strings (I might have gotten the specific method wrong from Then the percent-encoded bit should be trivially satisfied by not allowing for any |
Beta Was this translation helpful? Give feedback.
It's not clear what exactly it means by "not be percent encoded"; whether that's supposed to be "no percent-encoded characters in the name" or what. But basically, it seems like a direct translation is probably best:
(you could use
digit
/letter
, but that would be more than just ASCII in this case; depends on how strict you want to be. Or you could build a set of all the letters and use a singleoneOf
, which would be more efficient in this case). Then you want to stitch them together, which can probably be done withspan
for simplicity: