Skip to content

Instantly share code, notes, and snippets.

@erica

erica/match.swift

Last active Apr 20, 2016
Embed
What would you like to do?
#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