Skip to content

Commit

Permalink
resolves bug where string rather than rune used for calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
boyter committed Nov 11, 2021
1 parent 1644d34 commit 3d9a30d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,11 @@ func IndexAllIgnoreCase(haystack string, needle string, limit int) [][]int {
for _, term := range searchTerms {
potentialMatches := IndexAll(haystack, term, -1)

haystackRune := []rune(haystack)
for _, match := range potentialMatches {
// We have a potential match, so now see if it actually matches
// by getting the actual value out of our haystack
if len(haystack) < match[0]+len(needle) {
if len(haystackRune) < match[0]+len(needleRune) {
continue
}

Expand All @@ -242,9 +243,11 @@ func IndexAllIgnoreCase(haystack string, needle string, limit int) [][]int {
for match[0]+e > len(haystack) {
e--
}

// Cut off the number at the end to the number we need which is the length of the needle runes
toMatch := []rune(haystack[match[0] : match[0]+e])[:len(needleRune)]


// what we need to do is iterate the runes of the haystack portion we are trying to
// match and confirm that the same rune position is a actual match or case fold match
// if they are keep looking, if they are not bail out as its not a real match
Expand Down
40 changes: 40 additions & 0 deletions index_bug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package str

import (
"strings"
"testing"
)

var broken = `list1=[0,1,2,3,4,5,6,7,8,9]#制作一个0-9的列表
list1.reverse()#reverse()函数直接对列表中的元素践行反向
print(list1)
# the following line is where it is breaking
list2=[str(i) for i in list1]#将列表中的每一个数字转换成字符串
print(list2)
str1="".join(list2)#通过join()函数,将列表中的单个字符串拼接成一整个字符串
print(str1)
str2=str1[2:8]#对字符串中的第三到第八字符进行切片
print(str2)
str3=str2[::-1]#通过右边第一个开始对整个字符串开始切片,以实现其翻转
print(str3)
i=int(str3)#int()函数试讲字符串转换为整数
print(i)#这里输出的结果虽然与print(str3)相同,但是性质是不同的
#转换成二进制、八进制、十六进制
print('转换成二进制:',bin(i),'转换成八进制:',oct(i), '转换成十六进制:',hex(i))
#二进制、八进制、十六进制这几个进制相互转换的时候,都要先转换为十进制int()`


func TestIndexAllUnicodeOffset(t *testing.T) {
lines := strings.Split(strings.Replace(broken, "\r\n", "\n", -1), "\n")

// this has an exception
for _, l := range lines {
IndexAllIgnoreCase(l, "list1=[0,1,2,3,4,5,6,7,8,9]#制作一个0", -1)
}
}

0 comments on commit 3d9a30d

Please sign in to comment.