WebURL Documentation Beta

Extensions on StringProtocol

Methods

percent​Encoded(using:​)

@inlinable
public func percentEncoded<EncodeSet: PercentEncodeSet>(using encodeSet: EncodeSet) -> String 

Returns an ASCII string formed by percent-encoding this string's UTF-8 representation.

Percent-encoding transforms arbitrary bytes to ASCII strings (e.g. the byte value 200, or 0xC8, to the string "%C8"), with bytes within the ASCII range being restricted by encodeSet.

Form-encoding (as used by HTML forms) is a legacy variant of percent-encoding which includes substitutions; provide the appropriate SubstitutionMap when decoding form-encoded data to accurately recover the source contents.

// Percent-encoding can be used to escapes special characters, e.g. spaces.
"hello, world!".percentEncoded(using: .userInfoSet) // "hello,%20world!"

// Encode-sets determine which characters are encoded, and some perform substitutions.
"/usr/bin/swift".percentEncoded(using: .urlComponentSet) // "%2Fusr%2Fbin%2Fswift"
"king of the 🦆s".percentEncoded(using: .formEncoding) // "king+of+the+%F0%9F%A6%86s"

percent​Encoded(using:​)

@inlinable @_disfavoredOverload
public func percentEncoded<EncodeSet: PercentEncodeSet>(using encodeSet: EncodeSet._Member) -> String 

Returns an ASCII string formed by percent-encoding this string's UTF-8 representation.

Percent-encoding transforms arbitrary bytes to ASCII strings (e.g. the byte value 200, or 0xC8, to the string "%C8"), with bytes within the ASCII range being restricted by encodeSet.

Form-encoding (as used by HTML forms) is a legacy variant of percent-encoding which includes substitutions; provide the appropriate SubstitutionMap when decoding form-encoded data to accurately recover the source contents.

// Percent-encoding can be used to escapes special characters, e.g. spaces.
"hello, world!".percentEncoded(using: .userInfoSet) // "hello,%20world!"

// Encode-sets determine which characters are encoded, and some perform substitutions.
"/usr/bin/swift".percentEncoded(using: .urlComponentSet) // "%2Fusr%2Fbin%2Fswift"
"king of the 🦆s".percentEncoded(using: .formEncoding) // "king+of+the+%F0%9F%A6%86s"

percent​Decoded​Bytes​Array(substitutions:​)

@inlinable
public func percentDecodedBytesArray<Substitutions: SubstitutionMap>(substitutions: Substitutions) -> [UInt8] 

Returns the percent-decoding of this string as binary data.

Percent-decoding transforms certain ASCII sequences to bytes ("%AB" to the byte value 171, or 0xAB).

Form-encoding (as used by HTML forms) is a legacy variant of percent-encoding which includes substitutions; provide the appropriate SubstitutionMap to accurately decode form-encoded content, or .none for modern percent-encoded content.

let originalImage: Data = ...

// Encode the data, e.g. using form-encoding.
let encodedImage = originalImage.percentEncodedString(using: .formEncoding) // "%BAt_%E0%11%22%EB%10%2C%7F..."

// Decode the data, giving the appropriate substitution map.
let decodedImage = encodedImage.percentDecodedBytesArray(substitutions: .formEncoding)
assert(decodedImage.elementsEqual(originalImage)) // ✅

The returned data contains raw bytes, potenially including NULL bytes or invalid UTF-8.

percent​Decoded​Bytes​Array(substitutions:​)

@inlinable @inline(__always)
public func percentDecodedBytesArray<Substitutions: SubstitutionMap>(
  substitutions: Substitutions._Member
) -> [UInt8] 

Returns the percent-decoding of this string as binary data.

Percent-decoding transforms certain ASCII sequences to bytes ("%AB" to the byte value 171, or 0xAB).

Form-encoding (as used by HTML forms) is a legacy variant of percent-encoding which includes substitutions; provide the appropriate SubstitutionMap to accurately decode form-encoded content, or .none for modern percent-encoded content.

let originalImage: Data = ...

// Encode the data, e.g. using form-encoding.
let encodedImage = originalImage.percentEncodedString(using: .formEncoding) // "%BAt_%E0%11%22%EB%10%2C%7F..."

// Decode the data, giving the appropriate substitution map.
let decodedImage = encodedImage.percentDecodedBytesArray(substitutions: .formEncoding)
assert(decodedImage.elementsEqual(originalImage)) // ✅

The returned data contains raw bytes, potenially including NULL bytes or invalid UTF-8.

percent​Decoded​Bytes​Array()

@inlinable @inline(__always)
public func percentDecodedBytesArray() -> [UInt8] 

Returns the percent-decoding of this string as binary data.

Percent-decoding transforms certain ASCII sequences to bytes ("%AB" to the byte value 171, or 0xAB).

Form-encoding (as used by HTML forms) is a legacy variant of percent-encoding which includes substitutions; provide the appropriate SubstitutionMap to accurately decode form-encoded content, or .none for modern percent-encoded content.

This function is equivalent to calling percentDecodedBytesArray(substitutions: .none).

let url = WebURL("data:application/octet-stream,%BC%A8%CD")!

// Check the data URL's payload.
let payloadOptions = url.path.prefix(while: { $0 != "," })
if payloadOptions.hasSuffix(";base64") {
  // ... decode as base-64
} else {
  let encodedPayload = url.path[payloadOptions.endIndex...].dropFirst()
  let decodedPayload = encodedPayload.percentDecodedBytesArray()
  assert(decodedPayload == [0xBC, 0xA8, 0xCD]) // ✅
}

The returned data contains raw bytes, potenially including NULL bytes or invalid UTF-8.

percent​Decoded(substitutions:​)

@inlinable
public func percentDecoded<Substitutions: SubstitutionMap>(substitutions: Substitutions) -> String 

Returns the percent-decoding of this string, interpreted as UTF-8 code-units.

Percent-decoding transforms certain ASCII sequences to bytes ("%AB" to the byte value 171, or 0xAB).

Form-encoding (as used by HTML forms) is a legacy variant of percent-encoding which includes substitutions; provide the appropriate SubstitutionMap to accurately decode form-encoded content, or .none for modern percent-encoded content.

If the bytes obtained by percent-decoding this string represent anything other than UTF-8 text, use the percentDecodedByteArray function to decode the string as binary data.

// Decode percent-encoded UTF-8 as a string.
"hello,%20world!".percentDecoded(substitutions: .none) // "hello, world!"
"%2Fusr%2Fbin%2Fswift".percentDecoded(substitutions: .none) // "/usr/bin/swift"

// Form-encoded content requires a substitution map to accurately decode.
"king+of+the+%F0%9F%A6%86s".percentDecoded(substitutions: .formEncoding) // "king of the 🦆s"

percent​Decoded(substitutions:​)

@inlinable @inline(__always) @_disfavoredOverload
public func percentDecoded<Substitutions: SubstitutionMap>(substitutions: Substitutions._Member) -> String 

Returns the percent-decoding of this string, interpreted as UTF-8 code-units.

Percent-decoding transforms certain ASCII sequences to bytes ("%AB" to the byte value 171, or 0xAB).

Form-encoding (as used by HTML forms) is a legacy variant of percent-encoding which includes substitutions; provide the appropriate SubstitutionMap to accurately decode form-encoded content, or .none for modern percent-encoded content.

If the bytes obtained by percent-decoding this string represent anything other than UTF-8 text, use the percentDecodedByteArray function to decode the string as binary data.

// Decode percent-encoded UTF-8 as a string.
"hello,%20world!".percentDecoded(substitutions: .none) // "hello, world!"
"%2Fusr%2Fbin%2Fswift".percentDecoded(substitutions: .none) // "/usr/bin/swift"

// Form-encoded content requires a substitution map to accurately decode.
"king+of+the+%F0%9F%A6%86s".percentDecoded(substitutions: .formEncoding) // "king of the 🦆s"

percent​Decoded()

@inlinable @inline(__always)
public func percentDecoded() -> String 

Returns the percent-decoding of this string, interpreted as UTF-8 code-units.

Percent-decoding transforms certain ASCII sequences to bytes ("%AB" to the byte value 171, or 0xAB).

Form-encoding (as used by HTML forms) is a legacy variant of percent-encoding which includes substitutions; provide the appropriate SubstitutionMap to accurately decode form-encoded content, or .none for modern percent-encoded content.

If the bytes obtained by percent-decoding this string represent anything other than UTF-8 text, use the percentDecodedByteArray function to decode the string as binary data.

This function is equivalent to calling percentDecoded(substitutions: .none).

// Decode percent-encoded UTF-8 as a string.
"hello%2C%20world!".percentDecoded() // "hello, world!"
"%2Fusr%2Fbin%2Fswift".percentDecoded() // "/usr/bin/swift"
"%F0%9F%98%8E".percentDecoded() // "😎"