Skip to content

Commit 1c35f57

Browse files
committed
Add code tips
1 parent ad227c7 commit 1c35f57

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

benchmark/bench.lua

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
local bm = require 'benchmark'
2+
local urlencode = require 'urlencode'
3+
4+
local reporter = bm.bm(6)
5+
6+
-- original code from -> https://gist.github.com/cgwxyz/6053d51e8d7134dd2e30
7+
function encodeURI(str)
8+
if (str) then
9+
str = string.gsub (str, "\n", "\r\n")
10+
str = string.gsub (str, "([^%w ])",
11+
function (c) return string.format ("%%%02X", string.byte(c)) end)
12+
str = string.gsub (str, " ", "+")
13+
end
14+
return str
15+
end
16+
17+
function decodeURI(s)
18+
if(s) then
19+
s = string.gsub(s, '%%(%x%x)',
20+
function (hex) return string.char(tonumber(hex,16)) end )
21+
s = string.gsub(s, "+", " ")
22+
end
23+
return s
24+
end
25+
26+
reporter:report(function()
27+
for i = 1, 1e6 do
28+
encodeURI("hello こんにちは สวัสดี")
29+
end
30+
end, 'pure lua: encode url')
31+
32+
reporter:report(function()
33+
for i = 1, 1e6 do
34+
urlencode.encode_url("hello こんにちは สวัสดี")
35+
end
36+
end, 'encodeurl: encode url')
37+
38+
reporter:report(function()
39+
for i = 1, 1e6 do
40+
decodeURI("hello+%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF+%E0%B8%AA%E0%B8%A7%E0%B8%B1%E0%B8%AA%E0%B8%94%E0%B8%B5")
41+
end
42+
end, 'pure lua: decode url')
43+
44+
reporter:report(function()
45+
for i = 1, 1e6 do
46+
urlencode.decode_url("hello+%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF+%E0%B8%AA%E0%B8%A7%E0%B8%B1%E0%B8%AA%E0%B8%94%E0%B8%B5")
47+
end
48+
end, 'encodeurl: decode url')
49+

example/example.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
local urlencode = require 'urlencode'
2+
3+
print(urlencode.encode_url("hello こんにちは")) -- => hello+%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF
4+
print(urlencode.decode_url("hello+%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF")) -- => hello こんにちは

0 commit comments

Comments
 (0)