Skip to content

Instantly share code, notes, and snippets.

@debreuil
Created July 7, 2014 06:47
Show Gist options
  • Save debreuil/ae4a31d2797859b6148f to your computer and use it in GitHub Desktop.
Save debreuil/ae4a31d2797859b6148f to your computer and use it in GitHub Desktop.
extension String {
func getUnicodeIndex()->Int {
return Int(self.unicodeScalars[self.unicodeScalars.startIndex].value)
}
static func getUnicodeRangeFrom(from:Int, to:Int, closedRange:Bool)->String {
let step = from > to ? -1 : 1;
let final = closedRange ? to + step : to
let chars = Int[]((from..final).by(step)).map{String(UnicodeScalar($0))}
return "".join(chars)
}
}
@infix func .. (left: String, right: String) -> String {
return String.getUnicodeRangeFrom(
left.getUnicodeIndex(),
to:right.getUnicodeIndex(),
closedRange:false)
}
@infix func ... (left: String, right: String) -> String {
return String.getUnicodeRangeFrom(
left.getUnicodeIndex(),
to:right.getUnicodeIndex(),
closedRange:true)
}
let s1 = "三".."丏" // "三上下丌不与"
let s2 = "z"..."j" // "zyxwvutsrqponmlkj"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment