Git Product home page Git Product logo

zig-string's Introduction

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 String = @import("./zig-string.zig").String;
// ...

// Use your favorite allocator
var arena = 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!
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.zon

    const string = b.dependency("string", .{
        .target = target,
        .optimize = optimize,
    });
    exe.addModule("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 complexity please open an issue (I'm no expert when it comes to complexity)

Function Description
allocate Sets the internal buffer size
capacity Returns the capacity of the String
charAt Returns character at index
clear Clears the contents of the String
clone Copies this string to a new one
cmp Compares to string literal
concat Appends a string literal to the end
deinit De-allocates the String
find Finds first string literal appearance
rfind Finds last string literal appearance
init Creates a String with an Allocator
init_with_contents Creates a String with specified contents
insert Inserts a character at an index
isEmpty Checks if length is zero
iterator Returns a StringIterator over the String
len Returns count of characters stored
pop Removes the last character
remove Removes a character at an index
removeRange Removes a range of characters
repeat Repeats string n times
reverse Reverses all the characters
split Returns a slice based on delimiters
splitToString Returns a String based on delimiters
str Returns the String as a slice
substr Creates a string from a range
toLowercase Converts (ASCII) characters to lowercase
toOwned Creates an owned slice of the String
toUppercase Converts (ASCII) characters to uppercase
trim Removes whitelist from both ends
trimEnd Remove whitelist from the end
trimStart Remove whitelist from the start
truncate Realloc to the length
setStr Set's buffer value from string literal
writer Returns a std.io.Writer for the String
startsWith Determines if the given string begins with the given value
endsWith Determines if the given string ends with the given value
replace Replace all occurrences of the search string with the replacement string

zig-string's People

Contributors

arshidkv12 avatar beyley avatar deanoc avatar deevus avatar jakubszark avatar sytranvn avatar thechampagne avatar visendev avatar xfxpositions avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

zig-string's Issues

Replace `std.mem.copy` with `std.mem.copyForwards`

A recent PR for the Zig standard library removes the std.mem.copy function, which was an alias for std.mem.copyForwards. This function was used in toOwned().

A proposed fix:

pub fn toOwned(self: String) Error!?[]u8 {
    if (self.buffer != null) {
        const string = self.str();
        if (self.allocator.alloc(u8, string.len)) |newStr| {
            std.mem.copyForwards(u8, newStr, string); // replace std.mem.copy with std.mem.copyForwards
            return newStr;
        } else |_| {
            return Error.OutOfMemory;
        }
    }

    return null;
}

split and splitToString

Do you have an example of using the split and splitToString methods to get an array containing all the resulting slices or Strings instead of getting them one at a time and having to manage the index?

Here's an example I created where I have to call the method repeatedly to get the pieces:

    // TODO: Why must this be var and not const?
    var colors = try String.init_with_contents(allocator, "red,green,blue");
    defer colors.deinit();
    // Splits into []u8 slices.
    if (colors.split(",", 0)) |c1| {
        assert(std.mem.eql(u8, c1, "red"));
        if (colors.split(",", 4)) |c2| {
            assert(std.mem.eql(u8, c2, "green"));
        }
    }

`build.zig.zon` example broken

By referencing master the archive will change every time new commits are pushed to master in this repo meaning that if someone added that to their build.zig.zon file the dependency (and their corresponding hash) would become out of sync. I'd recommend the example showing how to pin to a specific sha like this:

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.