webmcp
diff framework/env/encode/mime/atom_token.lua @ 0:9fdfb27f8e67
Version 1.0.0
author | jbe/bsw |
---|---|
date | Sun Oct 25 12:00:00 2009 +0100 (2009-10-25) |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/encode/mime/atom_token.lua Sun Oct 25 12:00:00 2009 +0100 1.3 @@ -0,0 +1,41 @@ 1.4 +function encode.mime.atom_token(str) 1.5 + local charset = "UTF-8" -- TODO: support other charsets via locale system 1.6 + if 1.7 + string.find(str, "^[0-9A-Za-z!#%$%%&'%*%+%-/=%?%^_`{|}~]+$") 1.8 + then 1.9 + return str 1.10 + elseif 1.11 + string.find(str, "^[\t 0-9A-Za-z!#%$%%&'%*%+%-/=%?%^_`{|}~]+$") 1.12 + then 1.13 + return '"' .. str .. '"' 1.14 + elseif 1.15 + string.find(str, "^[\t -~]*$") 1.16 + then 1.17 + local parts = { '"' } 1.18 + for char in string.gmatch(str, ".") do 1.19 + if char == '"' or char == "\\" then 1.20 + parts[#parts+1] = "\\" 1.21 + end 1.22 + parts[#parts+1] = char 1.23 + end 1.24 + parts[#parts+1] = '"' 1.25 + return table.concat(parts) 1.26 + else 1.27 + local parts = { "=?", charset, "?Q?" } 1.28 + for char in string.gmatch(str, ".") do 1.29 + local byte = string.byte(char) 1.30 + if string.find(char, "^[0-9A-Za-z%.%-]$") then 1.31 + parts[#parts+1] = char 1.32 + else 1.33 + local byte = string.byte(char) 1.34 + if byte == 32 then 1.35 + parts[#parts+1] = "_" 1.36 + else 1.37 + parts[#parts+1] = string.format("=%02X", byte) 1.38 + end 1.39 + end 1.40 + end 1.41 + parts[#parts+1] = "?=" 1.42 + return table.concat(parts) 1.43 + end 1.44 +end 1.45 \ No newline at end of file