File: gwidth.go

package info (click to toggle)
golang-sourcehut-rockorager-vaxis 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: makefile: 9
file content (40 lines) | stat: -rw-r--r-- 752 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package vaxis

import (
	"strings"

	"github.com/mattn/go-runewidth"
	"github.com/rivo/uniseg"
)

type graphemeWidthMethod int

const (
	wcwidth graphemeWidthMethod = iota
	noZWJ                       // we do unicodeStd but not if there is a ZWJ
	unicodeStd
)

func gwidth(s string, method graphemeWidthMethod) int {
	switch method {
	case noZWJ:
		s = strings.ReplaceAll(s, "\u200D", "")
		return uniseg.StringWidth(s)
	case unicodeStd:
		return uniseg.StringWidth(s)
	default:
		total := 0
		for _, r := range s {
			if r >= 0xFE00 && r <= 0xFE0F {
				// Variation Selectors 1 - 16
				continue
			}
			if r >= 0xE0100 && r <= 0xE01EF {
				// Variation Selectors 17-256
				continue
			}
			total += runewidth.RuneWidth(r)
		}
		return total
	}
}