StringProtocol

public extension StringProtocol

NOTE: These extensions feature some use of Apple’s built-in CommonCrypto utility.

  • NOTE: Because of issues with bundles and resources, and whatnot, this will not be tested with the unit tests.

    Declaration

    Swift

    var localizedVariant: String { get }

    Return Value

    the localized string (main bundle) for this string, trying each of the specialized localizations.

  • NOTE: Because of issues with bundles and resources, and whatnot, this will not be tested with the unit tests.

    Declaration

    Swift

    var accessibilityLocalizedVariant: String { get }

    Return Value

    the localized string (main bundle) for this string, from an Accessibility.strings file.

  • NOTE: Because of issues with bundles and resources, and whatnot, this will not be tested with the unit tests.

    Declaration

    Swift

    var errorsLocalizedVariant: String { get }

    Return Value

    the localized string (main bundle) for this string, from an Errors.strings file.

  • 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.

  • This comes directly from here: https://stackoverflow.com/a/25471164/879365 Evaluate a string for proper email address form.

    Declaration

    Swift

    var isAValidEmailAddress: Bool { get }

    Return Value

    True, if the email address is in valid form.

  • 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 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 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.

  • 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.

StringProtocol Extension (Foundation-Required Functions)

  • This allows us to split a String, if one or more members of a CharacterSet are present.

    Declaration

    Swift

    func setSplit(_ inCharacterset: CharacterSet) -> [Self.SubSequence]

    Parameters

    inCharacterset

    A CharacterSet, containing all of the possible characters for a split. NOTE: If you want to mix high Unicode characters with ASCII characters in the input set, use the CharacterSet(String.unicodeScalars) constructor. The CharacterSet(charactersIn: String) constructor is buggy.

    Return Value

    An Array of Substrings. The result of the split.

  • This allows us to split a String, if one or more character members of a String are present.

    Declaration

    Swift

    func setSplit(charactersIn inString: String) -> [Self.SubSequence]

    Parameters

    charactersIn

    A String, containing all of the possible characters for a split.

    Return Value

    An Array of Substrings. The result of the split.

StringProtocol Extension (Computed Properties)

  • 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 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.

  • 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.

  • 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 }
  • This “scrubs” a String, returning it as a proper UUID format (either 4 hex characters, or a split 32-hex-character String, in 8-4-4-4-12 format.

    Declaration

    Swift

    var uuidFormat: String? { get }

    Return Value

    A traditional UUID format (may be XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX, or only 4 hex characters), or nil.