aboutsummaryrefslogtreecommitdiff
path: root/lib/std/json
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-06-24 16:58:19 -0700
committerGitHub <noreply@github.com>2023-06-24 16:58:19 -0700
commit146b79af153bbd5dafda0ba12a040385c7fc58f8 (patch)
tree67e3db8b444d65c667e314770fc983a7fc8ba293 /lib/std/json
parent13853bef0df3c90633021850cc6d6abaeea03282 (diff)
parent21ac0beb436f49fe49c6982a872f2dc48e4bea5e (diff)
downloadzig-146b79af153bbd5dafda0ba12a040385c7fc58f8.tar.gz
zig-146b79af153bbd5dafda0ba12a040385c7fc58f8.zip
Merge pull request #16163 from mlugg/feat/builtins-infer-dest-ty
Infer destination type of cast builtins using result type
Diffstat (limited to 'lib/std/json')
-rw-r--r--lib/std/json/scanner.zig8
-rw-r--r--lib/std/json/static.zig20
-rw-r--r--lib/std/json/stringify.zig4
-rw-r--r--lib/std/json/write_stream.zig6
4 files changed, 19 insertions, 19 deletions
diff --git a/lib/std/json/scanner.zig b/lib/std/json/scanner.zig
index 4fb7c1da01..274faba2ff 100644
--- a/lib/std/json/scanner.zig
+++ b/lib/std/json/scanner.zig
@@ -193,7 +193,7 @@ pub const TokenType = enum {
/// to get meaningful information from this.
pub const Diagnostics = struct {
line_number: u64 = 1,
- line_start_cursor: usize = @bitCast(usize, @as(isize, -1)), // Start just "before" the input buffer to get a 1-based column for line 1.
+ line_start_cursor: usize = @as(usize, @bitCast(@as(isize, -1))), // Start just "before" the input buffer to get a 1-based column for line 1.
total_bytes_before_current_input: u64 = 0,
cursor_pointer: *const usize = undefined,
@@ -1719,7 +1719,7 @@ const BitStack = struct {
pub fn push(self: *@This(), b: u1) Allocator.Error!void {
const byte_index = self.bit_len >> 3;
- const bit_index = @intCast(u3, self.bit_len & 7);
+ const bit_index = @as(u3, @intCast(self.bit_len & 7));
if (self.bytes.items.len <= byte_index) {
try self.bytes.append(0);
@@ -1733,8 +1733,8 @@ const BitStack = struct {
pub fn peek(self: *const @This()) u1 {
const byte_index = (self.bit_len - 1) >> 3;
- const bit_index = @intCast(u3, (self.bit_len - 1) & 7);
- return @intCast(u1, (self.bytes.items[byte_index] >> bit_index) & 1);
+ const bit_index = @as(u3, @intCast((self.bit_len - 1) & 7));
+ return @as(u1, @intCast((self.bytes.items[byte_index] >> bit_index) & 1));
}
pub fn pop(self: *@This()) u1 {
diff --git a/lib/std/json/static.zig b/lib/std/json/static.zig
index fd3d12d73a..f1926660f3 100644
--- a/lib/std/json/static.zig
+++ b/lib/std/json/static.zig
@@ -442,7 +442,7 @@ fn internalParse(
}
if (ptrInfo.sentinel) |some| {
- const sentinel_value = @ptrCast(*align(1) const ptrInfo.child, some).*;
+ const sentinel_value = @as(*align(1) const ptrInfo.child, @ptrCast(some)).*;
return try arraylist.toOwnedSliceSentinel(sentinel_value);
}
@@ -456,7 +456,7 @@ fn internalParse(
// Use our own array list so we can append the sentinel.
var value_list = ArrayList(u8).init(allocator);
_ = try source.allocNextIntoArrayList(&value_list, .alloc_always);
- return try value_list.toOwnedSliceSentinel(@ptrCast(*const u8, sentinel_ptr).*);
+ return try value_list.toOwnedSliceSentinel(@as(*const u8, @ptrCast(sentinel_ptr)).*);
}
if (ptrInfo.is_const) {
switch (try source.nextAllocMax(allocator, .alloc_if_needed, options.max_value_len.?)) {
@@ -518,8 +518,8 @@ fn internalParseFromValue(
},
.Float, .ComptimeFloat => {
switch (source) {
- .float => |f| return @floatCast(T, f),
- .integer => |i| return @floatFromInt(T, i),
+ .float => |f| return @as(T, @floatCast(f)),
+ .integer => |i| return @as(T, @floatFromInt(i)),
.number_string, .string => |s| return std.fmt.parseFloat(T, s),
else => return error.UnexpectedToken,
}
@@ -530,12 +530,12 @@ fn internalParseFromValue(
if (@round(f) != f) return error.InvalidNumber;
if (f > std.math.maxInt(T)) return error.Overflow;
if (f < std.math.minInt(T)) return error.Overflow;
- return @intFromFloat(T, f);
+ return @as(T, @intFromFloat(f));
},
.integer => |i| {
if (i > std.math.maxInt(T)) return error.Overflow;
if (i < std.math.minInt(T)) return error.Overflow;
- return @intCast(T, i);
+ return @as(T, @intCast(i));
},
.number_string, .string => |s| {
return sliceToInt(T, s);
@@ -686,7 +686,7 @@ fn internalParseFromValue(
switch (source) {
.array => |array| {
const r = if (ptrInfo.sentinel) |sentinel_ptr|
- try allocator.allocSentinel(ptrInfo.child, array.items.len, @ptrCast(*align(1) const ptrInfo.child, sentinel_ptr).*)
+ try allocator.allocSentinel(ptrInfo.child, array.items.len, @as(*align(1) const ptrInfo.child, @ptrCast(sentinel_ptr)).*)
else
try allocator.alloc(ptrInfo.child, array.items.len);
@@ -701,7 +701,7 @@ fn internalParseFromValue(
// Dynamic length string.
const r = if (ptrInfo.sentinel) |sentinel_ptr|
- try allocator.allocSentinel(ptrInfo.child, s.len, @ptrCast(*align(1) const ptrInfo.child, sentinel_ptr).*)
+ try allocator.allocSentinel(ptrInfo.child, s.len, @as(*align(1) const ptrInfo.child, @ptrCast(sentinel_ptr)).*)
else
try allocator.alloc(ptrInfo.child, s.len);
@memcpy(r[0..], s);
@@ -743,7 +743,7 @@ fn sliceToInt(comptime T: type, slice: []const u8) !T {
const float = try std.fmt.parseFloat(f128, slice);
if (@round(float) != float) return error.InvalidNumber;
if (float > std.math.maxInt(T) or float < std.math.minInt(T)) return error.Overflow;
- return @intCast(T, @intFromFloat(i128, float));
+ return @as(T, @intCast(@as(i128, @intFromFloat(float))));
}
fn sliceToEnum(comptime T: type, slice: []const u8) !T {
@@ -759,7 +759,7 @@ fn fillDefaultStructValues(comptime T: type, r: *T, fields_seen: *[@typeInfo(T).
inline for (@typeInfo(T).Struct.fields, 0..) |field, i| {
if (!fields_seen[i]) {
if (field.default_value) |default_ptr| {
- const default = @ptrCast(*align(1) const field.type, default_ptr).*;
+ const default = @as(*align(1) const field.type, @ptrCast(default_ptr)).*;
@field(r, field.name) = default;
} else {
return error.MissingField;
diff --git a/lib/std/json/stringify.zig b/lib/std/json/stringify.zig
index 6d10e95330..5de5db54b9 100644
--- a/lib/std/json/stringify.zig
+++ b/lib/std/json/stringify.zig
@@ -78,8 +78,8 @@ fn outputUnicodeEscape(
assert(codepoint <= 0x10FFFF);
// To escape an extended character that is not in the Basic Multilingual Plane,
// the character is represented as a 12-character sequence, encoding the UTF-16 surrogate pair.
- const high = @intCast(u16, (codepoint - 0x10000) >> 10) + 0xD800;
- const low = @intCast(u16, codepoint & 0x3FF) + 0xDC00;
+ const high = @as(u16, @intCast((codepoint - 0x10000) >> 10)) + 0xD800;
+ const low = @as(u16, @intCast(codepoint & 0x3FF)) + 0xDC00;
try out_stream.writeAll("\\u");
try std.fmt.formatIntValue(high, "x", std.fmt.FormatOptions{ .width = 4, .fill = '0' }, out_stream);
try out_stream.writeAll("\\u");
diff --git a/lib/std/json/write_stream.zig b/lib/std/json/write_stream.zig
index 760bad13fd..3a2750f5a1 100644
--- a/lib/std/json/write_stream.zig
+++ b/lib/std/json/write_stream.zig
@@ -176,8 +176,8 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
.ComptimeInt => {
return self.emitNumber(@as(std.math.IntFittingRange(value, value), value));
},
- .Float, .ComptimeFloat => if (@floatCast(f64, value) == value) {
- try self.stream.print("{}", .{@floatCast(f64, value)});
+ .Float, .ComptimeFloat => if (@as(f64, @floatCast(value)) == value) {
+ try self.stream.print("{}", .{@as(f64, @floatCast(value))});
self.popState();
return;
},
@@ -294,7 +294,7 @@ test "json write stream" {
fn getJsonObject(allocator: std.mem.Allocator) !Value {
var value = Value{ .object = ObjectMap.init(allocator) };
- try value.object.put("one", Value{ .integer = @intCast(i64, 1) });
+ try value.object.put("one", Value{ .integer = @as(i64, @intCast(1)) });
try value.object.put("two", Value{ .float = 2.0 });
return value;
}