# Searching and Counting

> StringZilla for Go

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

```go
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)`.

```go
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
```

`import "github.com/ashvardanian/stringzilla/golang"`

## Contains

Contains reports whether `substr` is within `str`.

```go
func Contains(str string, substr string) bool
```

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

## Count

Count returns the number of overlapping or non-overlapping instances of `substr` in `str`.

```go
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

## Index

Index returns the index of the first instance of `substr` in `str`, or -1 if `substr` is not present.

```go
func Index(str string, substr string) int64
```

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

## IndexAny

IndexAny returns the index of the first instance of any byte from `substr` in `str`, or -1 if none are present.

```go
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

## IndexByte

IndexByte returns the index of the first instance of a byte in `str`, or -1 if a byte is not present.

```go
func IndexByte(str string, c byte) int64
```

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

## LastIndex

LastIndex returns the index of the last instance of `substr` in `str`, or -1 if `substr` is not present.

```go
func LastIndex(str string, substr string) int64
```

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

## LastIndexAny

LastIndexAny returns the index of the last instance of any byte from `substr` in `str`, or -1 if none are present.

```go
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

## LastIndexByte

LastIndexByte returns the index of the last instance of a byte in `str`, or -1 if a byte is not present.

```go
func LastIndexByte(str string, c byte) int64
```

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

## Utf8Count

Utf8Count returns the number of Unicode codepoints in a UTF-8 string, SIMD-accelerated.

```go
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.

