Documentation
¶
Overview ¶
Package bytes provides byte-slice helpers mirroring github.com/gechr/x/strings: split, contains, indent/dedent, truncate, and blank checks.
Index ¶
- func AllEmpty(values ...[]byte) bool
- func AllNonEmpty(values ...[]byte) bool
- func AnyEmpty(values ...[]byte) bool
- func AnyNonEmpty(values ...[]byte) bool
- func CompareFold(a, b []byte) int
- func ContainsAll(s []byte, subslices ...[]byte) bool
- func ContainsAny(s []byte, subslices ...[]byte) bool
- func ContainsFold(s, subslice []byte) bool
- func CountAny(s []byte, chars string) int
- func DecodeSHA256(s []byte) ([sha256.Size]byte, error)
- func Dedent(s []byte) []byte
- func EnsureTrailingNewline(s []byte) []byte
- func HasPrefixFold(s, prefix []byte) bool
- func HasSuffixFold(s, suffix []byte) bool
- func HexEqual(a, b []byte) bool
- func Indent(s, prefix []byte) []byte
- func IsASCII(s []byte) bool
- func IsAlpha(s []byte) bool
- func IsAlphaChar(c byte) bool
- func IsAlphanumeric(s []byte) bool
- func IsAlphanumericChar(c byte) bool
- func IsBlank(s []byte) bool
- func IsDigitChar(c byte) bool
- func IsDigits(s []byte) bool
- func IsGitCommit(s []byte) bool
- func IsHex(s []byte) bool
- func IsHexChar(c byte) bool
- func IsSHA256(s []byte) bool
- func PadCenter(s []byte, width int) []byte
- func PadLeft(s []byte, width int) []byte
- func PadRight(s []byte, width int) []byte
- func SplitAny(s []byte, chars string) [][]byte
- func SplitBy(s, sep []byte) [][]byte
- func SplitLines(s []byte) [][]byte
- func SplitLinesRaw(s []byte) [][]byte
- func TrimPrefixes(s []byte, prefixes ...[]byte) []byte
- func TrimSuffixes(s []byte, suffixes ...[]byte) []byte
- func Truncate(s []byte, n int, marker []byte) []byte
- func TruncateLeft(s []byte, n int, marker []byte) []byte
- func TruncateMiddle(s []byte, n int, marker []byte) []byte
- func TruncateRight(s []byte, n int, marker []byte) []byte
- func Unwrap(s, prefix, suffix []byte) ([]byte, bool)
Examples ¶
- AllEmpty
- AllNonEmpty
- AnyEmpty
- AnyNonEmpty
- CompareFold
- ContainsAll
- ContainsAny
- ContainsFold
- CountAny
- DecodeSHA256
- Dedent
- EnsureTrailingNewline
- HasPrefixFold
- HasSuffixFold
- HexEqual
- Indent
- IsBlank
- IsDigits
- IsGitCommit
- IsHex
- IsHexChar
- IsSHA256
- PadCenter
- PadLeft
- PadRight
- SplitAny
- SplitBy
- SplitLines
- SplitLinesRaw
- TruncateMiddle
- Unwrap
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllEmpty ¶
AllEmpty reports whether every given slice is empty.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.AllEmpty([]byte(""), []byte("")))
fmt.Println(xbytes.AllEmpty([]byte(""), []byte("alpha")))
}
Output: true false
func AllNonEmpty ¶
AllNonEmpty reports whether every given slice is non-empty.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.AllNonEmpty([]byte("alpha"), []byte("beta"), []byte("charlie")))
fmt.Println(xbytes.AllNonEmpty([]byte("alpha"), []byte("")))
}
Output: true false
func AnyEmpty ¶
AnyEmpty reports whether any of the given slices is empty.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.AnyEmpty([]byte("alpha"), []byte(""), []byte("beta")))
fmt.Println(xbytes.AnyEmpty([]byte("alpha"), []byte("beta")))
}
Output: true false
func AnyNonEmpty ¶
AnyNonEmpty reports whether any of the given slices is non-empty.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.AnyNonEmpty([]byte(""), []byte("alpha"), []byte("")))
fmt.Println(xbytes.AnyNonEmpty([]byte(""), []byte("")))
}
Output: true false
func CompareFold ¶
CompareFold compares `a` and `b` case-insensitively, using the same simple case-folding as bytes.EqualFold, and returns -1, 0, or 1 following the cmp.Compare convention. `CompareFold(a, b) == 0` iff `bytes.EqualFold(a, b)`.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.CompareFold([]byte("Go"), []byte("go")))
fmt.Println(xbytes.CompareFold([]byte("abc"), []byte("ABD")))
fmt.Println(xbytes.CompareFold([]byte("B"), []byte("a")))
}
Output: 0 -1 1
func ContainsAll ¶
ContainsAll reports whether `s` contains all of the given `subslices`.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.ContainsAll([]byte("hello world"), []byte("hello"), []byte("world")))
fmt.Println(xbytes.ContainsAll([]byte("hello world"), []byte("hello"), []byte("moon")))
}
Output: true false
func ContainsAny ¶
ContainsAny reports whether `s` contains any of the given `subslices`.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.ContainsAny([]byte("hello world"), []byte("moon"), []byte("world")))
fmt.Println(xbytes.ContainsAny([]byte("hello world"), []byte("moon"), []byte("sun")))
}
Output: true false
func ContainsFold ¶
ContainsFold reports whether `s` contains `subslice`, case-insensitively using the same simple case-folding as bytes.EqualFold.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.ContainsFold([]byte("Hello, World"), []byte("WORLD")))
fmt.Println(xbytes.ContainsFold([]byte("Hello, World"), []byte("moon")))
}
Output: true false
func CountAny ¶
CountAny returns the number of Unicode code points in `s` that are contained in `chars`, following the cutset convention of bytes.IndexAny.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.CountAny([]byte("hello world"), "lo"))
}
Output: 5
func DecodeSHA256 ¶
DecodeSHA256 decodes a 64-digit hexadecimal sha256 digest. It returns the zero digest and an error if `s` has the wrong length or contains a non-hexadecimal character.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
digest, err := xbytes.DecodeSHA256(
[]byte("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
)
fmt.Printf("%x\n", digest[:4])
fmt.Println(err)
}
Output: e3b0c442 <nil>
func Dedent ¶
Dedent strips the longest common leading-whitespace prefix from non-empty lines. Whitespace-only lines are normalized to empty (Python textwrap.dedent) and CRLF line endings to LF.
Dedent([]byte(" foo\n bar\n baz")) // "foo\n bar\nbaz"
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Printf("%s\n", xbytes.Dedent([]byte(" foo\n bar\n baz")))
}
Output: foo bar baz
func EnsureTrailingNewline ¶
EnsureTrailingNewline trims any trailing newlines from `s` and appends exactly one, so the result always ends in a single `\n`. An empty slice becomes `\n`. The returned slice never aliases `s`.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Printf("%q\n", xbytes.EnsureTrailingNewline([]byte("hello\n\n")))
fmt.Printf("%q\n", xbytes.EnsureTrailingNewline([]byte("hello")))
}
Output: "hello\n" "hello\n"
func HasPrefixFold ¶
HasPrefixFold reports whether `s` begins with `prefix`, case-insensitively using the same simple case-folding as bytes.EqualFold.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.HasPrefixFold([]byte("Hello, World"), []byte("HELLO")))
fmt.Println(xbytes.HasPrefixFold([]byte("Hello, World"), []byte("world")))
}
Output: true false
func HasSuffixFold ¶
HasSuffixFold reports whether `s` ends with `suffix`, case-insensitively using the same simple case-folding as bytes.EqualFold.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.HasSuffixFold([]byte("Hello, World"), []byte("WORLD")))
fmt.Println(xbytes.HasSuffixFold([]byte("Hello, World"), []byte("hello")))
}
Output: true false
func HexEqual ¶
HexEqual reports whether `a` and `b` denote the same hexadecimal value, ignoring surrounding whitespace, an optional `0x` (or `0X`) prefix, and case. Two blank slices are equal; a blank slice never equals a non-blank one.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.HexEqual([]byte("0xDEADbeef"), []byte("deadbeef")))
fmt.Println(xbytes.HexEqual([]byte("0x1234"), []byte("0x5678")))
}
Output: true false
func Indent ¶
Indent prefixes every non-blank line of `s` with `prefix`. Blank and whitespace-only lines are normalized to empty, and CRLF line endings to LF.
Indent([]byte("foo\nbar"), []byte(" ")) // " foo\n bar"
Indent([]byte("foo\n\nbar"), []byte("> ")) // "> foo\n\n> bar"
Indent([]byte("foo\n \nbar"), []byte("> ")) // "> foo\n\n> bar"
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Printf("%s\n", xbytes.Indent([]byte("foo\nbar"), []byte("> ")))
}
Output: > foo > bar
func IsASCII ¶
IsASCII reports whether `s` is non-empty and consists entirely of ASCII characters (code points 0-127). An empty slice is not ASCII.
func IsAlpha ¶
IsAlpha reports whether `s` is non-empty and consists entirely of ASCII letters (a-z, A-Z). An empty slice is not alpha.
func IsAlphaChar ¶
IsAlphaChar reports whether `c` is an ASCII letter (a-z, A-Z).
func IsAlphanumeric ¶
IsAlphanumeric reports whether `s` is non-empty and consists entirely of ASCII letters (a-z, A-Z) or digits (0-9). An empty slice is not alphanumeric.
func IsAlphanumericChar ¶
IsAlphanumericChar reports whether `c` is an ASCII letter (a-z, A-Z) or digit (0-9).
func IsBlank ¶
IsBlank reports whether `s` is empty or consists only of whitespace.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.IsBlank([]byte(" \t\n")))
fmt.Println(xbytes.IsBlank([]byte("x")))
}
Output: true false
func IsDigitChar ¶
IsDigitChar reports whether `c` is an ASCII digit (0-9).
func IsDigits ¶
IsDigits reports whether `s` is non-empty and consists entirely of ASCII digits (0-9). An empty slice is not digits.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.IsDigits([]byte("12345")))
fmt.Println(xbytes.IsDigits([]byte("12a45")))
fmt.Println(xbytes.IsDigits([]byte("")))
}
Output: true false false
func IsGitCommit ¶
IsGitCommit reports whether `s` is 40 hexadecimal digits (a Git commit hash).
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.IsGitCommit([]byte("3b18e512dba79e4c8300dd08aeb37f8e728b8dad")))
fmt.Println(xbytes.IsGitCommit([]byte("deadbeef")))
}
Output: true false
func IsHex ¶
IsHex reports whether `s` is non-empty and consists entirely of hexadecimal digits. An empty slice is not hex.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.IsHex([]byte("deadBEEF42")))
fmt.Println(xbytes.IsHex([]byte("xyz")))
}
Output: true false
func IsHexChar ¶
IsHexChar reports whether `c` is a valid hexadecimal digit (0-9, a-f, A-F).
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(xbytes.IsHexChar('f'))
fmt.Println(xbytes.IsHexChar('F'))
fmt.Println(xbytes.IsHexChar('9'))
fmt.Println(xbytes.IsHexChar('g'))
}
Output: true true true false
func IsSHA256 ¶
IsSHA256 reports whether `s` is 64 hexadecimal digits (a sha256 digest).
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Println(
xbytes.IsSHA256([]byte("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")),
)
fmt.Println(xbytes.IsSHA256([]byte("deadbeef")))
}
Output: true false
func PadCenter ¶
PadCenter pads `s` with spaces on both sides to `width` runes, centring it. An odd rune of padding goes on the right. Slices already `width` runes or longer are returned unchanged. The returned slice never aliases `s`.
PadCenter([]byte("hi"), 5) // " hi "
Example ¶
PadCenter places the odd rune of padding on the right.
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Printf("%q\n", xbytes.PadCenter([]byte("hi"), 5))
}
Output: " hi "
func PadLeft ¶
PadLeft pads `s` with spaces on the left to `width` runes, right-aligning it. Slices already `width` runes or longer are returned unchanged. Width is counted in runes; for display-width-aware handling of ANSI text use the github.com/gechr/x/ansi package. The returned slice never aliases `s`.
PadLeft([]byte("hi"), 5) // " hi"
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Printf("%q\n", xbytes.PadLeft([]byte("hi"), 5))
}
Output: " hi"
func PadRight ¶
PadRight pads `s` with spaces on the right to `width` runes, left-aligning it. Slices already `width` runes or longer are returned unchanged. The returned slice never aliases `s`.
PadRight([]byte("hi"), 5) // "hi "
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Printf("%q\n", xbytes.PadRight([]byte("hi"), 5))
}
Output: "hi "
func SplitAny ¶
SplitAny splits `s` around each occurrence of any Unicode code point in `chars`, following the cutset convention of bytes.IndexAny. Empty segments between adjacent separators are preserved, matching bytes.Split semantics. If `chars` is empty, SplitAny returns a single-element slice containing `s`.
Example ¶
Empty segments between adjacent separators are preserved.
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Printf("%q\n", xbytes.SplitAny([]byte("a,b;;c"), ",;"))
}
Output: ["a" "b" "" "c"]
func SplitBy ¶
SplitBy splits `s` by `sep`, trims whitespace from each part, and drops empty values.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Printf("%q\n", xbytes.SplitBy([]byte(" a | b || c "), []byte("|")))
}
Output: ["a" "b" "c"]
func SplitLines ¶
SplitLines splits `s` into non-empty trimmed lines.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Printf("%q\n", xbytes.SplitLines([]byte("foo\n\n bar \n")))
}
Output: ["foo" "bar"]
func SplitLinesRaw ¶
SplitLinesRaw splits `s` into lines losslessly, normalizing CRLF to LF: every line is kept verbatim - empty lines and the trailing empty element included - so the result joins back with `"\n"` without losing content or line numbers.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Printf("%q\n", xbytes.SplitLinesRaw([]byte("foo\r\nbar\n")))
}
Output: ["foo" "bar" ""]
func TrimPrefixes ¶
TrimPrefixes returns `s` with the first matching prefix in `prefixes` removed. At most one prefix is removed; if none match, `s` is returned unchanged.
func TrimSuffixes ¶
TrimSuffixes returns `s` with the first matching suffix in `suffixes` removed. At most one suffix is removed; if none match, `s` is returned unchanged.
func Truncate ¶
Truncate is an alias for TruncateRight, the most common form: it keeps the head and trims the tail.
func TruncateLeft ¶
TruncateLeft shortens `s` to at most `n` runes (including `marker`) by removing characters from the left, prepending `marker` when truncation occurs. The tail is kept.
TruncateLeft([]byte("hello world"), 8, []byte("…")) // "…o world"
func TruncateMiddle ¶
TruncateMiddle shortens `s` to at most `n` runes (including `marker`) by removing characters from the middle, inserting `marker` between the kept head and tail so both ends stay visible. This suits hashes and paths, where the start and end are the recognisable parts.
TruncateMiddle([]byte("0123456789abcdef"), 7, []byte("…")) // "012…def"
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
fmt.Printf("%s\n", xbytes.TruncateMiddle([]byte("0123456789abcdef"), 7, []byte("…")))
}
Output: 012…def
func TruncateRight ¶
TruncateRight shortens `s` to at most `n` runes (including `marker`) by removing characters from the right, appending `marker` when truncation occurs. The head is kept. For display-width-aware truncation of ANSI text use github.com/gechr/x/ansi.Truncate.
TruncateRight([]byte("hello world"), 8, []byte("…")) // "hello w…"
TruncateRight([]byte("hi"), 8, []byte("…")) // "hi"
func Unwrap ¶
Unwrap returns `s` with the leading `prefix` and trailing `suffix` removed and reports whether both were present. Unlike a bytes.TrimPrefix + bytes.TrimSuffix chain, nothing is removed unless `s` starts with `prefix` AND ends with `suffix`, so a one-sided match is returned unchanged.
Example ¶
package main
import (
"fmt"
xbytes "github.com/gechr/x/bytes"
)
func main() {
quoted, ok := xbytes.Unwrap([]byte(`"quoted"`), []byte(`"`), []byte(`"`))
fmt.Println(string(quoted), ok)
oneSided, ok := xbytes.Unwrap([]byte(`"one-sided`), []byte(`"`), []byte(`"`))
fmt.Println(string(oneSided), ok)
}
Output: quoted true "one-sided false
Types ¶
This section is empty.