lib/str.nim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import tables, strutils
import ../vendor/slre
import
../core/types,
../core/parser,
../core/interpreter,
../core/utils
define("str")
.symbol("split") do (i: In):
let sep = i.pop
let s = i.pop
if s.isString and sep.isString:
for e in s.strVal.split(sep.strVal):
i.push e.newVal
else:
i.error errIncorrect, "Two strings are required on the stack"
.symbol("match") do (i: In):
let reg = i.pop
let str = i.pop
if str.isString and reg.isString:
var matches = str.strVal.match(reg.strVal)
var res = newSeq[MinValue](0)
for s in matches:
res.add s.newVal
i.push res.newVal
else:
i.error(errIncorrect, "Two strings are required on the stack")
.symbol("match?") do (i: In):
let reg = i.pop
let str = i.pop
if str.isString and reg.isString:
var matches = str.strVal.match(reg.strVal)
if matches.len > 0:
i.push true.newVal
else:
i.push false.newVal
else:
i.error(errIncorrect, "Two strings are required on the stack")
.symbol("replace") do (i: In):
let s_replace = i.pop
let reg = i.pop
let s_find = i.pop
if reg.isString and s_replace.isString and s_find.isString:
i.push s_find.strVal.gsub(reg.strVal, s_replace.strVal).newVal
else:
i.error(errIncorrect, "Three strings are required on the stack")
.finalize()
|