Backtestshugorun-20261007-020926#15332

Issue #15332 Merged fix PR #15333 Run 7 Oct 2026 · run-20261007-020926 gohugoio/hugo

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 the function the same correct way. Each uses utf8.DecodeRuneInString plus unicode.ToLower in helpers.FirstLower, wraps it in a Namespace.FirstLower that calls cast.ToStringE, and registers it via AddMethodMapping with an example. Correctness is therefore equivalent.

Tests: both add a helpers-level TestFirstLower, but B's coverage is stronger. B's helper test adds an all-caps case ("FOO" -> "fOO"), which confirms only the first rune changes. B also adds a namespace-level test in tpl/strings/strings_test.go covering template.HTML, []byte and a non-ASCII rune, plus an error case (tstNoStringer). That exercises the template-facing entry point the issue asks for. A tests only the helper.

Scope: Patch A reformats every AddMethodMapping call in tpl/strings/init.go, moving the first argument onto its own line. It also rewrites the doc comments on FirstUpper, ToLower and ToUpper. This is unrelated churn that buries the one meaningful addition in a large diff. Patch B is minimal in init.go and strings.go. It also adds a docs page and a docs.yaml entry, which is neutral-to-helpful for a user-facing template function.

Craft: B's example ('Hugo Rocks!' -> 'hugo Rocks!') shows that only the first character changes, which is more informative than A's example. B matches the surrounding code conventions without reformatting them.

Overall, B wins on tests and scope with equal correctness.

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

47m

Wall-clock time in the container

Full test suite

No new failures

3,766 passed · 101 skipped · 3,869 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

101vs 146 merged

Changed lines, ours vs merged

This 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+101 −0
docs/content/en/functions/strings/FirstLower.mdnew file mode 100644index 000000000..49319c471@@ -0,0 +1,17 @@+---+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]+---++{{< new-in 0.167.0 />}}++```go-html-template+{{ strings.FirstLower "Foo" }} → foo+```docs/data/docs.yamlindex 6d2578339..f228a0063 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..a377396ef 100644@@ -66,6 +66,25 @@ 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"},+		{"FOO", "fOO"},+		{"", ""},+		{"Å", "å"},+	} {+		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..a3d939697 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..7cf3eca0b 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 World", "hello World"},+		{"hello", "hello"},+		{"Ärger", "ärger"},+		{"", ""},+		{template.HTML("Hypertext"), "hypertext"},+		{[]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 {