Skip to content

Convert a number or boolean argument with asString in four string functions - #144

Merged
zhengchun merged 1 commit into
antchfx:masterfrom
youdie006:scalar-string-args
Sep 27, 2026
Merged

zhengchun merged 1 commit into
antchfx:masterfrom
youdie006:scalar-string-args

Conversation

@youdie006

Copy link
Copy Markdown
Contributor

matches, normalize-space, substring and string-length switch on the first argument's type with only case string and case query, so a number or boolean falls through and the function works on "". translate, replace, lower-case and concat already convert through asString (func.go:290), which formats numbers and booleans the way string() does. This adds the same default to the four that were missing it.

                          before   after   (translate(12345,'2','x') = "1x345" in both)
string-length(12345)        0        5
string-length(true())       0        4
substring(12345, 2, 3)      ""       "234"
normalize-space(false())    ""       "false"
matches(123, "^123$")       false    true

In practice substring(sum(//price), 1, 3) or string-length(count(//item)) quietly return empty. One behaviour change to call out: matches(1, '^$') was true, because the pattern ran against "", and is now false.

This is the neighbour I mentioned at the end of #141.

Verification

Base 5dbec0f. New cases go into the existing Test_func_string_length, Test_func_substring, Test_func_matches and Test_func_normalize_space.

row func.go md5 failing
pristine fce2401a8e2c34dc9e76ec94f464dea0 all four
this PR 79a94deca5d3d6f1599f0b8fdc6f2442 none
fmt.Sprint(typ) instead of asString 87c3659419e6a37b831c39f507e672fc substring
without the matches default b773bbd55a438c876eeff698342bbf6a matches only
without the normalize-space default 458059f925b3e63bf67dbf3dff43cbfc normalize_space only
without the substring default bf06ca00efa58bdf901b666a255f3bf0 substring only
without the string-length default 7f63ef27c2c3279677ee70aca266df7f string_length only

Each partial row fails on its own function alone, so all four sites are needed. The fmt.Sprint row is why substring(1 div 0, 1, 3) is in the table: fmt.Sprint gives +Inf, asString gives Infinity.

testing.yml's go test ./... passes on go1.26.3. coverage.yml's go test -covermode atomic -coverprofile=coverage.out ./... passes on go1.16.15, the version it pins, at 85.3%. go vet ./... clean, gofmt -l empty for both files.

Written with AI assistance (Claude); the measurements above were run locally and I have reviewed the change.

…ctions

matches, normalize-space, substring and string-length switch only on string
and query, so a number or boolean first argument fell through with an empty
string. string-length(12345) returned 0, substring(12345, 2, 3) returned ""
and normalize-space(false()) returned "". translate, replace, lower-case and
concat already convert through asString, which formats numbers and booleans
the way string() does.
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 84.221% (+0.04%) from 84.179% — youdie006:scalar-string-args into antchfx:master

@zhengchun
zhengchun merged commit f7f8961 into antchfx:master Sep 27, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants