Skip to content

Commit

Permalink
[ruby/uri] [DOC] Common rdoc (ruby/uri#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
BurdetteLamar authored and matzbot committed Jan 7, 2023
1 parent 28cfc0c commit fd98169
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/uri/common.rb
Expand Up @@ -330,6 +330,8 @@ def self.regexp(schemes = nil)# :nodoc:
# and then to encoding +enc+.
#
# In either case, the returned string has forced encoding Encoding::US_ASCII.
#
# Related: URI.encode_uri_component (encodes <tt>' '</tt> as <tt>'%20'</tt>).
def self.encode_www_form_component(str, enc=nil)
_encode_uri_component(/[^*\-.0-9A-Z_a-z]/, TBLENCWWWCOMP_, str, enc)
end
Expand Down Expand Up @@ -362,20 +364,18 @@ def self.encode_www_form_component(str, enc=nil)
# URI.decode_www_form_component('Here+are+some+punctuation+characters%3A+%2C%3B%3F%3A')
# # => "Here are some punctuation characters: ,;?:"
#
# Related: URI.decode_uri_component (preserves <tt>'+'</tt>).
def self.decode_www_form_component(str, enc=Encoding::UTF_8)
_decode_uri_component(/\+|%\h\h/, str, enc)
end

# Encodes +str+ using URL encoding
#
# This encodes SP to %20 instead of +.
# Like URI.encode_www_form_component, except that <tt>' '</tt> (space)
# is encoded as <tt>'%20'</tt> (instead of <tt>'+'</tt>).
def self.encode_uri_component(str, enc=nil)
_encode_uri_component(/[^*\-.0-9A-Z_a-z]/, TBLENCURICOMP_, str, enc)
end

# Decodes given +str+ of URL-encoded data.
#
# This does not decode + to SP.
# Like URI.decode_www_form_component, except that <tt>'+'</tt> is preserved.
def self.decode_uri_component(str, enc=Encoding::UTF_8)
_decode_uri_component(/%\h\h/, str, enc)
end
Expand Down Expand Up @@ -419,6 +419,12 @@ def self._decode_uri_component(regexp, str, enc)
# URI.encode_www_form({foo: 0, bar: 1, baz: 2})
# # => "foo=0&bar=1&baz=2"
#
# The returned string is formed using method URI.encode_www_form_component,
# which converts certain characters:
#
# URI.encode_www_form('f#o': '/', 'b-r': '$')
# # => "f%23o=%2F&b-r=%24"
#
# When +enum+ is Array-like, each element +ele+ is converted to a field:
#
# - If +ele+ is an array of two or more elements,
Expand Down

0 comments on commit fd98169

Please sign in to comment.