Go · Searching and Counting

Searching and Counting

import "github.com/ashvardanian/stringzilla/golang" 6.0.0-rc.1 · 91a27bdd

These functions mirror the corresponding helpers in the standard strings package, but operate on raw bytes.

func Contains(str string, substr string) bool
func Index(str string, substr string) int64
func LastIndex(str string, substr string) int64
func IndexByte(str string, c byte) int64
func LastIndexByte(str string, c byte) int64
func IndexAny(str string, substr string) int64
func LastIndexAny(str string, substr string) int64
func Count(str string, substr string, overlap bool) int64

Index / LastIndex return the byte offset of the first / last match, or -1 if absent. IndexByte / LastIndexByte do the same for a single byte. IndexAny / LastIndexAny find the first / last byte that belongs to the byte set substr; note this is byte-set based, not Unicode-rune based like strings.IndexAny. Count returns the number of matches, with overlap selecting overlapping versus non-overlapping counting; an empty substr yields 1 + len(str).

haystack := "the quick brown fox"
sz.Contains(haystack, "brown")   // true
sz.Index(haystack, "brown")      // 10
sz.LastIndexByte(haystack, 'o')  // 17
sz.IndexAny(haystack, "aeiou")   // 2
sz.Count(haystack, "o", false)   // 2

API

Functions

ContainsContains reports whether substr is within str.
sz.go:15 ↗
func Contains(str string, substr string) bool

https://pkg.go.dev/strings#Contains

CountCount returns the number of overlapping or non-overlapping instances of substr in str.
sz.go:138 ↗
func Count(str string, substr string, overlap bool) int64

If substr is an empty string, returns 1 + the length of the str. https://pkg.go.dev/strings#Count

IndexIndex returns the index of the first instance of substr in str, or -1 if substr is not present.
sz.go:28 ↗
func Index(str string, substr string) int64

https://pkg.go.dev/strings#Index

IndexAnyIndexAny returns the index of the first instance of any byte from substr in str, or -1 if none are present.
sz.go:105 ↗
func IndexAny(str string, substr string) int64

Note: This is byte-set based (ASCII/bytes), not Unicode rune semantics like strings.IndexAny. https://pkg.go.dev/strings#IndexAny

IndexByteIndexByte returns the index of the first instance of a byte in str, or -1 if a byte is not present.
sz.go:66 ↗
func IndexByte(str string, c byte) int64

https://pkg.go.dev/strings#IndexByte

LastIndexLastIndex returns the index of the last instance of substr in str, or -1 if substr is not present.
sz.go:47 ↗
func LastIndex(str string, substr string) int64

https://pkg.go.dev/strings#LastIndex

LastIndexAnyLastIndexAny returns the index of the last instance of any byte from substr in str, or -1 if none are present.
sz.go:122 ↗
func LastIndexAny(str string, substr string) int64

Note: This is byte-set based (ASCII/bytes), not Unicode rune semantics like strings.LastIndexAny. https://pkg.go.dev/strings#LastIndexAny

LastIndexByteLastIndexByte returns the index of the last instance of a byte in str, or -1 if a byte is not present.
sz.go:81 ↗
func LastIndexByte(str string, c byte) int64

https://pkg.go.dev/strings#LastIndexByte

Utf8CountUtf8Count returns the number of Unicode codepoints in a UTF-8 string, SIMD-accelerated.
sz.go:52 ↗
func Utf8Count(str string) int

It counts non-continuation bytes, the ones not matching the 10xxxxxx pattern, which agrees with utf8.RuneCount on well-formed UTF-8 but can differ on malformed input: for example, a run of lone continuation bytes counts as zero here, while utf8.RuneCount counts one per byte.