Replace regexp based Chomp with builtin TrimRight

This commit is contained in:
bep 2015-01-20 12:41:08 +01:00
parent 99aee30410
commit 077d726b51
2 changed files with 41 additions and 44 deletions

View file

@ -29,7 +29,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"regexp"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -38,7 +37,6 @@ import (
var localTemplates *template.Template var localTemplates *template.Template
var tmpl Template var tmpl Template
var funcMap template.FuncMap var funcMap template.FuncMap
var chompRegexp *regexp.Regexp
type Template interface { type Template interface {
ExecuteTemplate(wr io.Writer, name string, data interface{}) error ExecuteTemplate(wr io.Writer, name string, data interface{}) error
@ -877,7 +875,7 @@ func Chomp(text interface{}) (string, error) {
return "", err return "", err
} }
return chompRegexp.ReplaceAllString(s, ""), nil return strings.TrimRight(s, "\r\n"), nil
} }
// Trim leading/trailing characters defined by b from a // Trim leading/trailing characters defined by b from a
@ -1245,44 +1243,43 @@ func (t *GoHtmlTemplate) LoadTemplates(absPath string) {
func init() { func init() {
funcMap = template.FuncMap{ funcMap = template.FuncMap{
"urlize": helpers.Urlize, "urlize": helpers.Urlize,
"sanitizeurl": helpers.SanitizeUrl, "sanitizeurl": helpers.SanitizeUrl,
"eq": Eq, "eq": Eq,
"ne": Ne, "ne": Ne,
"gt": Gt, "gt": Gt,
"ge": Ge, "ge": Ge,
"lt": Lt, "lt": Lt,
"le": Le, "le": Le,
"in": In, "in": In,
"intersect": Intersect, "intersect": Intersect,
"isset": IsSet, "isset": IsSet,
"echoParam": ReturnWhenSet, "echoParam": ReturnWhenSet,
"safeHtml": SafeHtml, "safeHtml": SafeHtml,
"safeCss": SafeCss, "safeCss": SafeCss,
"safeUrl": SafeUrl, "safeUrl": SafeUrl,
"markdownify": Markdownify, "markdownify": Markdownify,
"first": First, "first": First,
"where": Where, "where": Where,
"delimit": Delimit, "delimit": Delimit,
"sort": Sort, "sort": Sort,
"highlight": Highlight, "highlight": Highlight,
"add": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '+') }, "add": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '+') },
"sub": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '-') }, "sub": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '-') },
"div": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '/') }, "div": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '/') },
"mod": Mod, "mod": Mod,
"mul": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '*') }, "mul": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '*') },
"modBool": ModBool, "modBool": ModBool,
"lower": func(a string) string { return strings.ToLower(a) }, "lower": func(a string) string { return strings.ToLower(a) },
"upper": func(a string) string { return strings.ToUpper(a) }, "upper": func(a string) string { return strings.ToUpper(a) },
"title": func(a string) string { return strings.Title(a) }, "title": func(a string) string { return strings.Title(a) },
"partial": Partial, "partial": Partial,
"ref": Ref, "ref": Ref,
"relref": RelRef, "relref": RelRef,
"apply": Apply, "apply": Apply,
"chomp": Chomp, "chomp": Chomp,
"replace": Replace, "replace": Replace,
"trim": Trim, "trim": Trim,
} }
chompRegexp = regexp.MustCompile("[\r\n]+$")
} }

View file

@ -835,9 +835,9 @@ func TestMarkdownify(t *testing.T) {
func TestChomp(t *testing.T) { func TestChomp(t *testing.T) {
base := "\n This is\na story " base := "\n This is\na story "
for i, item := range []string{ for i, item := range []string{
"\n", "\n", "\n\n",
"\r", "\r", "\r\r",
"\r\n", "\r\n", "\r\n\r\n",
} { } {
chomped, _ := Chomp(base + item) chomped, _ := Chomp(base + item)