本文整理汇总了Golang中golang.org/x/net/html/charset.Lookup函数的典型用法代码示例。如果您正苦于以下问题:Golang Lookup函数的具体用法?Golang Lookup怎么用?Golang Lookup使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Lookup函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: transEnc
//「Golangで文字コード判定」qiita.com/nobuhito/items/ff782f64e32f7ed95e43
func transEnc(text string, encode string) (string, error) {
body := []byte(text)
var f []byte
encodings := []string{"sjis", "utf-8"}
if encode != "" {
encodings = append([]string{encode}, encodings...)
}
for _, enc := range encodings {
if enc != "" {
ee, _ := charset.Lookup(enc)
if ee == nil {
continue
}
var buf bytes.Buffer
ic := transform.NewWriter(&buf, ee.NewDecoder())
_, err := ic.Write(body)
if err != nil {
continue
}
err = ic.Close()
if err != nil {
continue
}
f = buf.Bytes()
break
}
}
return string(f), nil
}
示例2: defaultCharsetReader
func defaultCharsetReader(cs string, input io.Reader) (io.Reader, error) {
e, _ := charset.Lookup(cs)
if e == nil {
return nil, fmt.Errorf("cannot decode charset %v", cs)
}
return transform.NewReader(input, e.NewDecoder()), nil
}
示例3: Encoding
// Encoding returns an Encoding for the response body.
func (res *Response) Encoding() (encoding.Encoding, error) {
enc, _ := charset.Lookup(res.Charset)
if enc == nil {
return nil, fmt.Errorf("no encoding found for %s", res.Charset)
}
return enc, nil
}
示例4: ToUtf8WithErr
func ToUtf8WithErr(content []byte) (error, string) {
charsetLabel, err := DetectEncoding(content)
if err != nil {
return err, ""
}
if charsetLabel == "utf8" {
return nil, string(content)
}
encoding, _ := charset.Lookup(charsetLabel)
if encoding == nil {
return fmt.Errorf("unknow char decoder %s", charsetLabel), string(content)
}
result, n, err := transform.String(encoding.NewDecoder(), string(content))
// If there is an error, we concatenate the nicely decoded part and the
// original left over. This way we won't loose data.
if err != nil {
result = result + string(content[n:])
}
return err, result
}
示例5: to_utf8
// Shift-JIS -> UTF-8
func to_utf8(str string) (string, error) {
body, err := ioutil.ReadAll(transform.NewReader(strings.NewReader(str), japanese.ShiftJIS.NewEncoder()))
if err != nil {
return "", err
}
var f []byte
encodings := []string{"sjis", "utf-8"}
for _, enc := range encodings {
if enc != "" {
ee, _ := charset.Lookup(enc)
if ee == nil {
continue
}
var buf bytes.Buffer
ic := transform.NewWriter(&buf, ee.NewDecoder())
_, err := ic.Write(body)
if err != nil {
continue
}
err = ic.Close()
if err != nil {
continue
}
f = buf.Bytes()
break
}
}
return string(f), nil
}
示例6: NewUTF8Reader
func NewUTF8Reader(label string, r io.Reader) (io.Reader, error) {
e, _ := charset.Lookup(label)
if e == nil {
return nil, fmt.Errorf("unsupported charset: %q", label)
}
return transform.NewReader(r, unicode.BOMOverride(e.NewDecoder())), nil
}
示例7: encodeString
func encodeString(input_str, encode string) (output string, err_out error) {
enc, _ := charset.Lookup(encode)
r := transform.NewReader(strings.NewReader(input_str), enc.NewEncoder())
b, err := ioutil.ReadAll(r)
if err != nil {
return
}
return string(b), nil
}
示例8: parseHTML
func parseHTML(content []byte, cs string) (*html.Node, error) {
var r io.Reader = bytes.NewReader(content)
if cs != "utf-8" {
e, _ := charset.Lookup(cs)
r = transform.NewReader(r, e.NewDecoder())
}
return html.Parse(r)
}
示例9: decodeCharset
// DecodeCharset detects charset of str decodes it.
func decodeCharset(str, label string) (nstr string, err error) {
enc, _ := charset.Lookup(label)
if enc == nil {
enc, _, _ = charset.DetermineEncoding([]byte(str), "text/plain")
}
nstr, _, err = transform.String(enc.NewDecoder(), str)
if err != nil {
return nstr, err
}
return stripNonUTF8(nstr), nil
}
示例10: EncodeReader
func EncodeReader(s io.Reader, enc string) ([]byte, error) {
e, _ := charset.Lookup(enc)
if e == nil {
return nil, errors.New(fmt.Sprintf("unsupported charset: %q", enc))
}
var buf bytes.Buffer
writer := transform.NewWriter(&buf, e.NewEncoder())
_, err := io.Copy(writer, s)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
示例11: scanContent
// scanContent scans the content of a document for phrases,
// and updates tally.
func (conf *config) scanContent(content []byte, contentType, cs string, tally map[rule]int) {
if strings.Contains(contentType, "javascript") {
conf.scanJSContent(content, tally)
return
}
transformers := make([]transform.Transformer, 0, 3)
if cs != "utf-8" {
e, _ := charset.Lookup(cs)
transformers = append(transformers, e.NewDecoder())
}
if strings.Contains(contentType, "html") {
transformers = append(transformers, entityDecoder{})
}
transformers = append(transformers, new(wordTransformer))
ps := newPhraseScanner(conf.ContentPhraseList, func(s string) {
tally[rule{t: contentPhrase, content: s}]++
})
ps.scanByte(' ')
var t transform.Transformer
if len(transformers) == 1 {
t = transformers[0]
} else {
t = transform.Chain(transformers...)
}
r := transform.NewReader(bytes.NewReader(content), t)
buf := make([]byte, 4096)
for {
n, err := r.Read(buf)
for _, c := range buf[:n] {
ps.scanByte(c)
}
if err != nil {
if err != io.EOF {
log.Println("Error decoding page content:", err)
}
break
}
}
ps.scanByte(' ')
}
示例12: convertToUtf8
func convertToUtf8(s string) string {
b := []byte(s)
d := chardet.NewTextDetector()
r, err := d.DetectBest(b)
if err != nil {
return fmt.Sprintf("<Can't detect string charset: %s>", err.Error())
}
encoding, _ := charset.Lookup(r.Charset)
if encoding == nil {
return fmt.Sprintf("<Can't find encoding: %s>", r.Charset)
}
str, _, err := transform.String(encoding.NewDecoder(), s)
if err != nil {
return fmt.Sprintf("<Can't convert string from encoding %s to UTF8: %s>", r.Charset, err.Error())
}
return str
}
示例13: convToUTF8
func (r *Response) convToUTF8(preview []byte, query func(*url.URL) string) {
// Convert to UTF-8
if media.IsHTML(r.ContentType) {
e, name, certain := charset.DetermineEncoding(
preview, r.ContentType,
)
// according to charset package source, default unknown charset is windows-1252.
if !certain && name == "windows-1252" {
if e, name = charset.Lookup(query(r.URL)); e != nil {
certain = true
}
}
r.Charset, r.CertainCharset, r.Encoding = name, certain, e
if name != "" && e != nil {
r.Body, _ = util.NewUTF8Reader(name, r.Body)
}
}
}
示例14: decode
func decode(data []byte, charsetName string) ([]byte, error) {
encoding, _ := charset.Lookup(charsetName)
if encoding == nil {
return nil, fmt.Errorf("Unsupported charset: %v", charsetName)
}
reader := bytes.NewReader(data)
var b bytes.Buffer
writer := bufio.NewWriter(&b)
decodeReader := transform.NewReader(reader, encoding.NewDecoder())
if _, err := io.Copy(writer, decodeReader); err != nil {
return nil, err
}
if err := writer.Flush(); err != nil {
return nil, err
}
if isUTF8Charset(charsetName) {
return stripBOM(b.Bytes()), nil
}
return b.Bytes(), nil
}
示例15: main
func main() {
flag.Parse()
var in io.Reader
in = os.Stdin
if *cs != "utf-8" {
e, _ := charset.Lookup(*cs)
in = transform.NewReader(in, e.NewDecoder())
}
s := bufio.NewScanner(in)
for s.Scan() {
line := s.Text()
if strings.Contains(line, ">,<") {
continue
}
endPhrase := strings.Index(line, "><")
if endPhrase != -1 {
phrase := line[:endPhrase+1]
rest := line[endPhrase+2:]
endScore := strings.Index(rest, ">")
if endScore != -1 {
score := rest[:endScore]
rest = strings.TrimSpace(rest[endScore+1:])
fmt.Println(phrase, score, rest)
continue
}
}
fmt.Println(line)
}
if err := s.Err(); err != nil {
log.Println(err)
}
}