本文整理汇总了Golang中golang.org/x/text/transform.Bytes函数的典型用法代码示例。如果您正苦于以下问题:Golang Bytes函数的具体用法?Golang Bytes怎么用?Golang Bytes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Bytes函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Encode
func Encode(code uint8, text string) []byte {
switch code { // в зависимости от подходящей кодировки выбираем соответствующий метод кодирования
case 8: // ucs8
enc := unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewEncoder()
es, _, _ := transform.Bytes(enc, []byte(text))
return es
case 3: // latin1
es, _, _ := transform.Bytes(charmap.Windows1252.NewEncoder(), []byte(text))
return es
case 0: // декодируем в GSM 03.38
var result bytes.Buffer
for _, r := range text {
if nr, ok := utf8GsmChars[r]; ok { // делаем замены известным символам
result.WriteString(nr)
continue
}
if r > '\u007F' { // удаляем все, что не входит в формат
result.WriteRune('?')
continue
}
result.WriteRune(r) // добавляем как есть
}
return result.Bytes()
default:
return []byte(text)
}
}
示例2: Fuzz
func Fuzz(data []byte) int {
_, _, err := transform.Bytes(new(Normalize), data)
if err != nil {
panic(err)
}
_, _, err = transform.Bytes(ToCRLF{}, data)
if err != nil {
panic(err)
}
return 0
}
示例3: Bytes
// Bytes returns a new byte slice with the result of converting b using t. It
// calls Reset on t. It returns nil if any error was found. This can only happen
// if an error-producing Transformer is passed to If.
func (t Transformer) Bytes(b []byte) []byte {
b, _, err := transform.Bytes(t, b)
if err != nil {
return nil
}
return b
}
示例4: Bytes
// Bytes returns a new byte slice with the result of applying the profile to b.
func (p Profile) Bytes(b []byte) ([]byte, error) {
b, _, err := transform.Bytes(p.NewTransformer(), b)
if err == nil && p.options.disallowEmpty && len(b) == 0 {
return b, errors.New("enforce resulted in empty string")
}
return b, err
}
示例5: Bytes
// Bytes converts bytes from UTF-8. It returns the converted bytes or nil, err if
// any error occurred.
func (e *Encoder) Bytes(b []byte) ([]byte, error) {
b, _, err := transform.Bytes(e, b)
if err != nil {
return nil, err
}
return b, nil
}
示例6: encodeText
func encodeText(e encoding.Encoding, text string) []byte {
res, _, err := transform.Bytes(e.NewEncoder(), []byte(text))
if err != nil {
panic(err)
}
return res
}
示例7: Decode
// Decode from Latin1.
func (s Latin1) Decode() []byte {
e := charmap.Windows1252.NewDecoder()
es, _, err := transform.Bytes(e, s)
if err != nil {
return s
}
return es
}
示例8: Decode
// Decode from UCS2.
func (s UCS2) Decode() []byte {
e := unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
es, _, err := transform.Bytes(e.NewDecoder(), s)
if err != nil {
return s
}
return es
}
示例9: base64StringWithGB2312
func base64StringWithGB2312(txt string) string {
buf := bytes.NewBufferString("=?GB2312?B?")
bs, _, e := transform.Bytes(simplifiedchinese.GB18030.NewEncoder(), []byte(txt))
if nil != e {
return qpString(txt)
}
buf.WriteString(base64.StdEncoding.EncodeToString(bs))
buf.WriteString("?=")
return buf.String()
}
示例10: qpStringWithGB2312
func qpStringWithGB2312(txt string) string {
buf := bytes.NewBufferString("=?GB2312?Q?")
bs, _, e := transform.Bytes(simplifiedchinese.GB18030.NewEncoder(), []byte(txt))
if nil != e {
return qpString(txt)
}
w := qp.NewWriter(buf)
w.Write(bs)
w.Close()
buf.WriteString("?=")
return buf.String()
}
示例11: Decode
func Decode(code uint8, text []byte) string {
switch code {
case 8: // UCS2
es, _, _ := transform.Bytes(
unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewDecoder(), text)
return string(es)
case 3: // latin1 (windows1252)
es, _, _ := transform.Bytes(charmap.Windows1252.NewDecoder(), text)
return string(es)
case 0: // декодируем из формата GSM 03.38
var result bytes.Buffer
for _, r := range text {
if nr, ok := gsmUtf8Chars[rune(r)]; ok { // делаем замены известным символам
result.WriteString(nr)
continue
}
result.WriteByte(r) // добавляем как есть
}
return result.String()
default:
return string(text)
}
}
示例12: normalize
// normalize does unicode normalization.
func normalize(in []byte) ([]byte, error) {
// We need a new transformer for each input as it cannot be reused.
filter := func(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks (to be removed)
}
transformer := transform.Chain(norm.NFD, transform.RemoveFunc(filter), norm.NFC)
out, _, err := transform.Bytes(transformer, in)
out = bytes.Map(func(r rune) rune {
if unicode.IsPunct(r) { // Replace punctuations with spaces.
return ' '
}
return unicode.ToLower(r) // Convert to lower case.
}, out)
return out, err
}
示例13: removeNonAlphaNumeric
func removeNonAlphaNumeric(s string) string {
in := []rune(s)
res := make([]rune, len(in))
i := 0
for _, x := range s {
if x == '-' {
res[i] = ' '
i++
continue
}
if unicode.IsLetter(x) || unicode.IsDigit(x) || unicode.IsSpace(x) {
res[i] = unicode.ToLower(x)
i++
}
}
result, _, _ := transform.Bytes(transformer, []byte(string(res[:i])))
return string(result)
}
示例14: 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
}
示例15: ToUtf8
// ToUtf8 takes a page body, determines its character encoding, and converts
// it to UTF8.
func ToUtf8(html []byte) ([]byte, error) {
r, err := charsetDetector.DetectBest(html)
if err != nil {
return nil, err
}
if r.Charset == "UTF-8" || r.Charset == "ISO-8859-1" {
return html, nil
}
t, ok := charsetDetectors[r.Charset]
if !ok {
return nil, fmt.Errorf(
"could not find charset decoder for `%s`",
r.Charset)
}
html, _, err = transform.Bytes(t.NewDecoder(), html)
return html, err
}