-
Notifications
You must be signed in to change notification settings - Fork 4
/
lua-uni-words.lua
315 lines (303 loc) · 8 KB
/
lua-uni-words.lua
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
-- lua-uni-words.lua
-- Copyright 2020--2023 Marcel Krüger
--
-- This work may be distributed and/or modified under the
-- conditions of the LaTeX Project Public License, either version 1.3
-- of this license or (at your option) any later version.
-- The latest version of this license is in
-- http://www.latex-project.org/lppl.txt
-- and version 1.3 or later is part of all distributions of LaTeX
-- version 2005/12/01 or later.
--
-- This work has the LPPL maintenance status `maintained'.
--
-- The Current Maintainer of this work is Marcel Krüger
local extended_pictographic, property do
local p = require'lua-uni-parse'
local l = lpeg or require'lpeg'
extended_pictographic = p.parse_file('emoji-data',
l.Cg(p.fields(p.codepoint_range, 'Extended_Pictographic' * l.Cc(true))) + p.ignore_line,
p.multiset)
if not extended_pictographic then
error[[Break Property matching failed]]
end
property = p.parse_file('WordBreakProperty',
l.Cg(p.fields(p.codepoint_range, l.C(l.R('az', 'AZ', '__')^1))) + p.ignore_line,
p.multiset)
if not property then
error[[Break Property matching failed]]
end
end
local ignorable = { Extend = true, Format = true, ZWJ = true, }
local controls = { CR = true, LF = true, Newline = true, }
local function context_AHLetter_Mid(cp)
local prop = property[cp]
if ignorable[prop] then
return nil, context_AHLetter_Mid
end
if prop == 'ALetter' then
return false, 'ASTARTED'
end
if prop == 'Hebrew_Letter' then
return false, 'HSTARTED'
end
return true, 'PRE'
end
local function context_HLetter_Double(cp)
local prop = property[cp]
if ignorable[prop] then
return nil, context_HLetter_Double
end
if prop == 'Hebrew_Letter' then
return false, 'HSTARTED'
end
return true, 'PRE'
end
local function context_Numeric_Mid(cp)
local prop = property[cp]
if ignorable[prop] then
return nil, context_Numeric_Mid
end
if prop == 'Numeric' then
return false, 'NSTARTED'
end
return true, 'PRE'
end
local state_map state_map = {
START = function(prop)
if prop == 'CR' then
return 'CR', true
end
if prop == 'LF' or prop == 'Newline' then
return 'START', true
end
return state_map.PRE(prop), true
end,
PRE = function(prop)
if controls[prop] then
return state_map.START(prop)
end
if ignorable[prop] then
return 'PRE', false
end
if prop == 'WSegSpace' then
return 'WHITE', true
end
if prop == 'ALetter' then
return 'ASTARTED', true
end
if prop == 'Hebrew_Letter' then
return 'HSTARTED', true
end
if prop == 'Numeric' then
return 'NSTARTED', true
end
if prop == 'Katakana' then
return 'KSTARTED', true
end
if prop == 'ExtendNumLet' then
return 'EXTEND', true
end
if prop == 'Regional_Indicator' then
return 'RI', true
end
return 'PRE', true
end,
CR = function(prop)
if prop == 'LF' then
return 'START', false
else
return state_map.START(prop)
end
end,
WHITE = function(prop)
if prop == 'WSegSpace' then
return 'WHITE', false
else
return state_map.PRE(prop)
end
end,
EXTEND = function(prop)
if ignorable[prop] then
return 'EXTEND', false
end
if prop == 'ALetter' then
return 'ASTARTED', false
end
if prop == 'Hebrew_Letter' then
return 'HSTARTED', false
end
if prop == 'Katakana' then
return 'KSTARTED', false
end
if prop == 'Numeric' then
return 'NSTARTED', false
end
if prop == 'ExtendNumLet' then
return 'EXTEND', false
end
return state_map.PRE(prop)
end,
KSTARTED = function(prop)
if ignorable[prop] then
return 'KSTARTED', false
end
if prop == 'Katakana' then
return 'Katakana', false
end
if prop == 'ExtendNumLet' then
return 'EXTEND', false
end
return state_map.PRE(prop)
end,
RI = function(prop)
if ignorable[prop] then
return 'RI', false
end
if prop == 'Regional_Indicator' then
return 'PRE', false
end
return state_map.PRE(prop)
end,
ASTARTED = function(prop)
if ignorable[prop] then
return 'ASTARTED', false
end
if prop == 'ALetter' then
return 'ASTARTED', false
end
if prop == 'Hebrew_Letter' then
return 'HSTARTED', false
end
if prop == 'Numeric' then
return 'NSTARTED', false
end
if prop == 'ExtendNumLet' then
return 'EXTEND', false
end
if prop == 'MidLetter' or prop == 'MidNumLet' or prop == 'Single_Quote' then
return context_AHLetter_Mid
end
return state_map.PRE(prop)
end,
HSTARTED = function(prop)
if ignorable[prop] then
return 'HSTARTED', false
end
if prop == 'ALetter' then
return 'ASTARTED', false
end
if prop == 'Hebrew_Letter' then
return 'HSTARTED', false
end
if prop == 'Numeric' then
return 'NSTARTED', false
end
if prop == 'ExtendNumLet' then
return 'EXTEND', false
end
if prop == 'Single_Quote' then
return 'HSINGLE_QUOTE', false
end
if prop == 'MidLetter' or prop == 'MidNumLet' then
return context_AHLetter_Mid
end
if prop == 'Double_Quote' then
return context_HLetter_Double
end
return state_map.PRE(prop)
end,
HSINGLE_QUOTE = function(prop)
if ignorable[prop] then
return 'HSINGLE_QUOTE', false
end
if prop == 'ALetter' then
return 'ASTARTED', false
end
if prop == 'Hebrew_Letter' then
return 'HSTARTED', false
end
return state_map.PRE(prop)
end,
NSTARTED = function(prop)
if ignorable[prop] then
return 'NSTARTED', false
end
if prop == 'ALetter' then
return 'ASTARTED', false
end
if prop == 'Hebrew_Letter' then
return 'HSTARTED', false
end
if prop == 'Numeric' then
return 'NSTARTED', false
end
if prop == 'ExtendNumLet' then
return 'EXTEND', false
end
if prop == 'MidNum' or prop == 'MidNumLet' or prop == 'Single_Quote' then
return context_Numeric_Mid
end
return state_map.PRE(prop)
end,
}
local from_ZWJ, to_ZWJ = {}, {}
for k in next, state_map do
local zwj_state = 'ZWJ_' .. k
from_ZWJ[zwj_state], to_ZWJ[k] = k, zwj_state
end
-- The value of "state" is considered internal and should not be relied upon.
-- Just pass it to the function as is or pass nil. `nil` should only be passed when the passed codepoint starts a new cluster
local function read_codepoint(cp, state)
local mapped_state = from_ZWJ[state]
local new_word
local prop = property[cp]
state, new_word = state_map[mapped_state or state or 'START'](prop)
if mapped_state and extended_pictographic[cp] then
new_word = false
end
if prop == 'ZWJ' then
state = to_ZWJ[state]
end
return new_word, state
end
-- A Lua iterator for strings -- Only reporting the beginning of every word segment
local function word_boundaries_start(str)
local nextcode, str, i = utf8.codes(str)
local state = "START"
local saved_i, saved_code
return function()
local new_word, code
repeat
i, code = nextcode(str, i)
if saved_i then
new_word, state = state(code)
if new_word ~= nil then
i, code, saved_i, saved_code = saved_i, saved_code, nil, nil
end
else
if not i then return end
new_word, state = read_codepoint(code, state)
if new_word == nil then
saved_i, saved_code = i, code
end
end
until new_word
return i, code
end
end
-- A more useful iterator: returns the byterange of the segment in reverse order followed by a string with the word
local function word_boundaries(str)
local iter = word_boundaries_start(str)
return function(_, cur)
if cur == #str then return end
local new = iter()
if not new then return #str, cur + 1, str:sub(cur + 1) end
return new - 1, cur + 1, str:sub(cur + 1, new - 1)
end, nil, iter() - 1
end
return {
read_codepoint = read_codepoint,
word_boundaries_start = word_bounaries_start,
word_boundaries = word_boundaries,
}