summaryrefslogtreecommitdiffstats
path: root/jv_aux.c
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2013-11-29 12:50:02 -0600
committerNicolas Williams <nico@cryptonector.com>2013-12-04 18:21:39 -0600
commit884e6c7d8bc2595e9baade62bc1cfbc77a8a9dd3 (patch)
treecbc40b0b27d0a6076cb27988f9952f8b89f115b6 /jv_aux.c
parent5aadaa79ebd43bfba8d28d1e1487393085b64245 (diff)
Add string slicing
Diffstat (limited to 'jv_aux.c')
-rw-r--r--jv_aux.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/jv_aux.c b/jv_aux.c
index 89f36e69..f3260c9d 100644
--- a/jv_aux.c
+++ b/jv_aux.c
@@ -3,15 +3,19 @@
#include <assert.h>
#include "jv_alloc.h"
-static int parse_slice(jv array, jv slice, int* pstart, int* pend) {
+static int parse_slice(jv j, jv slice, int* pstart, int* pend) {
// Array slices
- int len = jv_array_length(jv_copy(array));
jv start_jv = jv_object_get(jv_copy(slice), jv_string("start"));
jv end_jv = jv_object_get(slice, jv_string("end"));
if (jv_get_kind(start_jv) == JV_KIND_NULL) {
jv_free(start_jv);
start_jv = jv_number(0);
}
+ int len;
+ if (jv_get_kind(j) == JV_KIND_ARRAY)
+ len = jv_array_length(jv_copy(j));
+ else
+ len = jv_string_length_codepoints(jv_copy(j));
if (jv_get_kind(end_jv) == JV_KIND_NULL) {
jv_free(end_jv);
end_jv = jv_number(len);
@@ -61,6 +65,14 @@ jv jv_get(jv t, jv k) {
v = jv_invalid_with_msg(jv_string_fmt("Start and end indices of an array slice must be numbers"));
jv_free(t);
}
+ } else if (jv_get_kind(t) == JV_KIND_STRING && jv_get_kind(k) == JV_KIND_OBJECT) {
+ int start, end;
+ if (parse_slice(t, k, &start, &end)) {
+ v = jv_string_slice(t, start, end);
+ } else {
+ v = jv_invalid_with_msg(jv_string_fmt("Start and end indices of an string slice must be numbers"));
+ jv_free(t);
+ }
} else if (jv_get_kind(t) == JV_KIND_NULL &&
(jv_get_kind(k) == JV_KIND_STRING ||
jv_get_kind(k) == JV_KIND_NUMBER ||