EncodeSQLBytes encodes the SQL byte array in 'in' to buf, to a format suitable for re-scanning. We don't use a straightforward hex encoding here with x'...' because the result would be less compact. We are trading a little more time during the encoding to have a little less bytes on the wire.
(buf *bytes.Buffer, in string)
| 202 | // compact. We are trading a little more time during the encoding to |
| 203 | // have a little less bytes on the wire. |
| 204 | func EncodeSQLBytes(buf *bytes.Buffer, in string) { |
| 205 | start := 0 |
| 206 | buf.WriteString("b'") |
| 207 | // Loop over the bytes of the string (i.e., don't use range over unicode |
| 208 | // code points). |
| 209 | for i, n := 0, len(in); i < n; i++ { |
| 210 | ch := in[i] |
| 211 | if encodedChar := stringencoding.EncodeMap[ch]; encodedChar != stringencoding.DontEscape { |
| 212 | buf.WriteString(in[start:i]) |
| 213 | buf.WriteByte('\\') |
| 214 | buf.WriteByte(encodedChar) |
| 215 | start = i + 1 |
| 216 | } else if ch == '\'' { |
| 217 | // We can't just fold this into stringencoding.EncodeMap because |
| 218 | // stringencoding.EncodeMap is also used for strings which |
| 219 | // aren't quoted with single-quotes |
| 220 | buf.WriteString(in[start:i]) |
| 221 | buf.WriteByte('\\') |
| 222 | buf.WriteByte(ch) |
| 223 | start = i + 1 |
| 224 | } else if ch < 0x20 || ch >= 0x7F { |
| 225 | buf.WriteString(in[start:i]) |
| 226 | // Escape non-printable characters. |
| 227 | buf.Write(stringencoding.HexMap[ch]) |
| 228 | start = i + 1 |
| 229 | } |
| 230 | } |
| 231 | buf.WriteString(in[start:]) |
| 232 | buf.WriteByte('\'') |
| 233 | } |
| 234 | |
| 235 | // EncodeByteArrayToRawBytes converts a SQL-level byte array into raw |
| 236 | // bytes according to the encoding specification in "be". |