Files
Go-http_framework/util/stringMatch/SundayMatch.go
T

46 lines
1.0 KiB
Go
Raw Normal View History

2021-03-02 13:33:07 +08:00
/*
* @Author : huangzj
* @Time : 2021/3/2 11:21
* @Description
*/
package stringMatch
2021-03-04 13:49:03 +08:00
func ShiftTable(mode string) []int {
2021-03-02 13:33:07 +08:00
shiftTable := make([]int, 256)
//一开始设置所有的字符匹配不上向右移动大小为:模式字符串长度 + 1(因为要移动到下一个字符的后一位)
for i := range shiftTable {
2021-03-04 13:49:03 +08:00
shiftTable[i] = len(mode) + 1
2021-03-02 13:33:07 +08:00
}
//设置模式字符串中每一个字符的移动长度.
2021-03-04 13:49:03 +08:00
for i := range mode {
shiftTable[mode[i]] = len(mode) - i
2021-03-02 13:33:07 +08:00
}
return shiftTable
}
func SundayMatch(allString, modeString string) int {
table := ShiftTable(modeString)
aLength := len(allString)
mLength := len(modeString)
i := 0 //这个参数记录的是移动过程中,,模式字符串对应原始字符串的第一个位置.
for i < aLength-mLength {
j := 0
for allString[i+j] == modeString[j] {
j++
if j >= len(modeString) {
return i
}
}
//找到移动后模式字符串对应原始字符串的第一个位置
move := allString[i+mLength]
2021-03-04 13:49:03 +08:00
i = i + table[move]
2021-03-02 13:33:07 +08:00
}
return -1
}