zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 7e3591bedd41bde8bcca892885376e2f4a8163a3 (tree)
parent ecc0108cea97772b6e921b36d8fdc8f90d5fc6cb
Author: r00ster91 <r00ster91@proton.me>
Date:   Wed,  8 Mar 2023 19:25:53 +0100

std.json.parseInternal: use switches instead of ifs

Diffstat:
Mlib/std/json.zig | 41++++++++++++++++++-----------------------
1 file changed, 18 insertions(+), 23 deletions(-)

diff --git a/lib/std/json.zig b/lib/std/json.zig @@ -1534,30 +1534,25 @@ fn parseInternal( child_options.allow_trailing_data = true; var found = false; inline for (structInfo.fields, 0..) |field, i| { - // TODO: using switches here segfault the compiler (#2727?) - if ((stringToken.escapes == .None and mem.eql(u8, field.name, key_source_slice)) or (stringToken.escapes == .Some and (field.name.len == stringToken.decodedLength() and encodesTo(field.name, key_source_slice)))) { - // if (switch (stringToken.escapes) { - // .None => mem.eql(u8, field.name, key_source_slice), - // .Some => (field.name.len == stringToken.decodedLength() and encodesTo(field.name, key_source_slice)), - // }) { + if (switch (stringToken.escapes) { + .None => mem.eql(u8, field.name, key_source_slice), + .Some => (field.name.len == stringToken.decodedLength() and encodesTo(field.name, key_source_slice)), + }) { if (fields_seen[i]) { - // switch (options.duplicate_field_behavior) { - // .UseFirst => {}, - // .Error => {}, - // .UseLast => {}, - // } - if (options.duplicate_field_behavior == .UseFirst) { - // unconditonally ignore value. for comptime fields, this skips check against default_value - parseFree(field.type, try parse(field.type, tokens, child_options), child_options); - found = true; - break; - } else if (options.duplicate_field_behavior == .Error) { - return error.DuplicateJSONField; - } else if (options.duplicate_field_behavior == .UseLast) { - if (!field.is_comptime) { - parseFree(field.type, @field(r, field.name), child_options); - } - fields_seen[i] = false; + switch (options.duplicate_field_behavior) { + .UseFirst => { + // unconditonally ignore value. for comptime fields, this skips check against default_value + parseFree(field.type, try parse(field.type, tokens, child_options), child_options); + found = true; + break; + }, + .Error => return error.DuplicateJSONField, + .UseLast => { + if (!field.is_comptime) { + parseFree(field.type, @field(r, field.name), child_options); + } + fields_seen[i] = false; + }, } } if (field.is_comptime) {