summaryrefslogtreecommitdiff
path: root/mac/.config/mpv/script-modules/utf8/context/runtime.lua
blob: 6fb024c5105d2aee9a081742cf9d99919c3dd1e0 (plain)
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
return function(utf8)

local utf8unicode = utf8.unicode
local utf8sub = utf8.sub
local sub = utf8.raw.sub
local byte = utf8.raw.byte
local utf8len = utf8.len
local utf8next = utf8.next
local rawgsub = utf8.raw.gsub
local utf8offset = utf8.offset
local utf8char = utf8.char

local util = utf8.util

local ctx = {}
local mt = {
  __index = ctx,
  __tostring = function(self)
    return rawgsub([[str: '${str}', char: ${pos} '${char}', func: ${func_pos}]], "${(.-)}", {
      str = self.str,
      pos = self.pos,
      char = self:get_char(),
      func_pos = self.func_pos,
    })
  end
}

function ctx.new(obj)
  obj = obj or {}
  local res = setmetatable({
    pos = obj.pos or 1,
    byte_pos = obj.pos or 1,
    str = assert(obj.str, "str is required"),
    len = obj.len,
    rawlen = obj.rawlen,
    bytes = obj.bytes,
    offsets = obj.offsets,
    starts = obj.starts or nil,
    functions = obj.functions or {},
    func_pos = obj.func_pos or 1,
    ends = obj.ends or nil,
    result = obj.result and util.copy(obj.result) or {},
    captures = obj.captures and util.copy(obj.captures, true) or {active = {}},
    modified = false,
  }, mt)
  if not res.bytes then
    local str = res.str
    local l = #str
    local bytes = utf8.config.int32array(l)
    local offsets = utf8.config.int32array(l)
    local c, bs, i = nil, 1, 1
    while bs <= l do
      bytes[i] = utf8unicode(str, bs, bs)
      offsets[i] = bs
      bs = utf8.next(str, bs)
      i = i + 1
    end
    res.bytes = bytes
    res.offsets = offsets
    res.byte_pos = res.pos
    res.len = i
    res.rawlen = l
  end

  return res
end

function ctx:clone()
  return self:new()
end

function ctx:next_char()
  self.pos = self.pos + 1
  self.byte_pos = self.pos
end

function ctx:prev_char()
  self.pos = self.pos - 1
  self.byte_pos = self.pos
end

function ctx:get_char()
  if self.len <= self.pos then return "" end
  return utf8char(self.bytes[self.pos])
end

function ctx:get_charcode()
  if self.len <= self.pos then return nil end
  return self.bytes[self.pos]
end

function ctx:next_function()
  self.func_pos = self.func_pos + 1
end

function ctx:get_function()
  return self.functions[self.func_pos]
end

function ctx:done()
  utf8.debug('done', self)
  coroutine.yield(self, self.result, self.captures)
end

function ctx:terminate()
  utf8.debug('terminate', self)
  coroutine.yield(nil)
end

return ctx

end