File: generate.go

package info (click to toggle)
golang-github-mattn-go-runewidth 0.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, bullseye-backports
  • size: 172 kB
  • sloc: sh: 9; makefile: 5
file content (212 lines) | stat: -rw-r--r-- 3,554 bytes parent folder | download
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//  build ignore

// Generate runewidth_table.go from data at https://unicode.org/

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"go/format"
	"io"
	"io/ioutil"
	"log"
	"os"
	"strings"
)

type rrange struct {
	lo rune
	hi rune
}

func generate(out io.Writer, v string, arr []rrange) {
	fmt.Fprintf(out, "var %s = table{\n\t", v)
	for i := 0; i < len(arr); i   {
		fmt.Fprintf(out, "{0xX, 0xX},", arr[i].lo, arr[i].hi)
		if i < len(arr)-1 {
			if i%3 == 2 {
				fmt.Fprint(out, "\n\t")
			} else {
				fmt.Fprint(out, " ")
			}
		}
	}
	fmt.Fprintln(out, "\n}")
}

func shapeup(p *[]rrange) {
	arr := *p
	for i := 0; i < len(arr)-1; i   {
		if arr[i].hi 1 == arr[i 1].lo {
			lo := arr[i].lo
			arr = append(arr[:i], arr[i 1:]...)
			arr[i].lo = lo
			i--
		}
	}
	*p = arr
}

func eastasian(out io.Writer, in io.Reader) error {
	scanner := bufio.NewScanner(in)

	dbl := []rrange{}
	amb := []rrange{}
	cmb := []rrange{}
	na := []rrange{}
	nu := []rrange{}
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, "#") {
			continue
		}
		var r1, r2 rune
		var ss string
		n, err := fmt.Sscanf(line, "%x..%x;%s ", &r1, &r2, &ss)
		if err != nil || n == 2 {
			n, err = fmt.Sscanf(line, "%x;%s ", &r1, &ss)
			if err != nil || n != 2 {
				continue
			}
			r2 = r1
		}

		if strings.Index(line, "COMBINING") != -1 {
			cmb = append(cmb, rrange{
				lo: r1,
				hi: r2,
			})
		}

		switch ss {
		case "W", "F":
			dbl = append(dbl, rrange{
				lo: r1,
				hi: r2,
			})
		case "A":
			amb = append(amb, rrange{
				lo: r1,
				hi: r2,
			})
		case "Na":
			if r1 > 0xFF {
				na = append(na, rrange{
					lo: r1,
					hi: r2,
				})
			}
		case "N":
			nu = append(nu, rrange{
				lo: r1,
				hi: r2,
			})
		}
	}

	shapeup(&cmb)
	generate(out, "combining", cmb)
	fmt.Fprintln(out)

	shapeup(&dbl)
	generate(out, "doublewidth", dbl)
	fmt.Fprintln(out)

	shapeup(&amb)
	generate(out, "ambiguous", amb)
	fmt.Fprint(out)

	shapeup(&na)
	generate(out, "notassigned", na)
	fmt.Fprintln(out)

	shapeup(&nu)
	generate(out, "neutral", nu)
	fmt.Fprintln(out)

	return nil
}

func emoji(out io.Writer, in io.Reader) error {
	scanner := bufio.NewScanner(in)

	for scanner.Scan() {
		line := scanner.Text()
		if strings.Index(line, "Extended_Pictographic ; No") != -1 {
			break
		}
	}

	if scanner.Err() != nil {
		return scanner.Err()
	}

	arr := []rrange{}
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, "#") {
			continue
		}
		var r1, r2 rune
		n, err := fmt.Sscanf(line, "%x..%x ", &r1, &r2)
		if err != nil || n == 1 {
			n, err = fmt.Sscanf(line, "%x ", &r1)
			if err != nil || n != 1 {
				continue
			}
			r2 = r1
		}
		if r2 < 0xFF {
			continue
		}

		arr = append(arr, rrange{
			lo: r1,
			hi: r2,
		})
	}

	shapeup(&arr)
	generate(out, "emoji", arr)

	return nil
}

func main() {
	var buf bytes.Buffer
	f := &buf
	fmt.Fprint(f, "// Code generated by script/generate.go. DO NOT EDIT.\n\n")

	fmt.Fprint(f, "package runewidth\n\n")

	f_eaw, err := os.Open("/usr/share/unicode/EastAsianWidth.txt")
	if err != nil {
		log.Fatal(err)
	}
	txt_eaw := new(bytes.Buffer)
	txt_eaw.ReadFrom(f_eaw)
	defer f_eaw.Close()

	eastasian(f, txt_eaw)

	f_ed, err := os.Open("/usr/share/unicode/emoji/emoji-data.txt")
	if err != nil {
		log.Fatal(err)
	}
	txt_ed := new(bytes.Buffer)
	txt_ed.ReadFrom(f_ed)
	defer f_ed.Close()

	emoji(f, txt_ed)

	out, err := format.Source(f.Bytes())
	if err != nil {
		log.Fatal(err)
	}
	err = ioutil.WriteFile("runewidth_table.go", out, 0666)
	if err != nil {
		log.Fatal(err)
	}
}