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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import tables, strutils import ../core/types, ../core/parser, ../core/interpreter, ../core/utils, ../core/regex define("str") .symbol("split") do (i: In): let sep = i.pop let s = i.pop var q = newSeq[MinValue](0) if s.isString and sep.isString: for e in s.strVal.split(sep.strVal): q.add e.newVal i.push q.newVal else: i.error errIncorrect, "Two strings are required on the stack" .symbol("search") do (i: In): let reg = i.pop let str = i.pop if str.isString and reg.isString: var matches = str.strVal.search(reg.strVal) var res = newSeq[MinValue](matches.len) for i in 0..matches.len-1: res[i] = matches[i].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: if str.strVal.match(reg.strVal): 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 regex.replace(s_find.strVal, reg.strVal, s_replace.strVal).newVal else: i.error(errIncorrect, "Three strings are required on the stack") .symbol("=~") do (i: In): let reg = i.pop let str = i.pop if str.isString and reg.isString: let results = str.strVal =~ reg.strVal var res = newSeq[MinValue](0) for r in results: res.add(r.newVal) i.push res.newVal else: i.error(errIncorrect, "Two strings are required on the stack") .finalize() |