Started refactor
This commit is contained in:
28
src_v2/utils/parser.nim
Normal file
28
src_v2/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