Created
November 14, 2014 07:11
-
-
Save qpSHiNqp/84cda00abbba350f20a2 to your computer and use it in GitHub Desktop.
URLEncoder/Decoder for lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[---------------------------------------------------------------------------- | |
Copyright 2014 S. Tanaka | |
All Rights Reserved. | |
-------------------------------------------------------------------------------- | |
URLEncoder.lua | |
Encodes any strings into URL encoded strings with a rule based on rawurlencode in PHP. | |
------------------------------------------------------------------------------]] | |
URLEncoder = { | |
encode = function(str) | |
if (not str) then | |
return str | |
end | |
str = string.gsub (str, "\n", "\r\n") | |
str = string.gsub (str, "[^%w.%-_~]", | |
function (c) return string.format ("%%%02X", string.byte(c)) end) | |
return str | |
end, | |
decode = function(str) | |
str = string.gsub(str, "%%([0-9a-fA-F][0-9a-fA-F])", | |
function (c) return string.char(tonumber("0x" .. c)) end) | |
str = string.gsub (str, "\n", "\r\n") | |
return str | |
end, | |
} |
使い方
require('URLEncoder')
coded = URLEncoder.encode("エンコードされたい文字列")
print(coded)
raw = URLEncoder.decode(coded)
print(raw)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PHPのrawurlencodeと同じ挙動(のつもり)のlua版url encoder/decoder
あんまりググっても見つからなかったので適当に作りました。
間違いがあったら教えて下さい。