.
This commit is contained in:
parent
aa890ea209
commit
2384be9a60
101
language.zig
101
language.zig
@ -197,87 +197,6 @@ fn addNull(self: *Self, allocator: mem.Allocator) !usize {
|
||||
return idx;
|
||||
}
|
||||
|
||||
fn getProperty(self: *Self, index: []const u8) ?StringIndex {
|
||||
return self.property_index.string_table.get(index);
|
||||
}
|
||||
|
||||
fn getNumber(self: *Self, index: usize) ?f64 {
|
||||
if (self.index.get(index)) |n| return n;
|
||||
return null;
|
||||
}
|
||||
|
||||
fn getObject(self: *Self, allocator: mem.Allocator, index: usize) !struct {
|
||||
[]StringIndex,
|
||||
[]usize,
|
||||
} {
|
||||
const entry = self.index.get(index);
|
||||
|
||||
if (entry.object.len == 0) {
|
||||
return .{ &.{}, &.{} };
|
||||
}
|
||||
|
||||
var pidx = entry.object.property_idx;
|
||||
var vidx = entry.object.value_idx;
|
||||
|
||||
const keys = try allocator.alloc(StringIndex, entry.object.len);
|
||||
const values = try allocator.alloc(usize, entry.object.len);
|
||||
|
||||
var i: usize = 0;
|
||||
|
||||
flag: switch (self.index.get(vidx)) {
|
||||
.array => {
|
||||
vidx += 1;
|
||||
continue :flag self.index.get(vidx);
|
||||
},
|
||||
.object => |obj| {
|
||||
var iter = StringIndex.iterator(
|
||||
@enumFromInt(obj.property_idx),
|
||||
self.property_index.string_bytes.items,
|
||||
);
|
||||
const slice = iter.next();
|
||||
keys[i] = @enumFromInt(obj.property_idx);
|
||||
values[i] = vidx;
|
||||
i += 1;
|
||||
pidx += slice.len + 1;
|
||||
vidx += 1;
|
||||
continue :flag self.index.get(vidx);
|
||||
},
|
||||
else => {
|
||||
// pidx += slice.len + 1;
|
||||
vidx += 1;
|
||||
continue :flag self.index.get(vidx);
|
||||
},
|
||||
}
|
||||
|
||||
return .{ keys, values };
|
||||
}
|
||||
|
||||
fn getArray(self: *Self, allocator: mem.Allocator, index: usize) ![]usize {
|
||||
const entry = self.index.get(index);
|
||||
|
||||
if (entry.array.len == 0) {
|
||||
return &.{};
|
||||
}
|
||||
|
||||
var idx = entry.array.start;
|
||||
const values = try allocator.alloc(usize, entry.array.len);
|
||||
|
||||
for (0..entry.array.len) |i| {
|
||||
values[i] = idx;
|
||||
idx += 1;
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
fn getBool(self: *Self, index: usize) ?bool {
|
||||
const entry = self.index.get(index) orelse return null;
|
||||
return entry.bool;
|
||||
}
|
||||
|
||||
fn getNull(self: *Self, index: usize) ?void {
|
||||
const entry = self.index.get(index) orelse return null;
|
||||
return entry.null;
|
||||
}
|
||||
// Recursively compute how many index slots a node occupies (including nested)
|
||||
fn skipSlots(self: *Self, slot: usize) usize {
|
||||
const e = self.index.get(slot);
|
||||
@ -306,19 +225,6 @@ fn skipSlots(self: *Self, slot: usize) usize {
|
||||
}
|
||||
}
|
||||
|
||||
// Compute bytes length of properties starting at pidx
|
||||
fn skipProps(self: *Self, pidx: usize, count: usize) usize {
|
||||
var total: usize = 0;
|
||||
var p = pidx;
|
||||
for (0..count) |_| {
|
||||
const key_slice = StringIndex.slice(@enumFromInt(p), &self.property_index);
|
||||
const len = key_slice.len + 1;
|
||||
total += len;
|
||||
p += len;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
fn skipNestedProps(self: *Self, pptr: *usize, slot: usize) void {
|
||||
const e = self.index.get(slot);
|
||||
if (e == .object) {
|
||||
@ -395,7 +301,12 @@ test getValue {
|
||||
\\ "cute": true,
|
||||
\\ "metadata": {
|
||||
\\ "post": [1,2,3],
|
||||
\\ "a": 2
|
||||
\\ "a": 2,
|
||||
\\ "c": {
|
||||
\\ "d": 4,
|
||||
\\ "uwu": [[[[[1], [2]]]]],
|
||||
\\ "x": true
|
||||
\\ }
|
||||
\\ },
|
||||
\\ "b": 3
|
||||
\\ }
|
||||
|
Loading…
x
Reference in New Issue
Block a user