package runes
import "golang.org/x/text/runes"
Package runes provide transforms for UTF-8 encoded text.
Index ¶
- type Set
- func In(rt *unicode.RangeTable) Set
- func NotIn(rt *unicode.RangeTable) Set
- func Predicate(f func(rune) bool) Set
- type Transformer
- func If(s Set, tIn, tNotIn transform.Transformer) Transformer
- func Map(mapping func(rune) rune) Transformer
- func Remove(s Set) Transformer
- func ReplaceIllFormed() Transformer
- func (t Transformer) Bytes(b []byte) []byte
- func (t Transformer) Reset()
- func (t Transformer) Span(b []byte, atEOF bool) (n int, err error)
- func (t Transformer) String(s string) string
- func (t Transformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error)
Examples ¶
Types ¶
type Set ¶
A Set is a collection of runes.
func In ¶
func In(rt *unicode.RangeTable) Set
In creates a Set with a Contains method that returns true for all runes in
the given RangeTable.
Code:play
Output:Example¶
package main
import (
"fmt"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/width"
)
func main() {
// Convert Latin characters to their canonical form, while keeping other
// width distinctions.
t := runes.If(runes.In(unicode.Latin), width.Fold, nil)
s, _, _ := transform.String(t, "アルアノリウ tech / アルアノリウ tech")
fmt.Println(s)
}
アルアノリウ tech / アルアノリウ tech
func NotIn ¶
func NotIn(rt *unicode.RangeTable) Set
NotIn creates a Set with a Contains method that returns true for all runes not in the given RangeTable.
func Predicate ¶
Predicate creates a Set with a Contains method that returns f(r).
type Transformer ¶
type Transformer struct {
// contains filtered or unexported fields
}
Transformer implements the transform.Transformer interface.
func If ¶
func If(s Set, tIn, tNotIn transform.Transformer) Transformer
If returns a transformer that applies tIn to consecutive runes for which
s.Contains(r) and tNotIn to consecutive runes for which !s.Contains(r). Reset
is called on tIn and tNotIn at the start of each run. A Nop transformer will
substitute a nil value passed to tIn or tNotIn. Invalid UTF-8 is translated
to RuneError to determine which transformer to apply, but is passed as is to
the respective transformer.
Code:play
Output:Example¶
package main
import (
"fmt"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/width"
)
func main() {
// Widen everything but ASCII.
isASCII := func(r rune) bool { return r <= unicode.MaxASCII }
t := runes.If(runes.Predicate(isASCII), nil, width.Widen)
s, _, _ := transform.String(t, "アルアノリウ tech / 中國 / 5₩")
fmt.Println(s)
}
アルアノリウ tech / 中國 / 5₩
func Map ¶
func Map(mapping func(rune) rune) Transformer
Map returns a Transformer that maps the runes in the input using the given
mapping. Illegal bytes in the input are converted to utf8.RuneError before
being passed to the mapping func.
Code:play
Output:Example¶
package main
import (
"fmt"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
)
func main() {
replaceHyphens := runes.Map(func(r rune) rune {
if unicode.Is(unicode.Hyphen, r) {
return '|'
}
return r
})
s, _, _ := transform.String(replaceHyphens, "a-b‐c⸗d﹣e")
fmt.Println(s)
}
a|b|c|d|e
func Remove ¶
func Remove(s Set) Transformer
Remove returns a Transformer that removes runes r for which s.Contains(r).
Illegal input bytes are replaced by RuneError before being passed to f.
Code:play
Output:Example¶
package main
import (
"fmt"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
func main() {
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
s, _, _ := transform.String(t, "résumé")
fmt.Println(s)
}
resume
func ReplaceIllFormed ¶
func ReplaceIllFormed() Transformer
ReplaceIllFormed returns a transformer that replaces all input bytes that are not part of a well-formed UTF-8 code sequence with utf8.RuneError.
func (Transformer) Bytes ¶
func (t Transformer) Bytes(b []byte) []byte
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 (Transformer) Reset ¶
func (t Transformer) Reset()
func (Transformer) Span ¶
func (t Transformer) Span(b []byte, atEOF bool) (n int, err error)
func (Transformer) String ¶
func (t Transformer) String(s string) string
String returns a string with the result of converting s using t. It calls Reset on t. It returns the empty string if any error was found. This can only happen if an error-producing Transformer is passed to If.
func (Transformer) Transform ¶
func (t Transformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error)
Source Files ¶
cond.go runes.go
- Version
- v0.22.0 (latest)
- Published
- Feb 4, 2025
- Platform
- linux/amd64
- Imports
- 3 packages
- Last checked
- 1 day ago –
Tools for package owners.