bytes

package
v0.5.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 7 Imported by: 0

README

bytes

import "github.com/gechr/x/bytes"

Package bytes provides byte-slice helpers mirroring strings: split, contains, indent/dedent, truncate, and blank checks.

Index

func AllEmpty

func AllEmpty(values ...[]byte) bool

AllEmpty reports whether every given slice is empty.

Example
fmt.Println(xbytes.AllEmpty([]byte(""), []byte("")))
fmt.Println(xbytes.AllEmpty([]byte(""), []byte("alpha")))

Output:

true
false

func AllNonEmpty

func AllNonEmpty(values ...[]byte) bool

AllNonEmpty reports whether every given slice is non-empty.

Example
fmt.Println(xbytes.AllNonEmpty([]byte("alpha"), []byte("beta"), []byte("charlie")))
fmt.Println(xbytes.AllNonEmpty([]byte("alpha"), []byte("")))

Output:

true
false

func AnyEmpty

func AnyEmpty(values ...[]byte) bool

AnyEmpty reports whether any of the given slices is empty.

Example
fmt.Println(xbytes.AnyEmpty([]byte("alpha"), []byte(""), []byte("beta")))
fmt.Println(xbytes.AnyEmpty([]byte("alpha"), []byte("beta")))

Output:

true
false

func AnyNonEmpty

func AnyNonEmpty(values ...[]byte) bool

AnyNonEmpty reports whether any of the given slices is non-empty.

Example
fmt.Println(xbytes.AnyNonEmpty([]byte(""), []byte("alpha"), []byte("")))
fmt.Println(xbytes.AnyNonEmpty([]byte(""), []byte("")))

Output:

true
false

func CompareFold

func CompareFold(a, b []byte) int

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

func ContainsAll(s []byte, subslices ...[]byte) bool

ContainsAll reports whether s contains all of the given subslices.

Example
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

func ContainsAny(s []byte, subslices ...[]byte) bool

ContainsAny reports whether s contains any of the given subslices.

Example
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

func ContainsFold(s, subslice []byte) bool

ContainsFold reports whether s contains subslice, case-insensitively using the same simple case-folding as bytes.EqualFold.

Example
fmt.Println(xbytes.ContainsFold([]byte("Hello, World"), []byte("WORLD")))
fmt.Println(xbytes.ContainsFold([]byte("Hello, World"), []byte("moon")))

Output:

true
false

func CountAny

func CountAny(s []byte, chars string) int

CountAny returns the number of Unicode code points in s that are contained in chars, following the cutset convention of bytes.IndexAny.

Example
fmt.Println(xbytes.CountAny([]byte("hello world"), "lo"))

Output:

5

func DecodeSHA256

func DecodeSHA256(s []byte) ([sha256.Size]byte, error)

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
digest, err := xbytes.DecodeSHA256(
    []byte("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
)
fmt.Printf("%x\n", digest[:4])
fmt.Println(err)

Output:

e3b0c442
<nil>

func Dedent

func Dedent(s []byte) []byte

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
fmt.Printf("%s\n", xbytes.Dedent([]byte("    foo\n      bar\n    baz")))

Output:

foo
  bar
baz

func EnsureTrailingNewline

func EnsureTrailingNewline(s []byte) []byte

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

func HasPrefixFold(s, prefix []byte) bool

HasPrefixFold reports whether s begins with prefix, case-insensitively using the same simple case-folding as bytes.EqualFold.

Example
fmt.Println(xbytes.HasPrefixFold([]byte("Hello, World"), []byte("HELLO")))
fmt.Println(xbytes.HasPrefixFold([]byte("Hello, World"), []byte("world")))

Output:

true
false

func HasSuffixFold

func HasSuffixFold(s, suffix []byte) bool

HasSuffixFold reports whether s ends with suffix, case-insensitively using the same simple case-folding as bytes.EqualFold.

Example
fmt.Println(xbytes.HasSuffixFold([]byte("Hello, World"), []byte("WORLD")))
fmt.Println(xbytes.HasSuffixFold([]byte("Hello, World"), []byte("hello")))

Output:

true
false

func HexEqual

func HexEqual(a, b []byte) bool

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
fmt.Println(xbytes.HexEqual([]byte("0xDEADbeef"), []byte("deadbeef")))
fmt.Println(xbytes.HexEqual([]byte("0x1234"), []byte("0x5678")))

Output:

true
false

func Indent

func Indent(s, prefix []byte) []byte

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
fmt.Printf("%s\n", xbytes.Indent([]byte("foo\nbar"), []byte("> ")))

Output:

> foo
> bar

func IsASCII

func IsASCII(s []byte) bool

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

func IsAlpha(s []byte) bool

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

func IsAlphaChar(c byte) bool

IsAlphaChar reports whether c is an ASCII letter (a-z, A-Z).

func IsAlphanumeric

func IsAlphanumeric(s []byte) bool

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

func IsAlphanumericChar(c byte) bool

IsAlphanumericChar reports whether c is an ASCII letter (a-z, A-Z) or digit (0-9).

func IsBlank

func IsBlank(s []byte) bool

IsBlank reports whether s is empty or consists only of whitespace.

Example
fmt.Println(xbytes.IsBlank([]byte(" \t\n")))
fmt.Println(xbytes.IsBlank([]byte("x")))

Output:

true
false

func IsDigitChar

func IsDigitChar(c byte) bool

IsDigitChar reports whether c is an ASCII digit (0-9).

func IsDigits

func IsDigits(s []byte) bool

IsDigits reports whether s is non-empty and consists entirely of ASCII digits (0-9). An empty slice is not digits.

Example
fmt.Println(xbytes.IsDigits([]byte("12345")))
fmt.Println(xbytes.IsDigits([]byte("12a45")))
fmt.Println(xbytes.IsDigits([]byte("")))

Output:

true
false
false

func IsGitCommit

func IsGitCommit(s []byte) bool

IsGitCommit reports whether s is 40 hexadecimal digits (a Git commit hash).

Example
fmt.Println(xbytes.IsGitCommit([]byte("3b18e512dba79e4c8300dd08aeb37f8e728b8dad")))
fmt.Println(xbytes.IsGitCommit([]byte("deadbeef")))

Output:

true
false

func IsHex

func IsHex(s []byte) bool

IsHex reports whether s is non-empty and consists entirely of hexadecimal digits. An empty slice is not hex.

Example
fmt.Println(xbytes.IsHex([]byte("deadBEEF42")))
fmt.Println(xbytes.IsHex([]byte("xyz")))

Output:

true
false

func IsHexChar

func IsHexChar(c byte) bool

IsHexChar reports whether c is a valid hexadecimal digit (0-9, a-f, A-F).

Example
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

func IsSHA256(s []byte) bool

IsSHA256 reports whether s is 64 hexadecimal digits (a sha256 digest).

Example
fmt.Println(
    xbytes.IsSHA256([]byte("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")),
)
fmt.Println(xbytes.IsSHA256([]byte("deadbeef")))

Output:

true
false

func PadCenter

func PadCenter(s []byte, width int) []byte

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.

fmt.Printf("%q\n", xbytes.PadCenter([]byte("hi"), 5))

Output:

" hi  "

func PadLeft

func PadLeft(s []byte, width int) []byte

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 ansi package. The returned slice never aliases s.

PadLeft([]byte("hi"), 5) // "   hi"
Example
fmt.Printf("%q\n", xbytes.PadLeft([]byte("hi"), 5))

Output:

"   hi"

func PadRight

func PadRight(s []byte, width int) []byte

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
fmt.Printf("%q\n", xbytes.PadRight([]byte("hi"), 5))

Output:

"hi   "

func SplitAny

func SplitAny(s []byte, chars string) [][]byte

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.

fmt.Printf("%q\n", xbytes.SplitAny([]byte("a,b;;c"), ",;"))

Output:

["a" "b" "" "c"]

func SplitBy

func SplitBy(s, sep []byte) [][]byte

SplitBy splits s by sep, trims whitespace from each part, and drops empty values.

Example
fmt.Printf("%q\n", xbytes.SplitBy([]byte(" a | b || c "), []byte("|")))

Output:

["a" "b" "c"]

func SplitLines

func SplitLines(s []byte) [][]byte

SplitLines splits s into non-empty trimmed lines.

Example
fmt.Printf("%q\n", xbytes.SplitLines([]byte("foo\n\n  bar \n")))

Output:

["foo" "bar"]

func SplitLinesRaw

func SplitLinesRaw(s []byte) [][]byte

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
fmt.Printf("%q\n", xbytes.SplitLinesRaw([]byte("foo\r\nbar\n")))

Output:

["foo" "bar" ""]

func TrimPrefixes

func TrimPrefixes(s []byte, prefixes ...[]byte) []byte

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

func TrimSuffixes(s []byte, suffixes ...[]byte) []byte

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

func Truncate(s []byte, n int, marker []byte) []byte

Truncate is an alias for TruncateRight, the most common form: it keeps the head and trims the tail.

func TruncateLeft

func TruncateLeft(s []byte, n int, marker []byte) []byte

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

func TruncateMiddle(s []byte, n int, marker []byte) []byte

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
fmt.Printf("%s\n", xbytes.TruncateMiddle([]byte("0123456789abcdef"), 7, []byte("…")))

Output:

012…def

func TruncateRight

func TruncateRight(s []byte, n int, marker []byte) []byte

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 ansi.Truncate.

TruncateRight([]byte("hello world"), 8, []byte("…")) // "hello w…"
TruncateRight([]byte("hi"), 8, []byte("…"))          // "hi"

func Unwrap

func Unwrap(s, prefix, suffix []byte) ([]byte, bool)

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

Documentation

Overview

Package bytes provides byte-slice helpers mirroring github.com/gechr/x/strings: split, contains, indent/dedent, truncate, and blank checks.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllEmpty

func AllEmpty(values ...[]byte) bool

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

func AllNonEmpty(values ...[]byte) bool

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

func AnyEmpty(values ...[]byte) bool

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

func AnyNonEmpty(values ...[]byte) bool

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

func CompareFold(a, b []byte) int

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

func ContainsAll(s []byte, subslices ...[]byte) bool

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

func ContainsAny(s []byte, subslices ...[]byte) bool

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

func ContainsFold(s, subslice []byte) bool

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

func CountAny(s []byte, chars string) int

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

func DecodeSHA256(s []byte) ([sha256.Size]byte, error)

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

func Dedent(s []byte) []byte

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

func EnsureTrailingNewline(s []byte) []byte

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

func HasPrefixFold(s, prefix []byte) bool

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

func HasSuffixFold(s, suffix []byte) bool

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

func HexEqual(a, b []byte) bool

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

func Indent(s, prefix []byte) []byte

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

func IsASCII(s []byte) bool

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

func IsAlpha(s []byte) bool

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

func IsAlphaChar(c byte) bool

IsAlphaChar reports whether `c` is an ASCII letter (a-z, A-Z).

func IsAlphanumeric

func IsAlphanumeric(s []byte) bool

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

func IsAlphanumericChar(c byte) bool

IsAlphanumericChar reports whether `c` is an ASCII letter (a-z, A-Z) or digit (0-9).

func IsBlank

func IsBlank(s []byte) bool

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

func IsDigitChar(c byte) bool

IsDigitChar reports whether `c` is an ASCII digit (0-9).

func IsDigits

func IsDigits(s []byte) bool

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

func IsGitCommit(s []byte) bool

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

func IsHex(s []byte) bool

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

func IsHexChar(c byte) bool

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

func IsSHA256(s []byte) bool

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

func PadCenter(s []byte, width int) []byte

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

func PadLeft(s []byte, width int) []byte

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

func PadRight(s []byte, width int) []byte

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

func SplitAny(s []byte, chars string) [][]byte

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

func SplitBy(s, sep []byte) [][]byte

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

func SplitLines(s []byte) [][]byte

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

func SplitLinesRaw(s []byte) [][]byte

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

func TrimPrefixes(s []byte, prefixes ...[]byte) []byte

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

func TrimSuffixes(s []byte, suffixes ...[]byte) []byte

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

func Truncate(s []byte, n int, marker []byte) []byte

Truncate is an alias for TruncateRight, the most common form: it keeps the head and trims the tail.

func TruncateLeft

func TruncateLeft(s []byte, n int, marker []byte) []byte

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

func TruncateMiddle(s []byte, n int, marker []byte) []byte

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

func TruncateRight(s []byte, n int, marker []byte) []byte

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

func Unwrap(s, prefix, suffix []byte) ([]byte, bool)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL