57 lines
1004 B
Lua
57 lines
1004 B
Lua
local utf8 = require("utf8")
|
|
local self = {}
|
|
|
|
function self.toTable(str)
|
|
local chars = {}
|
|
local pos = 1
|
|
for p,c in utf8.codes(str) do
|
|
local bytes = 1
|
|
if c > 127 then bytes = 2 end
|
|
if c > 255 then bytes = 3 end
|
|
if c > 65535 then bytes = 4 end
|
|
if c > 16777215 then bytes = 5 end
|
|
|
|
chars[pos] = string.sub(str,p,p + bytes - 1)
|
|
pos = pos + 1
|
|
end
|
|
|
|
return chars
|
|
end
|
|
|
|
function self.split(text,sep,count)
|
|
local output = {}
|
|
local outputLen = 0
|
|
local part = {}
|
|
local partLen = 0
|
|
|
|
local length = #text
|
|
local index = 1
|
|
while index <= length do
|
|
local char = text[index]
|
|
if char == sep then
|
|
outputLen = outputLen + 1
|
|
output[outputLen] = part
|
|
part = {}
|
|
partLen = 0
|
|
if outputLen == count then
|
|
index = index + 1
|
|
break
|
|
end
|
|
else
|
|
partLen = partLen + 1
|
|
part[partLen] = char
|
|
end
|
|
|
|
index = index + 1
|
|
end
|
|
|
|
if index <= length then
|
|
part = {unpack(text,index)}
|
|
end
|
|
|
|
outputLen = outputLen + 1
|
|
output[outputLen] = part
|
|
return output
|
|
end
|
|
|
|
return self |