Skip to content

Commit

Permalink
fix: Unescape(Escape(str)) now returns the original string
Browse files Browse the repository at this point in the history
Signed-off-by: Manik Rana <[email protected]>
  • Loading branch information
Maniktherana committed Jan 22, 2024
1 parent 9036de8 commit 148e507
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions go/sqlescape/ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,21 @@ func EscapeIDs(identifiers []string) []string {
return result
}

// UnescapeID reverses any backticking in the input string.
// UnescapeID reverses any backticking in the input string by EscapeID.
func UnescapeID(in string) string {
l := len(in)
if l >= 2 && in[0] == '`' && in[l-1] == '`' {
return in[1 : l-1]
in = in[1 : l-1]
var buf strings.Builder
buf.Grow(len(in))

for i := 0; i < len(in); i++ {
buf.WriteByte(in[i])
if i < len(in)-1 && in[i] == '`' && in[i+1] == '`' {
i++ // halves the number of backticks
}

Check warning on line 67 in go/sqlescape/ids.go

View check run for this annotation

Codecov / codecov/patch

go/sqlescape/ids.go#L59-L67

Added lines #L59 - L67 were not covered by tests
}
return buf.String()

Check warning on line 69 in go/sqlescape/ids.go

View check run for this annotation

Codecov / codecov/patch

go/sqlescape/ids.go#L69

Added line #L69 was not covered by tests
}
return in
}

0 comments on commit 148e507

Please sign in to comment.