package displaywidthimport ()// String calculates the display width of a string,// by iterating over grapheme clusters in the string// and summing their widths.func ( string) int {returnDefaultOptions.String()}// String calculates the display width of a string, for the given options, by// iterating over grapheme clusters in the string and summing their widths.func ( Options) ( string) int { := 0 := 0for < len() {// Try ASCII optimization := printableASCIILength([:])if > 0 { += += continue }// Not ASCII, use grapheme parsing := graphemes.FromString([:]) .AnsiEscapeSequences = .ControlSequences .AnsiEscapeSequences8Bit = .ControlSequences8Bit := for .Next() { := .Value() += graphemeWidth(, ) += len()// Quick check: if remaining might have printable ASCII, break to outer loopif < len() && [] >= 0x20 && [] <= 0x7E {break } }// Defensive, should not happen: if no progress was made, // skip a byte to prevent infinite loop. Only applies if // the grapheme parser misbehaves.if == { ++ } }return}// Bytes calculates the display width of a []byte,// by iterating over grapheme clusters in the byte slice// and summing their widths.func ( []byte) int {returnDefaultOptions.Bytes()}// Bytes calculates the display width of a []byte, for the given options, by// iterating over grapheme clusters in the slice and summing their widths.func ( Options) ( []byte) int { := 0 := 0for < len() {// Try ASCII optimization := printableASCIILength([:])if > 0 { += += continue }// Not ASCII, use grapheme parsing := graphemes.FromBytes([:]) .AnsiEscapeSequences = .ControlSequences .AnsiEscapeSequences8Bit = .ControlSequences8Bit := for .Next() { := .Value() += graphemeWidth(, ) += len()// Quick check: if remaining might have printable ASCII, break to outer loopif < len() && [] >= 0x20 && [] <= 0x7E {break } }// Defensive, should not happen: if no progress was made, // skip a byte to prevent infinite loop. Only applies if // the grapheme parser misbehaves.if == { ++ } }return}// Rune calculates the display width of a rune. You// should almost certainly use [String] or [Bytes] for// most purposes.//// The smallest unit of display width is a grapheme// cluster, not a rune. Iterating over runes to measure// width is incorrect in many cases.func ( rune) int {returnDefaultOptions.Rune()}// Rune calculates the display width of a rune, for the given options.//// You should almost certainly use [String] or [Bytes] for most purposes.//// The smallest unit of display width is a grapheme cluster, not a rune.// Iterating over runes to measure width is incorrect in many cases.func ( Options) ( rune) int {if < utf8.RuneSelf {returnasciiWidth(byte()) }// Surrogates (U+D800-U+DFFF) are invalid UTF-8.if >= 0xD800 && <= 0xDFFF {return0 }var [4]byte := utf8.EncodeRune([:], )// Skip the grapheme iteratorreturngraphemeWidth([:], )}const_Defaultproperty = 0// graphemeWidth returns the display width of a grapheme cluster.// The passed string must be a single grapheme cluster.func [ ~string | []byte]( , Options) int {iflen() == 0 {return0 }// C1 controls (0x80-0x9F) are zero-width when 8-bit control sequences // are enabled. This must be checked before the single-byte optimization // below, which would otherwise return width 1 for these bytes.if .ControlSequences8Bit && [0] >= 0x80 && [0] <= 0x9F {return0 }// Optimization: single-byte graphemes need no property lookupiflen() == 1 {returnasciiWidth([0]) }// Multi-byte grapheme clusters led by a C0 control (0x00-0x1F)if [0] <= 0x1F {return0 } , := lookup() := property()// Variation Selector 16 (VS16) requests emoji presentationif != _Wide && > 0 && len() >= +3 { := [ : +3]ifisVS16() { = _Wide }// VS15 (0x8E) requests text presentation but does not affect width, // in my reading of Unicode TR51. Falls through to return the base // character's property. }if .EastAsianWidth && == _East_Asian_Ambiguous { = _Wide }if > upperBound { = _Default }returnpropertyWidths[]}func ( byte) int {if <= 0x1F || == 0x7F {return0 }return1}// printableASCIILength returns the length of consecutive printable ASCII bytes// starting at the beginning of s.func [ string | []byte]( ) int { := 0for ; < len(); ++ { := []// Printable ASCII is 0x20-0x7E (space through tilde)if < 0x20 || > 0x7E {break } }// If the next byte is non-ASCII (>= 0x80), back off by 1. The grapheme // parser may group the last ASCII byte with subsequent non-ASCII bytes, // such as combining marks.if > 0 && < len() && [] >= 0x80 { -- }return}// isVS16 checks if the slice matches VS16 (U+FE0F) UTF-8 encoding// (EF B8 8F). It assumes len(s) >= 3.func [ ~string | []byte]( ) bool {return [0] == 0xEF && [1] == 0xB8 && [2] == 0x8F}// propertyWidths is a jump table of sorts, instead of a switchvarpropertyWidths = [4]int{_Default: 1,_Zero_Width: 0,_Wide: 2,_East_Asian_Ambiguous: 1,}constupperBound = property(len(propertyWidths) - 1)
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)