Files
org-parser/src_v2/utils/parser.nim
Florian Schroedl 892eec10d5 Started refactor
2022-08-09 22:12:50 +02:00

29 lines
1023 B
Nim

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)