summaryrefslogtreecommitdiffstats
path: root/builtin.c
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2013-11-30 00:44:14 -0600
committerNicolas Williams <nico@cryptonector.com>2013-12-04 18:21:41 -0600
commit346da34432f3b8087d2c100e910d68641b1b396f (patch)
treeaff96c8b3e76cec00a1fd76e6b21a9670536225d /builtin.c
parentfa316ac430221cc004bddf23eb5c96882e83a499 (diff)
Add ltrimstr and rtrimstr functions
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/builtin.c b/builtin.c
index 0bdc3320..e90dd2cf 100644
--- a/builtin.c
+++ b/builtin.c
@@ -108,6 +108,33 @@ static jv f_endswith(jv a, jv b) {
return ret;
}
+static jv f_ltrimstr(jv input, jv left) {
+ if (jv_get_kind(f_startswith(jv_copy(input), jv_copy(left))) != JV_KIND_TRUE) {
+ jv_free(left);
+ return input;
+ }
+ /*
+ * FIXME It'd be better to share the suffix with the original input --
+ * that we could do, we just can't share prefixes.
+ */
+ int prefixlen = jv_string_length_bytes(left);
+ jv res = jv_string_sized(jv_string_value(input) + prefixlen,
+ jv_string_length_bytes(jv_copy(input)) - prefixlen);
+ jv_free(input);
+ return res;
+}
+
+static jv f_rtrimstr(jv input, jv right) {
+ if (jv_get_kind(f_endswith(jv_copy(input), jv_copy(right))) == JV_KIND_TRUE) {
+ jv res = jv_string_sized(jv_string_value(input),
+ jv_string_length_bytes(jv_copy(input)) - jv_string_length_bytes(right));
+ jv_free(input);
+ return res;
+ }
+ jv_free(right);
+ return input;
+}
+
static jv f_minus(jv input, jv a, jv b) {
jv_free(input);
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
@@ -556,6 +583,8 @@ static const struct cfunction function_list[] = {
{(cfunction_ptr)f_keys, "keys", 1},
{(cfunction_ptr)f_startswith, "startswith", 2},
{(cfunction_ptr)f_endswith, "endswith", 2},
+ {(cfunction_ptr)f_ltrimstr, "ltrimstr", 2},
+ {(cfunction_ptr)f_rtrimstr, "rtrimstr", 2},
{(cfunction_ptr)jv_string_split, "split", 2},
{(cfunction_ptr)jv_string_explode, "explode", 1},
{(cfunction_ptr)jv_string_implode, "implode", 1},