Skip to content

Commit

Permalink
fix: エスケープ文字に'あ'などを指定するとEscapeLikeWithCharが正常に動作しない
Browse files Browse the repository at this point in the history
エスケープ文字がruneとして2以上のことを想定していないから。

そのようなケースはサポート外である旨を明記。
そういう使い方をしたらpanicさせる。
  • Loading branch information
KamikazeZirou committed Dec 24, 2021
1 parent e122010 commit a3d0d90
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 9 additions & 1 deletion sql-escape/escape.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package sql_escape

import "strings"
import (
"strings"
"unicode/utf8"
)

// EscapeLike escapes the special characters in the SQL Like statement.
// The escape character is regarded as '\\'.
Expand All @@ -9,7 +12,12 @@ func EscapeLike(s string) string {
}

// EscapeLikeWithChar escapes the special characters in the SQL Like statement.
// Set 'c' to a character with a length of 1 as rune.
func EscapeLikeWithChar(s string, c rune) string {
if utf8.RuneLen(c) != 1 {
panic("set 'c' to a character with a length of 1 as rune.")
}

var b strings.Builder
b.Grow(2 * (len(s)))

Expand Down
2 changes: 2 additions & 0 deletions sql-escape/escape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ func TestEscapeLikeWithChar(t *testing.T) {
name: "no-escape",
args: args{
s: "t",
c: '!',
},
want: "t",
},
{
name: "empty",
args: args{
s: "",
c: '!',
},
want: "",
},
Expand Down

0 comments on commit a3d0d90

Please sign in to comment.