A miscellaneous collection of extensions to Apple's Foundation framework.
collect(upTo:stripTerminator:)gathers items from the sequence into aRangeReplaceableCollectionof 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.
bitsreturns the individual bits of an integer, in an array. e.g.19.bits->[1, 2, 16].bitIndicesis likebitsbut returns the indices (as anIndexSet) instead of the values themselves, e.g.19.bitIndices->IndexSet([0...1], 4).
asHexString(uppercase:delimiterEvery:delimiter:)formats aDatainto hex (as aString), e.g.Data(bytes: "woot", count: 4).asHexString(delimiterEvery: 1)->"77 6F 6F 74".- It also has a shorthand version
asHexStringfor convenience (omitting the parentheses), if you don't need to customise its defaults.
- It also has a shorthand version
clampandclampedto 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
asStringwhich assumes UTF-8.
- It has a shorthand version
timeAgoreturns a human-readable, localised description of how long ago a givenDatewas, e.g. "2 hours ago".
intMaxis the upper bound of the range of integer values that can be accurately represented in the respective floating-point type.
asyncto return an async version of a sync iterator.typeErasedproperty to convert the iterator toAnyIterator, as a convenience for cases such as optional chaining.
POSIXis a simple constant for the POSIX locale, as a convenience so you can writeLocale.POSIXinstead ofLocale(identifier: "en_US_POSIX").
orNilStringreturns aStringdescribing the contents or the string literal "nil" if theOptionalis empty. ForOptional<String>it returns the containedStringdirectly (when present), for all other types it usesString(describing:).
quotedreturns 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\".""#