Forked from kristopherjohnson/StringBuilder.swift
Last active
August 29, 2015 14:13
-
-
Save felipeg48/c673a4c4a5d526568297 to your computer and use it in GitHub Desktop.
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
/** | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment