summaryrefslogtreecommitdiffstats
path: root/jv.c
diff options
context:
space:
mode:
authorStephen Dolan <mu@netsoc.tcd.ie>2013-05-15 00:37:38 +0100
committerStephen Dolan <mu@netsoc.tcd.ie>2013-05-15 00:37:38 +0100
commite83e51eb56a1de6e627d346f027d3ceb09ae3807 (patch)
treeee9a376e0f88ab654fec370e972a9930d3bcdd9d /jv.c
parentc496a924ce71317aa5560da0cbf5a4524bb8c226 (diff)
'length' function now measures string length in codepoints, not bytes.
Diffstat (limited to 'jv.c')
-rw-r--r--jv.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/jv.c b/jv.c
index b03c024c..9316aec1 100644
--- a/jv.c
+++ b/jv.c
@@ -8,6 +8,7 @@
#include "jv_alloc.h"
#include "jv.h"
+#include "jv_unicode.h"
/*
* Internal refcounting helpers
@@ -530,13 +531,23 @@ jv jv_string(const char* str) {
return jv_string_sized(str, strlen(str));
}
-int jv_string_length(jv j) {
+int jv_string_length_bytes(jv j) {
assert(jv_get_kind(j) == JV_KIND_STRING);
int r = jvp_string_length(jvp_string_ptr(&j.val.nontrivial));
jv_free(j);
return r;
}
+int jv_string_length_codepoints(jv j) {
+ assert(jv_get_kind(j) == JV_KIND_STRING);
+ const char* i = jv_string_value(j);
+ const char* end = i + jv_string_length_bytes(jv_copy(j));
+ int c = 0, len = 0;
+ while ((i = jvp_utf8_next(i, end, &c))) len++;
+ jv_free(j);
+ return len;
+}
+
uint32_t jv_string_hash(jv j) {
assert(jv_get_kind(j) == JV_KIND_STRING);
uint32_t hash = jvp_string_hash(jvp_string_ptr(&j.val.nontrivial));