string-cmp and string-cmp-ic added

This commit is contained in:
vaclavt
2022-03-13 10:32:43 +01:00
parent 65abc2fd07
commit cd9822fcf2
7 changed files with 26 additions and 2 deletions

View File

@@ -89,7 +89,6 @@ utils/local_install.sh
#### Language
- string functions
- compare - needed for sorting, cmp ignore case
- regexp match, regexp tokens
#### Performance

View File

@@ -134,6 +134,8 @@
|`(string-len str)`|Returns string length|`>>> (string-len "abcdef") => 6`|String manipulation|
|`(string-substr str pos len)`|Returns substring from str starting at pos with len. If pos is negative returns substring from the end of string. First character is on pos 0|`>>> (string-substr "ABCD" -2 2) => "CD"`|String manipulation|
|`(string-find str lookup pos)`|Returns position of lookup in str starting on position. First char index is 0. If not found returns nil|`>>> (string-find " long long int;" "long" 2) => 6`|String manipulation|
|`(string-cmp str1 str2)`|Compares two strings. Returns 0 where strangs are equal; < 0 when the value of the first character that does not match is lower in the compared string, or all compared characters match but the compared string is shorter.; >0 when the value of the first character that does not match is greater in the compared string, or all compared characters match but the compared string is longer.|`>>> (string-cmp "aaa" "xaa") => -23`|String manipulation|
|`(string-cmp-ic str1 str2)`|Compares two strings ignoring case. Returns 0, < 0, > 0.|`>>> (string-cmp-ic "aaa" "AaA") => 0`|String manipulation|
|`(int value)`|Cast an item to an int|`>>> (int 3.41) => 3`|Type casting|
|`(float value)`|Cast item to a float|`>>> (int 3.41) => 3.14`|Type casting|
|`(string value)`|Cast int or float item to a string|`>>> (string 3.14) => "3.14"`|Type casting|

18
ml.cpp
View File

@@ -184,6 +184,10 @@ std::vector<std::string> MlValue::get_used_atoms() {
}
}
bool MlValue::is_nil() const {
return type == NIL;
}
bool MlValue::is_builtin() const {
return type == BUILTIN;
}
@@ -1857,6 +1861,19 @@ MlValue string_find(std::vector<MlValue> args, MlEnvironment &env) {
return pos == -1 ? MlValue::nil() : MlValue((long) pos);
}
MlValue string_cmp(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 2)
throw MlError(MlValue("string-cmp", string_cmp), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
if ( (args[0].is_nil() || (args[0].is_list() && args[0].as_list().empty())) ||
(args[1].is_nil() || (args[1].is_list() && args[1].as_list().empty())) )
return MlValue::nil();
return MlValue((long)args[0].as_string().compare(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) {
@@ -2240,6 +2257,7 @@ std::map<const std::string, Builtin> builtin_funcs
std::make_pair("string-len", builtin::string_len),
std::make_pair("string-substr", builtin::string_substr),
std::make_pair("string-find", builtin::string_find),
std::make_pair("string-cmp", builtin::string_cmp),
// Casting operations
std::make_pair("int", builtin::cast_to_int),

1
ml.h
View File

@@ -141,6 +141,7 @@ public:
// Evaluate this value as lisp code.
MlValue eval(MlEnvironment &env);
bool is_nil() const;
bool is_builtin() const;
bool is_macro() const;
bool is_number() const;

View File

@@ -26,6 +26,8 @@
(defn string-downcase (str)
(string-case str "lower"))
(defn string-cmp-ic (a b)
(string-cmp (string-upcase a) (string-upcase b)))
(defn string-join (lst sep)
(do

View File

@@ -101,6 +101,8 @@
(ut::define-test "result of (string-find \" long long int;\" \" \")" '(ut::assert-equal 0 (string-find " long long int;" " ")))
(ut::define-test "result of (string-find \" long long int;\" \"o\")" '(ut::assert-equal 2 (string-find " long long int;" "o")))
(ut::define-test "result of (string-find \" long long int;\" \"float\")" '(ut::assert-nil (string-find " long long int;" "float")))
(ut::define-test "result of (string-cmp \"aaa\" \"aaa\")" '(ut::assert-equal 0 (string-cmp "aaa" "aaa")))
(ut::define-test "result of (string-cmp-ic \"aaa\" \"AaA\")" '(ut::assert-equal 0 (string-cmp-ic "aaa" "AaA")))
(ut::define-test "result of (is-pos? -1)" '(ut::assert-false (is-pos? -1)))
(ut::define-test "result of (is-neg? -1)" '(ut::assert-true (is-neg? -1)))