Sha256: a871956b2833bd99f1c063bee7eed9634c7e66001c391ef49ab1dc0553f89ffa

Contents?: true

Size: 1.63 KB

Versions: 85

Compression:

Stored size: 1.63 KB

Contents

# :stopdoc:

# Stolen from ruby core's uri/common.rb, with modifications to support 1.8.x
#
# https://github.com/ruby/ruby/blob/trunk/lib/uri/common.rb
#
#

module URI
  TBLENCWWWCOMP_ = {} # :nodoc:
  256.times do |i|
    TBLENCWWWCOMP_[i.chr] = '%%%02X' % i
  end
  TBLENCWWWCOMP_[' '] = '+'
  TBLENCWWWCOMP_.freeze
  TBLDECWWWCOMP_ = {} # :nodoc:
  256.times do |i|
    h, l = i>>4, i&15
    TBLDECWWWCOMP_['%%%X%X' % [h, l]] = i.chr
    TBLDECWWWCOMP_['%%%x%X' % [h, l]] = i.chr
    TBLDECWWWCOMP_['%%%X%x' % [h, l]] = i.chr
    TBLDECWWWCOMP_['%%%x%x' % [h, l]] = i.chr
  end
  TBLDECWWWCOMP_['+'] = ' '
  TBLDECWWWCOMP_.freeze

  # 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
  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
      str.gsub(/[^*\-.0-9A-Z_a-z]/) {|m| TBLENCWWWCOMP_[m]}
    end
  end

  # Decode given +str+ of URL-encoded form data.
  #
  # This decods + to SP.
  #
  # See URI.encode_www_form_component, URI.decode_www_form
  def self.decode_www_form_component(str, enc=nil)
    raise ArgumentError, "invalid %-encoding (#{str})" unless /\A(?:%[0-9a-fA-F]{2}|[^%])*\z/ =~ str
    str.gsub(/\+|%[0-9a-fA-F]{2}/) {|m| TBLDECWWWCOMP_[m]}
  end
end

Version data entries

85 entries across 74 versions & 21 rubygems

Version Path
graphael-on-rails-0.5.1 vendor/bundle/gems/rack-1.4.5/lib/rack/backports/uri/common_18.rb
graphael-on-rails-0.0.1 vendor/bundle/gems/rack-1.4.5/lib/rack/backports/uri/common_18.rb
challah-0.9.1.beta.3 vendor/bundle/gems/rack-1.4.3/lib/rack/backports/uri/common_18.rb
rack-1.5.2 lib/rack/backports/uri/common_18.rb
rack-1.4.5 lib/rack/backports/uri/common_18.rb
rack-1.3.10 lib/rack/backports/uri/common_18.rb
devise_sociable-0.1.0 vendor/bundle/gems/rack-1.4.4/lib/rack/backports/uri/common_18.rb
devise_sociable-0.1.0 vendor/bundle/gems/rack-1.5.1/lib/rack/backports/uri/common_18.rb
rack-1.5.1 lib/rack/backports/uri/common_18.rb
rack-1.5.0 lib/rack/backports/uri/common_18.rb
backbone-queryparams-rails-0.0.1 vendor/bundle/gems/rack-1.4.4/lib/rack/backports/uri/common_18.rb
railscast-assets-0.0.2 vendor/bundle/gems/backbone-forms-on-rails-0.10.0/vendor/bundle/gems/rack-1.4.4/lib/rack/backports/uri/common_18.rb
railscast-assets-0.0.2 vendor/bundle/gems/rack-1.4.4/lib/rack/backports/uri/common_18.rb
backbone-forms-on-rails-0.10.0 vendor/bundle/gems/rack-1.4.4/lib/rack/backports/uri/common_18.rb
backbone-forms-on-rails-0.10.0 vendor/bundle/gems/backbone-forms-on-rails-0.10.0/vendor/bundle/gems/rack-1.4.4/lib/rack/backports/uri/common_18.rb
rack-1.5.0.beta.2 lib/rack/backports/uri/common_18.rb
rack-1.4.4 lib/rack/backports/uri/common_18.rb
rack-1.3.9 lib/rack/backports/uri/common_18.rb
backbone-deep-rails-0.0.1 vendor/bundle/gems/rack-1.4.3/lib/rack/backports/uri/common_18.rb
rack-1.5.0.beta.1 lib/rack/backports/uri/common_18.rb