text: golang.org/x/text/width Index | Examples | Files

package width

import "golang.org/x/text/width"

Package width provides functionality for handling different widths in text.

Wide characters behave like ideographs; they tend to allow line breaks after each character and remain upright in vertical text layout. Narrow characters are kept together in words or runs that are rotated sideways in vertical text layout.

For more information, see http://unicode.org/reports/tr11/.

Index

Examples

Package Files

kind_string.go tables9.0.0.go transform.go trieval.go width.go

Constants

const UnicodeVersion = "9.0.0"

UnicodeVersion is the Unicode version from which the tables in this package are derived.

type Kind Uses

type Kind int

Kind indicates the type of width property as defined in http://unicode.org/reports/tr11/.

const (
    // Neutral characters do not occur in legacy East Asian character sets.
    Neutral Kind = iota

    // EastAsianAmbiguous characters that can be sometimes wide and sometimes
    // narrow and require additional information not contained in the character
    // code to further resolve their width.
    EastAsianAmbiguous

    // EastAsianWide characters are wide in its usual form. They occur only in
    // the context of East Asian typography. These runes may have explicit
    // halfwidth counterparts.
    EastAsianWide

    // EastAsianNarrow characters are narrow in its usual form. They often have
    // fullwidth counterparts.
    EastAsianNarrow

    // EastAsianFullwidth characters have a compatibility decompositions of type
    // wide that map to a narrow counterpart.
    EastAsianFullwidth

    // EastAsianHalfwidth characters have a compatibility decomposition of type
    // narrow that map to a wide or ambiguous counterpart, plus U+20A9 ₩ WON
    // SIGN.
    EastAsianHalfwidth
)

func (Kind) String Uses

func (i Kind) String() string

type Properties Uses

type Properties struct {
    // contains filtered or unexported fields
}

Properties provides access to width properties of a rune.

func Lookup Uses

func Lookup(b []byte) (p Properties, size int)

Lookup reports the Properties of the first rune in b and the number of bytes of its UTF-8 encoding.

func LookupRune Uses

func LookupRune(r rune) Properties

LookupRune reports the Properties of rune r.

func LookupString Uses

func LookupString(s string) (p Properties, size int)

LookupString reports the Properties of the first rune in s and the number of bytes of its UTF-8 encoding.

func (Properties) Folded Uses

func (p Properties) Folded() rune

Folded returns the folded variant of a rune or 0 if the rune is canonical.

func (Properties) Kind Uses

func (p Properties) Kind() Kind

Kind returns the Kind of a rune as defined in Unicode TR #11. See http://unicode.org/reports/tr11/ for more details.

func (Properties) Narrow Uses

func (p Properties) Narrow() rune

Narrow returns the narrow variant of a rune or 0 if the rune is already narrow or doesn't have a narrow variant.

func (Properties) Wide Uses

func (p Properties) Wide() rune

Wide returns the wide variant of a rune or 0 if the rune is already wide or doesn't have a wide variant.

type Transformer Uses

type Transformer struct {
    // contains filtered or unexported fields
}

Transformer implements the transform.Transformer interface.

var (
    // Fold is a transform that maps all runes to their canonical width.
    //
    // Note that the NFKC and NFKD transforms in golang.org/x/text/unicode/norm
    // provide a more generic folding mechanism.
    Fold Transformer = Transformer{foldTransform{}}

    // Widen is a transform that maps runes to their wide variant, if
    // available.
    Widen Transformer = Transformer{wideTransform{}}

    // Narrow is a transform that maps runes to their narrow variant, if
    // available.
    Narrow Transformer = Transformer{narrowTransform{}}
)

Code:

s := "abヲ₩○¥A"
f := width.Fold.String(s)
fmt.Printf("%U: %s\n", []rune(s), s)
fmt.Printf("%U: %s\n", []rune(f), f)

Output:

[U+0061 U+0062 U+FF66 U+FFE6 U+FFEE U+FFE5 U+FF21]: abヲ₩○¥A
[U+0061 U+0062 U+30F2 U+20A9 U+25CB U+00A5 U+0041]: abヲ₩○¥A

Code:

s := "abヲ₩○¥A"
n := width.Narrow.String(s)
fmt.Printf("%U: %s\n", []rune(s), s)
fmt.Printf("%U: %s\n", []rune(n), n)

// Ambiguous characters with a halfwidth equivalent get mapped as well.
s = "←"
n = width.Narrow.String(s)
fmt.Printf("%U: %s\n", []rune(s), s)
fmt.Printf("%U: %s\n", []rune(n), n)

Output:

[U+0061 U+0062 U+30F2 U+FFE6 U+25CB U+FFE5 U+FF21]: abヲ₩○¥A
[U+0061 U+0062 U+FF66 U+20A9 U+FFEE U+00A5 U+0041]: abヲ₩○¥A
[U+2190]: ←
[U+FFE9]: ←

Code:

s := "ab¥ヲ₩○"
w := width.Widen.String(s)
fmt.Printf("%U: %s\n", []rune(s), s)
fmt.Printf("%U: %s\n", []rune(w), w)

Output:

[U+0061 U+0062 U+00A5 U+FF66 U+20A9 U+FFEE]: ab¥ヲ₩○
[U+FF41 U+FF42 U+FFE5 U+30F2 U+FFE6 U+25CB]: ab¥ヲ₩○

func (Transformer) Bytes Uses

func (t Transformer) Bytes(b []byte) []byte

Bytes returns a new byte slice with the result of applying t to b.

func (Transformer) Reset Uses

func (t Transformer) Reset()

Reset implements the transform.Transformer interface.

func (Transformer) Span Uses

func (t Transformer) Span(src []byte, atEOF bool) (n int, err error)

Span implements the transform.SpanningTransformer interface.

func (Transformer) String Uses

func (t Transformer) String(s string) string

String returns a string with the result of applying t to s.

func (Transformer) Transform Uses

func (t Transformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error)

Transform implements the transform.Transformer interface.

Package width imports 3 packages (graph) and is imported by 19 packages. Updated 2017-12-16. Refresh now. Tools for package owners.