Method: URI.encode_www_form_component
- Defined in:
- lib/tire/rubyext/uri_escape.rb
.encode_www_form_component(s) ⇒ Object
Encode given +s+ to URL-encoded form data.
This method doesn't convert *, -, ., 0-9, A-Z, _, a-z, but does convert SP (ASCII space) to + and converts others to %XX.
This is an implementation of http://www.w3.org/TR/html5/forms.html#url-encoded-form-data
See URI.decode_www_form_component, URI.encode_www_form
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tire/rubyext/uri_escape.rb', line 26 def self.encode_www_form_component(s) str = s.to_s if RUBY_VERSION < "1.9" && $KCODE =~ /u/i str.gsub(/([^ a-zA-Z0-9_.-]+)/) do '%' + $1.unpack('H2' * Rack::Utils.bytesize($1)).join('%').upcase end.tr(' ', '+') else if TBLENCWWWCOMP_.empty? tbl = {} 256.times do |i| tbl[i.chr] = '%%%02X' % i end tbl[' '] = '+' begin TBLENCWWWCOMP_.replace(tbl) TBLENCWWWCOMP_.freeze rescue end end str.gsub(/[^*\-.0-9A-Z_a-z]/) {|m| TBLENCWWWCOMP_[m]} end end |