A miscellaneous collection of extensions to Apple's Foundation framework.
collect(upTo:stripTerminator:)
gathers items from the sequence into aRangeReplaceableCollection
of your choosing, up until either the end of the sequence or the given terminator subsequence is encountered. The terminator can optionally be included in the returned collection.
longestPrefix(where:)
determines the longest prefix that matches a given condition (optionally performing a transformation on that prefix, as well), using a binary search.
bits
returns the individual bits of an integer, in an array. e.g.19.bits
->[1, 2, 16]
.bitIndices
is likebits
but returns the indices (as anIndexSet
) instead of the values themselves, e.g.19.bitIndices
->IndexSet([0...1], 4)
.
asHexString(uppercase:delimiterEvery:delimiter:)
formats aData
into hex (as aString
), e.g.Data(bytes: "woot", count: 4).asHexString(delimiterEvery: 1)
->"77 6F 6F 74"
.- It also has a shorthand version
asHexString
for convenience (omitting the parentheses), if you don't need to customise its defaults.
- It also has a shorthand version
clamp
andclamped
to conform a value into a given range (supporting all finite range types), e.g.5.clamped(..<0)
->-1
.- Note: up-to-but-not-including ranges (
..<
) are only supported on types that are alsoStrideable
.
- Note: up-to-but-not-including ranges (
asString(encoding:)
is a more ergonomic version ofString(data:encoding:)
.- It has a shorthand version
asString
which assumes UTF-8.
- It has a shorthand version
timeAgo
returns a human-readable, localised description of how long ago a givenDate
was, e.g. "2 hours ago".
intMax
is the upper bound of the range of integer values that can be accurately represented in the respective floating-point type.
async
to return an async version of a sync iterator.typeErased
property to convert the iterator toAnyIterator
, as a convenience for cases such as optional chaining.
POSIX
is a simple constant for the POSIX locale, as a convenience so you can writeLocale.POSIX
instead ofLocale(identifier: "en_US_POSIX")
.
orNilString
returns aString
describing the contents or the string literal "nil" if theOptional
is empty. ForOptional<String>
it returns the containedString
directly (when present), for all other types it usesString(describing:)
.
quoted
returns a quoted version of the string, with backslash-escaping for existing quotes and backslashes in the string. e.g.#"Hello, "Alex" \ "Alexis"."#.quoted
->#""Hello, \"Alex\" \\ \"Alexis\".""#