Backtestshugorun-20261024-162305#15332
tpl/strings: Add a strings.FirstLower function
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches implement the function identically (DecodeRuneInString + unicode.ToLower in helpers, a cast.ToStringE wrapper in the tpl namespace, and an AddMethodMapping entry), and the independent tests pass equally on both. The differences are in tests and scope. Patch B adds tests at both layers: a helpers test with more cases (including a multibyte leading rune in a longer word and an all-caps input) and a tpl/strings namespace test that follows the surrounding convention (t.Parallel, qt, template.HTML, []byte, int, and a non-stringer error case), which exercises the cast path the template function actually uses. Patch A tests only the helper. Patch B also adds the documentation page and docs.yaml entry that the issue's docs-site motivation implies. Patch A, by contrast, reformats every AddMethodMapping call in tpl/strings/init.go to put the method on its own line and rewrites the doc comments of ToLower/ToUpper, which is unrelated churn that obscures a tiny feature addition and makes the diff harder to review. On correctness they tie; on tests and scope B is clearly better.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
30m 14s
Wall-clock time in the container
Full test suite
No new failures
1,904 passed · 4 skipped · 1,936 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
102vs 146 merged
Changed lines, ours vs merged
The issue
View on GitHubThis would be symmetrical with the existing strings.FirstUpper function.
Current:
{{ $s := "Hello" }}
{{ print (strings.Substr $s 0 1 | strings.ToLower) (strings.Substr $s 1) }}
Desired:
{{ $s := "Hello" }}
{{ $s | strings.FirstLower }}
This proposal stems from some work on the docs site. In addition to the syntax improvement, this is about 2x faster.
The two patches
docs/content/en/functions/strings/FirstLower.mdnew file mode 100644index 000000000..cc37eba09@@ -0,0 +1,16 @@+---+title: strings.FirstLower+description: Returns the given string, converting the first character to lowercase.+categories: []+keywords: []+params:+ functions_and_methods:+ aliases: []+ returnType: string+ signatures: [strings.FirstLower STRING]+aliases: [/functions/strings.firstlower]+---++```go-html-template+{{ strings.FirstLower "Foo" }} → foo+```docs/data/docs.yamlindex 6d2578339..c011affe7 100644@@ -4433,6 +4433,14 @@ tpl: Examples: - - '{{ findRESubmatch `<a\s*href="(.+?)">(.+?)</a>` `<li><a href="#foo">Foo</a></li> <li><a href="#bar">Bar</a></li>` | print | safeHTML }}' - '[[<a href="#foo">Foo</a> #foo Foo] [<a href="#bar">Bar</a> #bar Bar]]'+ FirstLower:+ Aliases: null+ Args:+ - s+ Description: FirstLower converts s making the first character lower case.+ Examples:+ - - '{{ "Hugo rocks!" | strings.FirstLower }}'+ - hugo rocks! FirstUpper: Aliases: null Args:helpers/general.goindex 95d3fbcd5..7e61db1b0 100644@@ -57,6 +57,15 @@ func FirstUpper(s string) string { return string(unicode.ToUpper(r)) + s[n:] } +// FirstLower returns a string with the first character as lower case.+func FirstLower(s string) string {+ if s == "" {+ return ""+ }+ r, n := utf8.DecodeRuneInString(s)+ return string(unicode.ToLower(r)) + s[n:]+}+ // ReaderToBytes takes an io.Reader argument, reads from it // and returns bytes. func ReaderToBytes(lines io.Reader) []byte {helpers/general_test.goindex 42b1703bb..3f9fbf504 100644@@ -66,6 +66,26 @@ func TestFirstUpper(t *testing.T) { } } +func TestFirstLower(t *testing.T) {+ for i, this := range []struct {+ in string+ expect string+ }{+ {"Foo", "foo"},+ {"Foo Bar", "foo Bar"},+ {"foo bar", "foo bar"},+ {"HUGO", "hUGO"},+ {"", ""},+ {"Å", "å"},+ {"Ångström", "ångström"},+ } {+ result := helpers.FirstLower(this.in)+ if result != this.expect {+ t.Errorf("[%d] got %s but expected %s", i, result, this.expect)+ }+ }+}+ func TestHasStringsPrefix(t *testing.T) { for i, this := range []struct { s []stringtpl/strings/init.goindex d05133374..5365da773 100644@@ -101,6 +101,13 @@ func init() { }, ) + ns.AddMethodMapping(ctx.FirstLower,+ nil,+ [][2]string{+ {`{{ "Hugo rocks!" | strings.FirstLower }}`, `hugo rocks!`},+ },+ )+ ns.AddMethodMapping(ctx.FirstUpper, nil, [][2]string{tpl/strings/strings.goindex 24aff8588..dd574fd58 100644@@ -473,6 +473,16 @@ func (ns *Namespace) FirstUpper(s any) (string, error) { return helpers.FirstUpper(ss), nil } +// FirstLower converts s making the first character lower case.+func (ns *Namespace) FirstLower(s any) (string, error) {+ ss, err := cast.ToStringE(s)+ if err != nil {+ return "", err+ }++ return helpers.FirstLower(ss), nil+}+ // ToLower returns a copy of the input s with all Unicode letters mapped to their // lower case. func (ns *Namespace) ToLower(s any) (string, error) {tpl/strings/strings_test.goindex d628ca5d3..c7943eb8a 100644@@ -654,6 +654,38 @@ func TestTitle(t *testing.T) { } } +func TestFirstLower(t *testing.T) {+ t.Parallel()+ c := qt.New(t)++ for _, test := range []struct {+ s any+ expect any+ }{+ {"Hello", "hello"},+ {"Hugo Rocks", "hugo Rocks"},+ {"hello", "hello"},+ {"Åpen", "åpen"},+ {"", ""},+ {template.HTML("LoWeR"), "loWeR"},+ {[]byte("BYTES"), "bYTES"},+ {123, "123"},+ // errors+ {tstNoStringer{}, false},+ } {++ result, err := ns.FirstLower(test.s)++ if b, ok := test.expect.(bool); ok && !b {+ c.Assert(err, qt.Not(qt.IsNil))+ continue+ }++ c.Assert(err, qt.IsNil)+ c.Assert(result, qt.Equals, test.expect)+ }+}+ func TestToLower(t *testing.T) { t.Parallel() c := qt.New(t)
helpers/general.goindex 95d3fbcd5..cd3d01ca3 100644@@ -48,7 +48,16 @@ func TCPListen() (net.Listener, *net.TCPAddr, error) { return nil, nil, fmt.Errorf("unable to obtain a valid tcp port: %v", addr) } -// FirstUpper returns a string with the first character as upper case.+// FirstLower returns s with the first Unicode letter mapped to lowercase.+func FirstLower(s string) string {+ if s == "" {+ return ""+ }+ r, n := utf8.DecodeRuneInString(s)+ return string(unicode.ToLower(r)) + s[n:]+}++// FirstUpper returns s with the first Unicode letter mapped to uppercase. func FirstUpper(s string) string { if s == "" { return ""helpers/general_test.goindex 42b1703bb..492ad467f 100644@@ -66,6 +66,22 @@ func TestFirstUpper(t *testing.T) { } } +func TestFirstLower(t *testing.T) {+ c := qt.New(t)+ for _, this := range []struct {+ in string+ expect string+ }{+ {"Foo", "foo"},+ {"Foo Bar", "foo Bar"},+ {"foo bar", "foo bar"},+ {"", ""},+ {"Å", "å"},+ } {+ c.Assert(helpers.FirstLower(this.in), qt.Equals, this.expect)+ }+}+ func TestHasStringsPrefix(t *testing.T) { for i, this := range []struct { s []stringtpl/strings/init.goindex d05133374..a5daa5251 100644@@ -31,14 +31,16 @@ func init() { Context: func(cctx context.Context, args ...any) (any, error) { return ctx, nil }, } - ns.AddMethodMapping(ctx.Chomp,+ ns.AddMethodMapping(+ ctx.Chomp, []string{"chomp"}, [][2]string{ {`{{ chomp "<p>Blockhead</p>\n" | safeHTML }}`, `<p>Blockhead</p>`}, }, ) - ns.AddMethodMapping(ctx.Contains,+ ns.AddMethodMapping(+ ctx.Contains, nil, [][2]string{ {`{{ strings.Contains "abc" "b" }}`, `true`},@@ -46,7 +48,8 @@ func init() { }, ) - ns.AddMethodMapping(ctx.ContainsAny,+ ns.AddMethodMapping(+ ctx.ContainsAny, nil, [][2]string{ {`{{ strings.ContainsAny "abc" "bcd" }}`, `true`},@@ -54,34 +57,40 @@ func init() { }, ) - ns.AddMethodMapping(ctx.ContainsNonSpace,+ ns.AddMethodMapping(+ ctx.ContainsNonSpace, nil, [][2]string{}, ) - ns.AddMethodMapping(ctx.Count,+ ns.AddMethodMapping(+ ctx.Count, nil, [][2]string{ {`{{ "aabab" | strings.Count "a" }}`, `3`}, }, ) - ns.AddMethodMapping(ctx.CountRunes,+ ns.AddMethodMapping(+ ctx.CountRunes, []string{"countrunes"}, [][2]string{}, ) - ns.AddMethodMapping(ctx.CountWords,+ ns.AddMethodMapping(+ ctx.CountWords, []string{"countwords"}, [][2]string{}, ) - ns.AddMethodMapping(ctx.Diff,+ ns.AddMethodMapping(+ ctx.Diff, nil, [][2]string{}, ) - ns.AddMethodMapping(ctx.FindRE,+ ns.AddMethodMapping(+ ctx.FindRE, []string{"findRE"}, [][2]string{ {@@ -91,7 +100,8 @@ func init() { }, ) - ns.AddMethodMapping(ctx.FindRESubmatch,+ ns.AddMethodMapping(+ ctx.FindRESubmatch, []string{"findRESubmatch"}, [][2]string{ {@@ -101,14 +111,24 @@ func init() { }, ) - ns.AddMethodMapping(ctx.FirstUpper,+ ns.AddMethodMapping(+ ctx.FirstLower,+ nil,+ [][2]string{+ {`{{ "Hugo rocks!" | strings.FirstLower }}`, `hugo rocks!`},+ },+ )++ ns.AddMethodMapping(+ ctx.FirstUpper, nil, [][2]string{ {`{{ "hugo rocks!" | strings.FirstUpper }}`, `Hugo rocks!`}, }, ) - ns.AddMethodMapping(ctx.HasPrefix,+ ns.AddMethodMapping(+ ctx.HasPrefix, []string{"hasPrefix"}, [][2]string{ {`{{ hasPrefix "Hugo" "Hu" }}`, `true`},@@ -116,7 +136,8 @@ func init() { }, ) - ns.AddMethodMapping(ctx.HasSuffix,+ ns.AddMethodMapping(+ ctx.HasSuffix, []string{"hasSuffix"}, [][2]string{ {`{{ hasSuffix "Hugo" "go" }}`, `true`},@@ -124,14 +145,16 @@ func init() { }, ) - ns.AddMethodMapping(ctx.Repeat,+ ns.AddMethodMapping(+ ctx.Repeat, nil, [][2]string{ {`{{ "yo" | strings.Repeat 4 }}`, `yoyoyoyo`}, }, ) - ns.AddMethodMapping(ctx.Replace,+ ns.AddMethodMapping(+ ctx.Replace, []string{"replace"}, [][2]string{ {@@ -145,7 +168,8 @@ func init() { }, ) - ns.AddMethodMapping(ctx.ReplacePairs,+ ns.AddMethodMapping(+ ctx.ReplacePairs, nil, [][2]string{ {@@ -159,7 +183,8 @@ func init() { }, ) - ns.AddMethodMapping(ctx.ReplaceRE,+ ns.AddMethodMapping(+ ctx.ReplaceRE, []string{"replaceRE"}, [][2]string{ {@@ -173,12 +198,14 @@ func init() { }, ) - ns.AddMethodMapping(ctx.RuneCount,+ ns.AddMethodMapping(+ ctx.RuneCount, nil, [][2]string{}, ) - ns.AddMethodMapping(ctx.SliceString,+ ns.AddMethodMapping(+ ctx.SliceString, []string{"slicestr"}, [][2]string{ {`{{ slicestr "BatMan" 0 3 }}`, `Bat`},@@ -186,12 +213,14 @@ func init() { }, ) - ns.AddMethodMapping(ctx.Split,+ ns.AddMethodMapping(+ ctx.Split, []string{"split"}, [][2]string{}, ) - ns.AddMethodMapping(ctx.Substr,+ ns.AddMethodMapping(+ ctx.Substr, []string{"substr"}, [][2]string{ {`{{ substr "BatMan" 0 -3 }}`, `Bat`},@@ -199,7 +228,8 @@ func init() { }, ) - ns.AddMethodMapping(ctx.Title,+ ns.AddMethodMapping(+ ctx.Title, []string{"title"}, [][2]string{ {`{{ title "Bat man" }}`, `Bat Man`},@@ -207,35 +237,40 @@ func init() { }, ) - ns.AddMethodMapping(ctx.ToLower,+ ns.AddMethodMapping(+ ctx.ToLower, []string{"lower"}, [][2]string{ {`{{ lower "BatMan" }}`, `batman`}, }, ) - ns.AddMethodMapping(ctx.ToUpper,+ ns.AddMethodMapping(+ ctx.ToUpper, []string{"upper"}, [][2]string{ {`{{ upper "BatMan" }}`, `BATMAN`}, }, ) - ns.AddMethodMapping(ctx.Trim,+ ns.AddMethodMapping(+ ctx.Trim, []string{"trim"}, [][2]string{ {`{{ trim "++Batman--" "+-" }}`, `Batman`}, }, ) - ns.AddMethodMapping(ctx.TrimLeft,+ ns.AddMethodMapping(+ ctx.TrimLeft, nil, [][2]string{ {`{{ "aabbaa" | strings.TrimLeft "a" }}`, `bbaa`}, }, ) - ns.AddMethodMapping(ctx.TrimPrefix,+ ns.AddMethodMapping(+ ctx.TrimPrefix, nil, [][2]string{ {`{{ "aabbaa" | strings.TrimPrefix "a" }}`, `abbaa`},@@ -243,19 +278,22 @@ func init() { }, ) - ns.AddMethodMapping(ctx.TrimRight,+ ns.AddMethodMapping(+ ctx.TrimRight, nil, [][2]string{ {`{{ "aabbaa" | strings.TrimRight "a" }}`, `aabb`}, }, ) - ns.AddMethodMapping(ctx.TrimSpace,+ ns.AddMethodMapping(+ ctx.TrimSpace, nil, [][2]string{}, ) - ns.AddMethodMapping(ctx.TrimSuffix,+ ns.AddMethodMapping(+ ctx.TrimSuffix, nil, [][2]string{ {`{{ "aabbaa" | strings.TrimSuffix "a" }}`, `aabba`},@@ -263,7 +301,8 @@ func init() { }, ) - ns.AddMethodMapping(ctx.Truncate,+ ns.AddMethodMapping(+ ctx.Truncate, []string{"truncate"}, [][2]string{ {`{{ "this is a very long text" | truncate 10 " ..." }}`, `this is a ...`},tpl/strings/strings.goindex 24aff8588..9bbdac270 100644@@ -463,7 +463,17 @@ func (ns *Namespace) Title(s any) (string, error) { return ns.deps.Conf.CreateTitle(ss), nil } -// FirstUpper converts s making the first character upper case.+// FirstLower returns s with the first Unicode letter mapped to lowercase.+func (ns *Namespace) FirstLower(s any) (string, error) {+ ss, err := cast.ToStringE(s)+ if err != nil {+ return "", err+ }++ return helpers.FirstLower(ss), nil+}++// FirstUpper returns s with the first Unicode letter mapped to uppercase. func (ns *Namespace) FirstUpper(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil {@@ -473,8 +483,7 @@ func (ns *Namespace) FirstUpper(s any) (string, error) { return helpers.FirstUpper(ss), nil } -// ToLower returns a copy of the input s with all Unicode letters mapped to their-// lower case.+// ToLower returns s with all Unicode letters mapped to lowercase. func (ns *Namespace) ToLower(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil {@@ -484,8 +493,7 @@ func (ns *Namespace) ToLower(s any) (string, error) { return strings.ToLower(ss), nil } -// ToUpper returns a copy of the input s with all Unicode letters mapped to their-// upper case.+// ToUpper returns s with all Unicode letters mapped to uppercase. func (ns *Namespace) ToUpper(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil {