UnquoteBytes will decode []byte containing json string to go string ported from encoding/json/decode.go
(s []byte)
| 125 | // UnquoteBytes will decode []byte containing json string to go string |
| 126 | // ported from encoding/json/decode.go |
| 127 | func UnquoteBytes(s []byte) (t []byte, ok bool) { |
| 128 | if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' { |
| 129 | return |
| 130 | } |
| 131 | s = s[1 : len(s)-1] |
| 132 | |
| 133 | // Check for unusual characters. If there are none, |
| 134 | // then no unquoting is needed, so return a slice of the |
| 135 | // original bytes. |
| 136 | r := 0 |
| 137 | for r < len(s) { |
| 138 | c := s[r] |
| 139 | if c == '\\' || c == '"' || c < ' ' { |
| 140 | break |
| 141 | } |
| 142 | if c < utf8.RuneSelf { |
| 143 | r++ |
| 144 | continue |
| 145 | } |
| 146 | rr, size := utf8.DecodeRune(s[r:]) |
| 147 | if rr == utf8.RuneError && size == 1 { |
| 148 | break |
| 149 | } |
| 150 | r += size |
| 151 | } |
| 152 | if r == len(s) { |
| 153 | return s, true |
| 154 | } |
| 155 | |
| 156 | b := make([]byte, len(s)+2*utf8.UTFMax) |
| 157 | w := copy(b, s[0:r]) |
| 158 | for r < len(s) { |
| 159 | // Out of room? Can only happen if s is full of |
| 160 | // malformed UTF-8 and we're replacing each |
| 161 | // byte with RuneError. |
| 162 | if w >= len(b)-2*utf8.UTFMax { |
| 163 | nb := make([]byte, (len(b)+utf8.UTFMax)*2) |
| 164 | copy(nb, b[0:w]) |
| 165 | b = nb |
| 166 | } |
| 167 | switch c := s[r]; { |
| 168 | case c == '\\': |
| 169 | r++ |
| 170 | if r >= len(s) { |
| 171 | return |
| 172 | } |
| 173 | switch s[r] { |
| 174 | default: |
| 175 | return |
| 176 | case '"', '\\', '/', '\'': |
| 177 | b[w] = s[r] |
| 178 | r++ |
| 179 | w++ |
| 180 | case 'b': |
| 181 | b[w] = '\b' |
| 182 | r++ |
| 183 | w++ |
| 184 | case 'f': |
nothing calls this directly
no test coverage detected
searching dependent graphs…