Add trim/0, ltrim/0 and rtrim/0 that trims leading and trailing whitespace (#3056)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Mattias Wadman · committed by GitHub · parent 81f4f88 · Browse files · 7 files·+144 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
7 files+144 added−0 removed
- tests:+26 added−0 removed
- other:+118 added−0 removed
Provenance
- Commit
be437ec- Tree
43b578a- Author
Mattias Wadman <mattias.wadman@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
81f4f88
7 files changed+144 added−0 removed
modified:docs/content/manual/manual.yml+19 added
1770 unmodified lines1771 - program: '[.[]|rtrimstr("foo")]'1772 input: '["fo", "foo", "barfoo", "foobar", "foob"]'1773 output: ['["fo","","bar","foobar","foob"]']1774 1775+ - title: "`trim`, `ltrim`, `rtrim`"1776+ body: |1777+1778+ `trim` trims both leading and trailing whitespace.1779+1780+ `ltrim` trims only leading (left side) whitespace.1781+1782+ `rtrim` trims only trailing (right side) whitespace.1783+1784+ Whitespace characters are the usual `" "`, `"\n"` `"\t"`, `"\r"`1785+ and also all characters in the Unicode character database with the1786+ whitespace property. Note that what considers whitespace might1787+ change in the future.1788+1789+ examples:1790+ - program: 'trim, ltrim, rtrim'1791+ input: '" abc "'1792+ output: ['"abc"', '"abc "', '" abc"']1793+1794 - title: "`explode`"1795 body: |1796 1797 Converts an input string into an array of the string's1985 unmodified linesmodified:src/builtin.c+55 added
1195 unmodified lines1196 static jv f_string_indexes(jq_state *jq, jv a, jv b) {1197 return jv_string_indexes(a, b);1198 }1199 1200+enum trim_op {1201+ TRIM_LEFT = 1 << 0,1202+ TRIM_RIGHT = 1 << 11203+};1204+1205+static jv string_trim(jv a, int op) {1206+ if (jv_get_kind(a) != JV_KIND_STRING) {1207+ return ret_error(a, jv_string("trim input must be a string"));1208+ }1209+1210+ int len = jv_string_length_bytes(jv_copy(a));1211+ const char *start = jv_string_value(a);1212+ const char *trim_start = start;1213+ const char *end = trim_start + len;1214+ const char *trim_end = end;1215+ int c;1216+1217+ if (op & TRIM_LEFT) {1218+ for (;;) {1219+ const char *ns = jvp_utf8_next(trim_start, end, &c);1220+ if (!ns || !jvp_codepoint_is_whitespace(c))1221+ break;1222+ trim_start = ns;1223+ }1224+ }1225+1226+ // make sure not empty string or start trim has trimmed everything1227+ if ((op & TRIM_RIGHT) && trim_end > trim_start) {1228+ for (;;) {1229+ const char *ns = jvp_utf8_backtrack(trim_end-1, trim_start, NULL);1230+ jvp_utf8_next(ns, trim_end, &c);1231+ if (!jvp_codepoint_is_whitespace(c))1232+ break;1233+ trim_end = ns;1234+ if (ns == trim_start)1235+ break;1236+ }1237+ }1238+1239+ // no new string needed if there is nothing to trim1240+ if (trim_start == start && trim_end == end)1241+ return a;1242+1243+ jv ts = jv_string_sized(trim_start, trim_end - trim_start);1244+ jv_free(a);1245+ return ts;1246+}1247+1248+static jv f_string_trim(jq_state *jq, jv a) { return string_trim(a, TRIM_LEFT | TRIM_RIGHT); }1249+static jv f_string_ltrim(jq_state *jq, jv a) { return string_trim(a, TRIM_LEFT); }1250+static jv f_string_rtrim(jq_state *jq, jv a) { return string_trim(a, TRIM_RIGHT); }1251+1252 static jv f_string_implode(jq_state *jq, jv a) {1253 if (jv_get_kind(a) != JV_KIND_ARRAY) {1254 return ret_error(a, jv_string("implode input must be an array"));1255 }516 unmodified lines1772 {f_string_split, "split", 2},1773 {f_string_explode, "explode", 1},1774 {f_string_implode, "implode", 1},1775 {f_string_indexes, "_strindices", 2},1776+ {f_string_trim, "trim", 1},1777+ {f_string_ltrim, "ltrim", 1},1778+ {f_string_rtrim, "rtrim", 1},1779 {f_setpath, "setpath", 3}, // FIXME typechecking1780 {f_getpath, "getpath", 2},1781 {f_delpaths, "delpaths", 2},1782 {f_has, "has", 2},119 unmodified linesmodified:src/jv_unicode.c+18 added
116 unmodified lines117 }118 assert(out - start == jvp_utf8_encode_length(codepoint));119 return out - start;120 }121+122+// characters with White_Space property in:123+// https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt124+int jvp_codepoint_is_whitespace(int c) {125+ return126+ (c >= 0x0009 && c <= 0x000D) || // <control-0009>..<control-000D>127+ c == 0x0020 || // SPACE128+ c == 0x0085 || // <control-0085>129+ c == 0x00A0 || // NO-BREAK SPACE130+ c == 0x1680 || // OGHAM SPACE MARK131+ (c >= 0x2000 && c <= 0x200A) || // EN QUAD..HAIR SPACE132+ c == 0x2028 || // LINE SEPARATOR133+ c == 0x2029 || // PARAGRAPH SEPARATOR134+ c == 0x202F || // NARROW NO-BREAK SPACE135+ c == 0x205F || // MEDIUM MATHEMATICAL SPACE136+ c == 0x3000 // IDEOGRAPHIC SPACE137+ ;138+}modified:src/jv_unicode.h+2 added
7 unmodified lines8 int jvp_utf8_decode_length(char startchar);9 10 int jvp_utf8_encode_length(int codepoint);11 int jvp_utf8_encode(int codepoint, char* out);12+13+int jvp_codepoint_is_whitespace(int c);14 #endifmodified:tests/jq.test+20 added
1332 unmodified lines1333 [ index("aba"), rindex("aba"), indices("aba") ]1334 "xababababax"1335 [1,7,[1,3,5,7]]1336 1337+# trim1338+# \u000b is vertical tab (\v not supported by json)1339+map(trim), map(ltrim), map(rtrim)1340+[" \n\t\r\f\u000b", ""," ", "a", " a ", "abc", " abc ", " abc", "abc "]1341+["", "", "", "a", "a", "abc", "abc", "abc", "abc"]1342+["", "", "", "a", "a ", "abc", "abc ", "abc", "abc "]1343+["", "", "", "a", " a", "abc", " abc", " abc", "abc"]1344+1345+trim, ltrim, rtrim1346+"\u0009\u000A\u000B\u000C\u000D\u0020\u0085\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000abc\u0009\u000A\u000B\u000C\u000D\u0020\u0085\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000"1347+"abc"1348+"abc\u0009\u000A\u000B\u000C\u000D\u0020\u0085\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000"1349+"\u0009\u000A\u000B\u000C\u000D\u0020\u0085\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000abc"1350+1351+try trim catch ., try ltrim catch ., try rtrim catch .1352+1231353+"trim input must be a string"1354+"trim input must be a string"1355+"trim input must be a string"1356+1357 indices(1)1358 [0,1,1,2,3,4,1,5]1359 [1,2,6]1360 811 unmodified linesmodified:tests/man.test+6 added
600 unmodified lines601 [.[]|rtrimstr("foo")]602 ["fo", "foo", "barfoo", "foobar", "foob"]603 ["fo","","bar","foobar","foob"]604 605+trim, ltrim, rtrim606+" abc "607+"abc"608+"abc "609+" abc"610+611 explode612 "foobar"613 [102,111,111,98,97,114]614 353 unmodified linesmodified:jq.1.prebuilt+24 added
1928 unmodified lines1929 .fi1930 .1931 .IP "" 01932 .1933+.SS "trim, ltrim, rtrim"1934+\fBtrim\fR trims both leading and trailing whitespace\.1935+.1936+.P1937+\fBltrim\fR trims only leading (left side) whitespace\.1938+.1939+.P1940+\fBrtrim\fR trims only trailing (right side) whitespace\.1941+.1942+.P1943+Whitespace characters are the usual \fB" "\fR, \fB"\en"\fR \fB"\et"\fR, \fB"\er"\fR and also all characters in the Unicode character database with the whitespace property\. Note that what considers whitespace might change in the future\.1944+.1945+.IP "" 41946+.1947+.nf1948+1949+jq \'trim, ltrim, rtrim\'1950+ " abc "1951+=> "abc", "abc ", " abc"1952+.1953+.fi1954+.1955+.IP "" 01956+.1957 .SS "explode"1958 Converts an input string into an array of the string\'s codepoint numbers\.1959 .1960 .IP "" 42263 unmodified lines