From 6da7f5ed5a224a00d1f39eeb0e5d92939a0f80aa Mon Sep 17 00:00:00 2001 From: opennota Date: Wed, 29 Aug 2018 09:06:49 +0700 Subject: [PATCH 1/2] Add a test for GetLead --- routers/routers_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 routers/routers_test.go diff --git a/routers/routers_test.go b/routers/routers_test.go new file mode 100644 index 0000000..7003b0e --- /dev/null +++ b/routers/routers_test.go @@ -0,0 +1,16 @@ +package routers + +import ( + "strings" + "testing" + "unicode/utf8" +) + +func TestGetLeadDoesNotSplitRunes(t *testing.T) { + lead := GetLead("@" + strings.Repeat("ф", 300)) + for _, r := range lead { + if r == utf8.RuneError { + t.Fatalf("want valid UTF-8, got %q", lead) + } + } +} From 6892067929733b93e24567bdc9ea95bdd62c2555 Mon Sep 17 00:00:00 2001 From: opennota Date: Wed, 29 Aug 2018 09:10:01 +0700 Subject: [PATCH 2/2] Fix GetLead sometimes returning invalid UTF-8 --- routers/routers.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/routers/routers.go b/routers/routers.go index c2ac8f3..cdd1b8b 100644 --- a/routers/routers.go +++ b/routers/routers.go @@ -10,6 +10,7 @@ import ( "strconv" "strings" "time" + "unicode/utf8" jwt "github.com/dgrijalva/jwt-go" humanize "github.com/dustin/go-humanize" @@ -167,12 +168,12 @@ func ToStr(value interface{}) string { if value == nil { return "" } - return fmt.Sprintf("%s", value) + return fmt.Sprint(value) } // ToDate convert time 2 date func ToDate(t time.Time) string { - return fmt.Sprintf("%s", humanize.Time(t)) + return fmt.Sprint(humanize.Time(t)) } // GetLead return first paragraph @@ -180,7 +181,7 @@ func GetLead(s string) string { if len(s) < 300 { return s + ".." } - delim := strings.IndexRune(s, '\n') + delim := strings.IndexByte(s, '\n') if delim > 300 || delim < 0 { l := len([]rune(s)) if l > 300 { @@ -188,6 +189,13 @@ func GetLead(s string) string { } delim = strings.LastIndexByte(s[:l], ' ') if delim < 0 { + for l > 0 { + r, size := utf8.DecodeLastRuneInString(s[:l]) + if r != utf8.RuneError { + break + } + l -= size + } delim = l } }