Skip to content

Commit

Permalink
rework QuoteString and QuoteBytes as append-style
Browse files Browse the repository at this point in the history
  • Loading branch information
ninedraft committed Dec 9, 2024
1 parent 000ce9c commit 25a4bd3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
16 changes: 4 additions & 12 deletions internal/sanitize/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func (q *Query) Sanitize(args ...any) (string, error) {
case bool:
p = strconv.AppendBool(buf.AvailableBuffer(), arg)
case []byte:
p = quoteBytes(buf.AvailableBuffer(), arg)
p = QuoteBytes(buf.AvailableBuffer(), arg)
case string:
p = quoteString(buf.AvailableBuffer(), arg)
p = QuoteString(buf.AvailableBuffer(), arg)
case time.Time:
p = arg.Truncate(time.Microsecond).
AppendFormat(buf.AvailableBuffer(), "'2006-01-02 15:04:05.999999999Z07:00:00'")
Expand Down Expand Up @@ -135,11 +135,7 @@ func (q *Query) init(sql string) {
q.Parts = l.parts
}

func QuoteString(str string) string {
return string(quoteString(nil, str))
}

func quoteString(dst []byte, str string) []byte {
func QuoteString(dst []byte, str string) []byte {
const quote = "'"

n := strings.Count(str, quote)
Expand All @@ -166,11 +162,7 @@ func quoteString(dst []byte, str string) []byte {
return dst
}

func QuoteBytes(buf []byte) string {
return string(quoteBytes(nil, buf))
}

func quoteBytes(dst, buf []byte) []byte {
func QuoteBytes(dst, buf []byte) []byte {
dst = append(dst, `'\x`...)

n := hex.EncodedLen(len(buf))
Expand Down
8 changes: 4 additions & 4 deletions internal/sanitize/sanitize_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ func FuzzQuoteString(f *testing.F) {
f.Add("select 'quoted $42', $1")

f.Fuzz(func(t *testing.T, input string) {
got := sanitize.QuoteString(input)
got := sanitize.QuoteString(nil, input)
want := oldQuoteString(input)

if want != got {
if want != string(got) {
t.Errorf("got %q", got)
t.Fatalf("want %q", want)
}
Expand All @@ -32,10 +32,10 @@ func FuzzQuoteBytes(f *testing.F) {
f.Add([]byte("select 'quoted $42', $1"))

f.Fuzz(func(t *testing.T, input []byte) {
got := sanitize.QuoteBytes(input)
got := sanitize.QuoteBytes(nil, input)
want := oldQuoteBytes(input)

if want != got {
if want != string(got) {
t.Errorf("got %q", got)
t.Fatalf("want %q", want)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/sanitize/sanitize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestQuoteString(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

got := sanitize.QuoteString(input)
got := string(sanitize.QuoteString(nil, input))
want := oldQuoteString(input)

if got != want {
Expand All @@ -259,7 +259,7 @@ func TestQuoteBytes(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

got := sanitize.QuoteBytes(input)
got := string(sanitize.QuoteBytes(nil, input))
want := oldQuoteBytes(input)

if got != want {
Expand Down

0 comments on commit 25a4bd3

Please sign in to comment.