2017-05-12 05:24:39 +00:00
|
|
|
package zerolog
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
import "unicode/utf8"
|
2017-05-12 05:24:39 +00:00
|
|
|
|
|
|
|
const hex = "0123456789abcdef"
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendJSONString(dst []byte, s string) []byte {
|
|
|
|
dst = append(dst, '"')
|
2017-05-12 05:24:39 +00:00
|
|
|
for i := 0; i < len(s); {
|
|
|
|
if b := s[i]; b < utf8.RuneSelf {
|
|
|
|
switch b {
|
|
|
|
case '"', '\\':
|
2017-05-20 02:43:59 +00:00
|
|
|
dst = append(dst, '\\', b)
|
2017-05-12 05:24:39 +00:00
|
|
|
case '\b':
|
2017-05-20 02:43:59 +00:00
|
|
|
dst = append(dst, '\\', 'b')
|
2017-05-12 05:24:39 +00:00
|
|
|
case '\f':
|
2017-05-20 02:43:59 +00:00
|
|
|
dst = append(dst, '\\', 'f')
|
2017-05-12 05:24:39 +00:00
|
|
|
case '\n':
|
2017-05-20 02:43:59 +00:00
|
|
|
dst = append(dst, '\\', 'n')
|
2017-05-12 05:24:39 +00:00
|
|
|
case '\r':
|
2017-05-20 02:43:59 +00:00
|
|
|
dst = append(dst, '\\', 'r')
|
2017-05-12 05:24:39 +00:00
|
|
|
case '\t':
|
2017-05-20 02:43:59 +00:00
|
|
|
dst = append(dst, '\\', 't')
|
2017-05-12 05:24:39 +00:00
|
|
|
default:
|
|
|
|
if b >= 0x20 {
|
2017-05-20 02:43:59 +00:00
|
|
|
dst = append(dst, b)
|
2017-05-12 05:24:39 +00:00
|
|
|
} else {
|
2017-05-20 02:43:59 +00:00
|
|
|
dst = append(dst, '\\', 'u', '0', '0', hex[b>>4], hex[b&0xF])
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r, size := utf8.DecodeRuneInString(s[i:])
|
|
|
|
if r == utf8.RuneError && size == 1 {
|
2017-05-20 02:43:59 +00:00
|
|
|
dst = append(dst, `\ufffd`...)
|
2017-05-12 05:24:39 +00:00
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
2017-05-20 02:43:59 +00:00
|
|
|
dst = append(dst, s[i:i+size]...)
|
2017-05-12 05:24:39 +00:00
|
|
|
i += size
|
|
|
|
}
|
2017-05-20 02:43:59 +00:00
|
|
|
return append(dst, '"')
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendJSONBytes(dst []byte, s []byte) []byte {
|
|
|
|
dst = append(dst, '"')
|
|
|
|
for i := 0; i < len(s); {
|
|
|
|
if b := s[i]; b < utf8.RuneSelf {
|
|
|
|
switch b {
|
|
|
|
case '"', '\\':
|
|
|
|
dst = append(dst, '\\', b)
|
|
|
|
case '\b':
|
|
|
|
dst = append(dst, '\\', 'b')
|
|
|
|
case '\f':
|
|
|
|
dst = append(dst, '\\', 'f')
|
|
|
|
case '\n':
|
|
|
|
dst = append(dst, '\\', 'n')
|
|
|
|
case '\r':
|
|
|
|
dst = append(dst, '\\', 'r')
|
|
|
|
case '\t':
|
|
|
|
dst = append(dst, '\\', 't')
|
|
|
|
default:
|
|
|
|
if b >= 0x20 {
|
|
|
|
dst = append(dst, b)
|
|
|
|
} else {
|
|
|
|
dst = append(dst, '\\', 'u', '0', '0', hex[b>>4], hex[b&0xF])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r, size := utf8.DecodeRune(s[i:])
|
|
|
|
if r == utf8.RuneError && size == 1 {
|
|
|
|
dst = append(dst, `\ufffd`...)
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dst = append(dst, s[i:i+size]...)
|
|
|
|
i += size
|
|
|
|
}
|
|
|
|
return append(dst, '"')
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|