Documentation ¶
Overview ¶
Package bytes provide a library for working with byte or slice of bytes.
Index ¶
- Constants
- Variables
- func AppendInt16(data *[]byte, v int16)
- func AppendInt32(data *[]byte, v int32)
- func AppendUint16(data *[]byte, v uint16)
- func AppendUint32(data *[]byte, v uint32)
- func Concat(sb []byte, args ...interface{}) (out []byte)
- func Copy(src []byte) (dst []byte)
- func CutUntilToken(line, token []byte, startAt int, checkEsc bool) ([]byte, int, bool)
- func EncloseRemove(line, leftcap, rightcap []byte) ([]byte, bool)
- func EncloseToken(line, token, leftcap, rightcap []byte) (newline []byte, status bool)
- func InReplace(in, allowed []byte, c byte) (out []byte)
- func IsAlnum(b byte) bool
- func IsAlpha(b byte) bool
- func IsDigit(b byte) bool
- func IsDigits(data []byte) bool
- func IsHex(b byte) bool
- func IsSpace(b byte) bool
- func IsTokenAt(line, token []byte, p int) bool
- func JSONEscape(in []byte) []byte
- func JSONUnescape(in []byte, strict bool) ([]byte, error)
- func PrintHex(title string, data []byte, col int)
- func Random(seed []byte, n int) []byte
- func ReadHexByte(data []byte, x int) (b byte, ok bool)
- func ReadInt16(data []byte, x uint) int16
- func ReadInt32(data []byte, x uint) int32
- func ReadUint16(data []byte, x uint) uint16
- func ReadUint32(data []byte, x uint) uint32
- func SkipAfterToken(line, token []byte, startAt int, checkEsc bool) (int, bool)
- func ToLower(data *[]byte)
- func ToUpper(data *[]byte)
- func TokenFind(line, token []byte, startat int) (at int)
- func WriteUint16(data *[]byte, x uint, v uint16)
- func WriteUint32(data *[]byte, x uint, v uint32)
Examples ¶
Constants ¶
const ( // ASCIILetters contains list of lower and upper case characters in // ASCII. ASCIILetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // ASCIILettersNumber contains list of lower and upper case // characters in ASCII with numbers. ASCIILettersNumber = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890" // HexaLETTERS contains list of hexadecimal characters in upper cases. HexaLETTERS = "0123456789ABCDEF" // HexaLetters contains list of hexadecimal characters in lower and // upper cases. HexaLetters = "0123456789abcedfABCDEF" // Hexaletters contains list of hexadecimal characters in lower cases. Hexaletters = "0123456789abcedf" )
Variables ¶
var ( // ASCIISpaces contains list of white spaces in ASCII. ASCIISpaces = []byte{'\t', '\n', '\v', '\f', '\r', ' '} //nolint: gochecknoglobals )
Functions ¶
func Concat ¶ added in v0.5.0
Concat merge one or more slice of byte or string in arguments into slice of byte. Any type that is not []byte or string in arguments will be ignored.
func CutUntilToken ¶
CutUntilToken cut line until we found token.
If token found, it will return all cutted bytes before token, positition of byte after token, and boolean true.
If no token found, it will return false.
If `checkEsc` is true, token that is prefixed with escaped character '\' will be skipped.
Example ¶
line := []byte(`abc \def ghi`) cut, p, found := CutUntilToken(line, []byte("def"), 0, false) fmt.Printf("'%s' %d %t\n", cut, p, found) cut, p, found = CutUntilToken(line, []byte("def"), 0, true) fmt.Printf("'%s' %d %t\n", cut, p, found) cut, p, found = CutUntilToken(line, []byte("ef"), 0, true) fmt.Printf("'%s' %d %t\n", cut, p, found) cut, p, found = CutUntilToken(line, []byte("hi"), 0, true) fmt.Printf("'%s' %d %t\n", cut, p, found)
Output: 'abc \' 8 true 'abc def ghi' 12 false 'abc \d' 8 true 'abc \def g' 12 true
func EncloseRemove ¶
EncloseRemove given a line, remove all bytes inside it, starting from `leftcap` until the `rightcap` and return cutted line and status to true.
If no `leftcap` or `rightcap` is found, it will return line as is, and status will be false.
Example ¶
line := []byte(`[[ ABC ]] DEF`) leftcap := []byte(`[[`) rightcap := []byte(`]]`) got, changed := EncloseRemove(line, leftcap, rightcap) fmt.Printf("'%s' %t\n", got, changed)
Output: ' DEF' true
func EncloseToken ¶
EncloseToken will find `token` in `line` and enclose it with bytes from `leftcap` and `rightcap`. If at least one token found, it will return modified line with true status. If no token is found, it will return the same line with false status.
Example ¶
line := []byte(`// Copyright 2016-2018 "Shulhan <ms@kilabit.info>". All rights reserved.`) token := []byte(`"`) leftcap := []byte(`\`) rightcap := []byte(`_`) got, changed := EncloseToken(line, token, leftcap, rightcap) fmt.Printf("'%s' %t\n", got, changed)
Output: '// Copyright 2016-2018 \"_Shulhan <ms@kilabit.info>\"_. All rights reserved.' true
func InReplace ¶
InReplace do a reverse replace on input, any characters that is not on allowed, will be replaced with character c.
func IsAlnum ¶
IsAlnum will return true if byte is ASCII alphanumeric character, otherwise it will return false.
func IsAlpha ¶
IsAlpha will return true if byte is ASCII alphabet character, otherwise it will return false.
func IsDigits ¶
IsDigits will return true if all bytes are ASCII digit, otherwise it will return false.
func IsSpace ¶
IsSpace will return true if byte is ASCII white spaces character, otherwise it will return false.
func IsTokenAt ¶
IsTokenAt return true if `line` at index `p` match with `token`, otherwise it will return false. Empty token always return false.
Example ¶
line := []byte("Hello, world") token := []byte("world") token2 := []byte("worlds") tokenEmpty := []byte{} fmt.Printf("%t\n", IsTokenAt(line, tokenEmpty, 6)) fmt.Printf("%t\n", IsTokenAt(line, token, 6)) fmt.Printf("%t\n", IsTokenAt(line, token, 7)) fmt.Printf("%t\n", IsTokenAt(line, token, 8)) fmt.Printf("%t\n", IsTokenAt(line, token2, 8))
Output: false false true false false
func JSONEscape ¶
JSONEscape escape the following character: `"` (quotation mark), `\` (reverse solidus), `/` (solidus), `\b` (backspace), `\f` (formfeed), `\n` (newline), `\r` (carriage return`), `\t` (horizontal tab), and control character from 0 - 31.
References:
func JSONUnescape ¶
JSONUnescape unescape JSON bytes, reversing what BytesJSONEscape do.
If strict is true, any unknown control character will be returned as error. For example, in string "\x", "x" is not valid control character, and the function will return empty string and error. If strict is false, it will return "x".
func Random ¶
Random generate random sequence of value from seed with fixed length.
This function assume that random generator has been seeded.
func ReadHexByte ¶
ReadHexByte read two characters from data start from index "x" and convert them to byte.
func ReadUint16 ¶
ReadUint16 will convert two bytes from data start at `x` into uint16 and return it.
func ReadUint32 ¶
ReadUint32 will convert four bytes from data start at `x` into uint32 and return it.
func SkipAfterToken ¶
SkipAfterToken skip all bytes until matched token is found and return the index after the token and boolean true.
If `checkEsc` is true, token that is prefixed with escaped character '\' will be considered as non-match token.
If no token found it will return -1 and boolean false.
Example ¶
line := []byte(`abc \def ghi`) p, found := SkipAfterToken(line, []byte("def"), 0, false) fmt.Printf("%d %t\n", p, found) p, found = SkipAfterToken(line, []byte("def"), 0, true) fmt.Printf("%d %t\n", p, found) p, found = SkipAfterToken(line, []byte("ef"), 0, true) fmt.Printf("%d %t\n", p, found) p, found = SkipAfterToken(line, []byte("hi"), 0, true) fmt.Printf("%d %t\n", p, found)
Output: 8 true 12 false 8 true 12 true
func TokenFind ¶
TokenFind return the first index of matched token in line, start at custom index. If "startat" parameter is less than 0, then it will be set to 0. If token is empty or no token found it will return -1.
Example ¶
line := []byte("// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.") token := []byte("right") at := TokenFind(line, token, 0) fmt.Printf("%d\n", at)
Output: 7
Types ¶
This section is empty.