github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/internal/json/encoding.go (about) 1 /** 2 * Copyright 2023 CloudWeGo Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package json 18 19 import ( 20 "runtime" 21 "unsafe" 22 23 "github.com/cloudwego/dynamicgo/internal/native/types" 24 "github.com/cloudwego/dynamicgo/internal/rt" 25 ) 26 27 const ( 28 bytesNull = "null" 29 bytesTrue = "true" 30 bytesFalse = "false" 31 bytesObject = "{}" 32 bytesArray = "[]" 33 ) 34 35 func EncodeNull(buf []byte) []byte { 36 return append(buf, bytesNull...) 37 } 38 39 func EncodeBool(buf []byte, val bool) []byte { 40 if val { 41 return append(buf, bytesTrue...) 42 } 43 return append(buf, bytesFalse...) 44 } 45 46 func EncodeBaniry(buf []byte, val []byte) []byte { 47 out := append(buf, '"') 48 nb := len(val) 49 if nb == 0 { 50 out = append(out, '"') 51 return out 52 } 53 54 out = append(out, encodeBase64(val)...) 55 return append(out, '"') 56 } 57 58 func EncodeBase64(buf []byte, val []byte) []byte { 59 return append(buf, encodeBase64(val)...) 60 } 61 62 func EncodeString(buf []byte, val string) []byte { 63 buf = append(buf, '"') 64 if len(val) == 0 { 65 buf = append(buf, '"') 66 return buf 67 } 68 NoQuote(&buf, val) 69 buf = append(buf, '"') 70 return buf 71 } 72 73 74 func EncodeInt64(buf []byte, val int64) []byte { 75 i64toa(&buf, val) 76 return buf 77 } 78 79 func EncodeFloat64(buf []byte, val float64) ([]byte) { 80 f64toa(&buf, val) 81 return buf 82 } 83 84 func EncodeArrayBegin(buf []byte) []byte { 85 return append(buf, '[') 86 } 87 88 func EncodeArrayComma(buf []byte) []byte { 89 return append(buf, ',') 90 } 91 92 func EncodeArrayEnd(buf []byte) []byte { 93 return append(buf, ']') 94 } 95 96 func EncodeEmptyArray(buf []byte) []byte { 97 return append(buf, '[', ']') 98 } 99 100 func EncodeObjectBegin(buf []byte) []byte { 101 return append(buf, '{') 102 } 103 104 func EncodeObjectColon(buf []byte) []byte { 105 return append(buf, ':') 106 } 107 108 func EncodeObjectComma(buf []byte) []byte { 109 return append(buf, ',') 110 } 111 112 func EncodeObjectEnd(buf []byte) []byte { 113 return append(buf, '}') 114 } 115 116 func EncodeEmptyObject(buf []byte) []byte { 117 return append(buf, '{', '}') 118 } 119 120 func IsSpace(c byte) bool { 121 return (int(1<<c) & _blankCharsMask) != 0 122 } 123 124 const _blankCharsMask = (1 << ' ') | (1 << '\t') | (1 << '\r') | (1 << '\n') 125 126 //go:nocheckptr 127 func SkipBlank(src string, pos int) int { 128 se := uintptr(rt.IndexChar(src, len(src))) 129 sp := uintptr(rt.IndexChar(src, pos)) 130 131 for sp < se { 132 if !IsSpace(*(*byte)(unsafe.Pointer(sp))) { 133 break 134 } 135 sp += 1 136 } 137 if sp >= se { 138 return -int(types.ERR_EOF) 139 } 140 runtime.KeepAlive(src) 141 return int(sp - uintptr(rt.IndexChar(src, 0))) 142 }