// A helper function that reads a slice String from a Reader. The data was
// serialized by WriteStringSlice function.
430318
func ReadStringSlice(r Reader, sl *[]string) (err error) {
	var l VInt
	if err := l.ReadFrom(r, -1); err != nil {
		return errorsp.WithStacks(err)
	}
	if cap(*sl) < int(l) {
		*sl = make([]string, l)
	} else {
		*sl = (*sl)[:l]
	}
	for i := range *sl {
		if err := (*String)(&(*sl)[i]).ReadFrom(r, -1); err != nil {
			return errorsp.WithStacks(err)
		}
	}
	return nil
}
228004
func StringSlice(s string) Slice {
	raw := []byte(s)
	l := len(raw)
	if l <= 126 {
		return Slice(append([]byte{byte(0x40 + l)}, raw...))
	}
	buf := make([]byte, 1+8+l)
	buf[0] = 0xbf
	binary.LittleEndian.PutUint64(buf[1:], uint64(l))
	copy(buf[1+8:], raw)
	return buf
}
1321381
func (r *Reader) ReadRestString(encoding byte) (string, error) {
	b, err := r.ReadRest()
	if err != nil {
		return "", err
	}

	return Decoders[encoding].ConvertString(string(b))
}
391744
func formatSliceString(kind reflect.Kind, sliceSize int) string {
	if sliceSize == -1 {
		return fmt.Sprintf("[]%v", kind)
	}
	return fmt.Sprintf("[%d]%v", sliceSize, kind)
}
352875
func (enc *Encoder) SliceStringKey(k string, s []string) {
	enc.ArrayKey(k, EncodeArrayFunc(func(enc *Encoder) {
		for _, str := range s {
			enc.String(str)
		}
	}))
}
625457
func (ns *Namespace) SliceString(a interface{}, startEnd ...interface{}) (string, error) {
	aStr, err := cast.ToStringE(a)
	if err != nil {
		return "", err
	}

	var argStart, argEnd int

	argNum := len(startEnd)

	if argNum > 0 {
		if argStart, err = cast.ToIntE(startEnd[0]); err != nil {
			return "", errors.New("start argument must be integer")
		}
	}
	if argNum > 1 {
		if argEnd, err = cast.ToIntE(startEnd[1]); err != nil {
			return "", errors.New("end argument must be integer")
		}
	}

	if argNum > 2 {
		return "", errors.New("too many arguments")
	}

	asRunes := []rune(aStr)

	if argNum > 0 && (argStart < 0 || argStart >= len(asRunes)) {
		return "", errors.New("slice bounds out of range")
	}

	if argNum == 2 {
		if argEnd < 0 || argEnd > len(asRunes) {
			return "", errors.New("slice bounds out of range")
		}
		return string(asRunes[argStart:argEnd]), nil
	} else if argNum == 1 {
		return string(asRunes[argStart:]), nil
	} else {
		return string(asRunes[:]), nil
	}

}
607531
func (this Slice) String() string {
	wr := bytes.NewBuffer(nil)
	fmt.Fprint(wr, "Slice[")
	for i, v := range this {
		fmt.Fprintf(wr, "%v: %v", v.Key, print(v.Val))
		if i != len(this)-1 {
			fmt.Fprint(wr, " ")
		}
	}
	fmt.Fprintf(wr, "]")
	return wr.String()
}
663499
func (z *Reader) readString() (string, error) {
	var err error
	needConv := false
	for i := 0; ; i++ {
		if i >= len(z.buf) {
			return "", ErrHeader
		}
		z.buf[i], err = z.r.ReadByte()
		if err != nil {
			return "", err
		}
		if z.buf[i] > 0x7f {
			needConv = true
		}
		if z.buf[i] == 0 {
			// Digest covers the NUL terminator.
			z.digest = crc32.Update(z.digest, crc32.IEEETable, z.buf[:i+1])

			// Strings are ISO 8859-1, Latin-1 (RFC 1952, section 2.3.1).
			if needConv {
				s := make([]rune, 0, i)
				for _, v := range z.buf[:i] {
					s = append(s, rune(v))
				}
				return string(s), nil
			}
			return string(z.buf[:i]), nil
		}
	}
}
1129702
func ReadSliceFrom(r io.Reader, rm ReaderMaker, slice interface{}) (int64, error) {
	sliceValue := reflect.ValueOf(slice)
	return ReadFunc(r, rm, func(reader io.ReaderFrom) {
		elem := reflect.ValueOf(reader).Elem()
		reflect.Append(sliceValue, elem)
	})
}
1076143
func (bd *BinaryDecoder) ReadString() (string, error) {
	if err := checkEOF(bd.buf, bd.pos, 1); err != nil {
		return "", err
	}
	length, err := bd.ReadLong()
	if err != nil || length < 0 {
		return "", InvalidStringLength
	}
	if err := checkEOF(bd.buf, bd.pos, int(length)); err != nil {
		return "", err
	}
	value := string(bd.buf[bd.pos : bd.pos+length])
	bd.pos += length
	return value, nil
}
629705
func ReadSliceNoNest(rd io.Reader) (s Slice, err error) {
	// Just in case of programming mistake. Not intentionally used.
	defer func() {
		if r := recover(); r != nil {
			err = errors.New(fmt.Sprint(r))
		}
	}()

	return decodeSlice(rd, "", false)
}
376884
func sliceFromBufReader(r *bufio.Reader) (Slice, error) {
	// ByteSize is always found within first 16 bytes
	hdr, err := r.Peek(maxByteSizeBytes)
	if len(hdr) == 0 && err != nil {
		if Cause(err) == io.EOF {
			// Empty slice
			return nil, nil
		}
		return nil, WithStack(err)
	}
	s := Slice(hdr)
	size, err := s.ByteSize()
	if err != nil {
		return nil, WithStack(err)
	}
	// Now that we know the size, read the entire slice
	buf := make(Slice, size)
	offset := 0
	bytesRead := 0
	for ValueLength(bytesRead) < size {
		n, err := r.Read(buf[offset:])
		bytesRead += n
		offset += n
		if err != nil && ValueLength(bytesRead) < size {
			return nil, WithStack(err)
		}
	}
	return buf, nil
}
439603
func (r *ByteSliceReader) ReadByte() (byte, error) {
	d := []byte{0}
	_, err := r.Read(d)
	return d[0], err
}
604384
func sliceBytes(p unsafe.Pointer) unsafe.Pointer {
	hdr := (*reflect.SliceHeader)(p)
	return unsafe.Pointer(hdr.Data)
}
202295
func (d *Digest) WriteString(s string) (n int, err error) {
	var b []byte
	bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
	bh.Data = (*reflect.StringHeader)(unsafe.Pointer(&s)).Data
	bh.Len = len(s)
	bh.Cap = len(s)
	return d.Write(b)
}
1013067
func WriteStringSlice(w Writer, sl []string) error {
	if err := VInt(len(sl)).WriteTo(w); err != nil {
		return errorsp.WithStacks(err)
	}
	for _, s := range sl {
		if err := String(s).WriteTo(w); err != nil {
			return errorsp.WithStacks(err)
		}
	}
	return nil
}
1060568
func stringsFromByteSlice(buf []byte) (result []string) {
	index := 0
	for index < len(buf) {
		next := index + 1 + int(buf[index])
		result = append(result, string(buf[index+1:next]))
		index = next
	}
	return
}