Protocol
PercentEncodeSet
public protocol PercentEncodeSet
A set of ASCII code-points which should be percent-encoded.
Percent-encoding transforms arbitrary bytes to ASCII strings (e.g. the byte value 200, or 0xC8, to the string "%C8"),
and is most commonly used to escape special characters in URLs. Bytes within the ASCII range are encoded according
to the encode-set's shouldPercentEncode(ascii:)
method, and bytes which are not ASCII code-points are always
percent-encoded.
The following example demonstrates an encode-set which encodes ASCII single and double quotation marks:
struct NoQuotes: PercentEncodeSet {
func shouldPercentEncode(ascii codePoint: UInt8) -> Bool {
codePoint == Character("\"").asciiValue! ||
codePoint == Character("'").asciiValue!
}
}
#"Quoth the Raven "Nevermore.""#.percentEncoded(using: NoQuotes()) // "Quoth the Raven %22Nevermore.%22"
Form-encoding is a legacy variant of percent-encoding, including a SubstitutionMap
which replaces spaces with the "+" character.
Whilst you may define your own encode-sets with custom substitution maps, doing so is often unwise.
Default Implementations
substitutions
@inlinable @inline(__always)
public var substitutions: NoSubstitutions
c0ControlSet
@inlinable
public static var c0ControlSet: URLEncodeSet.C0Control
The C0 control percent-encode set.
fragmentSet
@inlinable
public static var fragmentSet: URLEncodeSet.Fragment
The fragment percent-encode set.
specialQuerySet
@inlinable
public static var specialQuerySet: URLEncodeSet.SpecialQuery
The special query percent-encode set.
userInfoSet
@inlinable
public static var userInfoSet: URLEncodeSet.UserInfo
The userinfo percent-encode set.
urlComponentSet
@inlinable
public static var urlComponentSet: URLEncodeSet.Component
The component percent-encode set.
formEncoding
@inlinable
public static var formEncoding: URLEncodeSet.FormEncoding
The application/x-www-form-urlencoded percent-encode set.
Requirements
shouldPercentEncode(ascii:)
func shouldPercentEncode(ascii codePoint: UInt8) -> Bool
Whether or not the given ASCII codePoint
should be percent-encoded.
Parameters
Name | Type | Description |
---|---|---|
codePoint | UInt8 |
An ASCII code-point. Always in the range |
Returns
Whether or not codePoint
should be percent-encoded.
Substitutions
associatedtype Substitutions: SubstitutionMap = NoSubstitutions
A SubstitutionMap
which may replace ASCII code-points that are not percent-encoded.
This is a legacy feature to support form-encoding; modern percent-encoding does not use substitutions. Should you have reason to create a custom substitution map, please observe the following guidelines:
-
The set of code-points to percent-encode and substitute must not overlap. There must be no code-point
x
for whichsubstitutions.substitute(ascii: x) != nil
andshouldPercentEncode(ascii: x) == true
. -
Substitute code-points returned by
substitute(ascii:)
should be percent-encoded. In other words, ify = substitutions.substitute(ascii: x)
,shouldPercentEncode(ascii: y)
should betrue
.
For example, form-encoding substitutes spaces with the "+" character. It would be incorrect for that encode-set to say that the space character should be both substituted and percent-encoded (#1 above), and it must ensure that actual "+" characters in the source data are percent-encoded, so all "+" characters in the result can be interpreted as encoded spaces (#2 above).
substitutions
var substitutions: Substitutions
The substitution map which applies to ASCII code-points that are not percent-encoded.