From 169457328db1c2788b01b79e7739851647a78274 Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Mon, 29 Sep 2025 23:15:43 +0800 Subject: [PATCH] Fix `rtrimstr("")` always output `""` (#3415) Example --- **Before this fix**: ```sh $ jq -n '"foo"|rtrimstr("")' "" ``` **After this fix**: ```sh $ jq -n '"foo"|rtrimstr("")' "foo" ``` --- src/builtin.jq | 2 +- tests/jq.test | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/builtin.jq b/src/builtin.jq index 40ef0f50..5804d43b 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -75,7 +75,7 @@ def todateiso8601: strftime("%Y-%m-%dT%H:%M:%SZ"); def fromdate: fromdateiso8601; def todate: todateiso8601; def ltrimstr($left): if startswith($left) then .[$left | length:] end; -def rtrimstr($right): if endswith($right) then .[:$right | -length] end; +def rtrimstr($right): if endswith($right) then .[:length - ($right | length)] end; def trimstr($val): ltrimstr($val) | rtrimstr($val); def match(re; mode): _match_impl(re; mode; false)|.[]; def match($val): ($val|type) as $vt | if $vt == "string" then match($val; null) diff --git a/tests/jq.test b/tests/jq.test index 6bfb6f85..14d98ba1 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -1512,6 +1512,18 @@ split("") ["fo", "foo", "barfoo", "foobarfoo", "foob"] ["fo","","bar","bar","b"] +[.[]|ltrimstr("")] +["a", "xx", ""] +["a", "xx", ""] + +[.[]|rtrimstr("")] +["a", "xx", ""] +["a", "xx", ""] + +[.[]|trimstr("")] +["a", "xx", ""] +["a", "xx", ""] + [(index(","), rindex(",")), indices(",")] "a,bc,def,ghij,klmno" [1,13,[1,4,8,13]]