Skip to content

Commit

Permalink
Fit to align FORMAT("%T", input)
Browse files Browse the repository at this point in the history
  • Loading branch information
apstndb committed Dec 19, 2024
1 parent 2f3206c commit a95f042
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
2 changes: 1 addition & 1 deletion char/is.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package char

func IsPrint(b byte) bool {
return 0x21 <= b && b <= 0x7D
return 0x20 <= b && b <= 0x7E // ' ' to '~'
}

func IsDigit(c byte) bool {
Expand Down
41 changes: 17 additions & 24 deletions token/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ func QuoteSQLBytes(bs []byte) string {
buf.WriteString("b")
buf.WriteRune(quote)
for _, b := range bs {
q := quoteSingleEscape(quote, rune(b))
q := quoteSingleEscape(rune(b), quote, false)
if q != "" {
buf.WriteString(q)
continue
}

// Note: char.IsPrint(' ') is false
if b == ' ' || char.IsPrint(b) {
if char.IsPrint(b) {
buf.WriteByte(b)
continue
}
Expand All @@ -60,7 +59,7 @@ func QuoteSQLBytes(bs []byte) string {
return buf.String()
}

// QuoteSQLString returns quoted string with SQL bytes escaping if needed,
// QuoteSQLIdent returns quoted identifier if needed,
// otherwise it returns the input string.
func QuoteSQLIdent(s string) string {
if !needQuoteSQLIdent(s) {
Expand All @@ -76,7 +75,7 @@ func QuoteSQLIdent(s string) string {

func quoteSQLStringContent(quote rune, s string, buf *bytes.Buffer) {
for _, r := range s {
q := quoteSingleEscape(quote, r)
q := quoteSingleEscape(r, quote, /* isString */ true)
if q != "" {
buf.WriteString(q)
continue
Expand All @@ -85,37 +84,31 @@ func quoteSQLStringContent(quote rune, s string, buf *bytes.Buffer) {
buf.WriteRune(r)
continue
}
if r > 0xFFFF {
switch {
case r < 0x80:
fmt.Fprintf(buf, `\x%02x`, uint64(r))
case r > 0xFFFF:
fmt.Fprintf(buf, `\U%08x`, uint64(r))
} else {
default:
fmt.Fprintf(buf, `\u%04x`, uint64(r))
}
}
}

func quoteSingleEscape(quote, r rune) string {
func quoteSingleEscape(r, quote rune, isString bool) string {
if quote == r {
return `\` + string(r)
}

switch r {
case '\a':
return `\a`
case '\b':
return `\b`
case '\f':
return `\f`
case '\n':
switch {
case r == quote:
return `\` + string(r)
case isString && r == '\n':
return `\n`
case '\r':
case isString && r == '\r':
return `\r`
case '\t':
case isString && r == '\t':
return `\t`
case '\v':
return `\v`
case '?':
return `\?`
case '\\':
case isString && r == '\\':
return `\\`
}
return ""
Expand Down

0 comments on commit a95f042

Please sign in to comment.