Skip to content
Myr

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

7 files+144 added−0 removed

modified:docs/content/manual/manual.yml+19 added
1770 unmodified lines
1771 - program: '[.[]|rtrimstr("foo")]'1772 input: '["fo", "foo", "barfoo", "foobar", "foob"]'1773 output: ['["fo","","bar","foobar","foob"]']17741775 - title: "`trim`, `ltrim`, `rtrim`"1776 body: |17771778 `trim` trims both leading and trailing whitespace.17791780 `ltrim` trims only leading (left side) whitespace.17811782 `rtrim` trims only trailing (right side) whitespace.17831784 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.17881789 examples:1790 - program: 'trim, ltrim, rtrim'1791 input: '" abc "'1792 output: ['"abc"', '"abc "', '" abc"']17931794 - title: "`explode`"1795 body: |17961797 Converts an input string into an array of the string's
1985 unmodified lines
modified:src/builtin.c+55 added
1195 unmodified lines
1196static jv f_string_indexes(jq_state *jq, jv a, jv b) {1197 return jv_string_indexes(a, b);1198}11991200enum trim_op {1201 TRIM_LEFT = 1 << 0,1202 TRIM_RIGHT = 1 << 11203};12041205static 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 }12091210 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;12161217 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 }12251226 // 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 }12381239 // no new string needed if there is nothing to trim1240 if (trim_start == start && trim_end == end)1241 return a;12421243 jv ts = jv_string_sized(trim_start, trim_end - trim_start);1244 jv_free(a);1245 return ts;1246}12471248static jv f_string_trim(jq_state *jq, jv a) { return string_trim(a, TRIM_LEFT | TRIM_RIGHT); }1249static jv f_string_ltrim(jq_state *jq, jv a) { return string_trim(a, TRIM_LEFT); }1250static jv f_string_rtrim(jq_state *jq, jv a) { return string_trim(a, TRIM_RIGHT); }12511252static 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 lines
1772 {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 lines
modified:src/jv_unicode.c+18 added
116 unmodified lines
117 }118 assert(out - start == jvp_utf8_encode_length(codepoint));119 return out - start;120}121122// characters with White_Space property in:123// https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt124int 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 lines
8int jvp_utf8_decode_length(char startchar);910int jvp_utf8_encode_length(int codepoint);11int jvp_utf8_encode(int codepoint, char* out);1213int jvp_codepoint_is_whitespace(int c);14#endif
modified:tests/jq.test+20 added
1332 unmodified lines
1333[ index("aba"), rindex("aba"), indices("aba") ]1334"xababababax"1335[1,7,[1,3,5,7]]13361337# trim1338# \u000b is vertical tab (\v not supported by json)1339map(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"]13441345trim, 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"13501351try trim catch ., try ltrim catch ., try rtrim catch .13521231353"trim input must be a string"1354"trim input must be a string"1355"trim input must be a string"13561357indices(1)1358[0,1,1,2,3,4,1,5]1359[1,2,6]1360
811 unmodified lines
modified:tests/man.test+6 added
600 unmodified lines
601[.[]|rtrimstr("foo")]602["fo", "foo", "barfoo", "foobar", "foob"]603["fo","","bar","foobar","foob"]604605trim, ltrim, rtrim606" abc "607"abc"608"abc "609" abc"610611explode612"foobar"613[102,111,111,98,97,114]614
353 unmodified lines
modified:jq.1.prebuilt+24 added
1928 unmodified lines
1929.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.P1943Whitespace 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.nf19481949jq \'trim, ltrim, rtrim\'1950 " abc "1951=> "abc", "abc ", " abc"1952.1953.fi1954.1955.IP "" 01956.1957.SS "explode"1958Converts an input string into an array of the string\'s codepoint numbers\.1959.1960.IP "" 4
2263 unmodified lines