mirror of
https://github.com/samsonjs/SwiftBatteries.git
synced 2026-03-25 09:25:51 +00:00
add more extensions
This commit is contained in:
parent
70a0bf9ec0
commit
641977704c
4 changed files with 106 additions and 0 deletions
70
Sources/CollectionType.swift
Normal file
70
Sources/CollectionType.swift
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import Foundation
|
||||
|
||||
#if os(Linux)
|
||||
import Glibc
|
||||
#else
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
public extension CollectionType {
|
||||
|
||||
/// Returns elements of the collection that match the given regular expression.
|
||||
/// If elements are String or NSString instances they will be matched directly.
|
||||
/// if they are NSObject instances their -description is matched. Otherwise
|
||||
/// they are not matched.
|
||||
func grep(regex: String) throws -> [Self.Generator.Element] {
|
||||
let re = try NSRegularExpression(pattern: regex, options: NSRegularExpressionOptions(rawValue: 0))
|
||||
return filter({ item -> Bool in
|
||||
if let string = item as? String {
|
||||
return re.matches(string)
|
||||
}
|
||||
if let string = item as? NSString {
|
||||
return re.matches(String(string))
|
||||
}
|
||||
if let object = item as? NSObject {
|
||||
return re.matches(object.description)
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// Returns a random integer between 1 and max
|
||||
func randomInt(max: Int) -> Int {
|
||||
#if os(Linux)
|
||||
let n = random() % max
|
||||
#else
|
||||
let n = arc4random_uniform(UInt32(max))
|
||||
#endif
|
||||
return 1 + Int(n)
|
||||
}
|
||||
|
||||
public extension CollectionType where Self.Index: Strideable {
|
||||
|
||||
/// Returns a random element, or nil if the collection is empty
|
||||
func sample() -> Generator.Element? {
|
||||
guard count > 0 else {
|
||||
return nil
|
||||
}
|
||||
let max = Int(count.toIntMax())
|
||||
let n = randomInt(max) - 1
|
||||
return self[startIndex.advancedBy(n)]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public extension CollectionType where Self.Generator.Element: Equatable {
|
||||
|
||||
/// Returns the unique elements of a sorted collection by collapsing runs of
|
||||
/// identical elements.
|
||||
func unique() -> [Self.Generator.Element] {
|
||||
var last: Self.Generator.Element?
|
||||
return filter({ item -> Bool in
|
||||
let isUnique: Bool = last == nil || last != item
|
||||
last = item
|
||||
return isUnique
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
2
Sources/Files.swift
Normal file
2
Sources/Files.swift
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
import Foundation
|
||||
|
||||
14
Sources/NSRegularExpression.swift
Normal file
14
Sources/NSRegularExpression.swift
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import Foundation
|
||||
|
||||
public extension NSRegularExpression {
|
||||
|
||||
func match(string: String) -> [NSTextCheckingResult] {
|
||||
let range = NSRange(location: 0, length: string.characters.count)
|
||||
return self.matchesInString(string, options: NSMatchingOptions(rawValue: 0), range: range)
|
||||
}
|
||||
|
||||
func matches(string: String) -> Bool {
|
||||
return self.match(string).count > 0
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -2,10 +2,30 @@ import Foundation
|
|||
|
||||
public extension String {
|
||||
|
||||
// Check if this string contains the given string.
|
||||
func contains(substring: String) -> Bool {
|
||||
return rangeOfString(substring) != nil
|
||||
}
|
||||
|
||||
// Check if this string matches the given regular expression.
|
||||
func matches(regex: String) -> Bool {
|
||||
return rangeOfString(regex, options:.RegularExpressionSearch) != nil
|
||||
}
|
||||
|
||||
// Match the given regular expression against this string and return all
|
||||
// the results.
|
||||
func match(regex: String) throws -> [NSTextCheckingResult] {
|
||||
let re = try NSRegularExpression(pattern: regex, options: NSRegularExpressionOptions(rawValue: 0))
|
||||
return re.match(self)
|
||||
}
|
||||
|
||||
func substringWithNSRange(range: NSRange) -> String {
|
||||
let start = startIndex.advancedBy(range.location)
|
||||
let end = start.advancedBy(range.length)
|
||||
return substringWithRange(Range(start:start, end:end))
|
||||
}
|
||||
|
||||
// Trim whitespace and newlines from the beginning and end of this string.
|
||||
func trim() -> String {
|
||||
return stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue