Skip to content
Myr

utf8bytelength: count UTF8 string bytelength

Before adoption: this commit came in with the history the project adopted, so no landing records it.

[Builtin name changed, and it only works on string inputs. -Nico]

authored by Joel Nothman · committed by Nicolas Williams · parent 63dd033 · Browse files · 3 files+28 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+28 added−0 removed

  • tests:+9 added−0 removed
  • other:+19 added−0 removed

3 files+28 added−0 removed

modified:docs/content/3.manual/manual.yml+12 added
660 unmodified lines
661 - program: '.[] | length'662 input: '[[1,2], "string", {"a":2}, null]'663 output: [2, 6, 1, 0]664665666 - title: "`utf8bytelength`"667 body: |668669 The builtin function `utf8bytelength` outputs the number of670 bytes used to encode a string in UTF-8.671672 examples:673 - program: 'utf8bytelength'674 input: '"\u03bc"'675 output: [2]676677 - title: "`keys`, `keys_unsorted`"678 body: |679680 The builtin function `keys`, when given an object, returns
2267 unmodified lines
modified:src/builtin.c+7 added
365 unmodified lines
366 return jv_dump_string(input, 0);367 }368}369370static jv f_utf8bytelength(jq_state *jq, jv input) {371 if (jv_get_kind(input) != JV_KIND_STRING)372 return type_error(input, "only strings have UTF-8 byte length");373 return jv_number(jv_string_length_bytes(input));374}375376#define CHARS_ALPHANUM "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"377378static jv escape_string(jv input, const char* escapings) {379
898 unmodified lines
1278 {(cfunction_ptr)f_lesseq, "_lesseq", 3},1279 {(cfunction_ptr)f_greatereq, "_greatereq", 3},1280 {(cfunction_ptr)f_contains, "contains", 2},1281 {(cfunction_ptr)f_length, "length", 1},1282 {(cfunction_ptr)f_utf8bytelength, "utf8bytelength", 1},1283 {(cfunction_ptr)f_type, "type", 1},1284 {(cfunction_ptr)f_isinfinite, "isinfinite", 1},1285 {(cfunction_ptr)f_isnan, "isnan", 1},1286 {(cfunction_ptr)f_isnormal, "isnormal", 1},
422 unmodified lines
modified:tests/jq.test+9 added
523 unmodified lines
524[.[] | length]525[[], {}, [1,2], {"a":42}, "asdf", "\u03bc"]526[0, 0, 2, 1, 4, 1]527528utf8bytelength529"asdf\u03bc"5306531532[.[] | try utf8bytelength catch .]533[[], {}, [1,2], 55, true, false]534["array ([]) only strings have UTF-8 byte length","object ({}) only strings have UTF-8 byte length","array ([1,2]) only strings have UTF-8 byte length","number (55) only strings have UTF-8 byte length","boolean (true) only strings have UTF-8 byte length","boolean (false) only strings have UTF-8 byte length"]535536537map(keys)538[{}, {"abcd":1,"abc":2,"abcde":3}, {"x":1, "z": 3, "y":2}]539[[], ["abc","abcd","abcde"], ["x","y","z"]]540
736 unmodified lines