Backtestshugorun-20261009-153657#15332
tpl/strings: Add a strings.FirstLower function
Better
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches implement helpers.FirstLower identically: they decode the first UTF-8 rune, lowercase it, and append the rest. Both wrap it in Namespace.FirstLower with cast.ToStringE and register it in init.go with the same example. Functionally they are equivalent and both resolve the issue correctly.
Tests: Patch B tests more. It adds a helper-level test and also a template-namespace test (tpl/strings/strings_test.go). That second test covers template.HTML input, []byte input, Unicode ('Élan'), and the error path for a non-stringer. Patch A only adds the helper-level test.
Scope: Patch A reformats every AddMethodMapping call in init.go, putting the first argument on a new line, and rewrites the doc comments on ToLower, ToUpper and FirstUpper. This churn has nothing to do with the feature and makes the actual change harder to spot in review. Patch B's change is minimal and matches the surrounding conventions. It also adds docs (a FirstLower.md page and a docs.yaml entry), which is neutral-to-helpful.
Overall, B has stronger test coverage and a cleaner, focused diff.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
15m 53s
Wall-clock time in the container
Full test suite
No new failures
1,828 passed · 75 skipped · 1,903 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
99vs 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
Our patch+99 −0
docs/content/en/functions/strings/FirstLower.mdnew file mode 100644index 000000000..860a7f2d8@@ -0,0 +1,15 @@+---+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]+---++```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..a67290f54 100644@@ -48,6 +48,15 @@ func TCPListen() (net.Listener, *net.TCPAddr, error) { return nil, nil, fmt.Errorf("unable to obtain a valid tcp port: %v", addr) } +// 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:]+}+ // FirstUpper returns a string with the first character as upper case. func FirstUpper(s string) string { if s == "" {helpers/general_test.goindex 42b1703bb..ff8591099 100644@@ -48,6 +48,25 @@ func TestResolveMarkup(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"},+ {"", ""},+ {"Å", "å"},+ {"ÅB", "åB"},+ } {+ result := helpers.FirstLower(this.in)+ if result != this.expect {+ t.Errorf("[%d] got %s but expected %s", i, result, this.expect)+ }+ }+}+ func TestFirstUpper(t *testing.T) { for i, this := range []struct { in 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..9a2a600c9 100644@@ -463,6 +463,16 @@ func (ns *Namespace) Title(s any) (string, error) { return ns.deps.Conf.CreateTitle(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+}+ // FirstUpper converts s making the first character upper case. func (ns *Namespace) FirstUpper(s any) (string, error) { ss, err := cast.ToStringE(s)tpl/strings/strings_test.goindex d628ca5d3..3bcd0d397 100644@@ -654,6 +654,37 @@ 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"},+ {"HELLO", "hELLO"},+ {"hello", "hello"},+ {"", ""},+ {"Élan", "élan"},+ {template.HTML("LoWeR"), "loWeR"},+ {[]byte("BYTES"), "bYTES"},+ // 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)
Merged fix · PR #15333+109 −37
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 {