pespin has submitted this change. ( https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/36754?usp=email )
Change subject: Misc_Helpers.ttcn: Add some more string handling API helpers ......................................................................
Misc_Helpers.ttcn: Add some more string handling API helpers
These are currently used by code in AMI_Functions doing some workaround for TEXT decoder limitations.
They can be merged independently now since anyway they are totally generic and may be useful for future users.
Change-Id: I3d6da125a10807b7a2f3ecad8145a046a322c7d6 --- M library/Misc_Helpers.ttcn 1 file changed, 40 insertions(+), 0 deletions(-)
Approvals: laforge: Looks good to me, but someone else must approve Jenkins Builder: Verified osmith: Looks good to me, approved
diff --git a/library/Misc_Helpers.ttcn b/library/Misc_Helpers.ttcn index 805254a..a742b0b 100644 --- a/library/Misc_Helpers.ttcn +++ b/library/Misc_Helpers.ttcn @@ -79,6 +79,31 @@ return count; }
+/* Return true if str ends exactly with token: */ +function f_str_endswith(charstring str, charstring token) return boolean +{ + if (lengthof(str) < lengthof(token)) { + return false; + } + var charstring str_end := substr(str, lengthof(str) - lengthof(token), lengthof(token)); + return str_end == token; +} + +/* Replace all matches of token "find" with "repl" in "str" */ +function f_str_replace(charstring str, charstring find, charstring repl) return charstring +{ + var integer pos := f_strstr(str, find, 0); + if (pos < 0) { + return str; + } + var charstring prefix := substr(str, 0, pos); + var integer suffix_pos := pos + lengthof(find); + var charstring suffix := substr(str, suffix_pos, lengthof(str) - suffix_pos); + + var charstring new_str := prefix & repl & f_str_replace(suffix, find, repl); + return new_str; +} + type record of charstring ro_charstring; function f_str_split(charstring str, charstring delim := "\n") return ro_charstring {