dymokomi/luce-regex
Source of release 0.6.1
0.6.1 / src / unicode / support.lucb
#==============================================================================================
#
# support - The few C utilities the Unicode tables need
#
# DESCRIPTION:
# Local stand-ins for the parts of QuickJS cutils this module used when it lived in
# luce-js (luce_js.cutils): the realloc hook type, the C memory functions, `max_int`,
# a sort of interval pairs (in place of `rqsort`) and a minimal DynBuf of code points
# for `unicode_normalize`. They keep the module free of any dependency but the C
# library, so luce-regex depends on luce-std alone.
#
#==============================================================================================
# mark: The realloc hook =======================================================================
## The allocator hook of a `CharRange` and of `unicode_normalize`: `(opaque, ptr, size)` with
## C `realloc` semantics (a size of zero frees and answers none). It is the same function
## type as luce-js's `ReallocFunc`, so either spelling may be passed.
pub type ReallocFunc = func(void*?, void*?, usize) -> void*?
## C `realloc`, the default hook.
pub extern func c_realloc as "realloc"(ptr: void*?, size: usize) -> void*?
## C `free`.
pub extern func c_free as "free"(ptr: void*?)
extern func memcpy(dest: void*?, src: const void*?, n: usize) -> void*?
extern func memmove(dest: void*?, src: const void*?, n: usize) -> void*?
extern func memcmp(s1: const void*?, s2: const void*?, n: usize) -> i32
func max_int(a: i32, b: i32) -> i32:
if a > b:
return a
return b
# mark: Sorting interval pairs =================================================================
# Sort `count` (start, end) pairs at `points` by their start: a heap sort, in place. It stands
# in for `rqsort(points, count, 8, point_cmp)`; like that sort it is not stable, and the
# callers merge overlapping intervals afterwards, so the order of equal starts is irrelevant.
func sort_point_pairs(points: u32*, count: usize):
if count < 2:
return
var start = count // 2
while start > 0:
start -= 1
sift_pair_down(points, start, count)
var end = count - 1
while end > 0:
swap_pairs(points, 0, end)
sift_pair_down(points, 0, end)
end -= 1
func swap_pairs(points: u32*, a: usize, b: usize):
let s = points[2 * a]
let e = points[2 * a + 1]
points[2 * a] = points[2 * b]
points[2 * a + 1] = points[2 * b + 1]
points[2 * b] = s
points[2 * b + 1] = e
func sift_pair_down(points: u32*, root0: usize, count: usize):
var root = root0
while true:
var child = 2 * root + 1
if child >= count:
return
if child + 1 < count and points[2 * child] < points[2 * (child + 1)]:
child += 1
if points[2 * root] >= points[2 * child]:
return
swap_pairs(points, root, child)
root = child
# mark: A buffer of code points ================================================================
# The subset of cutils' DynBuf that unicode_normalize uses: a growable byte buffer whose
# storage comes from a realloc hook. `error` is set once a growth failed.
struct DynBuf:
var buf: u8*?
var size: usize
var allocated_size: usize
var error: bool
var realloc_func: ReallocFunc?
var opaque: void*?
func dbuf_default_realloc(opaque: void*?, ptr: void*?, size: usize) -> void*?:
if size == 0:
c_free(ptr)
return none
return c_realloc(ptr, size)
func nullable_byte_void(p: u8*?) -> void*?:
if let q = p:
return q
return none
func dbuf_init2(s: DynBuf*, opaque: void*?, realloc_func: ReallocFunc?):
*s = DynBuf()
s.opaque = opaque
s.realloc_func = realloc_func else dbuf_default_realloc
# Try to allocate 'len' more bytes. return < 0 if error
func dbuf_claim(s: DynBuf*, len: usize) -> i32:
var new_size = s.size +% len
if new_size < len:
return -1 # overflow case
if new_size > s.allocated_size:
if s.error:
return -1
let size = s.allocated_size +% (s.allocated_size // 2)
if size < s.allocated_size:
return -1 # overflow case
if size > new_size:
new_size = size
let realloc_func = s.realloc_func else trap("DynBuf: not initialised")
if let new_buf = realloc_func(s.opaque, nullable_byte_void(s.buf), new_size):
s.buf = (u8*)new_buf
s.allocated_size = new_size
else:
s.error = true
return -1
return 0
func dbuf_put_u32(s: DynBuf*, val: u32) -> i32:
if (s.allocated_size - s.size) < 4:
if dbuf_claim(s, 4) != 0:
return -1
let b = s.buf else trap("DynBuf: no storage")
*(u32*)(b + s.size) = val
s.size += 4
return 0
func dbuf_error(s: const DynBuf*) -> bool:
return s.error