1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00

'length' function now measures string length in codepoints, not bytes.

This commit is contained in:
Stephen Dolan
2013-05-15 00:37:38 +01:00
parent c496a924ce
commit e83e51eb56
9 changed files with 29 additions and 17 deletions

13
jv.c
View File

@@ -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));