Skip to content

Commit d407fea

Browse files
committed
Add URI.encode_uri_component and URI.decode_uri_component
1 parent 9cabe4e commit d407fea

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

stdlib/uri/0/common.rbs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ end
115115
module URI
116116
include URI::RFC2396_REGEXP
117117

118+
# <!--
119+
# rdoc-file=lib/uri/common.rb
120+
# - decode_uri_component(str, enc=Encoding::UTF_8)
121+
# -->
122+
# Like URI.decode_www_form_component, except that `'+'` is preserved.
123+
#
124+
def self.decode_uri_component: (String str, ?encoding enc) -> String
125+
118126
# <!--
119127
# rdoc-file=lib/uri/common.rb
120128
# - decode_www_form(str, enc=Encoding::UTF_8, separator: '&', use__charset_: false, isindex: false)
@@ -189,6 +197,15 @@ module URI
189197
#
190198
def self.decode_www_form_component: (String str, ?encoding enc) -> String
191199

200+
# <!--
201+
# rdoc-file=lib/uri/common.rb
202+
# - encode_uri_component(str, enc=nil)
203+
# -->
204+
# Like URI.encode_www_form_component, except that `' '` (space) is encoded as
205+
# `'%20'` (instead of `'+'`).
206+
#
207+
def self.encode_uri_component: (String str, ?encoding enc) -> String
208+
192209
# <!--
193210
# rdoc-file=lib/uri/common.rb
194211
# - encode_www_form(enum, enc=nil)

test/stdlib/URI_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ class URISingletonTest < Test::Unit::TestCase
77
library "uri"
88
testing "singleton(::URI)"
99

10+
def test_decode_uri_component
11+
assert_send_type(
12+
"(String) -> String",
13+
URI, :decode_uri_component, "Hello%20World%20%E6%97%A5%E6%9C%AC%E8%AA%9E"
14+
)
15+
16+
assert_send_type(
17+
"(String, Encoding) -> String",
18+
URI, :decode_uri_component, "Hello%20World%20%E6%97%A5%E6%9C%AC%E8%AA%9E", Encoding::UTF_8
19+
)
20+
end
21+
1022
def test_decode_www_form
1123
assert_send_type "(String) -> Array[[String, String]]",
1224
URI, :decode_www_form, "a=1&a=2&b=3"
@@ -45,6 +57,17 @@ def test_decode_www_form_component
4557
URI, :decode_www_form_component, "%A1", "sjis"
4658
end
4759

60+
def test_encode_uri_component
61+
assert_send_type(
62+
"(String) -> String",
63+
URI, :encode_uri_component, "Hello World 日本語"
64+
)
65+
assert_send_type(
66+
"(String, Encoding) -> String",
67+
URI, :encode_uri_component, "Hello World 日本語", Encoding::UTF_8
68+
)
69+
end
70+
4871
def test_encode_www_form
4972
assert_send_type "(Array[[String, String | Numeric]]) -> String",
5073
URI, :encode_www_form, [["a", "1"], ["a", 2], ["b", "3"]]

0 commit comments

Comments
 (0)