A high-performance, @safe mutable string builder class for the D programming language. Modeled directly after .NET's System.Text.StringBuilder, it provides efficient methods for appending, inserting, removing, and replacing strings, with support for numeric types, interpolated expressions, and fluent chaining.
The purpose of this project is to experiment with various text handling issues that will be encountered during the Phobos 3 development process.
Add phobos.text.stringbuilder as a dependency in your dub.sdl file:
dependency "phobos.text.stringbuilder" version="~>0.2.0"
Or for dub.json:
"dependencies": {
"phobos.text.stringbuilder": "~>0.2.0"
}You can also add it via the command line:
dub add phobos.text.stringbuilder- .NET API Compatibility: Includes equivalents to
Append,AppendLine,AppendFormat,AppendJoin,Insert,Remove,Replace,Clear, andCopyTo. - Property Management: Controls for
Capacity,MaxCapacity, andLength(which pads with\0when expanded or truncates when shortened). - Fluent Interface: Mutating methods return
thisto allow method chaining. - Interpolated Strings: Natively supports D's
i"..."interpolated expression syntax (sb.append(i"Hello $(name)")). - Value Types: Automatic stringification for string types, characters, booleans, floating point, and integers without intermediate allocations where possible.
- UTF-8 Native: Internal storage uses an amortized-growth UTF-8
char[]buffer. - Safe & Pure: Heavily annotated with
@safe,pure,nothrow(and@nogcwhere no allocations occur).
import phobos.text.stringbuilder;
auto sb = new StringBuilder();
sb.append("Hello")
.append(" ")
.append("World");
assert(sb.toString() == "Hello World");auto sb = new StringBuilder();
string name = "Ada";
int age = 36;
sb.appendLine(i"$(name) is $(age) years old");
assert(sb.toString() == "Ada is 36 years old\n");auto sb = new StringBuilder("0000");
// Insert
sb.insert(2, 42);
assert(sb.toString() == "004200");
// Replace
sb.replace('0', '-');
assert(sb.toString() == "--42--");
// Remove
sb.remove(0, 2);
assert(sb.toString() == "42--");auto sb = new StringBuilder();
sb.appendFormat("GHI{0}{1}", 'J', 'k');
assert(sb.toString() == "GHIJk");
sb.clear();
sb.appendJoin(", ", 1, 2, 3);
assert(sb.toString() == "1, 2, 3");Contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.
LLM-Assisted Contributions:
We welcome code generated by Large Language Models (LLMs) such as GitHub Copilot, ChatGPT, or Claude. However, any LLM-based contributions must include the exact prompt(s) used to generate the contribution. Please append your prompts to the PROMPTS.txt file in the root of the repository as part of your pull request.
This project is licensed under the BSL-1.0 License. See the LICENSE file for details.