in string.go [96:158]
func encString(b []byte, v string) []byte {
if v == "" {
return encByte(b, BC_STRING_DIRECT)
}
var (
byteLen int
charLen int
vBuf = *bytes.NewBufferString(v)
byteRead int
charCount int
byteCount int
)
// Acquire (CHUNK_SIZE + 1) * 3 bytes since charCount could reach CHUNK_SIZE + 1.
bufp := gxbytes.AcquireBytes((CHUNK_SIZE + 1) * 3)
defer gxbytes.ReleaseBytes(bufp)
buf := *bufp
byteRead = 0
for {
if vBuf.Len() <= 0 {
break
}
charCount = 0
byteCount = 0
for charCount < CHUNK_SIZE {
r, _, err := vBuf.ReadRune()
if err != nil {
break
}
byteLen, charLen = encodeUcs4Rune(buf[byteCount:], r)
charCount += charLen
byteCount += byteLen
}
if charCount == 0 {
break
}
switch {
case vBuf.Len() > 0 && charCount >= CHUNK_SIZE:
b = encByte(b, BC_STRING_CHUNK)
b = encByte(b, PackUint16(uint16(charCount))...)
case charCount <= int(STRING_DIRECT_MAX):
b = encByte(b, byte(charCount+int(BC_STRING_DIRECT)))
case charCount <= STRING_SHORT_MAX:
b = encByte(b, byte((charCount>>8)+int(BC_STRING_SHORT)), byte(charCount))
default:
b = encByte(b, BC_STRING)
b = encByte(b, PackUint16(uint16(charCount))...)
}
b = append(b, buf[:byteCount]...)
byteRead = byteRead + byteCount
}
return b
}