Impliment range find functions and add unit test for contains and find.
This commit is contained in:
@@ -92,14 +92,15 @@ def back_inserter(container) { \n\
|
|||||||
bind(push_back, container, _); \n\
|
bind(push_back, container, _); \n\
|
||||||
}\n\
|
}\n\
|
||||||
\n\
|
\n\
|
||||||
def contains(container, item) : call_exists(range, container) { \n\
|
def contains(container, item, compare_func) : call_exists(range, container) { \n\
|
||||||
var t_range = range(container); \n\
|
var t_range = range(container); \n\
|
||||||
while (!t_range.empty()) { \n\
|
while (!t_range.empty()) { \n\
|
||||||
if ( eq(t_range.front(), item) ) { return true; } \n\
|
if ( compare_func(t_range.front(), item) ) { return true; } \n\
|
||||||
t_range.pop_front(); \n\
|
t_range.pop_front(); \n\
|
||||||
} \n\
|
} \n\
|
||||||
return false; \n\
|
return false; \n\
|
||||||
} \n\
|
} \n\
|
||||||
|
def contains(container, item) { return contains(container, item, eq) } \n\
|
||||||
def map(container, func, inserter) : call_exists(range, container) { \n\
|
def map(container, func, inserter) : call_exists(range, container) { \n\
|
||||||
var range = range(container); \n\
|
var range = range(container); \n\
|
||||||
while (!range.empty()) { \n\
|
while (!range.empty()) { \n\
|
||||||
@@ -280,37 +281,49 @@ def zip(x, y) { \n\
|
|||||||
zip_with(collate, x, y); \n\
|
zip_with(collate, x, y); \n\
|
||||||
}\n\
|
}\n\
|
||||||
# Returns the position of the second value string in the first value string\n\
|
# Returns the position of the second value string in the first value string\n\
|
||||||
def find(str, substr) { \n\
|
def string::find(substr) : type_name(substr) == "string" { \n\
|
||||||
int(find(str, substr, size_t(0))); \n\
|
int(find(this, substr, size_t(0))); \n\
|
||||||
} \n\
|
} \n\
|
||||||
# Returns the position of last match of the second value string in the first value string\n\
|
# Returns the position of last match of the second value string in the first value string\n\
|
||||||
def rfind(str, substr) { \n\
|
def string::rfind(substr) : type_name(substr) == "string" { \n\
|
||||||
int(rfind(str, substr, size_t(-1))); \n\
|
int(rfind(this, substr, size_t(-1))); \n\
|
||||||
} \n\
|
} \n\
|
||||||
# Returns the position of the first match of elements in the second value string in the first value string\n\
|
# Returns the position of the first match of elements in the second value string in the first value string\n\
|
||||||
def find_first_of(str, list) { \n\
|
def string::find_first_of(list) : type_name(list) == "string" { \n\
|
||||||
int(find_first_of(str, list, size_t(0))); \n\
|
int(find_first_of(this, list, size_t(0))); \n\
|
||||||
} \n\
|
} \n\
|
||||||
# Returns the position of the last match of elements in the second value string in the first value string\n\
|
# Returns the position of the last match of elements in the second value string in the first value string\n\
|
||||||
def find_last_of(str, list) { \n\
|
def string::find_last_of(list) : type_name(list) == "string" { \n\
|
||||||
int(find_last_of(str, list, size_t(-1))); \n\
|
int(find_last_of(this, list, size_t(-1))); \n\
|
||||||
} \n\
|
} \n\
|
||||||
# Returns the position of the first non-matching element in the second value string in the first value string\n\
|
# Returns the position of the first non-matching element in the second value string in the first value string\n\
|
||||||
def find_first_not_of(str, list) { \n\
|
def string::find_first_not_of(list) : type_name(list) == "string" { \n\
|
||||||
int(find_first_not_of(str, list, size_t(0))); \n\
|
int(find_first_not_of(this, list, size_t(0))); \n\
|
||||||
} \n\
|
} \n\
|
||||||
# Returns the position of the last non-matching element in the second value string in the first value string\n\
|
# Returns the position of the last non-matching element in the second value string in the first value string\n\
|
||||||
def find_last_not_of(str, list) { \n\
|
def string::find_last_not_of(list) : type_name(list) == "string" { \n\
|
||||||
int(find_last_not_of(str, list, size_t(-1))); \n\
|
int(find_last_not_of(this, list, size_t(-1))); \n\
|
||||||
} \n\
|
} \n\
|
||||||
def ltrim(str) { \n\
|
def string::ltrim() { \n\
|
||||||
drop_while(str, fun(x) { x == ' ' || x == '\t' }); \n\
|
drop_while(this, fun(x) { x == ' ' || x == '\t' }); \n\
|
||||||
} \n\
|
} \n\
|
||||||
def rtrim(str) { \n\
|
def string::rtrim() { \n\
|
||||||
reverse(drop_while(reverse(str), fun(x) { x == ' ' || x == '\t' })); \n\
|
reverse(drop_while(reverse(this), fun(x) { x == ' ' || x == '\t' })); \n\
|
||||||
} \n\
|
} \n\
|
||||||
def trim(str) { \n\
|
def string::trim() { \n\
|
||||||
ltrim(rtrim(str)); \n\
|
ltrim(rtrim(this)); \n\
|
||||||
} \
|
} \n\
|
||||||
|
def find(container, value, compare_func) : call_exists(range, container) && type_name(compare_func) == "function" { \n\
|
||||||
|
var range = range(container); \n\
|
||||||
|
while (!range.empty()) { \n\
|
||||||
|
if (compare_func(range.front(), value)) { \n\
|
||||||
|
return range; \n\
|
||||||
|
} else { \n\
|
||||||
|
range.pop_front(); \n\
|
||||||
|
} \n\
|
||||||
|
} \n\
|
||||||
|
return range; \n\
|
||||||
|
} \n\
|
||||||
|
def find(container, value) { return find(container, value, eq) } \
|
||||||
)
|
)
|
||||||
#endif /* CHAISCRIPT_PRELUDE_HPP_ */
|
#endif /* CHAISCRIPT_PRELUDE_HPP_ */
|
||||||
|
5
unittests/range_contains.chai
Normal file
5
unittests/range_contains.chai
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
var v = [1,2,"hi", "world", 5.5]
|
||||||
|
print(v.contains(5.5));
|
||||||
|
print(v.contains(0));
|
||||||
|
print(v.contains(1, lt));
|
||||||
|
print(v.contains(2, `==`));
|
4
unittests/range_contains.txt
Normal file
4
unittests/range_contains.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
true
|
||||||
|
false
|
||||||
|
false
|
||||||
|
true
|
5
unittests/range_find.chai
Normal file
5
unittests/range_find.chai
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
var v = [2, 1, "Hi", 5.5]
|
||||||
|
var r = v.find("Hi");
|
||||||
|
print(r);
|
||||||
|
var r2 = v.find(2, `<`);
|
||||||
|
print(r2);
|
2
unittests/range_find.txt
Normal file
2
unittests/range_find.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
[Hi, 5.5]
|
||||||
|
[1, Hi, 5.5]
|
Reference in New Issue
Block a user