diff --git a/README.md b/README.md index 58dadb1..5c83960 100644 --- a/README.md +++ b/README.md @@ -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 } ``` diff --git a/examples/unicode/main.zig b/examples/unicode/main.zig index 153eb0a..681ef7c 100644 --- a/examples/unicode/main.zig +++ b/examples/unicode/main.zig @@ -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}); } diff --git a/src/lib.zig b/src/lib.zig index 4b6eb37..a6a91f8 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -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, "[?]"); } @@ -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 @@ -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 @@ -244,7 +244,7 @@ pub fn deunicodeCustom(out: []u8, s: []const u8, custom_placeholder: []const u8) } } - return cursor; + return out[0..cursor]; } // -------------------------------------------------------------------------------- @@ -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); }