Skip to content

Swift package to make building strings easy with result builders

License

Notifications You must be signed in to change notification settings

mattcox/StringBuilder

Repository files navigation

StringBuilder

Swift Swift Package Manager

Welcome to StringBuilder, a Swift package for dynamically building strings using a resultBuilder.

Usage

Basic String Building

To build a basic multi-line string:

String {
    "Line 1"
    "Line 2"
}

Becomes:

"""
Line 1
Line 2
"""

Custom Separator

A custom separator can be used to control how the strings are joined:

String(separator: " != ") {
    "String 1"
    "String 2"
}

Becomes:

"String 1 != String 2"

String Convertible Types

Any type that conforms to CustomStringConvertible can be added to the string builder:

String {
    1234
    true
    UUID()
}

Becomes:

"""
1234
true
3D5E76E1-CB1D-4FEF-A5CA-3C67AA08BF47
"""

Nested String Builders

String builders can be nested with different separators at each level:

String {
    "One"
    
    String(separator: ".") {
        "Two"
        "Three"
    }
    
    "Four"
    
    String {
        String {
            "Five"
            
            String(separator: "\n\n") {
                "Six"
                "Seven"
            }
            
            "Eight"
        }
        
        "Nine"
    }
    
	"Ten"
}

Becomes:

"""
One
Two.Three
Four
Five
Six

Seven
Eight
Nine
Ten
"""

Installation

StringBuilder is distributed using the Swift Package Manager. To install it within another Swift package, add it as a dependency within your Package.swift manifest:

let package = Package(
    // . . .
    dependencies: [
        .package(url: "https://github.com/mattcox/StringBuilder.git", branch: "main")
    ],
    // . . .
)

If you’d like to use StringBuilder within an iOS, macOS, watchOS or tvOS app, then use Xcode’s File > Add Packages... menu command to add it to your project.

Import StringBuilder wherever you’d like to use it:

import StringBuilder

About

Swift package to make building strings easy with result builders

Topics

Resources

License

Stars

Watchers

Forks

Languages