From 7937b23423c65f41718edc684cb310d23e8da78c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 19 Nov 2018 16:44:00 +0800 Subject: =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=B8=AD=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/parser/calcline.lua | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'src/parser/calcline.lua') diff --git a/src/parser/calcline.lua b/src/parser/calcline.lua index 0c692a85..26f475d9 100644 --- a/src/parser/calcline.lua +++ b/src/parser/calcline.lua @@ -15,6 +15,13 @@ local function rowcol(str, n) return row, col end +local function rowcol_utf8(str, n) + row = 1 + fl = 1 + ROWCOL:match(str:sub(1, n)) + return row, utf8.len(str, fl, n) +end + local function position(str, _row, _col) local cur = 1 local row = 1 @@ -37,6 +44,28 @@ local function position(str, _row, _col) end end +local function position_utf8(str, _row, _col) + local cur = 1 + local row = 1 + while true do + if row == _row then + return utf8.offset(str, _col, cur) + elseif row > _row then + return cur - 1 + end + local pos = str:find('[\r\n]', cur) + if not pos then + return #str + end + row = row + 1 + if str:sub(pos, pos+1) == '\r\n' then + cur = pos + 2 + else + cur = pos + 1 + end + end +end + local NL = m.P'\r\n' + m.S'\r\n' local function line(str, row) @@ -57,6 +86,8 @@ end return { rowcol = rowcol, - line = line, + rowcol_utf8 = rowcol_utf8, position = position, + position_utf8 = position_utf8, + line = line, } -- cgit v1.2.3