tried WordsArray支持中文, 并且单元测试通过
This commit is contained in:
parent
ddef34cf20
commit
edecf0453e
|
@ -46,8 +46,10 @@ func (tried *Tried) Put(words string, values ...interface{}) {
|
|||
cur := tried.root
|
||||
var n *Node
|
||||
|
||||
for i := 0; i < len(words); i++ {
|
||||
w := tried.wiStore.Byte2Index(words[i])
|
||||
bytes := []byte(words)
|
||||
|
||||
for i := 0; i < len(bytes); i++ {
|
||||
w := tried.wiStore.Byte2Index(bytes[i])
|
||||
|
||||
if cur.data == nil {
|
||||
cur.data = make([]*Node, tried.wiStore.DataSize)
|
||||
|
@ -121,12 +123,12 @@ func (tried *Tried) Traversal(every func(cidx uint, value interface{}) bool) {
|
|||
func (tried *Tried) WordsArray() []string {
|
||||
var result []string
|
||||
|
||||
var traversal func([]rune, *Node)
|
||||
traversal = func(prefix []rune, cur *Node) {
|
||||
var traversal func([]byte, *Node)
|
||||
traversal = func(prefix []byte, cur *Node) {
|
||||
|
||||
for i, n := range cur.data {
|
||||
if n != nil {
|
||||
nextPrefix := append(prefix, rune(tried.wiStore.Index2Byte(uint(i))))
|
||||
nextPrefix := append(prefix, tried.wiStore.Index2Byte(uint(i)))
|
||||
traversal(nextPrefix, n)
|
||||
if n.value != nil {
|
||||
result = append(result, string(nextPrefix))
|
||||
|
@ -137,7 +139,7 @@ func (tried *Tried) WordsArray() []string {
|
|||
}
|
||||
|
||||
if tried.root != nil {
|
||||
traversal([]rune{}, tried.root)
|
||||
traversal([]byte{}, tried.root)
|
||||
}
|
||||
|
||||
return result
|
||||
|
|
|
@ -133,7 +133,7 @@ func indexWordUpperLowerDigital(w uint) byte {
|
|||
return byte(w) + 'a'
|
||||
}
|
||||
|
||||
// wordIndex256 all byte 不支持中文
|
||||
// wordIndex256 all byte 支持中文
|
||||
func wordIndex256(w byte) uint {
|
||||
return uint(w)
|
||||
}
|
||||
|
|
|
@ -84,7 +84,7 @@ func TestTried_String(t *testing.T) {
|
|||
wordsList = append(wordsList, []string{"azAZ09", "aRGFDSFDSzAasdZ06789", "A28374JHFudfsu09qwzzdsw874FDSAZfer"})
|
||||
|
||||
triedList = append(triedList, NewWithWordType(WordIndex256))
|
||||
wordsList = append(wordsList, []string{"21`3tcdbxcfhyop8901zc[]\\'/?()#$%^&**!\x01 09-213", "!@$*#))(#*", `\/213dsfsdf`})
|
||||
wordsList = append(wordsList, []string{"21`3tcdbxcf囉hyop打算8901zc[]\\'/?()#$%^&**!\x01 09-213", "的支持中文", "!@$*#)中文)(#*", `\/213dsfsdf`})
|
||||
|
||||
triedList = append(triedList, NewWithWordType(WordIndex32to126))
|
||||
wordsList = append(wordsList, []string{" 21`3tcdbxcfhyop8901zc[]\\'/?()#$%^&**! ", "AZaz09~ dys!@#$)(*^$#", "<>.,?/"})
|
||||
|
|
Loading…
Reference in New Issue
Block a user