]> granicus.if.org Git - jq/commitdiff
Add ltrimstr and rtrimstr functions
authorNicolas Williams <nico@cryptonector.com>
Sat, 30 Nov 2013 06:44:14 +0000 (00:44 -0600)
committerNicolas Williams <nico@cryptonector.com>
Thu, 5 Dec 2013 00:21:41 +0000 (18:21 -0600)
builtin.c

index 0bdc3320d1ac10b68b600c420cdb5b912009827d..e90dd2cfdb3409a70af9bd2af894dab0264f010a 100644 (file)
--- 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},