2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,5 +1,5 @@
The task is to provide a function or mechanism to convert a provided string
into URL encoding representation.
;Task:
Provide a function or mechanism to convert a provided string into URL encoding representation.
In URL encoding, special characters, control characters and extended characters
are converted into a percent symbol followed by a two digit hexadecimal code,
@ -7,28 +7,30 @@ So a space character encodes into %20 within the string.
For the purposes of this task, every character except 0-9, A-Z and a-z requires conversion, so the following characters all require conversion by default:
*ASCII control codes (Character ranges 00-1F hex (0-31 decimal) and 7F (127 decimal).
*ASCII symbols (Character ranges 32-47 decimal (20-2F hex))
*ASCII symbols (Character ranges 58-64 decimal (3A-40 hex))
*ASCII symbols (Character ranges 91-96 decimal (5B-60 hex))
*ASCII symbols (Character ranges 123-126 decimal (7B-7E hex))
*Extended characters with character codes of 128 decimal (80 hex) and above.
'''Example'''
* ASCII control codes (Character ranges 00-1F hex (0-31 decimal) and 7F (127 decimal).
* ASCII symbols (Character ranges 32-47 decimal (20-2F hex))
* ASCII symbols (Character ranges 58-64 decimal (3A-40 hex))
* ASCII symbols (Character ranges 91-96 decimal (5B-60 hex))
* ASCII symbols (Character ranges 123-126 decimal (7B-7E hex))
* Extended characters with character codes of 128 decimal (80 hex) and above.
<br>
;Example:
The string "<code><nowiki>http://foo bar/</nowiki></code>" would be encoded as "<code><nowiki>http%3A%2F%2Ffoo%20bar%2F</nowiki></code>".
'''Variations'''
;Variations:
* Lowercase escapes are legal, as in "<code><nowiki>http%3a%2f%2ffoo%20bar%2f</nowiki></code>".
* Some standards give different rules: RFC 3986, ''Uniform Resource Identifier (URI): Generic Syntax'', section 2.3, says that "-._~" should not be encoded. HTML 5, section [http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#url-encoded-form-data 4.10.22.5 URL-encoded form data], says to preserve "-._*", and to encode space " " to "+". The options below provide for utilization of an exception string, enabling preservation (non encoding) of particular characters to meet specific standards.
'''Options'''
;Options:
It is permissible to use an exception string (containing a set of symbols
that do not need to be converted).
However, this is an optional feature and is not a requirement of this task.
;See also
[[URL decoding]]
[[URL parser]]
;Related tasks:
* &nbsp; [[URL decoding]]
* &nbsp; [[URL parser]]
<br><br>

View file

@ -1,2 +1,2 @@
iex(1)> URI.encode("http://foo bar/", &(URI.char_unreserved?/1))
iex(1)> URI.encode("http://foo bar/", &URI.char_unreserved?/1)
"http%3A%2F%2Ffoo%20bar%2F"

View file

@ -1,13 +1,21 @@
Function URLEncode(URL As String, Exceptions As String = "") As String
For i As Integer = 0 To 255
If InStr(Exceptions, Chr(i)) > 0 Then Continue For i
Dim char As String = Chr(127) + Right("00" + Hex(i), 2)
URL = ReplaceAll(URL, Chr(i), char)
If i = 47 Then i = 57
If i = 64 Then i = 90
If i = 96 Then i = 122
If i = 126 Then i = 128
Function URLEncode(Data As String, ParamArray Exceptions() As String) As String
Dim buf As String
For i As Integer = 1 To Data.Len
Dim char As String = Data.Mid(i, 1)
Select Case Asc(char)
Case 48 To 57, 65 To 90, 97 To 122, 45, 46, 95
buf = buf + char
Else
If Exceptions.IndexOf(char) > -1 Then
buf = buf + char
Else
buf = buf + "%" + Left(Hex(Asc(char)) + "00", 2)
End If
End Select
Next
URL = ReplaceAll(URL, Chr(127), "%")
Return URL
Return buf
End Function
'usage
Dim s As String = URLEncode("http://foo bar/") ' no exceptions
Dim t As String = URLEncode("http://foo bar/", "!", "?", ",") ' with exceptions