From a1a427422b5ac70d34c51e53a835e8d965a79e72 Mon Sep 17 00:00:00 2001 From: RUBET Louis Date: Wed, 31 May 2017 18:25:28 +0200 Subject: [PATCH] #107: new STRING keyword size --- README.md | 1 + src/program.cpp | 1 + src/rpn-string.hpp | 10 ++++++++++ test/07-string.txt | 15 +++++++++++++++ 4 files changed, 27 insertions(+) diff --git a/README.md b/README.md index a90ce5a..a30bf03 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ rpn> |str->| convert a string into an object |chr| convert ASCII character code in stack level 1 into a string |num| return ASCII code of the first character of the string in stack level 1 as a real number +|size| return the length of the string #### branch diff --git a/src/program.cpp b/src/program.cpp index e85f7de..7d6f593 100644 --- a/src/program.cpp +++ b/src/program.cpp @@ -94,6 +94,7 @@ program::keyword_t program::s_keywords[] = { cmd_keyword, "str->", &program::strout, "convert a string into an object" }, { cmd_keyword, "chr", &program::chr, "convert ASCII character code in stack level 1 into a string" }, { cmd_keyword, "num", &program::num, "return ASCII code of the first character of the string in stack level 1 as a real number" }, + { cmd_keyword, "size", &program::strsize, "return the length of the string" }, //BRANCH { cmd_undef, "", NULL, "\nBRANCH"}, diff --git a/src/rpn-string.hpp b/src/rpn-string.hpp index 86762e6..32a544b 100644 --- a/src/rpn-string.hpp +++ b/src/rpn-string.hpp @@ -70,3 +70,13 @@ void num() number* numb = (number*)_stack->allocate_back(number::calc_size(), cmd_number); numb->_value = the_chr; } + +void strsize() +{ + MIN_ARGUMENTS(1); + ARG_MUST_BE_OF_TYPE(0, cmd_string); + + double len = ((ostring*)_stack->pop_back())->_len; + number* numb = (number*)_stack->allocate_back(number::calc_size(), cmd_number); + numb->_value = len; +} diff --git a/test/07-string.txt b/test/07-string.txt index ef43650..b71edc2 100644 --- a/test/07-string.txt +++ b/test/07-string.txt @@ -142,3 +142,18 @@ erase "" num -> stack should be 0 erase + +# size (1) +"hello" size +-> stack should be 5 +erase + +# size (2) +"" size +-> stack should be 0 +erase + +# size, str->, ->str +"hello" str-> ->str size +-> stack should be 7 +erase