diff options
author | João Valverde <j@v6e.pt> | 2023-06-24 14:54:03 +0100 |
---|---|---|
committer | João Valverde <j@v6e.pt> | 2023-06-26 00:46:18 +0000 |
commit | bd25b9f4cdce820dcb1951fd3c0acff6c5306d6b (patch) | |
tree | 516b2768d7d08e1e49cd94ff971e98da42ca3e5e /epan/ftypes/ftype-string.c | |
parent | bf9c4ff0fff01f46affa4c130cf9c4fce84c4bfa (diff) |
dfilter: Make string slices a return an FT_STRING
Allow string slices (indexing) to work with internationalized
strings. The old behavior of indexing on byte boundaries can
be obtained using raw slices.
Diffstat (limited to 'epan/ftypes/ftype-string.c')
-rw-r--r-- | epan/ftypes/ftype-string.c | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/epan/ftypes/ftype-string.c b/epan/ftypes/ftype-string.c index 195267f7aa..6f9eb1d970 100644 --- a/epan/ftypes/ftype-string.c +++ b/epan/ftypes/ftype-string.c @@ -127,17 +127,28 @@ string_is_zero(const fvalue_t *fv) static guint len(fvalue_t *fv) { - return (guint)fv->value.strbuf->len; + /* g_utf8_strlen returns glong for no apparent reason*/ + glong len = g_utf8_strlen(fv->value.strbuf->str, -1); + if (len < 0) + return 0; + return (guint)len; } static void -slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length) +slice(fvalue_t *fv, wmem_strbuf_t *buf, guint offset, guint length) { - guint8* data; - - data = (guint8*)fv->value.strbuf->str + offset; - - g_byte_array_append(bytes, data, length); + const char *str = fv->value.strbuf->str; + + /* Go to the starting offset */ + const char *p = g_utf8_offset_to_pointer(str, (glong)offset); + const char *n; + /* Copy 'length' codepoints to dst. Skip the terminating NULL */ + while (*p != '\0' && length-- > 0) { + n = g_utf8_next_char(p); + /* Append n - p bytes (one codepoint)*/ + wmem_strbuf_append_len(buf, p, n - p); + p = n; + } } static enum ft_result @@ -213,7 +224,7 @@ ftype_register_string(void) string_is_zero, /* is_zero */ NULL, /* is_negative */ len, - slice, + (FvalueSlice)slice, NULL, /* bitwise_and */ NULL, /* unary_minus */ NULL, /* add */ @@ -249,7 +260,7 @@ ftype_register_string(void) string_is_zero, /* is_zero */ NULL, /* is_negative */ len, - slice, + (FvalueSlice)slice, NULL, /* bitwise_and */ NULL, /* unary_minus */ NULL, /* add */ @@ -285,7 +296,7 @@ ftype_register_string(void) string_is_zero, /* is_zero */ NULL, /* is_negative */ len, - slice, + (FvalueSlice)slice, NULL, /* bitwise_and */ NULL, /* unary_minus */ NULL, /* add */ @@ -321,7 +332,7 @@ ftype_register_string(void) string_is_zero, /* is_zero */ NULL, /* is_negative */ len, - slice, + (FvalueSlice)slice, NULL, /* bitwise_and */ NULL, /* unary_minus */ NULL, /* add */ @@ -357,7 +368,7 @@ ftype_register_string(void) string_is_zero, /* is_zero */ NULL, /* is_negative */ len, - slice, + (FvalueSlice)slice, NULL, /* bitwise_and */ NULL, /* unary_minus */ NULL, /* add */ |