github.com/pingcap/tidb/parser@v0.0.0-20231013125129-93a834a6bf8d/charset/encoding_ascii.go (about) 1 // Copyright 2021 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package charset 15 16 import ( 17 "bytes" 18 go_unicode "unicode" 19 20 "golang.org/x/text/encoding" 21 ) 22 23 // EncodingASCIIImpl is the instance of encodingASCII 24 var EncodingASCIIImpl = &encodingASCII{encodingBase{enc: encoding.Nop}} 25 26 func init() { 27 EncodingASCIIImpl.self = EncodingASCIIImpl 28 } 29 30 // encodingASCII is the ASCII encoding. 31 type encodingASCII struct { 32 encodingBase 33 } 34 35 // Name implements Encoding interface. 36 func (*encodingASCII) Name() string { 37 return CharsetASCII 38 } 39 40 // Tp implements Encoding interface. 41 func (*encodingASCII) Tp() EncodingTp { 42 return EncodingTpASCII 43 } 44 45 // Peek implements Encoding interface. 46 func (*encodingASCII) Peek(src []byte) []byte { 47 if len(src) == 0 { 48 return src 49 } 50 return src[:1] 51 } 52 53 // IsValid implements Encoding interface. 54 func (*encodingASCII) IsValid(src []byte) bool { 55 srcLen := len(src) 56 for i := 0; i < srcLen; i++ { 57 if src[i] > go_unicode.MaxASCII { 58 return false 59 } 60 } 61 return true 62 } 63 64 // Transform implements Encoding interface. 65 func (e *encodingASCII) Transform(dest *bytes.Buffer, src []byte, op Op) ([]byte, error) { 66 if e.IsValid(src) { 67 return src, nil 68 } 69 return e.encodingBase.Transform(dest, src, op) 70 } 71 72 // Foreach implements Encoding interface. 73 func (*encodingASCII) Foreach(src []byte, _ Op, fn func(from, to []byte, ok bool) bool) { 74 for i, w := 0, 0; i < len(src); i += w { 75 w = 1 76 ok := true 77 if src[i] > go_unicode.MaxASCII { 78 w = len(EncodingUTF8Impl.Peek(src[i:])) 79 ok = false 80 } 81 if !fn(src[i:i+w], src[i:i+w], ok) { 82 return 83 } 84 } 85 }