dymokomi/luce-regex
Source of release 0.6.1
0.6.1 / src / unicode / sequences.lucb
#==============================================================================================
#
# sequences - The properties of strings: emoji sequences
#
# DESCRIPTION:
# Port of QuickJS libunicode.c lines 1919-2124: unicode_sequence_prop, which calls
# back once per sequence of a sequence property (Basic_Emoji, RGI_Emoji, ...).
#
#==============================================================================================
let seq_max_len: usize = 16
# Call `cb` with every code point of `cr` followed by `suffix` (the loops of the
# single-code-point sequence properties).
func sequence_each_point(cr: const CharRange*, suffix: const u32[], cb: UnicodeSequencePropCB,
opaque: void*?):
var seq: u32[seq_max_len]
for i in 0..<suffix.length:
seq[1 + i] = suffix[i]
let points = cr_points(cr)
var i: usize = 0
while i < points.length:
var c = points[i]
while c < points[i + 1]:
seq[0] = c
cb(opaque, seq[0..<1 + suffix.length])
c += 1
i += 2
func unicode_sequence_prop1(seq_prop_idx: i32, cb: UnicodeSequencePropCB, opaque: void*?,
cr: CharRange*) -> i32:
var seq: u32[seq_max_len]
match (UnicodeSequencePropertyEnum)seq_prop_idx:
.basic_emoji:
if unicode_prop1(cr, (i32)UnicodePropertyEnum.basic_emoji1) < 0:
return -1
sequence_each_point(cr, seq[0..<0], cb, opaque)
cr.len = 0
if unicode_prop1(cr, (i32)UnicodePropertyEnum.basic_emoji2) < 0:
return -1
let fe0f: u32[1] = [0xfe0f]
sequence_each_point(cr, fe0f, cb, opaque)
.rgi_emoji_modifier_sequence:
if unicode_prop1(cr, (i32)UnicodePropertyEnum.emoji_modifier_base) < 0:
return -1
let points = cr_points(cr)
var i: usize = 0
while i < points.length:
var c = points[i]
while c < points[i + 1]:
for j: u32 in 0..<5:
seq[0] = c
seq[1] = 0x1f3fb + j
cb(opaque, seq[0..<2])
c += 1
i += 2
.rgi_emoji_flag_sequence:
if unicode_prop1(cr, (i32)UnicodePropertyEnum.rgi_emoji_flag_sequence) < 0:
return -1
let points = cr_points(cr)
var i: usize = 0
while i < points.length:
var c = points[i]
while c < points[i + 1]:
let c0 = c // 26
let c1 = c % 26
seq[0] = 0x1F1E6 + c0
seq[1] = 0x1F1E6 + c1
cb(opaque, seq[0..<2])
c += 1
i += 2
.rgi_emoji_zwj_sequence:
rgi_emoji_zwj_sequences(cb, opaque)
.rgi_emoji_tag_sequence:
let tags = unicode_rgi_emoji_tag_sequence
var i: usize = 0
while i < tags.length:
var j: usize = 0
seq[j] = 0x1F3F4
j += 1
while true:
let c: u32 = tags[i]
i += 1
if c == 0x00:
break
seq[j] = 0xe0000 + c
j += 1
seq[j] = 0xe007f
j += 1
cb(opaque, seq[0..<j])
.emoji_keycap_sequence:
if unicode_prop1(cr, (i32)UnicodePropertyEnum.emoji_keycap_sequence) < 0:
return -1
let keycap: u32[2] = [0xfe0f, 0x20e3]
sequence_each_point(cr, keycap, cb, opaque)
.rgi_emoji:
# all prevous sequences
var i = (i32)UnicodeSequencePropertyEnum.basic_emoji
while i <= (i32)UnicodeSequencePropertyEnum.rgi_emoji_zwj_sequence:
let ret = unicode_sequence_prop1(i, cb, opaque, cr)
if ret < 0:
return ret
cr.len = 0
i += 1
_:
return -2
return 0
# The RGI_Emoji_ZWJ_Sequence case of unicode_sequence_prop1: decode each entry of
# unicode_rgi_emoji_zwj_sequence and call `cb` with all its skin tone and hair variants.
func rgi_emoji_zwj_sequences(cb: UnicodeSequencePropCB, opaque: void*?):
var seq: u32[seq_max_len]
var mod_pos: usize[2]
let tab = unicode_rgi_emoji_zwj_sequence
var i: usize = 0
while i < tab.length:
let len = (i32)tab[i]
i += 1
var k: usize = 0
var mod: i32 = 0
var mod_count: usize = 0
var hc_pos: isize = -1
for j: i32 in 0..<len:
var code: u32 = tab[i]
i += 1
let high: u32 = tab[i]
code |= high << 8
i += 1
let pres = code >> 15
let mod1 = (i32)((code >> 13) & 3)
code &= 0x1fff
var c: u32 = 0
if code < 0x1000:
c = code + 0x2000
else:
c = 0x1f000 + (code - 0x1000)
if c == 0x1f9b0:
hc_pos = (isize)k
seq[k] = c
k += 1
if mod1 != 0:
assert(mod_count < 2)
mod = mod1
mod_pos[mod_count] = k
mod_count += 1
seq[k] = 0 # will be filled later
k += 1
if pres != 0:
seq[k] = 0xfe0f
k += 1
if j < len - 1:
seq[k] = 0x200d
k += 1
# genrate all the variants
var n_mod: i32 = 1
match mod:
1: n_mod = 5
2: n_mod = 25
3: n_mod = 20
_: n_mod = 1
let n_hc: u32 = 4 if hc_pos >= 0 else 1
for hc_idx: u32 in 0..<n_hc:
for mod_idx: i32 in 0..<n_mod:
if hc_pos >= 0:
seq[(usize)hc_pos] = 0x1f9b0 + hc_idx
if mod == 1:
seq[mod_pos[0]] = 0x1f3fb + (u32)mod_idx
elif mod == 2 or mod == 3:
var i0 = mod_idx // 5
let i1 = mod_idx % 5
# avoid identical values
if mod == 3 and i0 >= i1:
i0 += 1
seq[mod_pos[0]] = 0x1f3fb + (u32)i0
seq[mod_pos[1]] = 0x1f3fb + (u32)i1
cb(opaque, seq[0..<k])
# build a unicode sequence property
# return -2 if not found, -1 if other error. 'cr' is used as temporary memory.
## Call `cb(opaque, sequence)` for every sequence of the sequence property `prop_name`
## (such as `RGI_Emoji`), using `cr` (initialised and empty) as scratch; 0, -1 when memory
## runs out, -2 when the name is unknown.
pub func unicode_sequence_prop(prop_name: str, cb: UnicodeSequencePropCB, opaque: void*?,
cr: CharRange*) -> i32:
let seq_prop_idx = unicode_find_name(unicode_sequence_prop_name_table, prop_name)
if seq_prop_idx < 0:
return -2
return unicode_sequence_prop1(seq_prop_idx, cb, opaque, cr)