charset

package
v0.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2026 License: Apache-2.0, Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CollationBin is the default collation for CharsetBin.
	CollationBin = "binary"
	// CollationUTF8 is the default collation for CharsetUTF8.
	CollationUTF8 = "utf8_bin"
	// CollationUTF8MB4 is the default collation for CharsetUTF8MB4.
	CollationUTF8MB4 = "utf8mb4_bin"
	// CollationASCII is the default collation for CharsetACSII.
	CollationASCII = "ascii_bin"
	// CollationLatin1 is the default collation for CharsetLatin1.
	CollationLatin1 = "latin1_bin"
	// CollationGBKBin is the default collation for CharsetGBK when new collation is disabled.
	CollationGBKBin = "gbk_bin"
	// CollationGBKChineseCI is the default collation for CharsetGBK when new collation is enabled.
	CollationGBKChineseCI = "gbk_chinese_ci"
	// CollationGB18030Bin is the default collation for CharsetGB18030 when new collation is disabled.
	CollationGB18030Bin = "gb18030_bin"
	// CollationGB18030ChineseCI is the default collation for CharsetGB18030 when new collation is enabled.
	CollationGB18030ChineseCI = "gb18030_chinese_ci"
)
View Source
const (
	// CharsetASCII is a subset of UTF8.
	CharsetASCII = "ascii"
	// CharsetBin is used for marking binary charset.
	CharsetBin = "binary"
	// CharsetLatin1 is a single byte charset.
	CharsetLatin1 = "latin1"
	// CharsetUTF8 is the default charset for string types.
	CharsetUTF8 = "utf8"
	// CharsetUTF8MB3 is 3 bytes utf8, a MySQL legacy encoding. "utf8" and "utf8mb3" are aliases.
	CharsetUTF8MB3 = "utf8mb3"
	// CharsetUTF8MB4 represents 4 bytes utf8, which works the same way as utf8 in Go.
	CharsetUTF8MB4 = "utf8mb4"
	// CharsetGB18030 represents 4 bytes gb18030.
	CharsetGB18030 = "gb18030"
	//revive:disable:exported
	CharsetARMSCII8 = "armscii8"
	CharsetBig5     = "big5"
	CharsetCP1250   = "cp1250"
	CharsetCP1251   = "cp1251"
	CharsetCP1256   = "cp1256"
	CharsetCP1257   = "cp1257"
	CharsetCP850    = "cp850"
	CharsetCP852    = "cp852"
	CharsetCP866    = "cp866"
	CharsetCP932    = "cp932"
	CharsetDEC8     = "dec8"
	CharsetEUCJPMS  = "eucjpms"
	CharsetEUCKR    = "euckr"
	CharsetGB2312   = "gb2312"
	CharsetGBK      = "gbk"
	CharsetGEOSTD8  = "geostd8"
	CharsetGreek    = "greek"
	CharsetHebrew   = "hebrew"
	CharsetHP8      = "hp8"
	CharsetKEYBCS2  = "keybcs2"
	CharsetKOI8R    = "koi8r"
	CharsetKOI8U    = "koi8u"
	CharsetLatin2   = "latin2"
	CharsetLatin5   = "latin5"
	CharsetLatin7   = "latin7"
	CharsetMacCE    = "macce"
	CharsetMacRoman = "macroman"
	CharsetSJIS     = "sjis"
	CharsetSWE7     = "swe7"
	CharsetTIS620   = "tis620"
	CharsetUCS2     = "ucs2"
	CharsetUJIS     = "ujis"
	CharsetUTF16    = "utf16"
	CharsetUTF16LE  = "utf16le"
	CharsetUTF32    = "utf32"
)
View Source
const (
	// OpReplaceNoErr is used to replace invalid bytes with '?'.
	OpReplaceNoErr  = opFromUTF8 | opTruncateReplace | opCollectFrom | opSkipError
	OpReplace       = opFromUTF8 | opTruncateReplace | opCollectFrom
	OpEncode        = opFromUTF8 | opTruncateTrim | opCollectTo
	OpEncodeNoErr   = OpEncode | opSkipError
	OpEncodeReplace = opFromUTF8 | opTruncateReplace | opCollectTo
	OpDecode        = opToUTF8 | opTruncateTrim | opCollectTo
	OpDecodeNoErr   = OpDecode | opSkipError
	OpDecodeReplace = opToUTF8 | opTruncateReplace | opCollectTo
)

Variables

View Source
var (
	// ErrUnknownCollation is unknown collation.
	ErrUnknownCollation = mysql.NewStdErr("ddl", mysql.ErrUnknownCollation)
	// ErrCollationCharsetMismatch is collation charset mismatch.
	ErrCollationCharsetMismatch = mysql.NewStdErr("ddl", mysql.ErrCollationCharsetMismatch)
)
View Source
var (
	// PadSpace is to mark that trailing spaces are insignificant in comparisons
	PadSpace = "PAD SPACE"
	// PadNone is to mark that trailing spaces are significant in comparisons
	PadNone = "NO PAD"
)
View Source
var CharacterSetInfos = map[string]*Charset{
	CharsetUTF8:    {CharsetUTF8, CollationUTF8, make(map[string]*Collation), "UTF-8 Unicode", 3},
	CharsetUTF8MB4: {CharsetUTF8MB4, CollationUTF8MB4, make(map[string]*Collation), "UTF-8 Unicode", 4},
	CharsetASCII:   {CharsetASCII, CollationASCII, make(map[string]*Collation), "US ASCII", 1},
	CharsetLatin1:  {CharsetLatin1, CollationLatin1, make(map[string]*Collation), "Latin1", 1},
	CharsetBin:     {CharsetBin, CollationBin, make(map[string]*Collation), "binary", 1},
	CharsetGBK:     {CharsetGBK, CollationGBKBin, make(map[string]*Collation), "Chinese Internal Code Specification", 2},
	CharsetGB18030: {CharsetGB18030, CollationGB18030Bin, make(map[string]*Collation), "China National Standard GB18030", 4},
}

CharacterSetInfos contains all the supported charsets.

View Source
var EncodingASCIIImpl = &encodingASCII{encodingBase{enc: encoding.Nop}}

EncodingASCIIImpl is the instance of encodingASCII

View Source
var EncodingBinImpl = &encodingBin{encodingBase{enc: encoding.Nop}}

EncodingBinImpl is the instance of encodingBin.

View Source
var EncodingLatin1Impl = &encodingLatin1{encodingUTF8{encodingBase{enc: encoding.Nop}}}

EncodingLatin1Impl is the instance of encodingLatin1. latin1 is handled with the utf8 implementation (a no-op transform) for backward compatibility.

View Source
var EncodingUTF8Impl = &encodingUTF8{encodingBase{enc: encoding.Nop}}

EncodingUTF8Impl is the instance of encodingUTF8.

View Source
var EncodingUTF8MB3StrictImpl = &encodingUTF8MB3Strict{
	encodingUTF8{
		encodingBase{
			enc: encoding.Nop,
		},
	},
}

EncodingUTF8MB3StrictImpl is the instance of encodingUTF8MB3Strict.

View Source
var ErrInvalidCharacterString = mysql.NewStdErr("parser", mysql.ErrInvalidCharacterString)

ErrInvalidCharacterString returns when the string is invalid in the specific charset.

Functions

func GetDefaultCollation

func GetDefaultCollation(charset string) (string, error)

GetDefaultCollation returns the default collation for charset.

func GetDefaultCollationLegacy

func GetDefaultCollationLegacy(charset string) (string, error)

GetDefaultCollationLegacy is compatible with the charset support in old version parser.

func HackSlice

func HackSlice(s string) (b []byte)

HackSlice converts string to slice without copy. Use at your own risk.

func HackString

func HackString(b []byte) (s string)

HackString converts slice to string without copy. Use it at your own risk.

func MySQLDefaultCollation

func MySQLDefaultCollation(cs string) (string, bool)

MySQLDefaultCollation returns the collation MySQL 8.0+ applies to a charset that is used without an explicit COLLATE clause, and whether cs is a charset this parser knows.

It deliberately does not go through GetCharsetInfo/GetDefaultCollation: those consult CharacterSetInfos first, which carries the *_bin defaults inherited from this parser's upstream (utf8mb4 -> utf8mb4_bin). MySQL's own default for utf8mb4 is utf8mb4_0900_ai_ci, which is what the charsets registry records, so callers that need to reason about what a live server would do must read that registry directly. Legacy utf8_* spellings are returned as-is; see utf8Alias.

func ValidCharsetAndCollation

func ValidCharsetAndCollation(cs string, co string) bool

ValidCharsetAndCollation checks the charset and the collation validity and returns a boolean.

Types

type Charset

type Charset struct {
	Name             string
	DefaultCollation string
	Collations       map[string]*Collation
	Desc             string
	Maxlen           int
}

Charset is a charset. Now we only support MySQL.

func GetCharsetInfo

func GetCharsetInfo(cs string) (*Charset, error)

GetCharsetInfo returns charset and collation for cs as name.

type Collation

type Collation struct {
	ID           int
	CharsetName  string
	Name         string
	IsDefault    bool
	Sortlen      int
	PadAttribute string
}

Collation is a collation. Now we only support MySQL.

func GetCollationByName

func GetCollationByName(name string) (*Collation, error)

GetCollationByName returns the collation by name.

type Encoding

type Encoding interface {
	// Name is the name of the encoding.
	Name() string
	// Tp is the type of the encoding.
	Tp() EncodingTp
	// Peek returns the next char.
	Peek(src []byte) []byte
	// MbLen returns multiple byte length, if the next character is single byte, return 0.
	MbLen(string) int
	// 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 current encoding.
	Foreach(src []byte, op Op, fn func(from, to []byte, ok bool) bool)
	// Transform map the bytes in src to dest according to Op.
	// **the caller should initialize the dest if it wants to avoid memory alloc every time,
	//   or else it will always make a new one**
	//
	// **the returned array may be the alias of `src`, edit the returned array on your own risk**
	Transform(dest *bytes.Buffer, src []byte, op Op) ([]byte, error)
	// ToUpper change a string to uppercase.
	ToUpper(src string) string
	// ToLower change a string to lowercase.
	ToLower(src string) string
}

Encoding provide encode/decode functions for a string with a specific charset.

func FindEncoding

func FindEncoding(charset string) Encoding

FindEncoding finds the encoding according to charset.

type EncodingTp

type EncodingTp int8

EncodingTp is the type of the encoding.

const (
	EncodingTpNone EncodingTp = iota
	EncodingTpUTF8
	EncodingTpUTF8MB3Strict
	EncodingTpASCII
	EncodingTpLatin1
	EncodingTpBin
)

type Op

type Op int16

Op is used by Encoding.Transform.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL