support luajit for bits

This commit is contained in:
ruki
2020-12-06 22:20:44 +08:00
parent cdfbe51a38
commit 2845ba423d
3 changed files with 53 additions and 15 deletions

30
src/ltui/base/bit.lua Normal file
View File

@@ -0,0 +1,30 @@
--!A cross-platform terminal ui library based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-2020, TBOOX Open Source Group.
--
-- @author ruki
-- @file bit.lua
--
-- define module: bit
local bit = bit or {}
-- bit/and operation
function bit.band(a, b)
return a & b
end
-- load bit module
return bit

View File

@@ -19,11 +19,15 @@
--
-- load modules
local log = require("ltui/base/log")
local view = require("ltui/view")
local event = require("ltui/event")
local action = require("ltui/action")
local curses = require("ltui/curses")
local log = require("ltui/base/log")
local view = require("ltui/view")
local event = require("ltui/event")
local action = require("ltui/action")
local curses = require("ltui/curses")
local luajit, bit = pcall(require, "bit")
if not luajit then
bit = require("ltui/base/bit")
end
-- define module
local label = label or view()
@@ -129,7 +133,7 @@ function label:splitext(text, width)
while #line > width do
local size = 0
for i = 1, #line do
if (line:byte(i) & 0xc0) ~= 0x80 then
if bit.band(line:byte(i), 0xc0) ~= 0x80 then
size = size + 1
if size > width then
table.insert(result, line:sub(1, i - 1))

View File

@@ -19,14 +19,18 @@
--
-- load modules
local log = require("ltui/base/log")
local view = require("ltui/view")
local label = require("ltui/label")
local event = require("ltui/event")
local border = require("ltui/border")
local curses = require("ltui/curses")
local textarea = require("ltui/textarea")
local action = require("ltui/action")
local log = require("ltui/base/log")
local view = require("ltui/view")
local label = require("ltui/label")
local event = require("ltui/event")
local border = require("ltui/border")
local curses = require("ltui/curses")
local textarea = require("ltui/textarea")
local action = require("ltui/action")
local luajit, bit = pcall(require, "bit")
if not luajit then
bit = require("ltui/base/bit")
end
-- define module
local textedit = textedit or textarea()
@@ -85,7 +89,7 @@ function textedit:on_event(e)
if #text > 0 then
local size = 1
-- while continuation byte
while (text:byte(#text - size + 1) & 0xc0) == 0x80 do
while bit.band(text:byte(#text - size + 1), 0xc0) == 0x80 do
size = size + 1
end
self:text_set(text:sub(1, #text - size))