Documentation ¶
Overview ¶
Package bytes provide a library for working with byte or slice of bytes.
Index ¶
- 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 Indexes(s []byte, token []byte) (idxs []int)
- func IsTokenAt(line, token []byte, p int) bool
- func MergeSpaces(in []byte) (out []byte)
- func PrintHex(title string, data []byte, col int)
- 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 SnippetByIndexes(s []byte, indexes []int, sniplen int) (snippets [][]byte)
- func TokenFind(line, token []byte, startat int) (at int)
- func WordIndexes(s []byte, word []byte) (idxs []int)
- func WriteUint16(data *[]byte, x uint, v uint16)
- func WriteUint32(data *[]byte, x uint, v uint32)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
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 Indexes ¶ added in v0.10.1
Indexes returns the index of the all instance of token in s, or nil if token is not present in s.
Example ¶
s := []byte("moo moomoo") token := []byte("moo") idxs := Indexes(s, token) fmt.Println(idxs)
Output: [0 4 7]
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 MergeSpaces ¶ added in v0.16.0
MergeSpaces convert sequences of white spaces into single space ' '.
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 SnippetByIndexes ¶ added in v0.10.1
SnippetByIndexes take snippet in between of each index with minimum snippet length. The sniplen is the length before and after index, not the length of all snippet.
Example ¶
s := []byte("// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.") indexes := []int{3, 20, len(s) - 4} snippets := SnippetByIndexes(s, indexes, 5) for _, snip := range snippets { fmt.Printf("%s\n", snip) }
Output: // Copyr 18, Shulha reserved.
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
func WordIndexes ¶ added in v0.10.1
WordIndexes returns the index of the all instance of word in s as long as word is separated by space or at the beginning or end of s.
Example ¶
s := []byte("moo moomoo moo") token := []byte("moo") idxs := WordIndexes(s, token) fmt.Println(idxs)
Output: [0 11]
Types ¶
This section is empty.