func SnippetByIndexes(s []byte, indexes []int, sniplen int) (snippets [][]byte) {
	var start, end int
	for _, idx := range indexes {
		start = idx - sniplen
		if start < 0 {
			start = 0
		}
		end = idx + sniplen
		if end > len(s) {
			end = len(s)
		}
		snippets = append(snippets, s[start:end])
	}
	return snippets
}
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.
package bytes
import (
	"bytes"
	"fmt"
	"unicode"
)
func AppendInt16(data []byte, v int16) []byte {
	data = append(data, byte(v>>8))
	data = append(data, byte(v))
	return data
}
func AppendInt32(data []byte, v int32) []byte {
	data = append(data, byte(v>>24))
	data = append(data, byte(v>>16))
	data = append(data, byte(v>>8))
	data = append(data, byte(v))
	return data
}
func AppendUint16(data []byte, v uint16) []byte {
	data = append(data, byte(v>>8))
	data = append(data, byte(v))
	return data
}
func AppendUint32(data []byte, v uint32) []byte {
	data = append(data, byte(v>>24))
	data = append(data, byte(v>>16))
	data = append(data, byte(v>>8))
	data = append(data, byte(v))
	return data
}
func Concat(args ...interface{}) (out []byte) {
	for _, arg := range args {
		switch v := arg.(type) {
		case string:
			out = append(out, []byte(v)...)
		case []byte:
			out = append(out, v...)
		}
	}
	return out
}
func Copy(src []byte) (dst []byte) {
	if len(src) == 0 {
		return
	}
	dst = make([]byte, len(src))
	copy(dst, src)
	return
}
func CutUntilToken(text, token []byte, startAt int, checkEsc bool) (cut []byte, pos int, found bool) {
	var isEsc bool
	textlen := len(text)
	tokenlen := len(token)
	if tokenlen == 0 {
		return text, -1, false
	}
	if startAt < 0 {
		startAt = 0
	}
	for pos = startAt; pos < textlen; pos++ {
		if checkEsc && text[pos] == '\\' {
			if isEsc {
				cut = append(cut, '\\')
				isEsc = false
			} else {
				isEsc = true
			}
			continue
		}
		if text[pos] != token[0] {
			if isEsc {
				cut = append(cut, '\\')
				isEsc = false
			}
			cut = append(cut, text[pos])
			continue
		}
		found = IsTokenAt(text, token, pos)
		if !found {
			if isEsc {
				cut = append(cut, '\\')
				isEsc = false
			}
			cut = append(cut, text[pos])
			continue
		}
		if isEsc {
			cut = append(cut, token...)
			pos = pos + tokenlen - 1
			isEsc = false
			continue
		}
		return cut, pos + tokenlen, true
	}
	return cut, pos, false
}
func EncloseRemove(text, leftToken, rightToken []byte) (cut []byte, found bool) {
	lidx := TokenFind(text, leftToken, 0)
	if lidx < 0 {
		return text, false
	}
	ridx := TokenFind(text, rightToken, lidx+1)
	if ridx < 0 {
		return text, false
	}
	cut = make([]byte, 0, len(text[:lidx])+len(text[ridx:]))
	cut = append(cut, text[:lidx]...)
	cut = append(cut, text[ridx+len(rightToken):]...)
	cut, _ = EncloseRemove(cut, leftToken, rightToken)
	return cut, true
}
func EncloseToken(text, token, leftcap, rightcap []byte) (
	newtext []byte,
	found bool,
) {
	enclosedLen := len(token)
	startat := 0
	for {
		foundat := TokenFind(text, token, startat)
		if foundat < 0 {
			newtext = append(newtext, text[startat:]...)
			break
		}
		newtext = append(newtext, text[startat:foundat]...)
		newtext = append(newtext, leftcap...)
		newtext = append(newtext, token...)
		newtext = append(newtext, rightcap...)
		startat = foundat + enclosedLen
	}
	if startat > 0 {
		found = true
	}
	return newtext, found
}
func InReplace(text, allowed []byte, c byte) []byte {
	if len(text) == 0 {
		return nil
	}
	var found bool
	for x := 0; x < len(text); x++ {
		found = false
		for y := 0; y < len(allowed); y++ {
			if text[x] == allowed[y] {
				found = true
				break
			}
		}
		if !found {
			text[x] = c
		}
	}
	return text
}
func Indexes(text, token []byte) (idxs []int) {
	if len(text) == 0 || len(token) == 0 {
		return nil
	}
	offset := 0
	for {
		idx := bytes.Index(text, token)
		if idx == -1 {
			break
		}
		idxs = append(idxs, offset+idx)
		skip := idx + len(token)
		offset += skip
		text = text[skip:]
	}
	return idxs
}
func IsTokenAt(text, token []byte, p int) bool {
	textlen := len(text)
	tokenlen := len(token)
	if tokenlen == 0 {
		return false
	}
	if p < 0 {
		p = 0
	}
	if p+tokenlen > textlen {
		return false
	}
	for x := 0; x < tokenlen; x++ {
		if text[p] != token[x] {
			return false
		}
		p++
	}
	return true
}
func MergeSpaces(in []byte) (out []byte) {
	var isSpace bool
	for _, c := range in {
		if c == ' ' || c == '\t' || c == '\v' || c == '\f' || c == '\n' || c == '\r' {
			isSpace = true
			continue
		}
		if isSpace {
			out = append(out, ' ')
			isSpace = false
		}
		out = append(out, c)
	}
	if isSpace {
		out = append(out, ' ')
	}
	return out
}
func PrintHex(title string, data []byte, col int) {
	var (
		start, x int
		c        byte
	)
	fmt.Print(title)
	for x, c = range data {
		if x%col == 0 {
			if x > 0 {
				fmt.Print(" ||")
			}
			for y := start; y < x; y++ {
				if data[y] >= 33 && data[y] <= 126 {
					fmt.Printf(" %c", data[y])
				} else {
					fmt.Print(" .")
				}
			}
			fmt.Printf("\n%4d -", x)
			start = x
		}
		fmt.Printf(" %02X", c)
	}
	rest := col - (x % col)
	if rest > 0 {
		for y := 1; y < rest; y++ {
			fmt.Print("   ")
		}
		fmt.Print(" ||")
	}
	for y := start; y <= x; y++ {
		if data[y] >= 33 && data[y] <= 126 {
			fmt.Printf(" %c", data[y])
		} else {
			fmt.Print(" .")
		}
	}
	fmt.Println()
}
func ReadHexByte(data []byte, x int) (b byte, ok bool) {
	if x < 0 {
		return 0, false
	}
	if len(data) < x+2 {
		return 0, false
	}
	var y int = 4
	for y >= 0 {
		switch {
		case data[x] >= '0' && data[x] <= '9':
			b |= (data[x] - '0') << y
		case data[x] >= 'A' && data[x] <= 'F':
			b |= (data[x] - ('A' - 10)) << y
		case data[x] >= 'a' && data[x] <= 'f':
			b |= (data[x] - ('a' - 10)) << y
		default:
			return 0, false
		}
		y -= 4
		x++
	}
	return b, true
}
func ReadInt16(data []byte, x uint) (v int16) {
	if x+1 >= uint(len(data)) {
		return 0
	}
	v = int16(data[x]) << 8
	v |= int16(data[x+1])
	return v
}
func ReadInt32(data []byte, x uint) (v int32) {
	if x+3 >= uint(len(data)) {
		return 0
	}
	v = int32(data[x]) << 24
	v |= int32(data[x+1]) << 16
	v |= int32(data[x+2]) << 8
	v |= int32(data[x+3])
	return v
}
func ReadUint16(data []byte, x uint) (v uint16) {
	if x+1 >= uint(len(data)) {
		return 0
	}
	v = uint16(data[x]) << 8
	v |= uint16(data[x+1])
	return v
}
func ReadUint32(data []byte, x uint) (v uint32) {
	if x+3 >= uint(len(data)) {
		return 0
	}
	v = uint32(data[x]) << 24
	v |= uint32(data[x+1]) << 16
	v |= uint32(data[x+2]) << 8
	v |= uint32(data[x+3])
	return v
}
func SkipAfterToken(text, token []byte, startAt int, checkEsc bool) (int, bool) {
	textlen := len(text)
	escaped := false
	if startAt < 0 {
		startAt = 0
	}
	p := startAt
	for ; p < textlen; p++ {
		if checkEsc && text[p] == '\\' {
			escaped = true
			continue
		}
		if text[p] != token[0] {
			if escaped {
				escaped = false
			}
			continue
		}
		found := IsTokenAt(text, token, p)
		if !found {
			if escaped {
				escaped = false
			}
			continue
		}
		if checkEsc && escaped {
			escaped = false
			continue
		}
		p += len(token)
		return p, true
	}
	return -1, false
}