Skip to content

Commit 0a681ca

Browse files
authored
Merge pull request #55 from Ryoga-exe/zig-0.15.1
Update for Zig 0.15.1
2 parents f6f9e5d + 50eb981 commit 0a681ca

File tree

5 files changed

+87
-64
lines changed

5 files changed

+87
-64
lines changed

.github/workflows/main.yml

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@ jobs:
1212
build:
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@v4
15+
- name: Checkout
16+
uses: actions/checkout@v4
1617

17-
- name: Download zig
18-
run: wget https://ziglang.org/builds/zig-linux-x86_64-0.15.0-dev.75+03123916e.tar.xz
19-
20-
- name: Extract
21-
run: tar -xf zig-linux-x86_64-0.15.0-dev.75+03123916e.tar.xz
22-
23-
- name: Alias
24-
run: alias zig=$PWD/zig-linux-x86_64-0.15.0-dev.75+03123916e/zig
25-
26-
- name: Version
27-
run: $PWD/zig-linux-x86_64-0.15.0-dev.75+03123916e/zig version
18+
- name: Setup Zig
19+
uses: mlugg/setup-zig@v2
20+
with:
21+
version: 0.15.1
2822

2923
- name: Test
30-
run: $PWD/zig-linux-x86_64-0.15.0-dev.75+03123916e/zig build test
24+
run: zig build test

build.zig

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@ pub fn build(b: *std.Build) void {
44
const target = b.standardTargetOptions(.{});
55
const optimize = b.standardOptimizeOption(.{});
66

7-
_ = b.addModule("string", .{ .root_source_file = b.path("zig-string.zig") });
7+
_ = b.addModule("string", .{
8+
.root_source_file = b.path("zig-string.zig"),
9+
.target = target,
10+
.optimize = optimize,
11+
});
812

9-
var main_tests = b.addTest(.{
13+
const test_mod = b.addModule("string-tests", .{
1014
.root_source_file = b.path("zig-string-tests.zig"),
1115
.target = target,
1216
.optimize = optimize,
1317
});
1418

19+
const main_tests = b.addTest(.{
20+
.root_module = test_mod,
21+
});
22+
23+
const run_main_tests = b.addRunArtifact(main_tests);
1524
const test_step = b.step("test", "Run library tests");
16-
test_step.dependOn(&main_tests.step);
25+
test_step.dependOn(&run_main_tests.step);
1726
}

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.{
22
.name = .zig_string,
33
.version = "0.10.0",
4-
.minimum_zig_version = "0.14.0",
4+
.minimum_zig_version = "0.15.1",
55
.fingerprint = 0xd2ee692e5a4bdae9,
66
.paths = .{""},
77
}

zig-string-tests.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,11 @@ test "String Tests" {
184184
try expectEqual(myStr.size, 0);
185185

186186
// writer
187-
const writer = myStr.writer();
188-
const length = try writer.write("This is a Test!");
187+
var writer = myStr.writer(&.{});
188+
const w = &writer.interface;
189+
const length = try w.write("This is a Test!");
189190
try expectEqual(length, 15);
191+
try w.flush();
190192

191193
// owned
192194
const mySlice = try myStr.toOwned();
@@ -300,4 +302,4 @@ test "includes Tests" {
300302

301303
try expect(myString.includesLiteral("") == false);
302304
try expect(myString.includesString(needle) == false);
303-
}
305+
}

zig-string.zig

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,16 @@ pub const String = struct {
359359

360360
/// Splits the String into slices, based on a delimiter.
361361
pub fn splitAll(self: *const String, delimiters: []const u8) ![][]const u8 {
362-
var splitArr = std.ArrayList([]const u8).init(std.heap.page_allocator);
363-
defer splitArr.deinit();
362+
const allocator = std.heap.page_allocator;
363+
var splitArr: std.ArrayList([]const u8) = .empty;
364+
errdefer splitArr.deinit(allocator);
364365

365366
var i: usize = 0;
366367
while (self.split(delimiters, i)) |slice| : (i += 1) {
367-
try splitArr.append(slice);
368+
try splitArr.append(allocator, slice);
368369
}
369370

370-
return try splitArr.toOwnedSlice();
371+
return try splitArr.toOwnedSlice(allocator);
371372
}
372373

373374
/// Splits the String into a new string, based on delimiters and an index
@@ -385,22 +386,20 @@ pub const String = struct {
385386
/// Splits the String into a slice of new Strings, based on delimiters.
386387
/// The user of this function is in charge of the memory of the new Strings.
387388
pub fn splitAllToStrings(self: *const String, delimiters: []const u8) ![]String {
388-
var splitArr = std.ArrayList(String).init(std.heap.page_allocator);
389-
defer splitArr.deinit();
389+
const allocator = std.heap.page_allocator;
390+
var splitArr: std.ArrayList(String) = .empty;
391+
errdefer splitArr.deinit(allocator);
390392

391393
var i: usize = 0;
392394
while (try self.splitToString(delimiters, i)) |splitStr| : (i += 1) {
393-
try splitArr.append(splitStr);
395+
try splitArr.append(allocator, splitStr);
394396
}
395397

396-
return try splitArr.toOwnedSlice();
398+
return try splitArr.toOwnedSlice(allocator);
397399
}
398400

399401
/// Splits the String into a slice of Strings by new line (\r\n or \n).
400402
pub fn lines(self: *String) ![]String {
401-
var lineArr = std.ArrayList(String).init(std.heap.page_allocator);
402-
defer lineArr.deinit();
403-
404403
var selfClone = try self.clone();
405404
defer selfClone.deinit();
406405

@@ -486,45 +485,66 @@ pub const String = struct {
486485
}
487486

488487
// Writer functionality for the String.
489-
pub usingnamespace struct {
490-
pub const Writer = std.io.Writer(*String, Error, appendWrite);
491-
492-
pub fn writer(self: *String) Writer {
493-
return .{ .context = self };
494-
}
495-
496-
fn appendWrite(self: *String, m: []const u8) !usize {
497-
try self.concat(m);
498-
return m.len;
488+
// pub const Writer = std.io.Writer(*String, Error, appendWrite);
489+
pub const Writer = struct {
490+
string: *String,
491+
interface: std.Io.Writer,
492+
err: ?Error = null,
493+
494+
fn drain(w: *std.Io.Writer, data: []const []const u8, splat: usize) std.Io.Writer.Error!usize {
495+
_ = splat;
496+
const a: *@This() = @alignCast(@fieldParentPtr("interface", w));
497+
const buffered = w.buffered();
498+
if (buffered.len != 0) return w.consume(a.string.appendWrite(buffered) catch |err| {
499+
a.err = err;
500+
return error.WriteFailed;
501+
});
502+
return a.string.appendWrite(data[0]) catch |err| {
503+
a.err = err;
504+
return error.WriteFailed;
505+
};
499506
}
500507
};
501508

502-
// Iterator support
503-
pub usingnamespace struct {
504-
pub const StringIterator = struct {
505-
string: *const String,
506-
index: usize,
507-
508-
pub fn next(it: *StringIterator) ?[]const u8 {
509-
if (it.string.buffer) |buffer| {
510-
if (it.index == it.string.size) return null;
511-
const i = it.index;
512-
it.index += String.getUTF8Size(buffer[i]);
513-
return buffer[i..it.index];
514-
} else {
515-
return null;
516-
}
517-
}
509+
pub fn writer(self: *String, buffer: []u8) Writer {
510+
return .{
511+
.string = self,
512+
.interface = .{
513+
.buffer = buffer,
514+
.vtable = &.{ .drain = Writer.drain },
515+
},
518516
};
517+
}
519518

520-
pub fn iterator(self: *const String) StringIterator {
521-
return StringIterator{
522-
.string = self,
523-
.index = 0,
524-
};
519+
fn appendWrite(self: *String, m: []const u8) !usize {
520+
try self.concat(m);
521+
return m.len;
522+
}
523+
524+
// Iterator support
525+
pub const StringIterator = struct {
526+
string: *const String,
527+
index: usize,
528+
529+
pub fn next(it: *StringIterator) ?[]const u8 {
530+
if (it.string.buffer) |buffer| {
531+
if (it.index == it.string.size) return null;
532+
const i = it.index;
533+
it.index += String.getUTF8Size(buffer[i]);
534+
return buffer[i..it.index];
535+
} else {
536+
return null;
537+
}
525538
}
526539
};
527540

541+
pub fn iterator(self: *const String) StringIterator {
542+
return StringIterator{
543+
.string = self,
544+
.index = 0,
545+
};
546+
}
547+
528548
/// Returns whether or not a character is whitelisted
529549
fn inWhitelist(char: u8, whitelist: []const u8) bool {
530550
var i: usize = 0;
@@ -609,7 +629,6 @@ pub const String = struct {
609629

610630
/// Checks if the needle String is within the source String
611631
pub fn includesString(self: *String, needle: String) bool {
612-
613632
if (self.size == 0 or needle.size == 0) return false;
614633

615634
if (self.buffer) |buffer| {
@@ -627,7 +646,6 @@ pub const String = struct {
627646

628647
/// Checks if the needle literal is within the source String
629648
pub fn includesLiteral(self: *String, needle: []const u8) bool {
630-
631649
if (self.size == 0 or needle.len == 0) return false;
632650

633651
if (self.buffer) |buffer| {

0 commit comments

Comments
 (0)