From 68c8a4e1787fe112dcb595e2f5bd3d4c3e31933d Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Wed, 15 Dec 2021 13:32:48 +0100 Subject: [PATCH] add string REV & SPLIT --- lib/core/string.rb | 22 ++++++++++++++++++++++ spec/language_string_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/lib/core/string.rb b/lib/core/string.rb index 9c685f2..a781c5f 100644 --- a/lib/core/string.rb +++ b/lib/core/string.rb @@ -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 diff --git a/spec/language_string_spec.rb b/spec/language_string_spec.rb index 8d38621..5e2bf75 100644 --- a/spec/language_string_spec.rb +++ b/spec/language_string_spec.rb @@ -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