-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-tables
executable file
·113 lines (102 loc) · 2.84 KB
/
update-tables
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
#! /bin/bash
#
# update-tables
# Copyright (C) 2016 Adrian Perez <[email protected]>
#
# Distributed under terms of the MIT license.
#
set -e
WIDE_URL='http://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt'
ZERO_URL='http://www.unicode.org/Public/UNIDATA/extracted/DerivedGeneralCategory.txt'
# The version numbers of the files above are parsed from the files themselves.
WIDE_VER='(unknown)'
ZERO_VER='(unknown)'
srcdir=$(dirname "$0")
WIDE_FILE="${srcdir}/${WIDE_URL##*/}"
ZERO_FILE="${srcdir}/${ZERO_URL##*/}"
[[ -r ${WIDE_FILE} ]] || wget -c -O "${WIDE_FILE}" "${WIDE_URL}"
[[ -r ${ZERO_FILE} ]] || wget -c -O "${ZERO_FILE}" "${ZERO_URL}"
# Pad hex values to be 8 characters wide, and prepend "0x" to them.
u32hex () {
local -i len=${#1}
local -i nzeros=$(( 8 - len ))
echo -n '0x'
for (( ; nzeros > 0 ; nzeros-- )) ; do
echo -n '0'
done
echo -n "$1"
}
format_range () {
if [[ $1 = *..* ]] ; then
u32hex "${1%..*}"
echo -n ','
u32hex "${1#*..}"
echo ','
else
u32hex "${1}"
echo -n ','
u32hex "${1}"
echo ','
fi
}
zero_table () {
local tmpfile="${TMPDIR:-/tmp}/update-tables-$$-${RANDOM}"
echo "-- Autogenerated from ${ZERO_FILE##*/}"
echo 'return {'
while read -r line ; do
if [[ -z ${line} || ${line} = \#* ]] ; then
read line rest <<< "${line}"
if [[ ${rest} = *.txt ]] ; then
ZERO_VER=${rest}
elif [[ ${rest} = Date:\ 20* || ${rest} = ©\ 20* ]] ; then
ZERO_VER="${ZERO_VER}, ${rest}"
fi
continue
fi
read -a items <<< "${line}"
if [[ ${items[2]} == Mn || ${items[2]} == Me ]] ; then
format_range "${items[0]}"
fi
done > "${tmpfile}"
sort "${tmpfile}"
rm "${tmpfile}"
echo '}'
}
wide_table () {
local tmpfile="${TMPDIR:-/tmp}/update-tables-$$-${RANDOM}"
echo "-- Autogenerated from ${WIDE_FILE##*/}"
echo 'return {'
while read -r item rest ; do
if [[ -z ${item} || ${item} = \#* ]] ; then
if [[ ${rest} = *.txt ]] ; then
WIDE_VER=${rest}
elif [[ ${rest} = Date:\ 20* || ${rest} = ©\ 20* ]] ; then
WIDE_VER="${WIDE_VER}, ${rest}"
fi
continue
fi
range=${item%;*}
flags=${item#*;}
if [[ ${flags} = W* || ${flags} = F* ]] ; then
format_range "${range}"
fi
done > "${tmpfile}"
sort "${tmpfile}"
rm "${tmpfile}"
echo '}'
}
make_readme () {
# Pick newest LuaRocks version
local V=$(find "${srcdir}/luarocks" -name 'wcwidth-[0-9]*.rockspec' \
| sed 's,^.*/wcwidth-\([0-9\.]\+\).*$,\1,' | sort -g | tail -1)
sed -e "s+@@WIDE_FILE@@+${WIDE_FILE##*/}+g" \
-e "s+@@ZERO_FILE@@+${ZERO_FILE##*/}+g" \
-e "s+@@WIDE_URL@@+${WIDE_URL}+g" \
-e "s+@@ZERO_URL@@+${ZERO_URL}+g" \
-e "s+@@WIDE_VER@@+${WIDE_VER}+g" \
-e "s+@@ZERO_VER@@+${ZERO_VER}+g" \
-e "s+@@LUAROCKS_VER@@+${V}+g"
}
wide_table < "${WIDE_FILE}" > "${srcdir}/wcwidth/widetab.lua"
zero_table < "${ZERO_FILE}" > "${srcdir}/wcwidth/zerotab.lua"
make_readme < "${srcdir}/README.md.in" > "${srcdir}/README.md"