let fiveTimesHello = 5 * "hello" // "hellohellohellohellohello"
let helloPlus5 = "hello" + 5 // "hello5"
Last active
July 1, 2016 00:18
-
-
Save pauljohanneskraft/5e00f07f5e6dd653cb09e608e2192626 to your computer and use it in GitHub Desktop.
some String manipulations
This file contains 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
func * (left: UInt, right: String) -> String { | |
var res = right | |
res *= left | |
return res | |
} | |
func * (left: String, right: UInt) -> String { | |
var res = left | |
res *= right | |
return res | |
} | |
func *= ( left: inout String, right: UInt) -> String { | |
if right == 0 { | |
left = "" | |
return left | |
} | |
let orig = left | |
if right < 5 { | |
for _ in 1..<right { | |
left += orig | |
} | |
return left | |
} | |
let half = right / 2 | |
let m = left * half | |
left = m + m | |
if (right & 1 == 1) { | |
left += orig | |
} | |
return left | |
} | |
func +<T>(left: String, right: T) -> String { | |
return "\(left)\(right)" | |
} | |
func +<T>(left: T, right: String) -> String { | |
return "\(left)\(right)" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment