Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions go/mysql/collations/charset/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package charset
import (
"fmt"
"unicode/utf8"

"vitess.io/vitess/go/hack"
)

func failedConversionError(from, to Charset, input []byte) error {
Expand Down Expand Up @@ -126,6 +128,78 @@ func Convert(dst []byte, dstCharset Charset, src []byte, srcCharset Charset) ([]
}
}

func Expand(dst []rune, src []byte, srcCharset Charset) []rune {
switch srcCharset := srcCharset.(type) {
case Charset_utf8mb3, Charset_utf8mb4:
if dst == nil {
return []rune(string(src))
}
dst = make([]rune, 0, len(src))
for _, cp := range string(src) {
dst = append(dst, cp)
}
return dst
case Charset_binary:
if dst == nil {
dst = make([]rune, 0, len(src))
}
for _, c := range src {
dst = append(dst, rune(c))
}
return dst
default:
if dst == nil {
dst = make([]rune, 0, len(src))
}
for len(src) > 0 {
cp, width := srcCharset.DecodeRune(src)
src = src[width:]
dst = append(dst, cp)
}
return dst
}
}

func Collapse(dst []byte, src []rune, dstCharset Charset) []byte {
switch dstCharset := dstCharset.(type) {
case Charset_utf8mb3, Charset_utf8mb4:
if dst == nil {
return hack.StringBytes(string(src))
}
return append(dst, hack.StringBytes(string(src))...)
case Charset_binary:
if dst == nil {
dst = make([]byte, 0, len(src))
}
for _, b := range src {
dst = append(dst, byte(b))
}
return dst
default:
nDst := 0
if dst == nil {
dst = make([]byte, len(src)*dstCharset.MaxWidth())
} else {
dst = dst[:cap(dst)]
}
for _, c := range src {
if len(dst)-nDst < 4 {
newDst := make([]byte, len(dst)*2)
copy(newDst, dst[:nDst])
dst = newDst
}
w := dstCharset.EncodeRune(dst[nDst:], c)
if w < 0 {
if w = dstCharset.EncodeRune(dst[nDst:], '?'); w < 0 {
break
}
}
nDst += w
}
return dst[:nDst]
}
}

func ConvertFromUTF8(dst []byte, dstCharset Charset, src []byte) ([]byte, error) {
return Convert(dst, dstCharset, src, Charset_utf8mb4{})
}
Expand Down
9 changes: 5 additions & 4 deletions go/mysql/collations/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,11 @@ func makeEnv(version collver) *Environment {
// A few interesting character set values.
// See http://dev.mysql.com/doc/internals/en/character-set.html#packet-Protocol::CharacterSet
const (
CollationUtf8ID = 33
CollationUtf8mb4ID = 255
CollationBinaryID = 63
CollationUtf8mb4BinID = 46
CollationUtf8ID = 33
CollationUtf8mb4ID = 255
CollationBinaryID = 63
CollationUtf8mb4BinID = 46
CollationLatin1Swedish = 8
)

// Binary is the default Binary collation
Expand Down
25 changes: 25 additions & 0 deletions go/mysql/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,31 @@ const (
ERJSONValueTooBig = ErrorCode(3150)
ERJSONDocumentTooDeep = ErrorCode(3157)

ERRegexpStringNotTerminated = ErrorCode(3684)
ERRegexpBufferOverflow = ErrorCode(3684)
ERRegexpIllegalArgument = ErrorCode(3685)
ERRegexpIndexOutOfBounds = ErrorCode(3686)
ERRegexpInternal = ErrorCode(3687)
ERRegexpRuleSyntax = ErrorCode(3688)
ERRegexpBadEscapeSequence = ErrorCode(3689)
ERRegexpUnimplemented = ErrorCode(3690)
ERRegexpMismatchParen = ErrorCode(3691)
ERRegexpBadInterval = ErrorCode(3692)
ERRRegexpMaxLtMin = ErrorCode(3693)
ERRegexpInvalidBackRef = ErrorCode(3694)
ERRegexpLookBehindLimit = ErrorCode(3695)
ERRegexpMissingCloseBracket = ErrorCode(3696)
ERRegexpInvalidRange = ErrorCode(3697)
ERRegexpStackOverflow = ErrorCode(3698)
ERRegexpTimeOut = ErrorCode(3699)
ERRegexpPatternTooBig = ErrorCode(3700)
ERRegexpInvalidCaptureGroup = ErrorCode(3887)
ERRegexpInvalidFlag = ErrorCode(3900)

ERCharacterSetMismatch = ErrorCode(3995)

ERWrongParametersToNativeFct = ErrorCode(1583)

// max execution time exceeded
ERQueryTimeout = ErrorCode(3024)

Expand Down
Loading