Skip to content

Commit

Permalink
Fixed split edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubSzark committed Jan 26, 2021
1 parent 01e030f commit 60ef4e1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 10 additions & 1 deletion zig-string-tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,17 @@ test "String Tests" {
assert(eql(u8, myStr.split("💯", 1).?, "Hello"));
assert(eql(u8, myStr.split("💯", 2).?, ""));
assert(eql(u8, myStr.split("💯", 3).?, "Hello"));
assert(eql(u8, myStr.split("💯", 5).?, "Hello"));
assert(eql(u8, myStr.split("💯", 6).?, ""));

// toLowercasr & toUppercase
var splitStr = String.init(&arena.allocator);
defer splitStr.deinit();

try splitStr.concat("variable='value'");
assert(eql(u8, splitStr.split("=", 0).?, "variable"));
assert(eql(u8, splitStr.split("=", 1).?, "'value'"));

// toLowercase & toUppercase
myStr.toUppercase();
assert(myStr.cmp("💯HELLO💯💯HELLO💯💯HELLO💯"));
myStr.toLowercase();
Expand Down
6 changes: 5 additions & 1 deletion zig-string.zig
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ pub const String = struct {
return self.size == 0;
}

/// Splits the String into a slice based on a delimiter and an index
/// Splits the String into a slice, based on a delimiter and an index
pub fn split(self: String, delimiter: []const u8, index: usize) ?[]const u8 {
if (self.buffer) |buffer| {
var i: usize = 0;
Expand All @@ -307,6 +307,10 @@ pub const String = struct {

i += size;
}

if (i >= self.size - 1 and block == index) {
return buffer[start..self.size];
}
}

return null;
Expand Down

0 comments on commit 60ef4e1

Please sign in to comment.