Last active
December 29, 2015 02:05
-
-
Save erica/329dad5fbd5872d874f8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
internal extension CollectionType where Index: BidirectionalIndexType { | |
internal func _lastIndexOf(@noescape isElement: Generator.Element -> Bool) -> Index? { | |
for i in indices.reverse() | |
where isElement(self[i]) { | |
return i | |
} | |
return nil | |
} | |
} | |
internal extension CollectionType where Index: BidirectionalIndexType, Generator.Element: Equatable { | |
internal func _lastIndexOf(element: Generator.Element) -> Index? { | |
return _lastIndexOf { e in e == element } | |
} | |
} | |
public extension String { | |
/// Retake on "lastPathComponent" and "pathExtension" | |
/// but a little more general in behavior | |
/// | |
/// - Authors: aciidb, erica, oisdk, with space-assist from jweinberg | |
public func suffixFrom( | |
boundary: Character, | |
searchingBackwards backwards: Bool = true, | |
includingBoundary include: Bool = false) -> String { | |
guard let i = backwards ? | |
characters._lastIndexOf(boundary) : | |
characters.indexOf(boundary) | |
else { return self } | |
return String(characters.suffixFrom(include ? i : i.successor())) | |
} | |
/// Alternative to lastPathComponent, pathExtension | |
/// but taking prefix instead of suffix | |
/// | |
/// - Authors: aciidb, erica, oisdk, with space-assist from jweinberg | |
public func prefixTo( | |
boundary: Character, | |
searchingForwards forwards: Bool = true, | |
includingBoundary include: Bool = false) -> String { | |
guard let i = forwards ? | |
characters.indexOf(boundary) : | |
characters._lastIndexOf(boundary) | |
else { return self } | |
return String(include ? characters.prefixThrough(i) : characters.prefixUpTo(i)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment