Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pgproto3/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,15 +397,20 @@ func traceDoubleQuotedString(buf []byte) string {
return `"` + string(buf) + `"`
}

const hexChars = "0123456789abcdef"

// traceSingleQuotedString returns buf as a single-quoted string with non-printable characters hex-escaped. It is
// roughly equivalent to pqTraceOutputNchar in libpq.
func traceSingleQuotedString(buf []byte) string {
sb := &strings.Builder{}
sb.Grow(len(buf)*4 + 2) // worst case: every byte escaped (\xNN) + quotes

sb.WriteByte('\'')
for _, b := range buf {
if b < 32 || b > 126 {
fmt.Fprintf(sb, `\x%x`, b)
sb.WriteString(`\x`)
sb.WriteByte(hexChars[b>>4])
sb.WriteByte(hexChars[b&0x0f])
} else {
sb.WriteByte(b)
}
Expand Down
Loading