StringProtocol

public extension StringProtocol
public extension StringProtocol where Index == String.Index

These are a variety of cool String extensions that add some great extra cheese on the pizza. NOTE: These extensions feature some use of Apple’s built-in CommonCrypto utility.

  • Declaration

    Swift

    var localizedVariant: String { get }

    Return Value

    the localized string (main bundle) for this string.

  • md5

    This calculates an MD5 checksum of the String, and returns it as an uppercase hex String.

    Mostly from Matt Eaton’s blog entry.

    It has direct access to Apple’s built-in CommonCrypto library as per this SO question

    Declaration

    Swift

    var md5: String { get }

    Return Value

    an MD5 hash of the String, as an uppercase hex String.

  • This is a cast for the MD5 function. The convention attribute just says that it’s a “raw” C function.

    Declaration

    Swift

    typealias CC_MD5_TYPE = @convention(c) (UnsafeRawPointer, UInt32, UnsafeMutableRawPointer) -> UnsafeMutableRawPointer
  • This calculates a SHA256 checksum of the String, and returns it as an uppercase hex String.

    Mostly from Matt Eaton’s blog entry.

    It has direct access to Apple’s built-in CommonCrypto library as per this SO question

    Declaration

    Swift

    var sha256: String { get }

    Return Value

    a SHA256 hash of the String, as an uppercase hex String.

  • This is a cast for the SHA256 function. The convention attribute just says that it’s a “raw” C function.

    Declaration

    Swift

    typealias CC_SHA256_TYPE = @convention(c) (UnsafeRawPointer, UInt32, UnsafeMutableRawPointer) -> UnsafeMutableRawPointer
  • Declaration

    Swift

    var hexDump8: [String] { get }

    Return Value

    The String, converted into an Array of uppercase 2-digit hex strings (leading 0s, 1 8-bit character per element). This actually takes the UTF8 value of each character.

  • Declaration

    Swift

    var hexDump16: [String] { get }

    Return Value

    The String, converted into an Array of uppercase 4-digit hex strings (leading 0s, 1 16-bit character per element). This actually takes the UTF16 value of each character.

  • This extension lets us uppercase only the first letter of the string (used for weekdays). From here: https://stackoverflow.com/a/28288340/879365

    Declaration

    Swift

    var firstUppercased: String { get }

    Return Value

    The string, with only the first letter uppercased.

  • The following computed property comes from this: http://stackoverflow.com/a/27736118/879365

    This extension function cleans up a URI string.

    Declaration

    Swift

    var urlEncodedString: String? { get }

    Return Value

    a string, cleaned for URI.

  • The opposite of the above

    This extension function takes a URI-encoded String, and decodes it into a regular String.

    Declaration

    Swift

    var urlDecodedString: String? { get }

    Return Value

    a string, restored from URI encoding.

  • This simply strips out all non-binary characters in the string, leaving only valid binary digits.

    Declaration

    Swift

    var binaryOnly: String { get }
  • This simply strips out all non-octal characters in the string, leaving only valid octal digits.

    Declaration

    Swift

    var octalOnly: String { get }
  • This simply strips out all non-decimal characters in the string, leaving only valid decimal digits.

    Declaration

    Swift

    var decimalOnly: String { get }
  • This simply strips out all non-hex characters in the string, leaving only valid, uppercased (forced) hex digits.

    Declaration

    Swift

    var hexOnly: String { get }
  • A fairly blunt-instrument hex digit pair-to-ASCII character converter. It isn’t particularly intelligent.

    For example: “0x30313233” or “30 31 32 33” or “#30-31-32-33-H” or “30The 3132Quick Brown Fox Jumps Over the Lazy Doge33” all turn into “0123”.

    Declaration

    Swift

    var hex2UTF8: String! { get }

    Return Value

    The String, assumed to be hex numbers (two characters, 0-F, separated by non-hex characters, or not separated); converted to an ASCII string (no spaces). Nil, if the string failed to yeild a UTF8 value. This requires two characters (0-9a-fA-F), side-by-side for each character. They may or may not be separated. Single characters are ignored.

  • Another fairly brute-force simple parser. This computed property will return an Int, extracted from the String, if the String is a Hex number. It will return nil, if the number cannot be extracted. For example, “20” would return 32, “F100” will return 61696, and “3” will return 3. “G” would return nil, but “George” would return 238 (“EE”).

    Declaration

    Swift

    var hex2Int: Int! { get }

Available where Index == String.Index

  • This allows us to find the first index of a substring.

    Declaration

    Swift

    func index(of inString: Self, options inOptions: String.CompareOptions = []) -> Index?

    Parameters

    of

    The substring we’re looking for.

    options

    The String options for the search.

    Return Value

    The index of the first occurrence. Nil, if does not occur.

  • This allows us to find the last index of a substring.

    Declaration

    Swift

    func endIndex(of inString: Self, options inOptions: String.CompareOptions = []) -> Index?

    Parameters

    of

    The substring we’re looking for.

    options

    The String options for the search.

    Return Value

    The index of the last occurrence. Nil, if does not occur.

  • This returns an Array of indexes that map all the occurrences of a given substring.

    Declaration

    Swift

    func indexes(of inString: Self, options inOptions: String.CompareOptions = []) -> [Index]

    Parameters

    of

    The substring we’re looking for.

    options

    The String options for the search.

    Return Value

    an Array, containing the indexes of each occurrence. Empty Array, if does not occur.

  • This returns an Array of Index Ranges that map all the occurrences of a given substring.

    Declaration

    Swift

    func ranges(of inString: Self, options inOptions: String.CompareOptions = []) -> [Range<Index>]

    Parameters

    of

    The substring we’re looking for.

    options

    The String options for the search.

    Return Value

    an Array, containing the Ranges that map each occurrence. Empty Array, if does not occur.