Improved str module.
h3rald h3rald@h3rald.com
Sun, 11 Sep 2016 17:03:11 +0200
5 files changed,
131 insertions(+),
5 deletions(-)
M
core/utils.nim
→
core/utils.nim
@@ -408,10 +408,10 @@ b = i.pop
if not (a.isString and b.isString or a.isNumber and b.isNumber): raiseInvalid("Two numbers or two strings are required on the stack") -proc reqStringAndNumber*(i: var MinInterpreter, a, b: var MinValue) = +proc reqIntAndString*(i: var MinInterpreter, b, a: var MinValue) = b = i.pop a = i.pop - if not (a.isString and b.isNumber): + if not (a.isString and b.isInt): raiseInvalid("A string and a number are required on the stack") proc reqString*(i: var MinInterpreter, a: var MinValue) =
M
lib/min_str.nim
→
lib/min_str.nim
@@ -1,4 +1,4 @@
-import tables, strutils +import tables, strutils, sequtils import ../core/types, ../core/parser,@@ -22,6 +22,11 @@ for e in s.strVal.split(sep.strVal):
q.add e.newVal i.push q.newVal + .symbol("join") do (i: In): + var q, s: MinValue + i.reqStringLikeAndQuotation s, q + i.push q.qVal.mapIt($$it).join(s.getString).newVal + .symbol("search") do (i: In): var reg, str: MinValue i.reqTwoStrings reg, str@@ -44,7 +49,7 @@ var s_replace, reg, s_find: MinValue
i.reqThreeStrings s_replace, reg, s_find i.push regex.replace(s_find.strVal, reg.strVal, s_replace.strVal).newVal - .symbol("=~") do (i: In): + .symbol("regex") do (i: In): var reg, str: MinValue i.reqTwoStrings reg, str let results = str.strVal =~ reg.strVal@@ -52,5 +57,83 @@ var res = newSeq[MinValue](0)
for r in results: res.add(r.newVal) i.push res.newVal + + .symbol("lowercase") do (i: In): + var s: MinValue + i.reqStringLike s + i.push s.getString.toLower.newVal + + .symbol("uppercase") do (i: In): + var s: MinValue + i.reqStringLike s + i.push s.getString.toUpper.newVal + + .symbol("capitalize") do (i: In): + var s: MinValue + i.reqStringLike s + i.push s.getString.capitalize.newVal + + .symbol("titleize") do (i: In): + var s: MinValue + i.reqStringLike s + i.push s.getString.split(" ").mapIt(it.capitalize).join(" ").newVal + + .symbol("repeat") do (i: In): + var s, n: MinValue + i.reqIntAndString n, s + i.push s.getString.repeat(n.intVal).newVal + + .symbol("indent") do (i: In): + var s, n: MinValue + i.reqIntAndString n, s + i.push s.getString.indent(n.intVal).newVal + + .symbol("string") do (i: In): + var s = i.pop + i.push(($$s).newVal) + + .symbol("bool") do (i: In): + var v = i.pop + let strcheck = (v.isString and (v.getString == "false" or v.getString == "")) + let intcheck = v.isInt and v.intVal == 0 + let floatcheck = v.isFloat and v.floatVal == 0 + let boolcheck = v.isBool and v.boolVal == false + let quotcheck = v.isQuotation and v.qVal.len == 0 + if strcheck or intcheck or floatcheck or boolcheck or quotcheck: + i.push false.newVal + else: + i.push true.newVal + + .symbol("int") do (i: In): + var s = i.pop + if s.isString: + i.push s.getString.parseInt.newVal + elif s.isFloat: + i.push s.floatVal.int.newVal + elif s.isInt: + i.push s + elif s.isBool: + if s.boolVal == true: + i.push 1.int.newVal + else: + i.push 0.int.newVal + else: + raiseInvalid("Cannot convert a quotation to an integer.") + + .symbol("float") do (i: In): + var s = i.pop + if s.isString: + i.push s.getString.parseFloat.newVal + elif s.isInt: + i.push s.intVal.float.newVal + elif s.isFloat: + i.push s + elif s.isBool: + if s.boolVal == true: + i.push 1.float.newVal + else: + i.push 0.float.newVal + else: + raiseInvalid("Cannot convert a quotation to float.") .finalize()
M
lib/min_sys.nim
→
lib/min_sys.nim
@@ -121,7 +121,7 @@ sleep ms.intVal.int
.symbol("chmod") do (i: In): var s, perms: MinValue - i.reqStringAndNumber s, perms + i.reqIntAndString perms, s s.getString.setFilePermissions(perms.intVal.toFilePermissions) .symbol("symlink?") do (i: In):
M
prelude.min
→
prelude.min
@@ -33,6 +33,7 @@ 'scope :=>
'filter :select 'cons :prepend 'size :length +'regex :=~ ; Mathematical Operators (1 +) :succ
M
tests/str.min
→
tests/str.min
@@ -21,5 +21,47 @@ ("This is a difficult test" "s/difficult/simple/" =~ ("This is a simple test") ==) assert
("This is a DIFFICULT\n test" "s/difficult/simple/mis" =~ ("This is a simple\n test") ==) assert + ("this is a test" uppercase "THIS IS A TEST" ==) assert + + ("THIS IS A TEST" lowercase "this is a test" ==) assert + + ("test" capitalize "Test" ==) assert + + ("this is a test" titleize "This Is A Test" ==) assert + + ("+" 3 repeat "+++" ==) assert + + ("test" 4 indent " test" ==) assert + + ((1 3 "test") ", " join "1, 3, test" ==) assert + + (3 string "3" ==) assert + + ("false" bool false ==) assert + + ("" bool false ==) assert + + (0 bool false ==) assert + + (false bool false ==) assert + + (0.0 bool false ==) assert + + ("something" bool true ==) assert + + ("345" int 345 ==) assert + + (true int 1 ==) assert + + (3.5 int 3 ==) assert + + (3.5 float 3.5 ==) assert + + (3 float 3.0 ==) assert + + (false float 0.0 ==) assert + + ("3.678" float 3.678 ==) assert + report clear-stack