From 27af5a339a4d3c5712b5ed946a636a8c21916039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 21 Apr 2020 17:44:48 +0200 Subject: [PATCH] related: Fix toLower Don't change the slice. Fixes #7198 --- related/inverted_index.go | 7 +++++-- related/inverted_index_test.go | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/related/inverted_index.go b/related/inverted_index.go index fda6b9222..79dd4577c 100644 --- a/related/inverted_index.go +++ b/related/inverted_index.go @@ -274,9 +274,12 @@ func (cfg IndexConfig) ToKeywords(v interface{}) ([]Keyword, error) { keywords = append(keywords, StringKeyword(vv)) case []string: if toLower { - for i := 0; i < len(vv); i++ { - vv[i] = strings.ToLower(vv[i]) + vc := make([]string, len(vv)) + copy(vc, vv) + for i := 0; i < len(vc); i++ { + vc[i] = strings.ToLower(vc[i]) } + vv = vc } keywords = append(keywords, StringsToKeywords(vv...)...) case time.Time: diff --git a/related/inverted_index_test.go b/related/inverted_index_test.go index 57e722364..576928aea 100644 --- a/related/inverted_index_test.go +++ b/related/inverted_index_test.go @@ -201,6 +201,21 @@ func TestSearch(t *testing.T) { } +func TestToKeywordsToLower(t *testing.T) { + c := qt.New(t) + slice := []string{"A", "B", "C"} + config := IndexConfig{ToLower: true} + keywords, err := config.ToKeywords(slice) + c.Assert(err, qt.IsNil) + c.Assert(slice, qt.DeepEquals, []string{"A", "B", "C"}) + c.Assert(keywords, qt.DeepEquals, []Keyword{ + StringKeyword("a"), + StringKeyword("b"), + StringKeyword("c"), + }) + +} + func BenchmarkRelatedNewIndex(b *testing.B) { pages := make([]*testDoc, 100)