Skip to content
Myr

Populate captures also for zero-width matches

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

Instead of just using {"captures":[]}. sub functions are use captures for replacement expressions. If we don't populate, captures for empty matches, the replacement expression is run with an empty object as input instead of an object containing the named captures with "" as value: * before: $ jq -n '"123foo456bar" | gsub("[^a-z]*(?<x>[a-z]*)"; "Z\(.x)")' "ZfooZbarZnull" * after: $ jq -n '"123foo456bar" | gsub("[^a-z]*(?<x>[a-z]*)"; "Z\(.x)")' "ZfooZbarZ" --- I also removed a redundant result = NULL; if (result) { ... }

authored by Emanuele Torre · committed by Nico Williams · parent bb0f898 · Browse files · 2 files+16 added−4 removed
This commit

State

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

Diff stats

2 files+16 added−4 removed

  • tests:+5 added−1 removed
  • other:+11 added−3 removed

2 files+16 added−4 removed

modified:src/builtin.c+11 added-3 removed
929 unmodified lines
930 }931 jv match = jv_object_set(jv_object(), jv_string("offset"), jv_number(idx));932 match = jv_object_set(match, jv_string("length"), jv_number(0));933 match = jv_object_set(match, jv_string("string"), jv_string(""));934 jv captures = jv_array();935 for (int i = 1; i < region->num_regs; ++i) {936 jv cap = jv_object();937 cap = jv_object_set(cap, jv_string("offset"), jv_number(idx));938 cap = jv_object_set(cap, jv_string("string"), jv_string(""));939 cap = jv_object_set(cap, jv_string("length"), jv_number(0));940 cap = jv_object_set(cap, jv_string("name"), jv_null());941 captures = jv_array_append(captures, cap);942 }943 onig_foreach_name(reg, f_match_name_iter, &captures);934 match = jv_object_set(match, jv_string("captures"), jv_array());944 match = jv_object_set(match, jv_string("captures"), captures);945 result = jv_array_append(result, match);946 // ensure '"qux" | match("(?=u)"; "g")' matches just once947 start = (const UChar*)(input_string+region->end[0]+1);948 continue;
65 unmodified lines
1014 }1015 } while (global && start <= end);1016 onig_region_free(region,1);1017 region = NULL;1008 if (region)1009 onig_region_free(region,1);1018 onig_free(reg);1019 jv_free(input);1020 jv_free(regex);1021 return result;
850 unmodified lines
modified:tests/onig.test+5 added-1 removed
1# match builtin2[match("( )*"; "g")]3"abc"4[{"offset":0, "length":0, "string":"", "captures":[]}, {"offset":1, "length":0, "string":"", "captures":[]}, {"offset":2, "length":0, "string":"", "captures":[]}, {"offset":3, "length":0, "string":"", "captures":[]}]4[{"offset":0,"length":0,"string":"","captures":[{"offset":0,"string":"","length":0,"name":null}]},{"offset":1,"length":0,"string":"","captures":[{"offset":1,"string":"","length":0,"name":null}]},{"offset":2,"length":0,"string":"","captures":[{"offset":2,"string":"","length":0,"name":null}]},{"offset":3,"length":0,"string":"","captures":[{"offset":3,"string":"","length":0,"name":null}]}]56[match("( )*"; "gn")]7"abc"8[]
135 unmodified lines
144gsub("\\b(?<x>.)"; "\(.x|ascii_downcase)")145"ABC DEF"146"aBC dEF"147148gsub("[^a-z]*(?<x>[a-z]*)"; "Z\(.x)")149"123foo456bar"150"ZfooZbarZ"151152# utf-8153sub("(?<x>.)"; "\(.x)!")154"’"155"’!"
18 unmodified lines