From 148e5077638f92a455ec7f5a1c4db1a3d3588901 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Mon, 22 Jan 2024 20:39:27 +0530 Subject: [PATCH] fix: Unescape(Escape(str)) now returns the original string Signed-off-by: Manik Rana --- go/sqlescape/ids.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/go/sqlescape/ids.go b/go/sqlescape/ids.go index 3983db13362..6d9b92f7bc4 100644 --- a/go/sqlescape/ids.go +++ b/go/sqlescape/ids.go @@ -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 + } + } + return buf.String() } return in }