Last active
March 12, 2024 19:07
-
-
Save kristopherjohnson/1fc55e811d944a430289 to your computer and use it in GitHub Desktop.
A StringBuilder class for Swift
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
/** | |
Supports creation of a String from pieces | |
*/ | |
public class StringBuilder { | |
private var stringValue: String | |
/** | |
Construct with initial String contents | |
:param: string Initial value; defaults to empty string | |
*/ | |
public init(string: String = "") { | |
self.stringValue = string | |
} | |
/** | |
Return the String object | |
:return: String | |
*/ | |
public func toString() -> String { | |
return stringValue | |
} | |
/** | |
Return the current length of the String object | |
*/ | |
public var length: Int { | |
return countElements(stringValue) | |
} | |
/** | |
Append a String to the object | |
:param: string String | |
:return: reference to this StringBuilder instance | |
*/ | |
public func append(string: String) -> StringBuilder { | |
stringValue += string | |
return self | |
} | |
/** | |
Append a Printable to the object | |
:param: value a value supporting the Printable protocol | |
:return: reference to this StringBuilder instance | |
*/ | |
public func append<T: Printable>(value: T) -> StringBuilder { | |
stringValue += value.description | |
return self | |
} | |
/** | |
Append a String and a newline to the object | |
:param: string String | |
:return: reference to this StringBuilder instance | |
*/ | |
public func appendLine(string: String) -> StringBuilder { | |
stringValue += string + "\n" | |
return self | |
} | |
/** | |
Append a Printable and a newline to the object | |
:param: value a value supporting the Printable protocol | |
:return: reference to this StringBuilder instance | |
*/ | |
public func appendLine<T: Printable>(value: T) -> StringBuilder { | |
stringValue += value.description + "\n" | |
return self | |
} | |
/** | |
Reset the object to an empty string | |
:return: reference to this StringBuilder instance | |
*/ | |
public func clear() -> StringBuilder { | |
stringValue = "" | |
return self | |
} | |
} | |
/** | |
Append a String to a StringBuilder using operator syntax | |
:param: lhs StringBuilder | |
:param: rhs String | |
*/ | |
public func += (lhs: StringBuilder, rhs: String) { | |
lhs.append(rhs) | |
} | |
/** | |
Append a Printable to a StringBuilder using operator syntax | |
:param: lhs Printable | |
:param: rhs String | |
*/ | |
public func += <T: Printable>(lhs: StringBuilder, rhs: T) { | |
lhs.append(rhs.description) | |
} | |
/** | |
Create a StringBuilder by concatenating the values of two StringBuilders | |
:param: lhs first StringBuilder | |
:param: rhs second StringBuilder | |
:result StringBuilder | |
*/ | |
public func +(lhs: StringBuilder, rhs: StringBuilder) -> StringBuilder { | |
return StringBuilder(string: lhs.toString() + rhs.toString()) | |
} | |
// Examples | |
let sb = StringBuilder() | |
sb.append(1).append("+").append(1).append("=").append(2) | |
sb.toString() | |
let sb2 = StringBuilder() | |
sb2 += "2+2" | |
sb2 += "=" | |
sb2 += "4" | |
sb2.toString() | |
let sbAdd = sb + sb2 | |
sbAdd.toString() |
@GuyKahlon a bit late here, but I discovered that Swift's String class uses an exponential growth strategy that makes appending to a string a constant time operation when averaged over many appends. See here for the source. So this means that there's no need for a StringBuilder class in Swift as using += on a swift String is already doing the performant thing that Java's StringBuilder class is doing.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
…That said, I just encountered the first case where I needed a reference object to which I can send
append
messages.