瀏覽代碼

Character encoding converstion functions for Lua (as test/example)

bel 11 年之前
父節點
當前提交
d8adb70f43
共有 1 個文件被更改,包括 23 次插入0 次删除
  1. 23 0
      test/html_esc.lua

+ 23 - 0
test/html_esc.lua

@@ -35,3 +35,26 @@ htmlEscape[0] = "·" -- in this table, we use a 8 bit character set, where
 
 -- the conversion table should work as a convertion function for strings as well
 setmetatable(htmlEscape, {__call = function (tab,str) return string.gsub(str, ".", function (c) return tab[c:byte()] end) end})
+
+
+function htmlEsc(txt)
+    s = txt:gsub("%&", "&")
+    s = s:gsub("%<", "&lt;")
+    return s:gsub("%>", "&gt;")
+end
+
+
+function iso8859_1_to_utf8(txt)
+    local s = txt:gsub(".",
+      function (c)
+        local b = c:byte()
+        if b < 128 then
+          return c
+        elseif b < 192 then
+          return string.char(194, b)
+        else
+          return string.char(195, b-64)
+        end
+      end)
+    return s
+end