diff --git a/debug.lsp b/debug.lsp index 166d5f0..3e2f9f6 100644 --- a/debug.lsp +++ b/debug.lsp @@ -5,6 +5,15 @@ (print (string-ltrim " abc")) (print (string-trim " abc ")) +(defun string-upcase (str) + (string-case str "upper")) + +(defun string-downcase (str) + (string-case str "lower")) + +(print (string-upcase "abcABCD")) +(print (string-downcase "abcABCD")) + ;; (print (sprintf "%.2f" (list 1.25))) ;; (print (sprintf "%.2f" '(1.23456))) diff --git a/ml.cpp b/ml.cpp index 1af8db5..e42e051 100644 --- a/ml.cpp +++ b/ml.cpp @@ -1564,6 +1564,16 @@ namespace builtin { return MlValue(result); } + // converts string to upper or lower case + MlValue string_case(std::vector args, MlEnvironment &env) { + eval_args(args, env); + + if (args.size() != 2) + throw MlError(MlValue("string_case", string_case), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + + return MlValue::string(string_lucase(args[0].as_string(), args[1].as_string())); + } + // trims characters " \n\r\t" from left or right or both ends of a string MlValue string_rltrim(std::vector args, MlEnvironment &env) { eval_args(args, env); @@ -1571,8 +1581,7 @@ namespace builtin { if (args.size() != 3) throw MlError(MlValue("string_rltrim", string_rltrim), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS); - // TODO validate - return MlValue::string(trim(args[0].as_string(), args[1].as_string(), args[2].as_string())); + return MlValue::string(string_trim(args[0].as_string(), args[1].as_string(), args[2].as_string())); } MlValue string_pad(std::vector args, MlEnvironment &env) { @@ -1857,6 +1866,7 @@ MlValue MlEnvironment::get(const std::string &name) const { if (name == "string-split") return MlValue("string-split", builtin::string_split); if (name == "string-pad") return MlValue("string-pad", builtin::string_pad); if (name == "string-rltrim") return MlValue("string-rltrim", builtin::string_rltrim); + if (name == "string-case") return MlValue("string-case", builtin::string_case); // Casting operations if (name == "int") return MlValue("int", builtin::cast_to_int); diff --git a/ml_string.cpp b/ml_string.cpp index ca0425d..ac7e9d6 100644 --- a/ml_string.cpp +++ b/ml_string.cpp @@ -43,7 +43,17 @@ std::vector regexp_strsplit(const std::string &string_to_split, con return elems; } -std::string trim(std::string s, const std::string &chars_to_trim, const std::string &rltrim) { +std::string string_lucase(std::string s, const std::string &strcase) { + if (strcase == "upper") + std::transform(s.begin(), s.end(),s.begin(), ::toupper); + + if (strcase == "lower") + std::transform(s.begin(), s.end(),s.begin(), ::tolower); + + return s; +} + +std::string string_trim(std::string s, const std::string &chars_to_trim, const std::string &rltrim) { if (rltrim == "ltrim" || rltrim == "trim") s.erase(0, s.find_first_not_of(chars_to_trim)); diff --git a/ml_string.h b/ml_string.h index 00c68ab..7710a7b 100644 --- a/ml_string.h +++ b/ml_string.h @@ -14,6 +14,8 @@ bool regexp_search(const std::string &where, const std::string ®ex_str); std::vector regexp_strsplit(const std::string &string_to_split, const std::string &rgx_str); -std::string trim(std::string s, const std::string &chars_to_trim, const std::string &rltrim); +std::string string_lucase(std::string s, const std::string &strcase); + +std::string string_trim(std::string s, const std::string &chars_to_trim, const std::string &rltrim); std::string string_padd(const std::string & str, int pad_len, char fill_char, bool from_right); \ No newline at end of file diff --git a/stdlib/stdlib.lsp b/stdlib/stdlib.lsp index eeb8069..617304e 100644 --- a/stdlib/stdlib.lsp +++ b/stdlib/stdlib.lsp @@ -24,16 +24,17 @@ (defun string-trim (str) (string-rltrim str " \n\r\t" "trim")) -; pad string on the end (defun string-rpad (str length pad_char) (string-pad str length pad_char "rpad")) - -; pad string on the begining (defun string-lpad (str length pad_char) (string-pad str length pad_char "lpad")) +(defun string-upcase (str) + (string-case str "upper")) +(defun string-downcase (str) + (string-case str "lower")) -; pause for interval +; pause for interval of seconds (defun sleep (time) (system-cmd (+ "sleep " (string time)))) diff --git a/tests/test.lsp b/tests/test.lsp index 19837f0..d261f2c 100644 --- a/tests/test.lsp +++ b/tests/test.lsp @@ -186,4 +186,9 @@ (print (get-env "HOME")) +(print (string-rtrim "abc ")) +(print (string-ltrim " abc")) +(print (string-trim " abc ")) + + (print "Test ends") \ No newline at end of file