add string REV & SPLIT
This commit is contained in:
parent
a341cdf570
commit
68c8a4e178
2 changed files with 49 additions and 0 deletions
|
@ -78,6 +78,28 @@ module Rpl
|
||||||
|
|
||||||
[stack, dictionary]
|
[stack, dictionary]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# reverse string
|
||||||
|
def rev( stack, dictionary )
|
||||||
|
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[string]] )
|
||||||
|
|
||||||
|
stack << { type: :string,
|
||||||
|
value: "\"#{args[0][:value][1..-2].reverse}\"" }
|
||||||
|
|
||||||
|
[stack, dictionary]
|
||||||
|
end
|
||||||
|
|
||||||
|
# split string
|
||||||
|
def split( stack, dictionary )
|
||||||
|
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[string], %i[string]] )
|
||||||
|
|
||||||
|
args[1][:value][1..-2].split( args[0][:value][1..-2] ).each do |elt|
|
||||||
|
stack << { type: :string,
|
||||||
|
value: "\"#{elt}\"" }
|
||||||
|
end
|
||||||
|
|
||||||
|
[stack, dictionary]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -69,4 +69,31 @@ class TestLanguageString < Test::Unit::TestCase
|
||||||
assert_equal [{ value: 'str', type: :string }],
|
assert_equal [{ value: 'str', type: :string }],
|
||||||
lang.stack
|
lang.stack
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_rev
|
||||||
|
lang = Rpl::Language.new
|
||||||
|
lang.run '"my string to sub" rev'
|
||||||
|
|
||||||
|
assert_equal [{ value: '"my string to sub"'.reverse, type: :string }],
|
||||||
|
lang.stack
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_split
|
||||||
|
lang = Rpl::Language.new
|
||||||
|
lang.run '"my string to sub" " " split'
|
||||||
|
|
||||||
|
assert_equal [{ value: '"my"', type: :string },
|
||||||
|
{ value: '"string"', type: :string },
|
||||||
|
{ value: '"to"', type: :string },
|
||||||
|
{ value: '"sub"', type: :string }],
|
||||||
|
lang.stack
|
||||||
|
|
||||||
|
lang = Rpl::Language.new
|
||||||
|
lang.run '"my,string,to sub" "," split'
|
||||||
|
|
||||||
|
assert_equal [{ value: '"my"', type: :string },
|
||||||
|
{ value: '"string"', type: :string },
|
||||||
|
{ value: '"to sub"', type: :string }],
|
||||||
|
lang.stack
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue