Home

Awesome

Zig String (A UTF-8 String Library)

CI Github Repo Issues GitHub Repo stars

This library is a UTF-8 compatible string library for the Zig programming language. I made this for the sole purpose to further my experience and understanding of zig. Also it may be useful for some people who need it (including myself), with future projects. Project is also open for people to add to and improve. Please check the issues to view requested features.

Basic Usage

const std = @import("std");
const String = @import("./zig-string.zig").String;
// ...

// Use your favorite allocator
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();

// Create your String
var myString = String.init(arena.allocator());
defer myString.deinit();

// Use functions provided
try myString.concat("🔥 Hello!");
_ = myString.pop();
try myString.concat(", World 🔥");

// Success!
std.debug.assert(myString.cmp("🔥 Hello, World 🔥"));

Installation

Add this to your build.zig.zon

.dependencies = .{
    .string = .{
        .url = "https://github.com/JakubSzark/zig-string/archive/refs/heads/master.tar.gz",
        //the correct hash will be suggested by zig
    }
}

And add this to you build.zig

    const string = b.dependency("string", .{
        .target = target,
        .optimize = optimize,
    });
    exe.root_module.addImport("string", string.module("string"));

You can then import the library into your code like this

const String = @import("string").String;

How to Contribute

  1. Fork
  2. Clone
  3. Add Features (Use Zig FMT)
  4. Make a Test
  5. Pull Request
  6. Success!

Working Features

If there are any issues with <b>complexity</b> please <b>open an issue</b> (I'm no expert when it comes to complexity)

FunctionDescription
allocateSets the internal buffer size
capacityReturns the capacity of the String
charAtReturns character at index
clearClears the contents of the String
cloneCopies this string to a new one
cmpCompares to string literal
concatAppends a string literal to the end
deinitDe-allocates the String
findFinds first string literal appearance
rfindFinds last string literal appearance
includesLiteralWhether or not the provided literal is in the String
includesStringWhether or not the provided String is within the String
initCreates a String with an Allocator
init_with_contentsCreates a String with specified contents
insertInserts a character at an index
isEmptyChecks if length is zero
iteratorReturns a StringIterator over the String
lenReturns count of characters stored
popRemoves the last character
removeRemoves a character at an index
removeRangeRemoves a range of characters
repeatRepeats string n times
reverseReverses all the characters
splitReturns a slice based on delimiters and index
splitAllReturns a slice of slices based on delimiters
splitToStringReturns a String based on delimiters and index
splitAllToStringsReturns a slice of Strings based on delimiters
linesReturns a slice of Strings split by newlines
strReturns the String as a slice
substrCreates a string from a range
toLowercaseConverts (ASCII) characters to lowercase
toOwnedCreates an owned slice of the String
toUppercaseConverts (ASCII) characters to uppercase
toCapitalizedConverts the first (ASCII) character of each word to uppercase
trimRemoves whitelist from both ends
trimEndRemove whitelist from the end
trimStartRemove whitelist from the start
truncateRealloc to the length
setStrSet's buffer value from string literal
writerReturns a std.io.Writer for the String
startsWithDetermines if the given string begins with the given value
endsWithDetermines if the given string ends with the given value
replaceReplace all occurrences of the search string with the replacement string