Add ltrim, rtrim, and trim

This commit is contained in:
Jonathan Turner 2009-07-23 17:35:39 +00:00
parent 4127a6ed41
commit 2f591a25a6

View File

@ -12,7 +12,7 @@
#define CODE_STRING(x, y) #x ", " #y #define CODE_STRING(x, y) #x ", " #y
#define chaiscript_prelude CODE_STRING(\ #define chaiscript_prelude CODE_STRING(\
def new(x) { var retval = clone(x); clear(x); x } \ def new(x) { var retval = clone(x); clear(retval); retval; } \
# to_string for Pair()\n\ # to_string for Pair()\n\
def to_string(x) : call_exists(first, x) && call_exists(second, x) { \ def to_string(x) : call_exists(first, x) && call_exists(second, x) { \
"<" + x.first.to_string() + ", " + x.second.to_string() + ">"; \ "<" + x.first.to_string() + ", " + x.second.to_string() + ">"; \
@ -48,7 +48,7 @@ def push_back(container, x) : call_exists(push_back_ref, container, x) { contain
def insert_at(container, pos, x) { container.insert_ref_at(pos, clone(x)); } \n\ def insert_at(container, pos, x) { container.insert_ref_at(pos, clone(x)); } \n\
# Returns the reverse of the given container\n\ # Returns the reverse of the given container\n\
def reverse(container) {\ def reverse(container) {\
var retval = Vector(); \ var retval = new(container); \
var r = retro(range(container)); \ var r = retro(range(container)); \
while (!r.empty()) { \ while (!r.empty()) { \
retval.push_back(r.front()); \ retval.push_back(r.front()); \
@ -75,9 +75,9 @@ def map(container, func, inserter) : call_exists(range, container) { \
range.pop_front(); \ range.pop_front(); \
} \ } \
} \ } \
# Performs the second value function over the container first value. Creates a new Vector with the results\n\ # Performs the second value function over the container first value. Creates a new container with the results\n\
def map(container, func) { \ def map(container, func) { \
var retval = Vector(); \ var retval = new(container); \
map(container, func, back_inserter(retval));\ map(container, func, back_inserter(retval));\
retval;\ retval;\
}\ }\
@ -95,7 +95,7 @@ def foldl(container, func, initial) : call_exists(range, container){ \
def sum(container) { foldl(container, `+`, 0.0) } \ def sum(container) { foldl(container, `+`, 0.0) } \
# Returns the product of the elements of the given value\n\ # Returns the product of the elements of the given value\n\
def product(container) { foldl(container, `*`, 1.0) } \ def product(container) { foldl(container, `*`, 1.0) } \
# Returns a new Vector with the elements of the first value concatenated with the elements of the second value\n\ # Returns a new container with the elements of the first value concatenated with the elements of the second value\n\
def concat(x, y) : call_exists(clone, x) { \ def concat(x, y) : call_exists(clone, x) { \
var retval = x; \ var retval = x; \
var len = y.size(); \ var len = y.size(); \
@ -115,9 +115,9 @@ def take(container, num, inserter) : call_exists(range, container) { \
--i; \ --i; \
} \ } \
} \ } \
# Returns a new Vector with the given number of elements taken from the container\n\ # Returns a new container with the given number of elements taken from the container\n\
def take(container, num) {\ def take(container, num) {\
var retval = Vector(); \ var retval = new(container); \
take(container, num, back_inserter(retval)); \ take(container, num, back_inserter(retval)); \
retval; \ retval; \
}\ }\
@ -128,9 +128,9 @@ def take_while(container, f, inserter) : call_exists(range, container) { \
r.pop_front(); \ r.pop_front(); \
} \ } \
} \ } \
# Returns a new Vector with the given elements match the second value function\n\ # Returns a new container with the given elements match the second value function\n\
def take_while(container, f) {\ def take_while(container, f) {\
var retval = Vector(); \ var retval = new(container); \
take_while(container, f, back_inserter(retval)); \ take_while(container, f, back_inserter(retval)); \
retval;\ retval;\
}\ }\
@ -146,9 +146,9 @@ def drop(container, num, inserter) : call_exists(range, container) { \
r.pop_front(); \ r.pop_front(); \
} \ } \
} \ } \
# Returns a new Vector with the given number of elements dropped from the given container \n\ # Returns a new container with the given number of elements dropped from the given container \n\
def drop(container, num) {\ def drop(container, num) {\
var retval = Vector(); \ var retval = new(container); \
drop(container, num, back_inserter(retval)); \ drop(container, num, back_inserter(retval)); \
retval; \ retval; \
}\ }\
@ -162,9 +162,9 @@ def drop_while(container, f, inserter) : call_exists(range, container) { \
r.pop_front(); \ r.pop_front(); \
} \ } \
} \ } \
# Returns a new Vector with the given elements dropped that match the second value function\n\ # Returns a new container with the given elements dropped that match the second value function\n\
def drop_while(container, f) {\ def drop_while(container, f) {\
var retval = Vector(); \ var retval = new(container); \
drop_while(container, f, back_inserter(retval)); \ drop_while(container, f, back_inserter(retval)); \
retval; \ retval; \
}\ }\
@ -207,7 +207,7 @@ def filter(container, f, inserter) : call_exists(range, container) { \
} \ } \
# Returns a new Vector which match the second value function\n\ # Returns a new Vector which match the second value function\n\
def filter(container, f) { \ def filter(container, f) { \
var retval = Vector(); \ var retval = new(container); \
filter(container, f, back_inserter(retval));\ filter(container, f, back_inserter(retval));\
retval;\ retval;\
}\ }\
@ -271,6 +271,14 @@ def find_first_not_of(str, list) { \
def find_last_not_of(str, list) { \ def find_last_not_of(str, list) { \
int(find_last_not_of(str, list, size_t(-1))); \ int(find_last_not_of(str, list, size_t(-1))); \
} \ } \
def ltrim(str) { \
drop_while(str, fun(x) { x == ' ' || x == '\t' }); \
} \
def rtrim(str) { \
reverse(drop_while(reverse(str), fun(x) { x == ' ' || x == '\t' })); \
} \
def trim(str) { \
ltrim(rtrim(str)); \
} \
) )
#endif /* CHAISCRIPT_PRELUDE_HPP_ */ #endif /* CHAISCRIPT_PRELUDE_HPP_ */