// Convert a ISO 8859 byte sequence into a UTF-8 byte sequence.
// If this function returns a UnknownByteError, the charset of the
// Converter does not have a unicode mapping for a byte found in latin.
331807
func (c *Converter) Decode(latin []byte) (utf_8 []byte, err error) {

	var offset, i int
	var ok bool
	var utf8symbol []byte
	var errmsg string
	var buf *bytes.Buffer

	buf = bytes.NewBuffer(make([]byte, len(latin)*2))
	buf.Reset()

	for offset < len(latin) {

		if latin[offset] < utf8.RuneSelf {
			buf.WriteByte(latin[offset])
			offset++
		} else {
			utf8symbol, ok = c.latinToUtf8[latin[offset]]
			if !ok {
				errmsg = fmt.Sprintf("0x%X not valid in: %s", latin[offset], c.id)
				err = UnknownByteError(errmsg)
				return buf.Bytes(), err
			}
			for i = 0; i < len(utf8symbol); i++ {
				buf.WriteByte(utf8symbol[i])
			}
			offset++
		}
	}
	return buf.Bytes(), err
}
614127
func uint64ToBytes(u uint64) []byte {
	buf := make([]byte, 8)
	binary.BigEndian.PutUint64(buf, u)
	return buf
}
227222
func DecodeIfBinaryToBytes(in []byte) []byte {
	if binaryFmt(in) {
		var b bytes.Buffer
		Cbor2JsonManyObjects(bytes.NewReader(in), &b)
		return b.Bytes()
	}
	return in
}
835949
func toASCII(s string) string {
	if isASCII(s) {
		return s
	}
	b := make([]byte, 0, len(s))
	for _, c := range s {
		if c < 0x80 && c != 0x00 {
			b = append(b, byte(c))
		}
	}
	return string(b)
}
319033
func StripUTF8Chars(i []byte) (o []byte) {
	buf := i
	for len(buf) > 0 {
		r, size := utf8.DecodeRune(buf)
		if r == utf8.RuneError && size == 1 {
			buf = buf[size:]
			continue
		}
		o = append(o, buf[:size]...)
		buf = buf[size:]
	}
	return
}
1038124
func UnicodeChar(table *unicode.RangeTable) gopter.Gen {
	if table == nil || len(table.R16)+len(table.R32) == 0 {
		return Fail(reflect.TypeOf(rune('a')))
	}
	return func(genParams *gopter.GenParameters) *gopter.GenResult {
		tableIdx := genParams.Rng.Intn(len(table.R16) + len(table.R32))

		var selectedRune rune
		if tableIdx < len(table.R16) {
			r := table.R16[tableIdx]
			runeOffset := uint16(genParams.Rng.Int63n(int64((r.Hi-r.Lo+1)/r.Stride))) * r.Stride
			selectedRune = rune(runeOffset + r.Lo)
		} else {
			r := table.R32[tableIdx-len(table.R16)]
			runeOffset := uint32(genParams.Rng.Int63n(int64((r.Hi-r.Lo+1)/r.Stride))) * r.Stride
			selectedRune = rune(runeOffset + r.Lo)
		}
		genResult := gopter.NewGenResult(selectedRune, gopter.NoShrinker)
		genResult.Sieve = func(v interface{}) bool {
			return unicode.Is(table, v.(rune))
		}
		return genResult
	}
}
731021
func (res *Response) Encoding() (encoding.Encoding, error) {
	enc, _ := charset.Lookup(res.Charset)
	if enc == nil {
		return nil, fmt.Errorf("no encoding found for %s", res.Charset)
	}
	return enc, nil
}
27483
func Int64ToBytes(v int64) []byte {
	ret := make([]byte, 8)
	Int64ToBytesTo(v, ret)
	return ret
}
446056
func decodeUTF16(in []byte) string {
	utf16encoded := []uint16{}
	for i := 0; i+1 < len(in); i += 2 {
		var ch uint16
		ch = uint16(in[i]) + uint16(in[i+1])<<8
		utf16encoded = append(utf16encoded, ch)
	}
	s := string(utf16.Decode(utf16encoded))
	if len(s) > 0 && s[len(s)-1] == 0 {
		s = s[:len(s)-1]
	}
	return s
}
699279
func isutf8(buf []byte, blen int) int {
	if blen <= 2 {
		return 0
	}
	// invalid utf-8 code
	if buf[0] == byte(0xc0) || buf[0] == byte(0xc1) ||
		buf[0] == byte(0xfe) || buf[0] == byte(0xff) {
		return 0
	}

	var (
		i      int = 3
		maxlen int = 6
	)
	if blen < 6 {
		maxlen = blen
	}
	for ; i < maxlen; i++ {
		fb := utf8FirstByte[i]
		mk := utf8FirstMask[i]
		if buf[0]&mk == fb {
			res := true
			for j := 1; j <= i-1; j++ {
				b := buf[j]
				if b == byte(0xc0) || b == byte(0xc1) ||
					b == byte(0xfe) || b == byte(0xff) {
					return 0
				}
				if b&byte(0xc0) != 0x80 {
					res = false
					break
				}
			}
			if res {
				return i
			}
		}
	}
	return 0
}
436391
func encode(s []byte) []byte {
	// len(s) is sufficient for UTF-8 to UTF-16 conversion if there are no
	// control code points (see table below).
	b := make([]byte, 0, len(s)+4)
	for len(s) > 0 {
		r, size := utf8.DecodeRune(s)
		if r > utf8.MaxRune {
			r, size = utf8.RuneError, 1 // Bug fix (issue 3785)
		}
		s = s[size:]
		if r1, r2 := utf16.EncodeRune(r); r1 != repl {
			b = append(b, byte(r1>>8), byte(r1))
			r = r2
		}
		b = append(b, byte(r>>8), byte(r))
	}

	// Encode as base64
	n := b64Enc.EncodedLen(len(b)) + 2
	b64 := make([]byte, n)
	b64Enc.Encode(b64[1:], b)

	// Strip padding
	n -= 2 - (len(b)+2)%3
	b64 = b64[:n]

	// Add UTF-7 shifts
	b64[0] = '&'
	b64[n-1] = '-'
	return b64
}
307823
func (p *gc_bin_parser) rawByte() byte {
	b := p.data[0]
	r := 1
	if b == '|' {
		b = p.data[1]
		r = 2
		switch b {
		case 'S':
			b = '$'
		case '|':
			// nothing to do
		default:
			panic("unexpected escape sequence in export data")
		}
	}
	p.data = p.data[r:]
	p.read += r
	return b

}
1236878
func parseBytea(s []byte) (result []byte, err error) {
	if len(s) >= 2 && bytes.Equal(s[:2], []byte("\\x")) {
		// bytea_output = hex
		s = s[2:] // trim off leading "\\x"
		result = make([]byte, hex.DecodedLen(len(s)))
		_, err := hex.Decode(result, s)
		if err != nil {
			return nil, err
		}
	} else {
		// bytea_output = escape
		for len(s) > 0 {
			if s[0] == '\\' {
				// escaped '\\'
				if len(s) >= 2 && s[1] == '\\' {
					result = append(result, '\\')
					s = s[2:]
					continue
				}

				// '\\' followed by an octal number
				if len(s) < 4 {
					return nil, fmt.Errorf("invalid bytea sequence %v", s)
				}
				r, err := strconv.ParseInt(string(s[1:4]), 8, 9)
				if err != nil {
					return nil, fmt.Errorf("could not parse bytea value: %s", err.Error())
				}
				result = append(result, byte(r))
				s = s[4:]
			} else {
				// We hit an unescaped, raw byte.  Try to read in as many as
				// possible in one go.
				i := bytes.IndexByte(s, '\\')
				if i == -1 {
					result = append(result, s...)
					break
				}
				result = append(result, s[:i]...)
				s = s[i:]
			}
		}
	}

	return result, nil
}
78920
func (bot *ExchangeBot) ConvertedStateBytes(symbol string) ([]byte, error) {
	state, err := bot.ConvertedState(symbol)
	if err != nil {
		return nil, err
	}
	return bot.jsonify(state)
}
694948
func ReverseBytes64(x uint64) uint64 {
	const m = 1<<64 - 1
	x = x>>8&(m3&m) | x&(m3&m)<<8
	x = x>>16&(m4&m) | x&(m4&m)<<16
	return x>>32 | x<<32
}
1298042
func isOperatorChar(c byte) bool {
	if c < 0 {
		return false
	}
	return bytes.IndexByte([]byte("+-*/%&|!<>=~^"), c) != -1
}
475317
func Convert(input []byte, output []byte, fromEncoding string, toEncoding string) (bytesRead int, bytesWritten int, err error) {
	// create a temporary converter
	converter, err := NewConverter(fromEncoding, toEncoding)

	if err == nil {
		// call converter's Convert
		bytesRead, bytesWritten, err = converter.Convert(input, output)

		if err == nil {
			var shiftBytesWritten int

			// call Convert with a nil input to generate any end shift sequences
			_, shiftBytesWritten, err = converter.Convert(nil, output[bytesWritten:])

			// add shift bytes to total bytes
			bytesWritten += shiftBytesWritten
		}

		// close the converter
		converter.Close()
	}

	return
}