From de17302ad0e899750acec9be0fd384937be862b4 Mon Sep 17 00:00:00 2001 From: John Guo Date: Sun, 19 Dec 2021 21:04:35 +0800 Subject: [PATCH] fix issue in gstr.ReplaceI --- text/gstr/gstr_case.go | 2 +- text/gstr/gstr_replace.go | 21 +++-- text/gstr/gstr_z_unit_replace_test.go | 90 +++++++++++++++++++ ...unit_basic_test.go => gstr_z_unit_test.go} | 51 ----------- 4 files changed, 106 insertions(+), 58 deletions(-) create mode 100644 text/gstr/gstr_z_unit_replace_test.go rename text/gstr/{gstr_z_unit_basic_test.go => gstr_z_unit_test.go} (89%) diff --git a/text/gstr/gstr_case.go b/text/gstr/gstr_case.go index 0f70f27b9..c07fff726 100644 --- a/text/gstr/gstr_case.go +++ b/text/gstr/gstr_case.go @@ -69,7 +69,7 @@ func CaseSnakeFirstUpper(word string, underscore ...string) string { } for { - m := firstCamelCaseStart.FindAllStringSubmatch(word, 1) + m = firstCamelCaseStart.FindAllStringSubmatch(word, 1) if len(m) > 0 && m[0][1] != "" { w := strings.ToLower(m[0][1]) w = w[:len(w)-1] + replace + string(w[len(w)-1]) diff --git a/text/gstr/gstr_replace.go b/text/gstr/gstr_replace.go index fe6ade3b5..713fc02c7 100644 --- a/text/gstr/gstr_replace.go +++ b/text/gstr/gstr_replace.go @@ -7,8 +7,9 @@ package gstr import ( - "github.com/gogf/gf/v2/internal/utils" "strings" + + "github.com/gogf/gf/v2/internal/utils" ) // Replace returns a copy of the string `origin` @@ -32,13 +33,21 @@ func ReplaceI(origin, search, replace string, count ...int) string { return origin } var ( - length = len(search) - searchLower = strings.ToLower(search) + searchLength = len(search) + searchLower = strings.ToLower(search) + originLower string + pos int + diff = len(replace) - searchLength ) for { - originLower := strings.ToLower(origin) - if pos := strings.Index(originLower, searchLower); pos != -1 { - origin = origin[:pos] + replace + origin[pos+length:] + originLower = strings.ToLower(origin) + if pos = Pos(originLower, searchLower, pos); pos != -1 { + origin = origin[:pos] + replace + origin[pos+searchLength:] + if diff < 0 { + pos += -diff + } else { + pos += diff + 1 + } if n--; n == 0 { break } diff --git a/text/gstr/gstr_z_unit_replace_test.go b/text/gstr/gstr_z_unit_replace_test.go new file mode 100644 index 000000000..81e049f37 --- /dev/null +++ b/text/gstr/gstr_z_unit_replace_test.go @@ -0,0 +1,90 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +// go test *.go -bench=".*" + +package gstr_test + +import ( + "testing" + + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/test/gtest" + "github.com/gogf/gf/v2/text/gstr" +) + +func Test_Replace(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s1 := "abcdEFG乱入的中文abcdefg" + t.Assert(gstr.Replace(s1, "ab", "AB"), "ABcdEFG乱入的中文ABcdefg") + t.Assert(gstr.Replace(s1, "EF", "ef"), "abcdefG乱入的中文abcdefg") + t.Assert(gstr.Replace(s1, "MN", "mn"), s1) + + t.Assert(gstr.ReplaceByArray(s1, g.ArrayStr{ + "a", "A", + "A", "-", + "a", + }), "-bcdEFG乱入的中文-bcdefg") + + t.Assert(gstr.ReplaceByMap(s1, g.MapStrStr{ + "a": "A", + "G": "g", + }), "AbcdEFg乱入的中文Abcdefg") + }) +} + +func Test_ReplaceI_1(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s1 := "abcd乱入的中文ABCD" + s2 := "a" + t.Assert(gstr.ReplaceI(s1, "ab", "aa"), "aacd乱入的中文aaCD") + t.Assert(gstr.ReplaceI(s1, "ab", "aa", 0), "abcd乱入的中文ABCD") + t.Assert(gstr.ReplaceI(s1, "ab", "aa", 1), "aacd乱入的中文ABCD") + + t.Assert(gstr.ReplaceI(s1, "abcd", "-"), "-乱入的中文-") + t.Assert(gstr.ReplaceI(s1, "abcd", "-", 1), "-乱入的中文ABCD") + + t.Assert(gstr.ReplaceI(s1, "abcd乱入的", ""), "中文ABCD") + t.Assert(gstr.ReplaceI(s1, "ABCD乱入的", ""), "中文ABCD") + + t.Assert(gstr.ReplaceI(s2, "A", "-"), "-") + t.Assert(gstr.ReplaceI(s2, "a", "-"), "-") + + t.Assert(gstr.ReplaceIByArray(s1, g.ArrayStr{ + "abcd乱入的", "-", + "-", "=", + "a", + }), "=中文ABCD") + + t.Assert(gstr.ReplaceIByMap(s1, g.MapStrStr{ + "ab": "-", + "CD": "=", + }), "-=乱入的中文-=") + }) +} + +func Test_ReplaceI_2(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + t.Assert(gstr.ReplaceI("aaa", "A", "-a-"), `-a--a--a-`) + t.Assert(gstr.ReplaceI("aaaa", "AA", "-"), `--`) + t.Assert(gstr.ReplaceI("a a a", "A", "b"), `b b b`) + t.Assert(gstr.ReplaceI("aaaaaa", "aa", "a"), `aaa`) + t.Assert(gstr.ReplaceI("aaaaaa", "AA", "A"), `AAA`) + t.Assert(gstr.ReplaceI("aaa", "A", "AA"), `AAAAAA`) + t.Assert(gstr.ReplaceI("aaa", "A", "AA"), `AAAAAA`) + t.Assert(gstr.ReplaceI("a duration", "duration", "recordduration"), `a recordduration`) + }) + // With count parameter. + gtest.C(t, func(t *gtest.T) { + t.Assert(gstr.ReplaceI("aaaaaa", "aa", "a", 2), `aaaa`) + t.Assert(gstr.ReplaceI("aaaaaa", "AA", "A", 1), `Aaaaa`) + t.Assert(gstr.ReplaceI("aaaaaa", "AA", "A", 3), `AAA`) + t.Assert(gstr.ReplaceI("aaaaaa", "AA", "A", 4), `AAA`) + t.Assert(gstr.ReplaceI("aaa", "A", "AA", 2), `AAAAa`) + t.Assert(gstr.ReplaceI("aaa", "A", "AA", 3), `AAAAAA`) + t.Assert(gstr.ReplaceI("aaa", "A", "AA", 4), `AAAAAA`) + }) +} diff --git a/text/gstr/gstr_z_unit_basic_test.go b/text/gstr/gstr_z_unit_test.go similarity index 89% rename from text/gstr/gstr_z_unit_basic_test.go rename to text/gstr/gstr_z_unit_test.go index 19ffeedc9..5744d3ed0 100644 --- a/text/gstr/gstr_z_unit_basic_test.go +++ b/text/gstr/gstr_z_unit_test.go @@ -11,61 +11,10 @@ package gstr_test import ( "testing" - "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/text/gstr" ) -func Test_Replace(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - s1 := "abcdEFG乱入的中文abcdefg" - t.Assert(gstr.Replace(s1, "ab", "AB"), "ABcdEFG乱入的中文ABcdefg") - t.Assert(gstr.Replace(s1, "EF", "ef"), "abcdefG乱入的中文abcdefg") - t.Assert(gstr.Replace(s1, "MN", "mn"), s1) - - t.Assert(gstr.ReplaceByArray(s1, g.ArrayStr{ - "a", "A", - "A", "-", - "a", - }), "-bcdEFG乱入的中文-bcdefg") - - t.Assert(gstr.ReplaceByMap(s1, g.MapStrStr{ - "a": "A", - "G": "g", - }), "AbcdEFg乱入的中文Abcdefg") - }) -} - -func Test_ReplaceI_1(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - s1 := "abcd乱入的中文ABCD" - s2 := "a" - t.Assert(gstr.ReplaceI(s1, "ab", "aa"), "aacd乱入的中文aaCD") - t.Assert(gstr.ReplaceI(s1, "ab", "aa", 0), "abcd乱入的中文ABCD") - t.Assert(gstr.ReplaceI(s1, "ab", "aa", 1), "aacd乱入的中文ABCD") - - t.Assert(gstr.ReplaceI(s1, "abcd", "-"), "-乱入的中文-") - t.Assert(gstr.ReplaceI(s1, "abcd", "-", 1), "-乱入的中文ABCD") - - t.Assert(gstr.ReplaceI(s1, "abcd乱入的", ""), "中文ABCD") - t.Assert(gstr.ReplaceI(s1, "ABCD乱入的", ""), "中文ABCD") - - t.Assert(gstr.ReplaceI(s2, "A", "-"), "-") - t.Assert(gstr.ReplaceI(s2, "a", "-"), "-") - - t.Assert(gstr.ReplaceIByArray(s1, g.ArrayStr{ - "abcd乱入的", "-", - "-", "=", - "a", - }), "=中文ABCD") - - t.Assert(gstr.ReplaceIByMap(s1, g.MapStrStr{ - "ab": "-", - "CD": "=", - }), "-=乱入的中文-=") - }) -} - func Test_ToLower(t *testing.T) { gtest.C(t, func(t *gtest.T) { s1 := "abcdEFG乱入的中文abcdefg"