Module: URI
- Defined in:
- lib/gollum/uri_encode_component.rb
Constant Summary collapse
- @@hexCharCodeArray =
Lazily initialized.
0
Class Method Summary collapse
-
.Encode(uri, unescape) ⇒ Object
ECMA-262, section 15.1.3.
-
.isAlphaNumeric(cc) ⇒ Object
isAlphaNumeric(‘’.ord) => false.
- .unescapePredicate(cc) ⇒ Object
- .URIAddEncodedOctetToBuffer(octet, result, index) ⇒ Object
-
.URIEncodeComponent(componentString) ⇒ Object
component must be String.
- .URIEncodeOctets(octets, result, index) ⇒ Object
- .URIEncodePair(cc1, cc2, result, index) ⇒ Object
- .URIEncodeSingle(cc, result, index) ⇒ Object
Class Method Details
.Encode(uri, unescape) ⇒ Object
ECMA-262, section 15.1.3
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/gollum/uri_encode_component.rb', line 172 def Encode(uri, unescape) uriLength = uri.length; # We are going to pass result to %StringFromCharCodeArray # which does not expect any getters/setters installed # on the incoming array. result = Array.new(uriLength); index = 0; k = -1; while ((k+=1) < uriLength) do cc1 = uri.charCodeAt(k); next if cc1.nil? if (self.send(unescape, cc1)) result[index] = cc1; index += 1 else if (cc1 >= 0xDC00 && cc1 <= 0xDFFF); throw("URI malformed") end if (cc1 < 0xD800 || cc1 > 0xDBFF) index = URIEncodeSingle(cc1, result, index); else k+=1; if (k == uriLength); throw("URI malformed") end cc2 = uri.charCodeAt(k); if (cc2 < 0xDC00 || cc2 > 0xDFFF); throw("URI malformed") end index = URIEncodePair(cc1, cc2, result, index); end end end # use .compact to get rid of nils from charCodeAt # return %StringFromCharCodeArray(result); # 'c' = 8 bit signed char # http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-pack return result.compact.pack 'c*' end |
.isAlphaNumeric(cc) ⇒ Object
isAlphaNumeric(‘’.ord) => false
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/gollum/uri_encode_component.rb', line 58 def isAlphaNumeric(cc) # a - z if (97 <= cc && cc <= 122); return true end # A - Z if (65 <= cc && cc <= 90); return true end # 0 - 9 if (48 <= cc && cc <= 57); return true end return false end |
.unescapePredicate(cc) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/gollum/uri_encode_component.rb', line 75 def unescapePredicate(cc) if (isAlphaNumeric(cc)); return true end # ! if (cc == 33); return true end # '()* if (39 <= cc && cc <= 42); return true end # -. if (45 <= cc && cc <= 46); return true end # _ if (cc == 95); return true end # ~ if (cc == 126); return true end return false end |
.URIAddEncodedOctetToBuffer(octet, result, index) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/gollum/uri_encode_component.rb', line 124 def URIAddEncodedOctetToBuffer(octet, result, index) result[index] = 37; # Char code of '%'. index += 1 result[index] = @@hexCharCodeArray[octet >> 4]; index += 1 result[index] = @@hexCharCodeArray[octet & 0x0F]; index += 1 return index; end |
.URIEncodeComponent(componentString) ⇒ Object
component must be String
167 168 169 |
# File 'lib/gollum/uri_encode_component.rb', line 167 def URIEncodeComponent(componentString) Encode(componentString, :unescapePredicate); end |
.URIEncodeOctets(octets, result, index) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/gollum/uri_encode_component.rb', line 134 def URIEncodeOctets(octets, result, index) if (@@hexCharCodeArray == 0) @@hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70]; end index = URIAddEncodedOctetToBuffer(octets[0], result, index); if (octets[1]); index = URIAddEncodedOctetToBuffer(octets[1], result, index) end if (octets[2]); index = URIAddEncodedOctetToBuffer(octets[2], result, index) end if (octets[3]); index = URIAddEncodedOctetToBuffer(octets[3], result, index) end return index; end |
.URIEncodePair(cc1, cc2, result, index) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/gollum/uri_encode_component.rb', line 152 def URIEncodePair(cc1, cc2, result, index) u = ((cc1 >> 6) & 0xF) + 1; w = (cc1 >> 2) & 0xF; x = cc1 & 3; y = (cc2 >> 6) & 0xF; z = cc2 & 63; octets = Array.new(4); octets[0] = (u >> 2) + 240; octets[1] = (((u & 3) << 4) | w) + 128; octets[2] = ((x << 4) | y) + 128; octets[3] = z + 128; return URIEncodeOctets(octets, result, index); end |
.URIEncodeSingle(cc, result, index) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/gollum/uri_encode_component.rb', line 103 def URIEncodeSingle(cc, result, index) x = (cc >> 12) & 0xF; y = (cc >> 6) & 63; z = cc & 63; octets = Array.new(3); if (cc <= 0x007F) octets[0] = cc; elsif (cc <= 0x07FF) octets[0] = y + 192; octets[1] = z + 128; else octets[0] = x + 224; octets[1] = y + 128; octets[2] = z + 128; end return URIEncodeOctets(octets, result, index); end |