示例#1
0
文件: charset.go 项目: devacto/grobot
// Lookup returns the encoding with the specified label, and its canonical
// name. It returns nil and the empty string if label is not one of the
// standard encodings for HTML. Matching is case-insensitive and ignores
// leading and trailing whitespace. Encoders will use HTML escape sequences for
// runes that are not supported by the character set.
func Lookup(label string) (e encoding.Encoding, name string) {
	e, err := htmlindex.Get(label)
	if err != nil {
		return nil, ""
	}
	name, _ = htmlindex.Name(e)
	return &htmlEncoding{e}, name
}
示例#2
0
// Find returns
func findEncoding(name string) (encoding.Encoding, bool) {
	if name == "" {
		return Plain, true
	}
	d, ok := encodings[strings.ToLower(name)]
	if ok {
		return d, ok
	}

	codec, err := htmlindex.Get(name)
	return codec, err == nil
}
示例#3
0
// FindEncoding searches for an EncodingFactoryby name.
func FindEncoding(name string) (EncodingFactory, bool) {
	if name == "" {
		return Plain, true
	}
	d, ok := encodings[strings.ToLower(name)]
	if ok {
		return d, ok
	}

	codec, err := htmlindex.Get(name)
	if err != nil {
		return nil, false
	}
	return enc(codec), true
}
示例#4
0
文件: text.go 项目: jwatt/kythe
// ToUTF8 converts the given encoded text to a UTF-8 string.
func ToUTF8(encodingName string, b []byte) (string, error) {
	if encodingName == "" {
		return transformBytes(encoding.Replacement.NewEncoder(), b)
	}

	e, err := htmlindex.Get(encodingName)
	if err != nil {
		return "", ErrUnsupportedEncoding
	}
	var t transform.Transformer
	if e == encoding.Replacement {
		t = encoding.Replacement.NewEncoder()
	} else {
		t = e.NewDecoder()
	}
	return transformBytes(t, b)
}
示例#5
0
// read reader content to string, using charset specified
func readToStringWithCharset(reader io.Reader, charset string) (string, error) {
	charset = strings.ToUpper(charset)
	var data []byte
	var err error
	if charset == "UTF-8" || charset == "UTF8" {
		data, err = ioutil.ReadAll(reader)
	} else {
		if charset == "GBK" || charset == "GB2312" {
			charset = "GB18030"
		}
		var encoder encoding.Encoding
		encoder, err = htmlindex.Get(charset)
		if err != nil {
			return "", err
		}
		data, err = ioutil.ReadAll(transform.NewReader(reader, encoder.NewDecoder()))
	}
	if err != nil {
		return "", err
	}
	return string(data), err
}