add string REV & SPLIT

This commit is contained in:
Gwenhael Le Moine 2021-12-15 13:32:48 +01:00
parent a341cdf570
commit 68c8a4e178
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
2 changed files with 49 additions and 0 deletions

View file

@ -78,6 +78,28 @@ module Rpl
[stack, dictionary]
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

View file

@ -69,4 +69,31 @@ class TestLanguageString < Test::Unit::TestCase
assert_equal [{ value: 'str', type: :string }],
lang.stack
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