width - ActiveState ActiveGo 1.8
...

Package width

import "golang.org/x/text/width"
Overview
Index
Examples

Overview ▾

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/.

Constants

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

const UnicodeVersion = "9.0.0"

type Kind

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

type Kind int
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

func (i Kind) String() string

type Properties

Properties provides access to width properties of a rune.

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

func Lookup

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

func LookupRune(r rune) Properties

LookupRune reports the Properties of rune r.

func LookupString

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

func (p Properties) Folded() rune

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

func (Properties) Kind

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

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

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

Transformer implements the transform.Transformer interface.

type Transformer struct {
    // contains filtered or unexported fields
}
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{}}
)

Example (Fold)

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

Example (Narrow)

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]: ←

Example (Widen)

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

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

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

func (Transformer) Reset

func (t Transformer) Reset()

Reset implements the transform.Transformer interface.

func (Transformer) Span

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

Span implements the transform.SpanningTransformer interface.

func (Transformer) String

func (t Transformer) String(s string) string

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

func (Transformer) Transform

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

Transform implements the transform.Transformer interface.