Skip to content

Make compile on 0.7.0, Remove string struct, and update older code. #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
41 changes: 26 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@

A String struct made for Zig.
Implements common string operations such as substring searching.

Note: Most of these functions are in std.mem.

Inspired by this repo: https://github.com/clownpriest/strings/

To test:
```
$ cd zig-string/
$ zig test string.zig
```bash
cd zig-string
zig test string.zig
```

Basic Usage:
```zig
const std = @import("std");
const String = @import("/some/path/string.zig").String;
const print = std.debug.print;
const string = @import("string.zig");

pub fn main() !void {
var buf: [1024]u8 = undefined;
var fba = std.heap.ThreadSafeFixedBufferAllocator.init(buf[0..]);
var s = try String.init(&fba.allocator, "hello, world");
defer s.deinit();
var matches = try s.findSubstringIndices(&fba.allocator, "hello");
defer fba.allocator.free(matches);
var buffer: [1024]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buffer);
const allocator = &fba.allocator;

var string_lit = "hello, world";
var matches = try string.findSubstringIndices(allocator, string_lit, "world");
defer allocator.free(matches);

// Should print:
// 0
// hello, world
// true
// 7
print("{}\n", .{ string.contains(allocator, string_lit, "hello") });
for (matches) |val| {
std.debug.warn("{}\n", val);
print("{}\n", .{ val });
}
std.debug.warn("{}\n", s.toSliceConst());
}
```

This example can be run with:
```bash
cd zig-string
zig run readme.zig
```
15 changes: 15 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const Builder = @import("std").build.Builder;

pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const lib = b.addStaticLibrary("string", "string.zig");
lib.setBuildMode(mode);
lib.install();

var string_tests = b.addTest("string.zig");
string_tests.setBuildMode(mode);


const test_step = b.step("test", "Run library tests");
test_step.dependOn(&string_tests.step);
}
21 changes: 21 additions & 0 deletions readme.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const std = @import("std");
const print = std.debug.print;
const string = @import("string.zig");

pub fn main() !void {
var buffer: [1024]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buffer);
const allocator = &fba.allocator;

var string_lit = "hello, world";
var matches = try string.findSubstringIndices(allocator, string_lit, "world");
defer allocator.free(matches);

// Should print:
// true
// 7
print("{}\n", .{ string.contains(allocator, string_lit, "hello") });
for (matches) |val| {
print("{}\n", .{ val });
}
}
Loading