String 查找[]字节片中出现的所有字符串

String 查找[]字节片中出现的所有字符串,string,go,byte,String,Go,Byte,我想找到包含在字节数组中的所有字符串的索引 func findAllOccurrences(data []byte, searches []string) map[string][]int { var results map[string][]int for _, search := range searches { firstMatch = bytes.Index(data, []byte(search)) results[search] =

我想找到包含在字节数组中的所有字符串的索引

func findAllOccurrences(data []byte, searches []string) map[string][]int {
    var results map[string][]int

    for _, search := range searches {
        firstMatch = bytes.Index(data, []byte(search))
        results[search] = append(results[search], firstMatch)

        // How do I find subsequent the rest of the matches?
    }

    return results
}

查找第一个
Index()
非常简单,但是如何以惯用的方式查找所有索引而不消耗不必要的内存呢?

好的,下面是我评论中的解决方案,通过阅读
LastIndex
而不是第一个,不确定它是否有效,但这确实有效,您只需按相反的顺序获取索引,你可以在阅读的时候修正它

package main

import (
    "fmt"
    "bytes"
)

func main() {
    str1:= "foobarfoobarfoobarfoobarfoofoobar"
    arr := make([]string, 2)
    arr[0]="foo"
    arr[1]="bar"
    res:=findAllOccurrences([]byte(str1), arr)
    fmt.Println(res)
}


func findAllOccurrences(data []byte, searches []string) map[string][]int {
    results:= make(map[string][]int,0)

    for _, search := range searches {
    index := len(data)
    tmp:=data
    for true{
        match := bytes.LastIndex(tmp[0:index], []byte(search))
        if match==-1{
            break
        }else{
            index=match
            results[search]=append(results[search], match)
            }
        }
    }

    return results
}

希望这有帮助!:)

好的,下面是我的评论中的解决方案,通过阅读
LastIndex
,而不是首先阅读,不确定它是否有效,但这确实有效,您只需按相反的顺序获取索引,您可以在阅读时随时修复索引

package main

import (
    "fmt"
    "bytes"
)

func main() {
    str1:= "foobarfoobarfoobarfoobarfoofoobar"
    arr := make([]string, 2)
    arr[0]="foo"
    arr[1]="bar"
    res:=findAllOccurrences([]byte(str1), arr)
    fmt.Println(res)
}


func findAllOccurrences(data []byte, searches []string) map[string][]int {
    results:= make(map[string][]int,0)

    for _, search := range searches {
    index := len(data)
    tmp:=data
    for true{
        match := bytes.LastIndex(tmp[0:index], []byte(search))
        if match==-1{
            break
        }else{
            index=match
            results[search]=append(results[search], match)
            }
        }
    }

    return results
}
希望这有帮助!:)

如图所示,您可以为每次搜索将
数据
分配给另一个切片变量,然后在每次匹配后重新切片该变量。赋值仅复制长度、容量和指针。重新选择只会更改slice变量的长度和指针:它不会影响底层数组,也不是新的分配。我添加这个答案是为了澄清内存效率,并证明您仍然可以使用
字节。索引
,并且您可以在传统的for循环中使用它作为起点和增量:

package main

import (
    "bytes"
    "fmt"
)

func findAllOccurrences(data []byte, searches []string) map[string][]int {
    results := make(map[string][]int)
    for _, search := range searches {
        searchData := data
        term := []byte(search)
        for x, d := bytes.Index(searchData, term), 0; x > -1; x, d = bytes.Index(searchData, term), d+x+1 {
            results[search] = append(results[search], x+d)
            searchData = searchData[x+1 : len(searchData)]
        }
    }
    return results
}

func main() {
    fmt.Println(findAllOccurrences([]byte(`foo foo hey foo`), []string{`foo`, `hey`, ` `}))
}
印刷品

map[foo:[0 4 12] hey:[8]  :[3 7 11]]
如图所示,您可以为每次搜索将
数据
分配给另一个切片变量,然后在每次匹配后重新切片该变量。赋值仅复制长度、容量和指针。重新选择只会更改slice变量的长度和指针:它不会影响底层数组,也不是新的分配。我添加这个答案是为了澄清内存效率,并证明您仍然可以使用
字节。索引
,并且您可以在传统的for循环中使用它作为起点和增量:

package main

import (
    "bytes"
    "fmt"
)

func findAllOccurrences(data []byte, searches []string) map[string][]int {
    results := make(map[string][]int)
    for _, search := range searches {
        searchData := data
        term := []byte(search)
        for x, d := bytes.Index(searchData, term), 0; x > -1; x, d = bytes.Index(searchData, term), d+x+1 {
            results[search] = append(results[search], x+d)
            searchData = searchData[x+1 : len(searchData)]
        }
    }
    return results
}

func main() {
    fmt.Println(findAllOccurrences([]byte(`foo foo hey foo`), []string{`foo`, `hey`, ` `}))
}
印刷品

map[foo:[0 4 12] hey:[8]  :[3 7 11]]

我看到还有一种方法叫做
LastIndex
,您可以使用它,并在下一次迭代中不断减小字节数组的大小。它返回最后一个匹配的索引,因此您将始终拥有一个少一个的数组,并且您可以保留任何计数或匹配。我看到还有一个名为
LastIndex
的方法,您可以使用它,并在下一次迭代中不断减小字节数组的大小。它返回最后一个匹配的索引,因此您将始终拥有一个少一个的数组,并且您可以保留任何计数或匹配。