MCPcopy Create free account
hub / github.com/dolthub/doltgresql / EncodeSQLBytes

Function EncodeSQLBytes

postgres/parser/lex/encode.go:217–246  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

215// compact. We are trading a little more time during the encoding to
216// have a little less bytes on the wire.
217func EncodeSQLBytes(buf *bytes.Buffer, in string) {
218 start := 0
219 buf.WriteString("b'")
220 // Loop over the bytes of the string (i.e., don't use range over unicode
221 // code points).
222 for i, n := 0, len(in); i < n; i++ {
223 ch := in[i]
224 if encodedChar := stringencoding.EncodeMap[ch]; encodedChar != stringencoding.DontEscape {
225 buf.WriteString(in[start:i])
226 buf.WriteByte('\\')
227 buf.WriteByte(encodedChar)
228 start = i + 1
229 } else if ch == '\'' {
230 // We can't just fold this into stringencoding.EncodeMap because
231 // stringencoding.EncodeMap is also used for strings which
232 // aren't quoted with single-quotes
233 buf.WriteString(in[start:i])
234 buf.WriteByte('\\')
235 buf.WriteByte(ch)
236 start = i + 1
237 } else if ch < 0x20 || ch >= 0x7F {
238 buf.WriteString(in[start:i])
239 // Escape non-printable characters.
240 buf.Write(stringencoding.HexMap[ch])
241 start = i + 1
242 }
243 }
244 buf.WriteString(in[start:])
245 buf.WriteByte('\'')
246}
247
248// EncodeByteArrayToRawBytes converts a SQL-level byte array into raw
249// bytes according to the encoding specification in "be".

Callers 1

FormatMethod · 0.92

Calls 2

WriteStringMethod · 0.80
WriteMethod · 0.45

Tested by

no test coverage detected