Skip to content

Commit

Permalink
expression, parser: update the all the usage for IsValid
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta committed Dec 22, 2021
1 parent e2a29ea commit 0b33162
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 23 deletions.
2 changes: 1 addition & 1 deletion expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ func (b *builtinConvertSig) evalString(row chunk.Row) (string, bool, error) {
return string(ret), false, err
}
enc := charset.FindEncoding(resultTp.Charset)
if !charset.IsValidString(enc, expr) {
if !enc.IsValid(hack.Slice(expr)) {
replace, _ := enc.Transform(nil, hack.Slice(expr), charset.OpReplace)
return string(replace), false, nil
}
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_string_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ func (b *builtinConvertSig) vecEvalString(input *chunk.Chunk, result *chunk.Colu
continue
}
exprI := expr.GetBytes(i)
if !charset.IsValid(enc, exprI) {
if !enc.IsValid(exprI) {
encBuf, _ = enc.Transform(encBuf, exprI, charset.OpReplace)
result.AppendBytes(encBuf)
} else {
Expand Down
3 changes: 2 additions & 1 deletion expression/collation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/hack"
"github.com/pingcap/tidb/util/logutil"
)

Expand Down Expand Up @@ -315,7 +316,7 @@ func safeConvert(ctx sessionctx.Context, ec *ExprCollation, args ...Expression)
if isNull {
continue
}
if !charset.IsValidString(enc, str) {
if !enc.IsValid(hack.Slice(str)) {
return false
}
} else {
Expand Down
17 changes: 1 addition & 16 deletions parser/charset/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Encoding interface {
Tp() EncodingTp
// Peek returns the next char.
Peek(src []byte) []byte
// IsValid checks whether a utf-8 string is valid in the current encoding.
// IsValid checks whether the utf-8 bytes can be convert to valid string in current encoding.
IsValid(src []byte) bool
// Foreach iterates the characters in in current encoding.
Foreach(src []byte, op Op, fn func(from, to []byte, ok bool) bool)
Expand Down Expand Up @@ -103,21 +103,6 @@ const (
OpDecodeReplace = opToUTF8 | opTruncateReplace | opCollectTo
)

// IsValid checks whether the bytes is valid in current encoding.
func IsValid(e Encoding, src []byte) bool {
isValid := true
e.Foreach(src, opFromUTF8, func(from, to []byte, ok bool) bool {
isValid = ok
return ok
})
return isValid
}

// IsValidString is a string version of IsValid.
func IsValidString(e Encoding, str string) bool {
return IsValid(e, Slice(str))
}

// CountValidBytes counts the first valid bytes in src that
// can be encode to the current encoding.
func CountValidBytes(e Encoding, src []byte) int {
Expand Down
3 changes: 1 addition & 2 deletions parser/charset/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ func TestEncodingValidate(t *testing.T) {
enc = charset.EncodingUTF8MB3StrictImpl
}
strBytes := []byte(tc.str)
ok := charset.IsValid(enc, strBytes)
require.Equal(t, tc.ok, ok, msg)
require.Equal(t, tc.ok, enc.IsValid(strBytes), msg)
replace, _ := enc.Transform(nil, strBytes, charset.OpReplace)
require.Equal(t, tc.expected, string(replace), msg)
}
Expand Down
4 changes: 2 additions & 2 deletions parser/charset/encoding_utf8.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (e *encodingUTF8) IsValid(src []byte) bool {

// Transform implements Encoding interface.
func (e *encodingUTF8) Transform(dest, src []byte, op Op) ([]byte, error) {
if IsValid(e, src) {
if e.IsValid(src) {
return src, nil
}
return e.encodingBase.Transform(dest, src, op)
Expand Down Expand Up @@ -120,7 +120,7 @@ func (e *encodingUTF8MB3Strict) Foreach(src []byte, op Op, fn func(srcCh, dstCh

// Transform implements Encoding interface.
func (e *encodingUTF8MB3Strict) Transform(dest, src []byte, op Op) ([]byte, error) {
if IsValid(e, src) {
if e.IsValid(src) {
return src, nil
}
return e.encodingBase.Transform(dest, src, op)
Expand Down

0 comments on commit 0b33162

Please sign in to comment.