charconv

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2023 License: MIT Imports: 10 Imported by: 0

README

charconv

封装golang.org/x/text库中encoding的包,实现字符串编码转换。封装github.com/saintfish/chardet包实现文件编码检测。

由于golang使用utf-8作为其原生编码方式,在这种语境下,编码和解码的含义如下:

编码:utf-8 => 字符集x

解码:字符集x => utf-8

Documentation

Index

Constants

View Source
const (
	GBK     = "GBK"
	GB18030 = "GB18030"
	Big5    = "Big5"
)

中文

View Source
const (
	EUCJP     = "EUC-JP"
	ISO2022JP = "ISO-2022-JP"
	ShiftJIS  = "Shift_JIS"
)

日文

View Source
const (
	UTF8    = "UTF-8"
	UTF16   = "UTF-16"
	UTF16BE = "UTF-16BE"
	UTF16LE = "UTF-16LE"
)

Unicode

View Source
const (
	Macintosh = "macintosh"

	IBM037   = "IBM037"
	IBM437   = "IBM437"
	IBM850   = "IBM850"
	IBM852   = "IBM852"
	IBM855   = "IBM855"
	IBM00858 = "IBM00858"
	IBM860   = "IBM860"
	IBM862   = "IBM862"
	IBM863   = "IBM863"
	IBM865   = "IBM865"
	IBM866   = "IBM866"
	IBM1047  = "IBM1047"
	IBM01140 = "IBM01140"

	ISO88591  = "ISO-8859-1"
	ISO88592  = "ISO-8859-2"
	ISO88593  = "ISO-8859-3"
	ISO88594  = "ISO-8859-4"
	ISO88595  = "ISO-8859-5"
	ISO88596  = "ISO-8859-6"
	ISO88596E = "ISO-8859-6-E"
	ISO88596I = "ISO-8859-6-I"
	ISO88597  = "ISO-8859-7"
	ISO88598  = "ISO-8859-8"
	ISO88598E = "ISO-8859-8-E"
	ISO88598I = "ISO-8859-8-I"
	ISO88599  = "ISO-8859-9"
	ISO885910 = "ISO-8859-10"
	ISO885913 = "ISO-8859-13"
	ISO885914 = "ISO-8859-14"
	ISO885915 = "ISO-8859-15"
	ISO885916 = "ISO-8859-16"
	KOI8R     = "KOI8-R"
	KOI8U     = "KOI8-U"

	Windows874  = "Windows-874"
	Windows1250 = "Windows-1250"
	Windows1251 = "Windows-1251"
	Windows1252 = "Windows-1252"
	Windows1253 = "Windows-1253"
	Windows1254 = "Windows-1254"
	Windows1255 = "Windows-1255"
	Windows1256 = "Windows-1256"
	Windows1257 = "Windows-1257"
	Windows1258 = "Windows-1258"
)

其他字符集

View Source
const CreateOrTrunc = os.O_RDWR | os.O_CREATE | os.O_TRUNC
View Source
const (
	EUCKR = "EUC-KR"
)

Variables

This section is empty.

Functions

func CloseQuietly added in v0.1.0

func CloseQuietly(file *os.File)

func CodecOf

func CodecOf(charsetName string) (*encoding.Encoder, *encoding.Decoder, error)

CodecOf 获取charsetNamed对应的Encoder和Decoder

func Convert

func Convert(src io.Reader, dest io.Writer, decoder *encoding.Decoder, encoder *encoding.Encoder) error

func ConvertBetweenCharsets added in v0.1.0

func ConvertBetweenCharsets(src io.Reader, srcCharset string, dest io.Writer, destCharset string) error

func ConvertFileBetweenCharsets added in v0.1.0

func ConvertFileBetweenCharsets(
	srcFilePath string,
	srcFileCharset string,
	destFilePath string,
	destFileCharset string,
	destFileFlag int,
) error

func CopyTmpFileTo added in v0.1.0

func CopyTmpFileTo(tmp *os.File, destPath string, destFlag int) error

func Decode

func Decode(src io.Reader, dest io.Writer, decoder *encoding.Decoder) error

func DecodeBytes

func DecodeBytes(src []byte, dest io.Writer, decoder *encoding.Decoder) error

func DecodeBytesToBytes added in v0.1.0

func DecodeBytesToBytes(src []byte, initBuffSize int, decoder *encoding.Decoder) ([]byte, error)

func DecodeBytesToBytesWithCharset added in v0.1.0

func DecodeBytesToBytesWithCharset(src []byte, initBuffSize int, srcCharset string) ([]byte, error)

func DecodeBytesToFile added in v0.1.0

func DecodeBytesToFile(src []byte, destFilePath string, destFileFlag int, decoder *encoding.Decoder) error

func DecodeBytesToFileWithCharset added in v0.1.0

func DecodeBytesToFileWithCharset(src []byte, destFilePath string, destFileFlag int, srcCharset string) error

func DecodeBytesWithCharset added in v0.1.0

func DecodeBytesWithCharset(src []byte, dest io.Writer, srcCharset string) error

func DecodeFile added in v0.1.0

func DecodeFile(srcFilePath string, dest io.Writer, decoder *encoding.Decoder) error

func DecodeFileToFile added in v0.1.0

func DecodeFileToFile(srcFilePath string, destFilePath string, destFileFlag int, decoder *encoding.Decoder) error

func DecodeFileToFileWithCharset added in v0.1.0

func DecodeFileToFileWithCharset(srcFilePath string, destFilePath string, destFileFlag int, srcCharset string) error

func DecodeFileWithCharset added in v0.1.0

func DecodeFileWithCharset(srcFilePath string, dest io.Writer, srcCharset string) error

func DecodeToBytes added in v0.1.0

func DecodeToBytes(src io.Reader, initBuffSize int, decoder *encoding.Decoder) ([]byte, error)

func DecodeToBytesWithCharset added in v0.1.0

func DecodeToBytesWithCharset(src io.Reader, initBuffSize int, srcCharset string) ([]byte, error)

func DecodeToFile added in v0.0.3

func DecodeToFile(src io.Reader, destFilePath string, destFileFlag int, decoder *encoding.Decoder) error

func DecodeToFileWithCharset added in v0.1.0

func DecodeToFileWithCharset(src io.Reader, destFilePath string, destFileFlag int, srcCharset string) error

func DecodeWithCharset added in v0.1.0

func DecodeWithCharset(src io.Reader, dest io.Writer, srcCharset string) error

func DecoderOf

func DecoderOf(charsetName string) *encoding.Decoder

DecoderOf 获取charsetName对应的Decoder

func Encode

func Encode(src io.Reader, dest io.Writer, destEncoder *encoding.Encoder) error

Encode 基础编码方法

func EncodeFile added in v0.1.0

func EncodeFile(srcFilePath string, dest io.Writer, destEncoder *encoding.Encoder) error

func EncodeFileToBytes added in v0.1.0

func EncodeFileToBytes(srcFilePath string, initBuffSize int, destEncoder *encoding.Encoder) ([]byte, error)

func EncodeFileToBytesWithCharset added in v0.1.0

func EncodeFileToBytesWithCharset(srcFilePath string, initBuffSize int, destCharset string) ([]byte, error)

func EncodeFileToFile added in v0.1.0

func EncodeFileToFile(srcFilePath string, destFilePath string, destFileFlag int, destEncoder *encoding.Encoder) error

func EncodeFileToFileWithCharset added in v0.1.0

func EncodeFileToFileWithCharset(srcFilePath string, destFilePath string, destFileFlag int, destCharset string) error

func EncodeFileWithCharset added in v0.1.0

func EncodeFileWithCharset(srcFilePath string, dest io.Writer, destCharset string) error

func EncodeString added in v0.1.0

func EncodeString(src string, dest io.Writer, destEncoder *encoding.Encoder) error

func EncodeStringToBytes added in v0.1.0

func EncodeStringToBytes(src string, initBuffSize int, destEncoder *encoding.Encoder) ([]byte, error)

func EncodeStringToBytesWithCharset added in v0.1.0

func EncodeStringToBytesWithCharset(src string, initBuffSize int, destCharset string) ([]byte, error)

func EncodeStringWithCharset added in v0.1.0

func EncodeStringWithCharset(src string, dest io.Writer, destCharset string) error

func EncodeToBytes added in v0.1.0

func EncodeToBytes(src io.Reader, initBuffSize int, destEncoder *encoding.Encoder) ([]byte, error)

func EncoderOf

func EncoderOf(charsetName string) *encoding.Encoder

EncoderOf 获取charsetName对应Encoder

func EncodingOf

func EncodingOf(charsetName string) encoding.Encoding

EncodingOf 获取charsetName对应Encoding对象

func GuessBest

func GuessBest(data []byte) (result *chardet.Result, err error)

GuessBest 猜测data编码,返回最为接近的结果

func GuessBestOf

func GuessBestOf(src io.Reader, bytesToDetect int) (result *chardet.Result, err error)

GuessBestOf 通过前bytesToDetect个字节,猜测src编码,返回最接近的结果

func GuessBestOfFile

func GuessBestOfFile(filePath string, bytesToDetect int) (*chardet.Result, error)

GuessBestOfFile 通过前bytesToDetect个字节猜测文件编码,返回最接近的结果

func IsCharsetSupported added in v0.1.0

func IsCharsetSupported(charset string) bool

IsCharsetSupported 判断字符集是受支持

func MakeByteBuffer added in v0.1.0

func MakeByteBuffer(initBufferSize int) *bytes.Buffer

func MakeTempFile added in v0.1.0

func MakeTempFile() (*os.File, error)

func RemoveQuietly added in v0.1.0

func RemoveQuietly(file *os.File)

Types

type ErrUnsupportedCharset

type ErrUnsupportedCharset struct {
	// contains filtered or unexported fields
}

func (ErrUnsupportedCharset) Error

func (e ErrUnsupportedCharset) Error() string

type ErrUnsupportedConversion

type ErrUnsupportedConversion struct {
	// contains filtered or unexported fields
}

func (ErrUnsupportedConversion) Error

func (e ErrUnsupportedConversion) Error() string

Jump to

Keyboard shortcuts

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