当前位置: 首页>>代码示例>>Golang>>正文


Golang encoding.Encoding类代码示例

本文整理汇总了Golang中golang.org/x/text/encoding.Encoding的典型用法代码示例。如果您正苦于以下问题:Golang Encoding类的具体用法?Golang Encoding怎么用?Golang Encoding使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Encoding类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: load

func load(direction string, enc encoding.Encoding) ([]byte, []byte, Transcoder, error) {
	basename, ext, count := "", "", 0
	for _, tf := range testdataFiles {
		if tf.enc == enc {
			basename, ext = tf.basename, tf.ext
			count++
		}
	}
	if count != 1 {
		if count == 0 {
			return nil, nil, nil, fmt.Errorf("no testdataFiles for %s", enc)
		}
		return nil, nil, nil, fmt.Errorf("too many testdataFiles for %s", enc)
	}
	dstFile := fmt.Sprintf("testdata/%s-%s.txt", basename, ext)
	srcFile := fmt.Sprintf("testdata/%s-utf-8.txt", basename)
	var coder Transcoder = encoding.ReplaceUnsupported(enc.NewEncoder())
	if direction == "Decode" {
		dstFile, srcFile = srcFile, dstFile
		coder = enc.NewDecoder()
	}
	dst, err := ioutil.ReadFile(dstFile)
	if err != nil {
		return nil, nil, nil, err
	}
	src, err := ioutil.ReadFile(srcFile)
	if err != nil {
		return nil, nil, nil, err
	}
	return dst, src, coder, nil
}
开发者ID:msoap,项目名称:html2data,代码行数:31,代码来源:encoding_test.go

示例2: encodeText

func encodeText(e encoding.Encoding, text string) []byte {
	res, _, err := transform.Bytes(e.NewEncoder(), []byte(text))
	if err != nil {
		panic(err)
	}
	return res
}
开发者ID:jwatt,项目名称:kythe,代码行数:7,代码来源:xrefs_test.go

示例3: NewReader

// NewReader returns a reader which decode from the given encoding, to utf8.
//
// If enc is nil, then only an utf8-enforcing replacement reader
// (see http://godoc.org/code.google.com/p/go.text/encoding#pkg-variables)
// is used.
func NewReader(r io.Reader, enc encoding.Encoding) io.Reader {
	if enc == nil || enc == encoding.Replacement {
		return transform.NewReader(r, encoding.Replacement.NewEncoder())
	}
	return transform.NewReader(r,
		transform.Chain(enc.NewDecoder(), encoding.Replacement.NewEncoder()))
}
开发者ID:xingskycn,项目名称:go,代码行数:12,代码来源:reader.go

示例4: ConvTo

func ConvTo(b []byte, e encoding.Encoding) (result []byte, err error) {
	w := new(bytes.Buffer)
	writer := transform.NewWriter(w, e.NewEncoder())
	defer writer.Close()

	if _, err = writer.Write(b); err != nil {
		return
	}
	return w.Bytes(), nil
}
开发者ID:fanyang01,项目名称:crawler,代码行数:10,代码来源:util.go

示例5: verifyFromUTF

func verifyFromUTF(enc encoding.Encoding, b byte, r rune) {

	encoder := enc.NewEncoder()

	out := make([]byte, 6)
	utf := make([]byte, utf8.RuneLen(r))
	utf8.EncodeRune(utf, r)

	ndst, nsrc, err := encoder.Transform(out, utf, true)
	So(err, ShouldBeNil)
	So(nsrc, ShouldEqual, len(utf))
	So(ndst, ShouldEqual, 1)
	So(b, ShouldEqual, out[0])
}
开发者ID:gdamore,项目名称:encoding,代码行数:14,代码来源:common_test.go

示例6: convertUnicodeToAscii

// Convert unicode text to ASCII text
// using specific codepage mapping.
func convertUnicodeToAscii(text string,
	codepage encoding.Encoding) []byte {
	b := []byte(text)
	// fmt.Printf("Text length: %d\n", len(b))
	var buf bytes.Buffer
	if codepage == nil {
		codepage = charmap.Windows1252
	}
	w := transform.NewWriter(&buf, codepage.NewEncoder())
	defer w.Close()
	w.Write(b)
	// fmt.Printf("Buffer length: %d\n", len(buf.Bytes()))
	return buf.Bytes()
}
开发者ID:d2r2,项目名称:go-max7219,代码行数:16,代码来源:matrix.go

示例7: GetUTF8HtmlTitle

func GetUTF8HtmlTitle(str string) string {
	var e encoding.Encoding
	var name string

	e, name, _ = charset.DetermineEncoding([]byte(str), "text/html")
	if name == "windows-1252" {
		e, name, _ = charset.DetermineEncoding([]byte(str), "text/html;charset=gbk")
	}
	r := transform.NewReader(strings.NewReader(str), e.NewDecoder())
	if b, err := ioutil.ReadAll(r); err != nil {
		return ""
	} else {
		return getHtmlTitle(string(b))
	}
	return ""
}
开发者ID:yetist,项目名称:xmppbot,代码行数:16,代码来源:net.go

示例8: verifyToUTF

func verifyToUTF(enc encoding.Encoding, b byte, r rune) {
	decoder := enc.NewDecoder()

	out := make([]byte, 6)
	nat := []byte{b}
	utf := make([]byte, utf8.RuneLen(r))
	utf8.EncodeRune(utf, r)

	ndst, nsrc, err := decoder.Transform(out, nat, true)
	So(err, ShouldBeNil)
	So(nsrc, ShouldEqual, 1)
	if !bytes.Equal(utf, out[:ndst]) {
		Printf("UTF expected %v, but got %v for %x\n", utf, out, b)
	}
	So(bytes.Equal(utf, out[:ndst]), ShouldBeTrue)
}
开发者ID:gdamore,项目名称:encoding,代码行数:16,代码来源:common_test.go

示例9: TextFromResponse

// There might be a shortcut when stream is not required, comparing to StreamFromResponse().
func TextFromResponse(response *http.Response) (text, textType string, err error) {
	charset, textType, err := CharsetFromContentType(response.Header.Get("Content-Type"))
	if err != nil {
		return
	}

	body := getBodyStream(response)

	var data []byte
	var stream io.Reader
	var encoding encoding.Encoding
	if (len(charset) == 0) && (textType == "html") {
		data, err = ioutil.ReadAll(body)
		if err != nil {
			return
		}
		charset = DetectCharset(data)
		if encoding, err = GetEncoding(charset); err != nil {
			return
		}
		// No encoding, it assumed as UTF-8.
		if encoding == nil {
			text = string(data)
			return
		}

		stream = transform.NewReader(bytes.NewReader(data), encoding.NewDecoder())
	} else {
		if encoding, err = GetEncoding(charset); err != nil {
			return
		}
		if encoding != nil {
			stream = transform.NewReader(body, encoding.NewDecoder())
		} else {
			stream = body
		}
	}

	if data, err = ioutil.ReadAll(stream); err == nil {
		text = string(data)
	}
	return
}
开发者ID:albertjin,项目名称:httputil2,代码行数:44,代码来源:client.go

示例10: NewLine

// NewLine creates a new Line reader object
func NewLine(input io.Reader, codec encoding.Encoding, bufferSize int) (*Line, error) {

	encoder := codec.NewEncoder()

	// Create newline char based on encoding
	nl, _, err := transform.Bytes(encoder, []byte{'\n'})
	if err != nil {
		return nil, err
	}

	return &Line{
		reader:     input,
		codec:      codec,
		bufferSize: bufferSize,
		nl:         nl,
		decoder:    codec.NewDecoder(),
		inBuffer:   streambuf.New(nil),
		outBuffer:  streambuf.New(nil),
	}, nil
}
开发者ID:YaSuenag,项目名称:hsbeat,代码行数:21,代码来源:line.go

示例11: readToStringWithCharset

// 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
}
开发者ID:caoqianli,项目名称:httpcap,代码行数:23,代码来源:text_utils.go

示例12: FileInfo

func (client *Client) FileInfo(url string, encoding encoding.Encoding) (filename string, size int, status int, errMsg error) {
	status = http.StatusOK

	resp, err := client.Head(url)
	if err != nil {
		status = http.StatusBadGateway
		errMsg = fmt.Errorf("Failed to HEAD from %s: %s", url, err)
		return
	}

	// Filename.
	disposition := resp.Header.Get("Content-Disposition")
	disposition, err = encoding.NewDecoder().String(disposition)
	if err != nil {
		status = http.StatusInternalServerError
		errMsg = fmt.Errorf("Failed to decode Content-Disposition: %s", err)
		return
	}

	// Parse disposition header.
	_, params, err := mime.ParseMediaType(disposition)
	if err != nil {
		status = http.StatusInternalServerError
		errMsg = fmt.Errorf("Failed to parse header Content-Disposition of file at %s: %s", url, err)
		return
	}
	filename = params["filename"]

	// File size.
	sizeStr := resp.Header.Get("Content-Length")
	size, err = strconv.Atoi(sizeStr)
	if err != nil {
		status = http.StatusInternalServerError
		errMsg = fmt.Errorf("Failed to convert Content-Length (%s) to int: %s", sizeStr, err)
		return
	}

	return
}
开发者ID:tsinghua-io,项目名称:api-server,代码行数:39,代码来源:client.go

示例13: ConvToUTF8

func ConvToUTF8(b []byte, e encoding.Encoding) (result []byte, err error) {
	reader := transform.NewReader(bytes.NewReader(b), unicode.BOMOverride(e.NewDecoder()))
	return ioutil.ReadAll(reader)
}
开发者ID:fanyang01,项目名称:crawler,代码行数:4,代码来源:util.go

示例14: enc

func enc(encoding encoding.Encoding) Decoder {
	return trans(encoding.NewDecoder())
}
开发者ID:GeoffColburn,项目名称:filebeat,代码行数:3,代码来源:encoding.go

示例15: dec

func dec(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
	return "Decode", e.NewDecoder(), nil
}
开发者ID:msoap,项目名称:html2data,代码行数:3,代码来源:all_test.go


注:本文中的golang.org/x/text/encoding.Encoding类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。