added string functions
This commit is contained in:
parent
337fb2f80d
commit
89dd5c6f07
|
|
@ -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)))
|
||||
|
|
|
|||
14
ml.cpp
14
ml.cpp
|
|
@ -1564,6 +1564,16 @@ namespace builtin {
|
|||
return MlValue(result);
|
||||
}
|
||||
|
||||
// converts string to upper or lower case
|
||||
MlValue string_case(std::vector<MlValue> 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<MlValue> 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<MlValue> 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);
|
||||
|
|
|
|||
|
|
@ -43,7 +43,17 @@ std::vector<std::string> 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));
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ bool regexp_search(const std::string &where, const std::string ®ex_str);
|
|||
|
||||
std::vector<std::string> 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);
|
||||
|
|
@ -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))))
|
||||
|
||||
|
|
|
|||
|
|
@ -186,4 +186,9 @@
|
|||
(print (get-env "HOME"))
|
||||
|
||||
|
||||
(print (string-rtrim "abc "))
|
||||
(print (string-ltrim " abc"))
|
||||
(print (string-trim " abc "))
|
||||
|
||||
|
||||
(print "Test ends")
|
||||
Loading…
Reference in New Issue