Init
This commit is contained in:
@@ -14,6 +14,10 @@ proc findMaybeFn*[T, B](fns: seq[T {.nimcall.} -> Maybe[B]], val: T): Maybe[B] =
|
||||
return res
|
||||
return Nothing[B]()
|
||||
|
||||
proc notNegative*[int](x: Maybe[int]): Maybe[int] =
|
||||
## Maps nil object to nothing
|
||||
x.filter(i => i >= 0)
|
||||
|
||||
when isMainModule:
|
||||
echo @[
|
||||
(x: int) => (if x == 2: Just("foo") else: Nothing[string]()),
|
||||
|
||||
15
src/utils/parsec_test.nim
Normal file
15
src/utils/parsec_test.nim
Normal file
@@ -0,0 +1,15 @@
|
||||
import sequtils
|
||||
import std/sugar
|
||||
import microparsec
|
||||
import results
|
||||
|
||||
echo toSeq(1..1000000)
|
||||
|
||||
# let headlineParser = manyTill(ch '*', space)
|
||||
# .flatMap((stars: seq[char]) => pure(stars))
|
||||
# # .flatMap((stars) => manyTill(anyChar, endOfLine)
|
||||
# # .flatMap(headline => pure(stars, headline))
|
||||
# # )
|
||||
|
||||
# echo headlineParser.parse("*** Headline")
|
||||
# echo headlineParser.parse("* Headline")
|
||||
28
src/utils/parser.nim
Normal file
28
src/utils/parser.nim
Normal file
@@ -0,0 +1,28 @@
|
||||
import std/parseutils
|
||||
|
||||
proc fastSubstr(s: string; token: var string; start, length: int) =
|
||||
token.setLen length
|
||||
for i in 0 ..< length: token[i] = s[i+start]
|
||||
|
||||
proc parseUntilBackwards*(s: string, token: var string, until: string,
|
||||
start = 0): int {.inline.} =
|
||||
## Parses a token and stores it in ``token``. Returns
|
||||
## the number of the parsed characters or 0 in case of an error. A token
|
||||
## consists of any character that comes before the `until` token.
|
||||
runnableExamples:
|
||||
var myToken: string
|
||||
doAssert parseUntil("Hello World", myToken, "Wor") == 6
|
||||
doAssert myToken == "Hello "
|
||||
doAssert parseUntil("Hello World", myToken, "Wor", 2) == 4
|
||||
doAssert myToken == "llo "
|
||||
var i = s.len
|
||||
while i > 0:
|
||||
if until.len > 0 and s[i] == until[until.len - 1]:
|
||||
var u = 1
|
||||
while i+u < s.len and u < until.len and s[i+u] == until[u]:
|
||||
inc u
|
||||
if u >= until.len: break
|
||||
dec(i)
|
||||
result = i-start
|
||||
fastSubstr(s, token, start, result)
|
||||
#token = substr(s, start, i-1)
|
||||
Reference in New Issue
Block a user