diff options
| author | 2022-02-05 01:10:53 +0100 | |
|---|---|---|
| committer | 2022-02-05 01:53:11 +0100 | |
| commit | 34ecdbfd06bb7e9eee9366f4396e6fb6918b595a (patch) | |
| tree | 811622a27cee6c51e89e59e16376fc96ca1f9a2e /sqlite.zig | |
| parent | build: add a macOS cross-compilation test target (diff) | |
| download | zig-sqlite-34ecdbfd06bb7e9eee9366f4396e6fb6918b595a.tar.gz zig-sqlite-34ecdbfd06bb7e9eee9366f4396e6fb6918b595a.tar.xz zig-sqlite-34ecdbfd06bb7e9eee9366f4396e6fb6918b595a.zip | |
fix sentinel
Diffstat (limited to '')
| -rw-r--r-- | sqlite.zig | 42 |
1 files changed, 23 insertions, 19 deletions
| @@ -903,12 +903,15 @@ pub fn Iterator(comptime Type: type) type { | |||
| 903 | u8 => { | 903 | u8 => { |
| 904 | const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i)); | 904 | const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i)); |
| 905 | 905 | ||
| 906 | if (arr.sentinel) |s| { | 906 | if (arr.sentinel) |sentinel_ptr| { |
| 907 | // An array with a sentinel need to be as big as the data, + 1 byte for the sentinel. | 907 | // An array with a sentinel need to be as big as the data, + 1 byte for the sentinel. |
| 908 | mem.set(u8, &ret, s); | ||
| 909 | if (size >= @as(usize, arr.len)) { | 908 | if (size >= @as(usize, arr.len)) { |
| 910 | return error.ArrayTooSmall; | 909 | return error.ArrayTooSmall; |
| 911 | } | 910 | } |
| 911 | |||
| 912 | // Set the sentinel in the result at the correct position. | ||
| 913 | const sentinel = @ptrCast(*const arr.child, sentinel_ptr).*; | ||
| 914 | ret[size] = sentinel; | ||
| 912 | } else if (size != arr.len) { | 915 | } else if (size != arr.len) { |
| 913 | // An array without a sentinel must have the exact same size as the data because we can't | 916 | // An array without a sentinel must have the exact same size as the data because we can't |
| 914 | // communicate the real size to the caller. | 917 | // communicate the real size to the caller. |
| @@ -920,9 +923,6 @@ pub fn Iterator(comptime Type: type) type { | |||
| 920 | const ptr = @ptrCast([*c]const u8, data)[0..size]; | 923 | const ptr = @ptrCast([*c]const u8, data)[0..size]; |
| 921 | 924 | ||
| 922 | mem.copy(u8, ret[0..], ptr); | 925 | mem.copy(u8, ret[0..], ptr); |
| 923 | if (arr.sentinel) |s| { | ||
| 924 | ret[size] = s; | ||
| 925 | } | ||
| 926 | } | 926 | } |
| 927 | }, | 927 | }, |
| 928 | else => @compileError("cannot read into array of " ++ @typeName(arr.child)), | 928 | else => @compileError("cannot read into array of " ++ @typeName(arr.child)), |
| @@ -966,7 +966,9 @@ pub fn Iterator(comptime Type: type) type { | |||
| 966 | fn dupeWithSentinel(comptime SliceType: type, allocator: mem.Allocator, data: []const u8) !SliceType { | 966 | fn dupeWithSentinel(comptime SliceType: type, allocator: mem.Allocator, data: []const u8) !SliceType { |
| 967 | switch (@typeInfo(SliceType)) { | 967 | switch (@typeInfo(SliceType)) { |
| 968 | .Pointer => |ptr_info| { | 968 | .Pointer => |ptr_info| { |
| 969 | if (ptr_info.sentinel) |sentinel| { | 969 | if (ptr_info.sentinel) |sentinel_ptr| { |
| 970 | const sentinel = @ptrCast(*const ptr_info.child, sentinel_ptr).*; | ||
| 971 | |||
| 970 | const slice = try allocator.alloc(u8, data.len + 1); | 972 | const slice = try allocator.alloc(u8, data.len + 1); |
| 971 | mem.copy(u8, slice, data); | 973 | mem.copy(u8, slice, data); |
| 972 | slice[data.len] = sentinel; | 974 | slice[data.len] = sentinel; |
| @@ -2249,19 +2251,21 @@ test "sqlite: read a single text value" { | |||
| 2249 | try testing.expectEqualStrings("Vincent", name.?.data); | 2251 | try testing.expectEqualStrings("Vincent", name.?.data); |
| 2250 | }, | 2252 | }, |
| 2251 | else => { | 2253 | else => { |
| 2252 | const span = blk: { | 2254 | const type_info = @typeInfo(typ); |
| 2253 | const type_info = @typeInfo(typ); | 2255 | switch (type_info) { |
| 2254 | break :blk switch (type_info) { | 2256 | .Pointer => { |
| 2255 | .Pointer => name.?, | 2257 | try testing.expectEqualStrings("Vincent", name.?); |
| 2256 | .Array => |arr| if (arr.sentinel) |s| | 2258 | }, |
| 2257 | mem.sliceTo(&name.?, s) | 2259 | .Array => |arr| if (arr.sentinel) |sentinel_ptr| { |
| 2258 | else | 2260 | const sentinel = @ptrCast(*const arr.child, sentinel_ptr).*; |
| 2259 | mem.span(&name.?), | 2261 | const res = mem.sliceTo(&name.?, sentinel); |
| 2260 | else => @compileError("invalid type " ++ @typeName(typ)), | 2262 | try testing.expectEqualStrings("Vincent", res); |
| 2261 | }; | 2263 | } else { |
| 2262 | }; | 2264 | const res = mem.span(&name.?); |
| 2263 | 2265 | try testing.expectEqualStrings("Vincent", res); | |
| 2264 | try testing.expectEqualStrings("Vincent", span); | 2266 | }, |
| 2267 | else => @compileError("invalid type " ++ @typeName(typ)), | ||
| 2268 | } | ||
| 2265 | }, | 2269 | }, |
| 2266 | } | 2270 | } |
| 2267 | } | 2271 | } |