Skip to content

Commit

Permalink
feat: return slice of buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanaasagi committed Jun 6, 2023
1 parent 05a4b03 commit ee00d06
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub fn main() !void {
// Using the buffer
var buffer: [1024]u8 = undefined;
const len = try deunicode(&buffer, "おはよう");
std.debug.print("{s}\n", .{buffer[0..len]}); // ohayou
const res2 = try deunicode(&buffer, "おはよう");
std.debug.print("{s}\n", .{res2}); // ohayou
}
```

Expand Down
4 changes: 2 additions & 2 deletions examples/unicode/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ pub fn main() !void {

// Using the buffer
var buffer: [1024]u8 = undefined;
const len = try deunicode(&buffer, "おはよう");
std.debug.print("{s}\n", .{buffer[0..len]});
const res2 = try deunicode(&buffer, "おはよう");
std.debug.print("{s}\n", .{res2});
}
11 changes: 5 additions & 6 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn deunicodeAlloc(allocator: Allocator, s: []const u8) ![]const u8 {
return try deunicodeCustomAlloc(allocator, s, "[?]");
}

pub fn deunicode(out: []u8, s: []const u8) !usize {
pub fn deunicode(out: []u8, s: []const u8) ![]const u8 {
return try deunicodeCustom(out, s, "[?]");
}

Expand Down Expand Up @@ -166,7 +166,7 @@ pub fn deunicodeCustomAlloc(allocator: Allocator, s: []const u8, custom_placehol
return out.toOwnedSlice();
}

pub fn deunicodeCustom(out: []u8, s: []const u8, custom_placeholder: []const u8) !usize {
pub fn deunicodeCustom(out: []u8, s: []const u8, custom_placeholder: []const u8) ![]const u8 {
var cursor: usize = 0;

// Fast path to skip over ASCII chars at the beginning of the string
Expand All @@ -183,7 +183,7 @@ pub fn deunicodeCustom(out: []u8, s: []const u8, custom_placeholder: []const u8)
cursor += ascii_len;

if (ascii_len >= s.len) {
return s.len;
return out[0..s.len];
}

// rest's length must >= 1
Expand Down Expand Up @@ -244,7 +244,7 @@ pub fn deunicodeCustom(out: []u8, s: []const u8, custom_placeholder: []const u8)
}
}

return cursor;
return out[0..cursor];
}

// --------------------------------------------------------------------------------
Expand All @@ -269,8 +269,7 @@ fn checkConversionBuf(str: []const u8, expect: []const u8) !bool {

var buf: [1024]u8 = undefined;

const len = try deunicode(&buf, str);
const res = buf[0..len];
const res = try deunicode(&buf, str);

return std.mem.eql(u8, res, expect);
}
Expand Down

0 comments on commit ee00d06

Please sign in to comment.