webmcp

annotate framework/env/encode/mime/atom_token.lua @ 405:c5f9a1b2f225

Updated year of copyright notice
author jbe
date Wed Jan 06 02:54:45 2016 +0100 (2016-01-06)
parents 9fdfb27f8e67
children
rev   line source
jbe/bsw@0 1 function encode.mime.atom_token(str)
jbe/bsw@0 2 local charset = "UTF-8" -- TODO: support other charsets via locale system
jbe/bsw@0 3 if
jbe/bsw@0 4 string.find(str, "^[0-9A-Za-z!#%$%%&'%*%+%-/=%?%^_`{|}~]+$")
jbe/bsw@0 5 then
jbe/bsw@0 6 return str
jbe/bsw@0 7 elseif
jbe/bsw@0 8 string.find(str, "^[\t 0-9A-Za-z!#%$%%&'%*%+%-/=%?%^_`{|}~]+$")
jbe/bsw@0 9 then
jbe/bsw@0 10 return '"' .. str .. '"'
jbe/bsw@0 11 elseif
jbe/bsw@0 12 string.find(str, "^[\t -~]*$")
jbe/bsw@0 13 then
jbe/bsw@0 14 local parts = { '"' }
jbe/bsw@0 15 for char in string.gmatch(str, ".") do
jbe/bsw@0 16 if char == '"' or char == "\\" then
jbe/bsw@0 17 parts[#parts+1] = "\\"
jbe/bsw@0 18 end
jbe/bsw@0 19 parts[#parts+1] = char
jbe/bsw@0 20 end
jbe/bsw@0 21 parts[#parts+1] = '"'
jbe/bsw@0 22 return table.concat(parts)
jbe/bsw@0 23 else
jbe/bsw@0 24 local parts = { "=?", charset, "?Q?" }
jbe/bsw@0 25 for char in string.gmatch(str, ".") do
jbe/bsw@0 26 local byte = string.byte(char)
jbe/bsw@0 27 if string.find(char, "^[0-9A-Za-z%.%-]$") then
jbe/bsw@0 28 parts[#parts+1] = char
jbe/bsw@0 29 else
jbe/bsw@0 30 local byte = string.byte(char)
jbe/bsw@0 31 if byte == 32 then
jbe/bsw@0 32 parts[#parts+1] = "_"
jbe/bsw@0 33 else
jbe/bsw@0 34 parts[#parts+1] = string.format("=%02X", byte)
jbe/bsw@0 35 end
jbe/bsw@0 36 end
jbe/bsw@0 37 end
jbe/bsw@0 38 parts[#parts+1] = "?="
jbe/bsw@0 39 return table.concat(parts)
jbe/bsw@0 40 end
jbe/bsw@0 41 end

Impressum / About Us