bytes

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2021 License: BSD-3-Clause Imports: 4 Imported by: 2

Documentation

Overview

Package bytes provide a library for working with byte or slice of bytes.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendInt16

func AppendInt16(data *[]byte, v int16)

AppendInt16 into slice of byte.

func AppendInt32

func AppendInt32(data *[]byte, v int32)

AppendInt32 into slice of byte.

func AppendUint16

func AppendUint16(data *[]byte, v uint16)

AppendUint16 into slice of byte.

func AppendUint32

func AppendUint32(data *[]byte, v uint32)

AppendUint32 into slice of byte.

func Concat added in v0.5.0

func Concat(sb []byte, args ...interface{}) (out []byte)

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 Copy

func Copy(src []byte) (dst []byte)

Copy slice of bytes from parameter.

func CutUntilToken

func CutUntilToken(line, token []byte, startAt int, checkEsc bool) ([]byte, int, bool)

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

func EncloseRemove(line, leftcap, rightcap []byte) ([]byte, bool)

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

func EncloseToken(line, token, leftcap, rightcap []byte) (
	newline []byte,
	status bool,
)

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

func InReplace(in, allowed []byte, c byte) (out []byte)

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

func Indexes(s []byte, token []byte) (idxs []int)

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

func IsTokenAt(line, token []byte, p int) bool

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

func MergeSpaces(in []byte) (out []byte)

MergeSpaces convert sequences of white spaces into single space ' '.

func PrintHex

func PrintHex(title string, data []byte, col int)

PrintHex will print each byte in slice as hexadecimal value into N column length.

func ReadHexByte

func ReadHexByte(data []byte, x int) (b byte, ok bool)

ReadHexByte read two characters from data start from index "x" and convert them to byte.

func ReadInt16

func ReadInt16(data []byte, x uint) int16

ReadInt16 will convert two bytes from data start at `x` into int16 and return it.

func ReadInt32

func ReadInt32(data []byte, x uint) int32

ReadInt32 will convert four bytes from data start at `x` into int32 and return it.

func ReadUint16

func ReadUint16(data []byte, x uint) uint16

ReadUint16 will convert two bytes from data start at `x` into uint16 and return it.

func ReadUint32

func ReadUint32(data []byte, x uint) uint32

ReadUint32 will convert four bytes from data start at `x` into uint32 and return it.

func SkipAfterToken

func SkipAfterToken(line, token []byte, startAt int, checkEsc bool) (int, bool)

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

func SnippetByIndexes(s []byte, indexes []int, sniplen int) (snippets [][]byte)

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

func TokenFind(line, token []byte, startat int) (at int)

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

func WordIndexes(s []byte, word []byte) (idxs []int)

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]

func WriteUint16

func WriteUint16(data *[]byte, x uint, v uint16)

WriteUint16 into slice of byte.

func WriteUint32

func WriteUint32(data *[]byte, x uint, v uint32)

WriteUint32 into slice of byte.

Types

This section is empty.

Jump to

Keyboard shortcuts

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