| 1 |
package displaywidth |
| 2 |
|
| 3 |
import ( |
| 4 |
"unicode/utf8" |
| 5 |
|
| 6 |
"github.com/clipperhouse/uax29/v2/graphemes" |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
func String(s string) int { |
| 13 |
return DefaultOptions.String(s) |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
func (options Options) String(s string) int { |
| 19 |
width := 0 |
| 20 |
pos := 0 |
| 21 |
|
| 22 |
for pos < len(s) { |
| 23 |
|
| 24 |
asciiLen := printableASCIILength(s[pos:]) |
| 25 |
if asciiLen > 0 { |
| 26 |
width += asciiLen |
| 27 |
pos += asciiLen |
| 28 |
continue |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
g := graphemes.FromString(s[pos:]) |
| 33 |
g.AnsiEscapeSequences = options.ControlSequences |
| 34 |
g.AnsiEscapeSequences8Bit = options.ControlSequences8Bit |
| 35 |
|
| 36 |
start := pos |
| 37 |
|
| 38 |
for g.Next() { |
| 39 |
v := g.Value() |
| 40 |
width += graphemeWidth(v, options) |
| 41 |
pos += len(v) |
| 42 |
|
| 43 |
|
| 44 |
if pos < len(s) && s[pos] >= 0x20 && s[pos] <= 0x7E { |
| 45 |
break |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
if pos == start { |
| 53 |
pos++ |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return width |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
func Bytes(s []byte) int { |
| 64 |
return DefaultOptions.Bytes(s) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
func (options Options) Bytes(s []byte) int { |
| 70 |
width := 0 |
| 71 |
pos := 0 |
| 72 |
|
| 73 |
for pos < len(s) { |
| 74 |
|
| 75 |
asciiLen := printableASCIILength(s[pos:]) |
| 76 |
if asciiLen > 0 { |
| 77 |
width += asciiLen |
| 78 |
pos += asciiLen |
| 79 |
continue |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
g := graphemes.FromBytes(s[pos:]) |
| 84 |
g.AnsiEscapeSequences = options.ControlSequences |
| 85 |
g.AnsiEscapeSequences8Bit = options.ControlSequences8Bit |
| 86 |
|
| 87 |
start := pos |
| 88 |
|
| 89 |
for g.Next() { |
| 90 |
v := g.Value() |
| 91 |
width += graphemeWidth(v, options) |
| 92 |
pos += len(v) |
| 93 |
|
| 94 |
|
| 95 |
if pos < len(s) && s[pos] >= 0x20 && s[pos] <= 0x7E { |
| 96 |
break |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
if pos == start { |
| 104 |
pos++ |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return width |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
func Rune(r rune) int { |
| 119 |
return DefaultOptions.Rune(r) |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
func (options Options) Rune(r rune) int { |
| 129 |
if r < utf8.RuneSelf { |
| 130 |
return asciiWidth(byte(r)) |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
if r >= 0xD800 && r <= 0xDFFF { |
| 135 |
return 0 |
| 136 |
} |
| 137 |
|
| 138 |
var buf [4]byte |
| 139 |
n := utf8.EncodeRune(buf[:], r) |
| 140 |
|
| 141 |
|
| 142 |
return graphemeWidth(buf[:n], options) |
| 143 |
} |
| 144 |
|
| 145 |
const _Default property = 0 |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
func graphemeWidth[T ~string | []byte](s T, options Options) int { |
| 150 |
if len(s) == 0 { |
| 151 |
return 0 |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
if options.ControlSequences8Bit && s[0] >= 0x80 && s[0] <= 0x9F { |
| 158 |
return 0 |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
if len(s) == 1 { |
| 163 |
return asciiWidth(s[0]) |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
if s[0] <= 0x1F { |
| 168 |
return 0 |
| 169 |
} |
| 170 |
|
| 171 |
p, sz := lookup(s) |
| 172 |
prop := property(p) |
| 173 |
|
| 174 |
|
| 175 |
if prop != _Wide && sz > 0 && len(s) >= sz+3 { |
| 176 |
vs := s[sz : sz+3] |
| 177 |
if isVS16(vs) { |
| 178 |
prop = _Wide |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
if options.EastAsianWidth && prop == _East_Asian_Ambiguous { |
| 186 |
prop = _Wide |
| 187 |
} |
| 188 |
|
| 189 |
if prop > upperBound { |
| 190 |
prop = _Default |
| 191 |
} |
| 192 |
|
| 193 |
return propertyWidths[prop] |
| 194 |
} |
| 195 |
|
| 196 |
func asciiWidth(b byte) int { |
| 197 |
if b <= 0x1F || b == 0x7F { |
| 198 |
return 0 |
| 199 |
} |
| 200 |
return 1 |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
func printableASCIILength[T string | []byte](s T) int { |
| 206 |
i := 0 |
| 207 |
for ; i < len(s); i++ { |
| 208 |
b := s[i] |
| 209 |
|
| 210 |
if b < 0x20 || b > 0x7E { |
| 211 |
break |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
if i > 0 && i < len(s) && s[i] >= 0x80 { |
| 219 |
i-- |
| 220 |
} |
| 221 |
|
| 222 |
return i |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
func isVS16[T ~string | []byte](s T) bool { |
| 228 |
return s[0] == 0xEF && s[1] == 0xB8 && s[2] == 0x8F |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
var propertyWidths = [4]int{ |
| 233 |
_Default: 1, |
| 234 |
_Zero_Width: 0, |
| 235 |
_Wide: 2, |
| 236 |
_East_Asian_Ambiguous: 1, |
| 237 |
} |
| 238 |
|
| 239 |
const upperBound = property(len(propertyWidths) - 1) |