From 4b4c230d486beb09e9dd7cfef8a20d55ecaad599 Mon Sep 17 00:00:00 2001 From: Kroum Tzanev Date: Fri, 20 Dec 2024 13:52:43 +0000 Subject: [PATCH 1/3] faster and cleaner goodName function --- src/text/template/funcs.go | 6 +++--- src/text/template/template.go | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/text/template/funcs.go b/src/text/template/funcs.go index 7d63cf8b7bb6db..52c0f94607076d 100644 --- a/src/text/template/funcs.go +++ b/src/text/template/funcs.go @@ -130,10 +130,10 @@ func goodName(name string) bool { } for i, r := range name { switch { + case unicode.IsLetter(r): case r == '_': - case i == 0 && !unicode.IsLetter(r): - return false - case !unicode.IsLetter(r) && !unicode.IsDigit(r): + case i > 0 && unicode.IsDigit(r): + default: return false } } diff --git a/src/text/template/template.go b/src/text/template/template.go index 78067af2add4ae..52525e82bf5ae2 100644 --- a/src/text/template/template.go +++ b/src/text/template/template.go @@ -169,6 +169,8 @@ func (t *Template) Delims(left, right string) *Template { // It must be called before the template is parsed. // It panics if a value in the map is not a function with appropriate return // type or if the name cannot be used syntactically as a function in a template. +// A valid function name is a sequence of letters, digits, and underscores, +// starting with a letter or an underscore. // It is legal to overwrite elements of the map. The return value is the template, // so calls can be chained. func (t *Template) Funcs(funcMap FuncMap) *Template { From 7b43f98b99007166dee048ea83c5bbb8bca25211 Mon Sep 17 00:00:00 2001 From: Kroum Tzanev Date: Fri, 20 Dec 2024 14:08:25 +0000 Subject: [PATCH 2/3] fix indentation : spaces to tabs --- src/text/template/funcs.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/text/template/funcs.go b/src/text/template/funcs.go index 52c0f94607076d..fef8e33244e8eb 100644 --- a/src/text/template/funcs.go +++ b/src/text/template/funcs.go @@ -130,10 +130,10 @@ func goodName(name string) bool { } for i, r := range name { switch { - case unicode.IsLetter(r): + case unicode.IsLetter(r): case r == '_': case i > 0 && unicode.IsDigit(r): - default: + default: return false } } From b8e5aa3ecb9a0668835a8318c7440c03923e46c8 Mon Sep 17 00:00:00 2001 From: Kroum Tzanev Date: Sun, 22 Dec 2024 10:19:19 +0000 Subject: [PATCH 3/3] html/template: adds documentation about function names --- src/html/template/template.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/html/template/template.go b/src/html/template/template.go index 2440fecbf9eaa9..4ab40f1408d8eb 100644 --- a/src/html/template/template.go +++ b/src/html/template/template.go @@ -333,8 +333,11 @@ type FuncMap = template.FuncMap // Funcs adds the elements of the argument map to the template's function map. // It must be called before the template is parsed. // It panics if a value in the map is not a function with appropriate return -// type. However, it is legal to overwrite elements of the map. The return -// value is the template, so calls can be chained. +// type or if the name cannot be used syntactically as a function in a template. +// A valid function name is a sequence of letters, digits, and underscores, +// starting with a letter or an underscore. +// It is legal to overwrite elements of the map. The return value is the template, +// so calls can be chained. func (t *Template) Funcs(funcMap FuncMap) *Template { t.text.Funcs(template.FuncMap(funcMap)) return t