import Foundation
extension String
{
var length: Int {
get {
return countElements(self)
}
}
func contains(s: String) -> Bool
{
return self.rangeOfString(s) ? true : false
}
func replace(target: String, withString: String) -> String
{
return self.stringByReplacingOccurrencesOfString(target, withString: withString, options: NSStringCompareOptions.LiteralSearch, range: nil)
}
subscript (i: Int) -> Character
{
get {
let index = advance(startIndex, i)
return self[index]
}
}
subscript (r: Range<Int>) -> String
{
get {
let startIndex = advance(self.startIndex, r.startIndex)
let endIndex = advance(self.startIndex, r.endIndex - 1)
return self[Range(start: startIndex, end: endIndex)]
}
}
func subString(startIndex: Int, length: Int) -> String
{
var start = advance(self.startIndex, startIndex)
var end = advance(self.startIndex, startIndex + length)
return self.substringWithRange(Range<String.Index>(start: start, end: end))
}
func indexOf(target: String) -> Int
{
var range = self.rangeOfString(target)
if let range = range {
return distance(self.startIndex, range.startIndex)
} else {
return -1
}
}
func indexOf(target: String, startIndex: Int) -> Int
{
var startRange = advance(self.startIndex, startIndex)
var range = self.rangeOfString(target, options: NSStringCompareOptions.LiteralSearch, range: Range<String.Index>(start: startRange, end: self.endIndex))
if let range = range {
return distance(self.startIndex, range.startIndex)
} else {
return -1
}
}
func lastIndexOf(target: String) -> Int
{
var index = -1
var stepIndex = self.indexOf(target)
while stepIndex > -1
{
index = stepIndex
if stepIndex + target.length < self.length {
stepIndex = indexOf(target, startIndex: stepIndex + target.length)
} else {
stepIndex = -1
}
}
return index
}
func isMatch(regex: String, options: NSRegularExpressionOptions) -> Bool
{
var error: NSError?
var exp = NSRegularExpression(pattern: regex, options: options, error: &error)
if let error = error {
println(error.description)
}
var matchCount = exp.numberOfMatchesInString(self, options: nil, range: NSMakeRange(0, self.length))
return matchCount > 0
}
func getMatches(regex: String, options: NSRegularExpressionOptions) -> [NSTextCheckingResult]
{
var error: NSError?
var exp = NSRegularExpression(pattern: regex, options: options, error: &error)
if let error = error {
println(error.description)
}
var matches = exp.matchesInString(self, options: nil, range: NSMakeRange(0, self.length))
return matches as [NSTextCheckingResult]
}
private var vowels: [String]
{
get
{
return ["a", "e", "i", "o", "u"]
}
}
private var consonants: [String]
{
get
{
return ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z"]
}
}
func pluralize(count: Int) -> String
{
if count == 1 {
return self
} else {
var lastChar = self.subString(self.length - 1, length: 1)
var secondToLastChar = self.subString(self.length - 2, length: 1)
var prefix = "", suffix = ""
if lastChar.lowercaseString == "y" && vowels.filter({x in x == secondToLastChar}).count == 0 {
prefix = self[0...self.length - 1]
suffix = "ies"
} else if lastChar.lowercaseString == "s" || (lastChar.lowercaseString == "o" && consonants.filter({x in x == secondToLastChar}).count > 0) {
prefix = self[0...self.length]
suffix = "es"
} else {
prefix = self[0...self.length]
suffix = "s"
}
return prefix + (lastChar != lastChar.uppercaseString ? suffix : suffix.uppercaseString)
}
}
}