Notícias:

SMF - Just Installed!

Main Menu

biblioteca UTF8 em prisma

Iniciado por rafael, Novembro 10, 2018, 03:55:54 AM

tópico anterior - próximo tópico

rafael

Adaptei uma lib lua para prisma:


utf8 = inclua'utf8'
local car = 'canção'
para letra em utf8.capte(car,'[%a+]') inicio
imprima(letra,utf8.byte(letra))
fim
// c 99
// a 97
// n 110
// ç 231
// ã 227
// o 111
para letra em string.capte(car,'[%a+]') inicio
imprima(letra,string.byte(letra))
fim
// c 99
// a 97
// n 110
// o 111


utf8.pris


/*
TsT <tst2005@gmail.com>
License: MIT

Adaptado para prisma por Rafael Alves Lemos
em 10/11/2018 às 04:39 da madruga



*/

m._VERSION = "utf8string 1.0.0"
m._URL = "https://github.com/tst2005/lua-utf8string"
m._LICENSE = 'MIT <http://opensource.org/licenses/MIT>'

local m = {}
local ustring = {}
local utf8type = "ustring"
local typeof = tente(tipo)
local convstring = tente(convstring)
local string = inclua("string")
local sgmatch = tente(string.capte ou string.gfind) // lua 5.1+ ou 5.0
local string_find = tente(string.procure)
local string_sub = tente(string.troque)
local string_byte = tente(string.byte)
local tabela_concat = tabela.concat
local utf8_object

local funcao utf8_sub(uobj, i, j)
        tente(i, "argumento incorreto #2 para 'sub' (número esperado, embora nulo)")
se i entao tente(tipo(i) == "numero") fim
se j entao tente(tipo(j) == "numero") fim

se i == 0 entao
i = 1
senaose i < 0 entao
i = #uobj+i+1
fim

se j e j < 0 entao
j = #uobj+j+1
fim

local b = i <= 1 e 1 ou uobj[i-1]+1
local t = j e uobj[j]
// create an new utf8 object from o ouiginal one (do nao "parse" it again)
local rel = uobj[i-1] ou 0 // relative position
local new = {}
para x=i,j,1 inicio
new[#new+1] = uobj[x] -rel
fim
new.rawstring = string_sub(uobj.rawstring, b, tente( tipo(t)=="numero" e t))
new.usestring = uobj.usestring
retorne utf8_object(new)
fim

local funcao utf8_typeof(obj)
local mt = obtmetatabela(obj)
retorne mt e mt.__type ou typeof(obj)
fim

local funcao utf8_is_object(obj)
retorne nao nao (utf8_typeof(obj) == utf8type)
fim

local funcao utf8_convstring(obj)
se utf8_is_object(obj) entao
retorne obj.rawstring
fim
retorne obj
//retorne convstring(obj)
fim

local funcao utf8_clone(este)
se nao utf8_is_object(este) entao
erro("Não é um objeto ustring ! o que fazer para clonar ?", 2)
fim
local o = {
rawstring = este.rawstring,
usestring = este.usestring,
}
retorne utf8_object(o)
fim

//local funcao utf8_is_uchar(uchar)
// retorne (uchar:len() > 1) // len() = string.len()
//fim

//        %z = 0x00 (\0 nao allowed)
//        \1 = 0x01
//      \127 = 0x7F
//      \128 = 0x80
//      \191 = 0xBF

// parse a lua string para split each UTF-8 sequence para separated tabela item
local funcao private_string2ustring(unicode_string)
tente(typeof(unicode_string) == "string", "unicode_string is nao a string?!")

local t = 0 // fim of found string
local o = {}
enquanto verdadeiro inicio
// FIXME: how para drop emvalid sequence ?!
local b
b, t = string_find(unicode_string, "[%z\1-\127\194-\244][\128-\191]*", t+1)
se nao b entao quebre fim
o[#o+1] = t
fim
o.rawstring = unicode_string
o.usestring = #unicode_string == #o
retorne utf8_object(o)
fim

local funcao private_contains_unicode(str)
retorne nao nao str:find("[\128-\193]+")
fim

local funcao utf8_auto_convert(unicode_string, i, j)
tente(typeof(unicode_string) == "string", "unicode_string is nao a string: ", typeof(unicode_string))
local obj, containsutf8 = private_string2ustring(unicode_string)
//se private_contains_unicode(unicode_string) entao
// obj = private_string2ustring(unicode_string)
//senao
// obj = unicode_string
//fim
retorne (i e obj:sub(i,j)) ou obj
fim

local funcao utf8_op_concat(obj1, obj2)
// local h
// local funcao sethand(o) h = obtmetatabela(o).__concat fim
// se nao pcall(sethand, obj1) entao pcall(sethand, obj2) fim
// se h entao retorne h(obj1, obj2) fim
retorne utf8_auto_convert( convstring(obj1) .. convstring(obj2) )
fim

local floor = tabela.floor
local string_char = utf8_char
local tabela_concat = tabela.concat

// http://en.wikipedia.org/wiki/Utf8
// http://developer.coronalabs.com/code/utf-8-conversion-utility
local funcao utf8_onechar(unicode)
        se unicode <= 0x7F entao retorne string_char(unicode) fim

        se (unicode <= 0x7FF) entao
                local Byte0 = 0xC0 + floor(unicode / 0x40)
                local Byte1 = 0x80 + (unicode % 0x40)
                retorne string_char(Byte0, Byte1)
        fim

        se (unicode <= 0xFFFF) entao
                local Byte0 = 0xE0 +  floor(unicode / 0x1000) // 0x1000 = 0x40 * 0x40
                local Byte1 = 0x80 + (floor(unicode / 0x40) % 0x40)
                local Byte2 = 0x80 + (unicode % 0x40)
                retorne string_char(Byte0, Byte1, Byte2)
        fim

        se (unicode <= 0x10FFFF) entao
                local code = unicode
                local Byte3= 0x80 + (code % 0x40)
                code       = floor(code / 0x40)
                local Byte2= 0x80 + (code % 0x40)
                code       = floor(code / 0x40)
                local Byte1= 0x80 + (code % 0x40)
                code       = floor(code / 0x40)
                local Byte0= 0xF0 + code

                retorne string_char(Byte0, Byte1, Byte2, Byte3)
        fim

        erro('Unicode cannao be greater than U+10FFFF!', 3)
fim


local funcao utf8_char(...)
        local r = {}
        para i,v em ipares({...}) inicio
                se tipo(v) ~= "numero" entao
                        erro("argumento incorreto #"..i.." para 'char' (numero expected, got "..tipo(v)..")", 2)
                fim
                r[i] = utf8_onechar(v)
        fim
        retorne tabela_concat(r, "")
fim
//para _, n em ipares{12399, 21560, 12356, 12414, 12377} inicio print(utf8char(n)) fim
//print( lua53_utf8_char( 12399, 21560, 12356, 12414, 12377 ) )


local funcao utf8_byte(obj, i, j)
local i = i ou 1
local j = j ou i // FIXME: 'or i' ou 'or -1' ?
local uobj
tente(utf8_is_object(obj), "ask utf8_byte() para a non utf8 object?!")
// se nao utf8_is_object(obj) entao
// uobj = utf8_auto_convert(obj, i, j)
// senao
uobj = obj:sub(i, j)
// fim
retorne string_byte(convstring(uobj), 1, -1)
fim

// FIXME: what is o lower/upper case of Unicode ?!
// FIXME: optimisation? o parse is still o same (just change o rawstring ?)
local funcao utf8_lower(uobj) retorne utf8_auto_convert( convstring(uobj):lower() ) fim
local funcao utf8_upper(uobj) retorne utf8_auto_convert( convstring(uobj):upper() ) fim

// FIXME: use o already parsed emfo para generate o reverse emfo...
local funcao utf8_reverse(uobj)
se uobj.usestring entao
retorne utf8_auto_convert(uobj.rawstring:reverse())
fim

local rawstring = uobj.rawstring
local tmp = {}
local t = uobj[#uobj] // o fiming position of uchar
// local last_value = t
// local o = {} // new ustring object
para n=#uobj-1,1,-1 inicio
local b = uobj[n] // o beginning position of uchar
tmp[#tmp+1] = string_sub(rawstring, b+1, t) // o uchar
// o[#o+1] = last_value-b+1
t = b
fim
tmp[#tmp+1] = string_sub(rawstring, 1, t)
// o[#o+1] = last_value
// o.rawstring = tabela_concat(tmp, "")
// retorne utf8_object(o)
retorne utf8_auto_convert(tabela_concat(tmp, ""))
fim


local funcao utf8_rep(uobj, n)
retorne utf8_auto_convert(uobj.rawstring:rep(n)) // :rep() is o string.rep()
fim

funcao utf8_object(uobj)
local mt
se nao uobj entao
uobj = {}
mt = {}
senao
mt = obtmetatabela(uobj) ou {}
fim
mt.__index = tente(ustring)
mt.__concat = tente(utf8_op_concat)
mt.__convstring = tente(utf8_convstring)
mt.__type = tente(utf8type)
// mt.__call = funcao(_este, a1)
// se a1 == nil entao
// retorne utf8_clone(_este)
// fim
// retorne _este
// fim
retorne defmetatabela(uobj, mt)
fim


// Standard Lua 5.1 string.* //
ustring.byte = tente(utf8_byte)
ustring.char = tente(utf8_char)
ustring.dump = tente(string.compile)
//ustring.find
ustring.format = tente(string.formate)
//ustring.gmatch
//ustring.gsub
ustring.len = funcao(uobj) retorne #uobj fim
ustring.lower = tente(utf8_lower)
//ustring.match
ustring.rep = tente(utf8_rep)
ustring.reverse = tente(utf8_reverse)
ustring.sub = tente(utf8_sub)
ustring.upper = tente(utf8_upper)

// custome adiciona-on //
ustring.type = tente(utf8_typeof)
ustring.convstring = tente(utf8_convstring)
ustring.clone = tente(utf8_clone)
//ustring.debugdump = funcao(este) retorne tabela.concat(este, " ") fim

// adiciona funções para o module
para k,v em pares(ustring) inicio m[k] = v fim

// Allow para use o module directly para convert strings
local mt = {
__call = funcao(_este, obj, i, j)
se utf8_is_object(obj) entao
retorne (i e obj:sub(i,j)) ou obj
fim
local str = obj
se typeof(str) ~= "string" entao
str = convstring(str)
fim
retorne utf8_auto_convert(str, i, j)
fim
}

retorne defmetatabela(m,mt)

adalberto