本文整理汇总了Golang中golang.org/x/text/transform.Chain函数的典型用法代码示例。如果您正苦于以下问题:Golang Chain函数的具体用法?Golang Chain怎么用?Golang Chain使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Chain函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewTransformer
// NewTransformer creates a new transform.Transformer that performs the PRECIS
// preparation and enforcement steps on the given UTF-8 encoded bytes.
func (p Profile) NewTransformer() *Transformer {
var ts []transform.Transformer
if p.options.allowwidechars {
ts = append(ts, width.Fold)
}
ts = append(ts, checker{p: p})
if p.options.width != nil {
ts = append(ts, width.Fold)
}
for _, f := range p.options.additional {
ts = append(ts, f())
}
if p.options.cases {
ts = append(ts, transform.Chain(
cases.Upper(language.Und), cases.Lower(language.Und),
))
}
ts = append(ts, p.options.norm)
// TODO: Apply directionality rule (blocking on the Bidi package)
// TODO: Add the disallow empty rule with a dummy transformer?
return &Transformer{transform.Chain(ts...)}
}
示例2: NormalizeTitle
func NormalizeTitle(title string) string {
normalizedTitle := title
normalizedTitle = strings.ToLower(normalizedTitle)
normalizedTitle = RomanizeHepburn(title)
normalizedTitle = strings.ToLower(normalizedTitle)
normalizedTitle = RemoveTrailingApostrophe(normalizedTitle)
normalizedTitle, _, _ = transform.String(transform.Chain(
norm.NFD,
transform.RemoveFunc(func(r rune) bool {
return unicode.Is(unicode.Mn, r)
}),
norm.NFC), normalizedTitle)
normalizedTitle = strings.ToLower(normalizedTitle)
normalizedTitle = regexp.MustCompile(`\(\d+\)`).ReplaceAllString(normalizedTitle, " ")
normalizedTitle = strings.Map(func(r rune) rune {
if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '.' {
return ' '
}
return r
}, normalizedTitle)
normalizedTitle = regexp.MustCompile(`\s+`).ReplaceAllString(normalizedTitle, " ")
normalizedTitle = strings.TrimSpace(normalizedTitle)
return normalizedTitle
}
示例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()))
}
示例4: NewTransformer
// NewTransformer creates a new transform.Transformer that performs the PRECIS
// preparation and enforcement steps on the given UTF-8 encoded bytes.
func (p *Profile) NewTransformer() *Transformer {
var ts []transform.Transformer
// These transforms are applied in the order defined in
// https://tools.ietf.org/html/rfc7564#section-7
if p.options.foldWidth {
ts = append(ts, width.Fold)
}
for _, f := range p.options.additional {
ts = append(ts, f())
}
if p.options.cases != nil {
ts = append(ts, p.options.cases)
}
ts = append(ts, p.options.norm)
if p.options.bidiRule {
ts = append(ts, bidirule.New())
}
ts = append(ts, &checker{p: p, allowed: p.Allowed()})
// TODO: Add the disallow empty rule with a dummy transformer?
return &Transformer{transform.Chain(ts...)}
}
示例5: main
func main() {
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
r := transform.NewReader(os.Stdin, t)
if _, err := io.Copy(os.Stdout, r); err != nil {
log.Fatal(err)
}
}
示例6: removeNlChars
func removeNlChars(str string) string {
isOk := func(r rune) bool {
return r < 32 || r >= 127
}
t := transform.Chain(norm.NFKD, transform.RemoveFunc(isOk))
str, _, _ = transform.String(t, str)
return str
}
示例7: ExampleRemove
func ExampleRemove() {
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
s, _, _ := transform.String(t, "résumé")
fmt.Println(s)
// Output:
// resume
}
示例8: decodeTransfer
// DecodeTransfer decodes base64, quoted-printable or plain text.
func decodeTransfer(r io.Reader, label string) io.Reader {
switch strings.ToLower(label) {
case "base64":
return base64.NewDecoder(base64.StdEncoding, transform.NewReader(r, nonASCIITransformer{}))
case "quoted-printable":
return quotedprintable.NewReader(transform.NewReader(r, transform.Chain(nonASCIITransformer{}, newlineAppendTransformer{})))
case "", "7bit", "8bit", "binary":
return r
default:
return failReader{fmt.Errorf("unsupported transfer encoding: %v", label)}
}
}
示例9: stripCtlAndExtFromUnicode
//function to sanitize input
//from: http://rosettacode.org/wiki/Strip_control_codes_and_extended_characters_from_a_string#Go
func stripCtlAndExtFromUnicode(str string) string {
isOk := func(r rune) bool {
return r < 32 || r >= 127
}
// The isOk filter is such that there is no need to chain to norm.NFC
t := transform.Chain(norm.NFKD, transform.RemoveFunc(isOk))
// This Transformer could also trivially be applied as an io.Reader
// or io.Writer filter to automatically do such filtering when reading
// or writing data anywhere.
str, _, _ = transform.String(t, str)
return str
}
示例10: GetCompatibleString
// GetCompatibleString removes all the special characters
// from the string name to create a new string compatible
// with different file names.
func GetCompatibleString(name string) string {
// Replace all the & signs with and text
name = strings.Replace(name, "&", "and", -1)
// Change all the characters to ASCII
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
result, _, _ := transform.String(t, name)
// Replace all the spaces with underscore
s, _ := regexp.Compile(`\s+`)
result = s.ReplaceAllString(result, "_")
// Remove all the non alphanumeric characters
r, _ := regexp.Compile(`\W`)
result = r.ReplaceAllString(result, "")
return result
}
示例11: normalize
func normalize(name, src string) (string, error) {
if name == "" {
name = baseWithoutExt(src)
}
t := transform.Chain(norm.NFD, transform.RemoveFunc(remove), norm.NFC)
name = strings.TrimSpace(name)
name, _, err := transform.String(t, name)
if err != nil {
return "", err
}
name = strings.ToLower(name)
name = strings.Replace(name, " ", "_", -1)
return name, nil
}
示例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: ExampleUTF8Validator
func ExampleUTF8Validator() {
for i := 0; i < 2; i++ {
transformer := charmap.Windows1252.NewEncoder()
if i == 1 {
transformer = transform.Chain(encoding.UTF8Validator, transformer)
}
dst := make([]byte, 256)
src := []byte("abc\xffxyz") // src is invalid UTF-8.
nDst, nSrc, err := transformer.Transform(dst, src, true)
fmt.Printf("i=%d: produced %q, consumed %q, error %v\n",
i, dst[:nDst], src[:nSrc], err)
}
// Output:
// i=0: produced "abc\x1axyz", consumed "abc\xffxyz", error <nil>
// i=1: produced "abc", consumed "abc", error encoding: invalid UTF-8
}
示例14: 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(' ')
}
示例15: ExampleUTF8Validator
func ExampleUTF8Validator() {
for i := 0; i < 2; i++ {
var transformer transform.Transformer
transformer = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewEncoder()
if i == 1 {
transformer = transform.Chain(encoding.UTF8Validator, transformer)
}
dst := make([]byte, 256)
src := []byte("abc\xffxyz") // src is invalid UTF-8.
nDst, nSrc, err := transformer.Transform(dst, src, true)
fmt.Printf("i=%d: produced %q, consumed %q, error %v\n",
i, dst[:nDst], src[:nSrc], err)
}
// Output:
// i=0: produced "\x00a\x00b\x00c\xff\xfd\x00x\x00y\x00z", consumed "abc\xffxyz", error <nil>
// i=1: produced "\x00a\x00b\x00c", consumed "abc", error encoding: invalid UTF-8
}