Skip to content

ellipticbit/phobos.text.stringbuilder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StringBuilder for D

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.

Installation via DUB

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

Features & Capabilities

  • .NET API Compatibility: Includes equivalents to Append, AppendLine, AppendFormat, AppendJoin, Insert, Remove, Replace, Clear, and CopyTo.
  • Property Management: Controls for Capacity, MaxCapacity, and Length (which pads with \0 when expanded or truncates when shortened).
  • Fluent Interface: Mutating methods return this to 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 @nogc where no allocations occur).

Usage Examples

Basic Usage and Chaining

import phobos.text.stringbuilder;

auto sb = new StringBuilder();
sb.append("Hello")
  .append(" ")
  .append("World");

assert(sb.toString() == "Hello World");

Interpolation and AppendLine

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");

Insert, Remove, and Replace

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--");

AppendFormat and AppendJoin

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");

Contributing

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.

License

This project is licensed under the BSL-1.0 License. See the LICENSE file for details.

About

A @safe mutable string builder class for the D programming language, modeled after .NET's System.Text.StringBuilder.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages