"translating" One character to another in lua

245 views Asked by At

I want to make a lua script that takes the input of a table, then outputs the strings in that table in their full width counterparts, eg

input = {"Hello", " ", "World"}
print(full(table.concat(input)))

and it will print "Hello World"

I tried it using this:

local encoding = [[ 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!゛#$%&()*+、ー。/:;〈=〉?@[\\]^_‘{|}~]]
function char(i)
   return encoding:sub(i:len(),i:len())
end
function decode(t)
   for i=1,#t do t[i]=char(t[i]) end
   return table.concat(t)
end
function returns(word, word_eol)
    print(char(word_eol[2]))
end

but that did not work

note: it is a plugin for hexchat that's why I have it as print(char(word_eol[2])))

Because when you hook a command in hexchat it spits out a table that is the command name, then what was entered after

2

There are 2 answers

0
brianush1 On BEST ANSWER

If (string) = [[ 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!゛#$%&()*+、ー。/:;〈=〉?@[\]^_‘{|}~]], you're finding the n th character of (string), with n being the length of the character, which will always be one. If I understand correctly, this will do the job, by having a separate alphabet and matching the characters.

local encoding = [[ 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!゛#$%&()*+、ー。/:;〈=〉?@[]^_‘{|}~]]
local decoding = [[ 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&()*+,-./:;{=}?@[]^_'{|}~]]
function char(i)
   local l = decoding:find(i,1,true)
   return encoding:sub(l,l)
end
function decode(t)
   for i=1,#t do t[i]=char(t[i]) end
   return table.concat(t)
end
function returns(word, word_eol)
    print(char(word_eol[2]))
end
1
Egor Skriptunoff On
function full(s)
   return (s:gsub('.', function(c)
      c = c:byte()
      if c == 0x20 then
         return string.char(0xE3, 0x80, 0x80)
      elseif c >= 0x21 and c <= 0x5F then
         return string.char(0xEF, 0xBC, c+0x60)
      elseif c >= 0x60 and c <= 0x7E then
         return string.char(0xEF, 0xBD, c+0x20)
      end
   end))
end