diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 46f23c84fe802d7c81f9791884a0f7ee722bea2b..8f4987e0c94ed7f4ab48fd09291e1f8d7b3101fb 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -233,7 +233,7 @@ pub const Allocator = struct { pub fn free(self: *Allocator, memory: var) void { const Slice = @typeInfo(@TypeOf(memory)).Pointer; const bytes = @sliceToBytes(memory); - const bytes_len = bytes.len + @boolToInt(Slice.sentinel != null); + const bytes_len = bytes.len + if (Slice.sentinel != null) @sizeOf(Slice.child) else 0; if (bytes_len == 0) return; const non_const_ptr = @intToPtr([*]u8, @ptrToInt(bytes.ptr)); @memset(non_const_ptr, undefined, bytes_len); diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig index 77783c3eddac3f55889c164cd8189d5c79a512ae..da0dbc3cb24da2894955476bda40eeba8aab858a 100644 --- a/lib/std/unicode.zig +++ b/lib/std/unicode.zig @@ -571,8 +571,9 @@ pub fn utf8ToUtf16LeWithNull(allocator: *mem.Allocator, utf8: []const u8) ![:0]u } } + const len = result.len; try result.append(0); - return result.toOwnedSlice()[0..:0]; + return result.toOwnedSlice()[0..len :0]; } /// Returns index of next character. If exact fit, returned index equals output slice length. @@ -619,12 +620,14 @@ test "utf8ToUtf16LeWithNull" { var bytes: [128]u8 = undefined; const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; const utf16 = try utf8ToUtf16LeWithNull(allocator, "𐐷"); - testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc\x00\x00", @sliceToBytes(utf16[0..])); + testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc", @sliceToBytes(utf16[0..])); + testing.expect(utf16[2] == 0); } { var bytes: [128]u8 = undefined; const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; const utf16 = try utf8ToUtf16LeWithNull(allocator, "\u{10FFFF}"); - testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf\x00\x00", @sliceToBytes(utf16[0..])); + testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", @sliceToBytes(utf16[0..])); + testing.expect(utf16[2] == 0); } }