Last active
April 20, 2016 19:56
-
-
Save erica/0628234c1214948dc293cb37530f1ec7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if os(Linux) | |
import Glibc | |
#else | |
import Darwin | |
#endif | |
// Thank you Mike Ash | |
extension String { | |
func contains(query: String, caseSensitive: Bool = false) -> Bool { | |
if query.isEmpty { return true } | |
let (s, q) = caseSensitive ? (self, query) : (self.lowercaseString, query.lowercaseString) | |
var chars = s.characters; let qchars = q.characters | |
while !chars.isEmpty { | |
if chars.startsWith(qchars) { return true } | |
chars.removeFirst() | |
} | |
return false | |
} | |
func prefixedBy(query: String, caseSensitive: Bool = false) -> Bool { | |
let (s, q) = caseSensitive ? (self, query) : (self.lowercaseString, query.lowercaseString) | |
return s.characters.startsWith(q.characters) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment