Skip to content

Instantly share code, notes, and snippets.

@evgeniyd
Last active November 29, 2016 09:56
Show Gist options
  • Save evgeniyd/c77b6f59dc2a6624fe7a557c26e1e6b0 to your computer and use it in GitHub Desktop.
Save evgeniyd/c77b6f59dc2a6624fe7a557c26e1e6b0 to your computer and use it in GitHub Desktop.
contains(_:) variant to work with Optional<UnicodeScalar>
// Swift 3
extension CharacterSet {
/// Test for membership of a particular `UnicodeScalar?` in the `CharacterSet`.
/// - important: This `contains(_:)` oveload works with `Optional<UnicodeScalar>`
///
/// Consider the following example:
/// ````
/// let flowermoji = "💐"
/// let ucscalar = UnicodeScalar(flowermoji.utf16.last!)
/// // ucscalar == nil
///
/// if CharacterSet.whitespaces.contains(ucscalar) {
/// print("contatins")
/// }
/// else {
/// print("does not contain")
/// }
/// // does not contain
/// ````
/// - returns: `false` when `member==.none`, otherwise it works as an original `contains(_: UnicodeScalar)`
/// - parameter member: optional unicode scalar which membership should be tested
public func contains(_ member: UnicodeScalar?) -> Bool {
guard let unicodeScalar: UnicodeScalar = member else {
return false
}
return self.contains(unicodeScalar)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment