WebURL Documentation Beta

Structure Lazily​Percent​Encoded

public struct LazilyPercentEncoded<Source, EncodeSet>: Collection, LazyCollectionProtocol
where Source: Collection, Source.Element == UInt8, EncodeSet: PercentEncodeSet 

A Collection which percent-encodes elements from its Source on-demand using a given EncodeSet.

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 which are not ASCII code-points are always percent-encoded, and bytes within the ASCII range are encoded according to the encode-set's shouldPercentEncode(ascii:) method.

Encode-sets may also include a SubstitutionMap. If a code-point is not percent-encoded, the substitution map may replace it with a different character. Content encoded with substitutions must be decoded using the same substitution map it was created with.

To encode a source collection, use the extension methods provided on LazyCollectionProtocol:

// Encode arbitrary data as an ASCII string.
let image: Data = ...
image.lazy.percentEncoded(using: .urlComponentSet) // ASCII bytes, decoding to "%BAt_%E0%11%22%EB%10%2C%7F..."

// Encode-sets determine which characters are encoded, and some perform substitutions.
let bytes = "hello, world!".utf8
bytes.lazy.percentEncoded(using: .urlComponentSet)
  .elementsEqual("hello%2C%20world!".utf8) // ✅
bytes.lazy.percentEncoded(using: .formEncoding)
  .elementsEqual("hello%2C+world%21".utf8) // ✅

The elements of this collection are guaranteed to be ASCII code-units, and hence valid UTF-8.

Conforms To

BidirectionalCollection
Collection
LazyCollectionProtocol

Properties

start​Index

public let startIndex: Index

end​Index

@inlinable
public var endIndex: Index 

is​Empty

@inlinable
public var isEmpty: Bool 

underestimated​Count

@inlinable
public var underestimatedCount: Int 

Methods

index(after:​)

@inlinable
public func index(after i: Index) -> Index 

form​Index(after:​)

@inlinable
public func formIndex(after i: inout Index)