| 1 |
// Copyright 2023 Charlie Vieth. All rights reserved. |
| 2 |
// Use of this source code is governed by the MIT license. |
| 3 |
|
| 4 |
//go:build s390x || ppc64 |
| 5 |
// +build s390x ppc64 |
| 6 |
|
| 7 |
// NOTE(cev): See the comment in indexbyte_simd.go for how the list of GOARCH |
| 8 |
// build tags was created (note: wasm is excluded here because it lacks an |
| 9 |
// assembly implementation of Count). |
| 10 |
|
| 11 |
package bytealg |
| 12 |
|
| 13 |
import ( |
| 14 |
"bytes" |
| 15 |
"strings" |
| 16 |
) |
| 17 |
|
| 18 |
func Count(b []byte, c byte) int { |
| 19 |
s := []byte{c} |
| 20 |
if !isAlpha(c) { |
| 21 |
return bytes.Count(b, s) |
| 22 |
} |
| 23 |
n := bytes.Count(b, s) |
| 24 |
s[0] ^= ' ' // swap case |
| 25 |
return n + bytes.Count(b, s) |
| 26 |
} |
| 27 |
|
| 28 |
func CountString(s string, c byte) int { |
| 29 |
if !isAlpha(c) { |
| 30 |
return strings.Count(s, string(c)) |
| 31 |
} |
| 32 |
return strings.Count(s, string(c)) + |
| 33 |
strings.Count(s, string(c^' ')) // swap case |
| 34 |
} |