Advertisement
Snusmumriken

Lua ansi/utf8 encode/decode

Sep 21st, 2017
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.15 KB | None | 0 0
  1. local ansi_decode={
  2.   [128]='\208\130',[129]='\208\131',[130]='\226\128\154',[131]='\209\147',[132]='\226\128\158',[133]='\226\128\166',
  3.   [134]='\226\128\160',[135]='\226\128\161',[136]='\226\130\172',[137]='\226\128\176',[138]='\208\137',[139]='\226\128\185',
  4.   [140]='\208\138',[141]='\208\140',[142]='\208\139',[143]='\208\143',[144]='\209\146',[145]='\226\128\152',
  5.   [146]='\226\128\153',[147]='\226\128\156',[148]='\226\128\157',[149]='\226\128\162',[150]='\226\128\147',[151]='\226\128\148',
  6.   [152]='\194\152',[153]='\226\132\162',[154]='\209\153',[155]='\226\128\186',[156]='\209\154',[157]='\209\156',
  7.   [158]='\209\155',[159]='\209\159',[160]='\194\160',[161]='\209\142',[162]='\209\158',[163]='\208\136',
  8.   [164]='\194\164',[165]='\210\144',[166]='\194\166',[167]='\194\167',[168]='\208\129',[169]='\194\169',
  9.   [170]='\208\132',[171]='\194\171',[172]='\194\172',[173]='\194\173',[174]='\194\174',[175]='\208\135',
  10.   [176]='\194\176',[177]='\194\177',[178]='\208\134',[179]='\209\150',[180]='\210\145',[181]='\194\181',
  11.   [182]='\194\182',[183]='\194\183',[184]='\209\145',[185]='\226\132\150',[186]='\209\148',[187]='\194\187',
  12.   [188]='\209\152',[189]='\208\133',[190]='\209\149',[191]='\209\151'
  13. }
  14. local utf8_decode={
  15.   [128]={[147]='\150',[148]='\151',[152]='\145',[153]='\146',[154]='\130',[156]='\147',[157]='\148',[158]='\132',[160]='\134',[161]='\135',[162]='\149',[166]='\133',[176]='\137',[185]='\139',[186]='\155'},
  16.   [130]={[172]='\136'},
  17.   [132]={[150]='\185',[162]='\153'},
  18.   [194]={[152]='\152',[160]='\160',[164]='\164',[166]='\166',[167]='\167',[169]='\169',[171]='\171',[172]='\172',[173]='\173',[174]='\174',[176]='\176',[177]='\177',[181]='\181',[182]='\182',[183]='\183',[187]='\187'},
  19.   [208]={[129]='\168',[130]='\128',[131]='\129',[132]='\170',[133]='\189',[134]='\178',[135]='\175',[136]='\163',[137]='\138',[138]='\140',[139]='\142',[140]='\141',[143]='\143',[144]='\192',[145]='\193',[146]='\194',[147]='\195',[148]='\196',
  20.   [149]='\197',[150]='\198',[151]='\199',[152]='\200',[153]='\201',[154]='\202',[155]='\203',[156]='\204',[157]='\205',[158]='\206',[159]='\207',[160]='\208',[161]='\209',[162]='\210',[163]='\211',[164]='\212',[165]='\213',[166]='\214',
  21.   [167]='\215',[168]='\216',[169]='\217',[170]='\218',[171]='\219',[172]='\220',[173]='\221',[174]='\222',[175]='\223',[176]='\224',[177]='\225',[178]='\226',[179]='\227',[180]='\228',[181]='\229',[182]='\230',[183]='\231',[184]='\232',
  22.   [185]='\233',[186]='\234',[187]='\235',[188]='\236',[189]='\237',[190]='\238',[191]='\239'},
  23.   [209]={[128]='\240',[129]='\241',[130]='\242',[131]='\243',[132]='\244',[133]='\245',[134]='\246',[135]='\247',[136]='\248',[137]='\249',[138]='\250',[139]='\251',[140]='\252',[141]='\253',[142]='\254',[143]='\255',[144]='\161',[145]='\184',
  24.   [146]='\144',[147]='\131',[148]='\186',[149]='\190',[150]='\179',[151]='\191',[152]='\188',[153]='\154',[154]='\156',[155]='\158',[156]='\157',[158]='\162',[159]='\159'},[210]={[144]='\165',[145]='\180'}
  25. }
  26.  
  27. local nmdc = {
  28.   [36] = '$',
  29.   [124] = '|'
  30. }
  31.  
  32. local function decode_ansi(s) -- ansi --> utf8
  33.   local r, b = {}
  34.   for i = 1, s and s:len() or 0 do
  35.         local index = #r + 1
  36.     b = s:byte(i)
  37.     if b < 128 then
  38.       r[index] = string.char(b)
  39.     else
  40.       if b > 239 then
  41.         r[index] = '\209'..string.char(b - 112)
  42.       elseif b > 191 then
  43.         r[index] = '\208'..string.char(b - 48)
  44.       elseif ansi_decode[b] then
  45.         r[index] = ansi_decode[b]
  46.       else
  47.         r[index] = '_'
  48.       end
  49.     end
  50.   end
  51.   return table.concat(r)
  52. end
  53.  
  54. local function encode_ansi(s) -- utf8 --> ansi
  55.   local a, j, r, b = 0, 0, {}
  56.   for i = 1, s and s:len() or 0 do
  57.         local index = #r + 1
  58.     b = s:byte(i)
  59.     if b < 128 then
  60.       if nmdc[b] then
  61.                 r[index] = nmdc[b]
  62.       else
  63.                 r[index] = string.char(b)
  64.       end
  65.     elseif a == 2 then
  66.       a, j = a - 1, b
  67.     elseif a == 1 then
  68.       a = a - 1
  69.             r[index] = utf8_decode[j][b]
  70.     elseif b == 226 then
  71.       a = 2
  72.     elseif b == 194 or b == 208 or b == 209 or b == 210 then
  73.       j, a = b, 1
  74.     else
  75.       r[index] = '_'
  76.     end
  77.   end
  78.   return table.concat(r)
  79. end
  80.  
  81. return {
  82.     encode = encode_ansi,
  83.     decode = decode_ansi
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement