cosmetic changes
This commit is contained in:
parent
0ccbdc4215
commit
a526379fd8
|
@ -15,7 +15,8 @@ pub fn pack(allocator: Allocator, input: [][*:0]const u8) packErr![]const u8 {
|
||||||
const len = try std.math.cast(c_uint, input.len);
|
const len = try std.math.cast(c_uint, input.len);
|
||||||
var source = c.cmph_io_vector_adapter(cvector, len);
|
var source = c.cmph_io_vector_adapter(cvector, len);
|
||||||
defer c.cmph_io_vector_adapter_destroy(source);
|
defer c.cmph_io_vector_adapter_destroy(source);
|
||||||
var config: *c.cmph_config_t = c.cmph_config_new(source) orelse return error.OutOfMemory;
|
var config: *c.cmph_config_t = c.cmph_config_new(source) orelse
|
||||||
|
return error.OutOfMemory;
|
||||||
c.cmph_config_set_algo(config, c.CMPH_BDZ);
|
c.cmph_config_set_algo(config, c.CMPH_BDZ);
|
||||||
c.cmph_config_set_b(config, 7);
|
c.cmph_config_set_b(config, 7);
|
||||||
var hash: *c.cmph_t = c.cmph_new(config) orelse return error.OutOfMemory;
|
var hash: *c.cmph_t = c.cmph_new(config) orelse return error.OutOfMemory;
|
||||||
|
|
|
@ -103,17 +103,19 @@ pub fn uvarint(buf: []const u8) error{Overflow}!Varint {
|
||||||
var s: u6 = 0;
|
var s: u6 = 0;
|
||||||
|
|
||||||
for (buf) |b, i| {
|
for (buf) |b, i| {
|
||||||
if (i == maxVarintLen64) {
|
if (i == maxVarintLen64)
|
||||||
// Catch byte reads past maxVarintLen64.
|
// Catch byte reads past maxVarintLen64.
|
||||||
// See issue https://golang.org/issues/41185
|
// See issue https://golang.org/issues/41185
|
||||||
return error.Overflow;
|
return error.Overflow;
|
||||||
}
|
|
||||||
|
|
||||||
if (b < 0x80) {
|
if (b < 0x80) {
|
||||||
if (i == maxVarintLen64 - 1 and b > 1) {
|
if (i == maxVarintLen64 - 1 and b > 1) {
|
||||||
return error.Overflow;
|
return error.Overflow;
|
||||||
}
|
}
|
||||||
return Varint{ .value = x | (@as(u64, b) << s), .bytesRead = i + 1 };
|
return Varint{
|
||||||
|
.value = x | (@as(u64, b) << s),
|
||||||
|
.bytesRead = i + 1,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
x |= (@as(u64, b & 0x7f) << s);
|
x |= (@as(u64, b & 0x7f) << s);
|
||||||
s = try std.math.add(u6, s, 7);
|
s = try std.math.add(u6, s, 7);
|
||||||
|
|
|
@ -64,9 +64,8 @@ const PackedGroup = struct {
|
||||||
const nextStart = pad.roundUp(usize, alignmentBits, endBlob);
|
const nextStart = pad.roundUp(usize, alignmentBits, endBlob);
|
||||||
|
|
||||||
var next: ?[]const u8 = null;
|
var next: ?[]const u8 = null;
|
||||||
if (nextStart < bytes.len) {
|
if (nextStart < bytes.len)
|
||||||
next = bytes[nextStart..];
|
next = bytes[nextStart..];
|
||||||
}
|
|
||||||
|
|
||||||
return Entry{
|
return Entry{
|
||||||
.group = PackedGroup{
|
.group = PackedGroup{
|
||||||
|
@ -79,10 +78,9 @@ const PackedGroup = struct {
|
||||||
|
|
||||||
const packErr = validate.InvalidRecord || Allocator.Error;
|
const packErr = validate.InvalidRecord || Allocator.Error;
|
||||||
fn validateUtf8(s: []const u8) InvalidRecord!void {
|
fn validateUtf8(s: []const u8) InvalidRecord!void {
|
||||||
if (!std.unicode.utf8ValidateSlice(s)) {
|
if (!std.unicode.utf8ValidateSlice(s))
|
||||||
return error.InvalidRecord;
|
return error.InvalidRecord;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub const Iterator = struct {
|
pub const Iterator = struct {
|
||||||
section: ?[]const u8,
|
section: ?[]const u8,
|
||||||
|
@ -174,7 +172,6 @@ test "construct PackedGroups" {
|
||||||
}
|
}
|
||||||
|
|
||||||
var i: u29 = 0;
|
var i: u29 = 0;
|
||||||
{
|
|
||||||
var it = PackedGroup.iterator(buf.items);
|
var it = PackedGroup.iterator(buf.items);
|
||||||
while (it.next()) |group| : (i += 1) {
|
while (it.next()) |group| : (i += 1) {
|
||||||
try testing.expectEqual(groups[i].gid, group.gid());
|
try testing.expectEqual(groups[i].gid, group.gid());
|
||||||
|
@ -183,7 +180,6 @@ test "construct PackedGroups" {
|
||||||
}
|
}
|
||||||
try testing.expectEqual(groups.len, i);
|
try testing.expectEqual(groups.len, i);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
test "Group.clone" {
|
test "Group.clone" {
|
||||||
var allocator = testing.allocator;
|
var allocator = testing.allocator;
|
||||||
|
|
|
@ -44,12 +44,10 @@ const Corpus = struct {
|
||||||
|
|
||||||
var users = try allocator.alloc(User, usersConst.len);
|
var users = try allocator.alloc(User, usersConst.len);
|
||||||
var groups = try allocator.alloc(Group, groupsConst.len);
|
var groups = try allocator.alloc(Group, groupsConst.len);
|
||||||
for (usersConst) |_, i| {
|
for (usersConst) |*user, i|
|
||||||
users[i] = try usersConst[i].clone(allocator);
|
users[i] = try user.clone(allocator);
|
||||||
}
|
for (groupsConst) |*group, i|
|
||||||
for (groupsConst) |_, i| {
|
groups[i] = try group.clone(allocator);
|
||||||
groups[i] = try groupsConst[i].clone(allocator);
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.sort(User, users, {}, cmpUser);
|
sort.sort(User, users, {}, cmpUser);
|
||||||
sort.sort(Group, groups, {}, cmpGroup);
|
sort.sort(Group, groups, {}, cmpGroup);
|
||||||
|
@ -60,29 +58,25 @@ const Corpus = struct {
|
||||||
var gid2group = AutoHashMap(u32, *const Group).init(allocator);
|
var gid2group = AutoHashMap(u32, *const Group).init(allocator);
|
||||||
for (users) |*user| {
|
for (users) |*user| {
|
||||||
var res1 = try name2user.getOrPut(user.name);
|
var res1 = try name2user.getOrPut(user.name);
|
||||||
if (res1.found_existing) {
|
if (res1.found_existing)
|
||||||
return error.Duplicate;
|
return error.Duplicate;
|
||||||
}
|
|
||||||
res1.value_ptr.* = user;
|
res1.value_ptr.* = user;
|
||||||
|
|
||||||
var res2 = try uid2user.getOrPut(user.uid);
|
var res2 = try uid2user.getOrPut(user.uid);
|
||||||
if (res2.found_existing) {
|
if (res2.found_existing)
|
||||||
return error.Duplicate;
|
return error.Duplicate;
|
||||||
}
|
|
||||||
|
|
||||||
res2.value_ptr.* = user;
|
res2.value_ptr.* = user;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (groups) |*group| {
|
for (groups) |*group| {
|
||||||
var res1 = try name2group.getOrPut(group.name);
|
var res1 = try name2group.getOrPut(group.name);
|
||||||
if (res1.found_existing) {
|
if (res1.found_existing)
|
||||||
return error.Duplicate;
|
return error.Duplicate;
|
||||||
}
|
|
||||||
res1.value_ptr.* = group;
|
res1.value_ptr.* = group;
|
||||||
var res2 = try gid2group.getOrPut(group.gid);
|
var res2 = try gid2group.getOrPut(group.gid);
|
||||||
if (res2.found_existing) {
|
if (res2.found_existing)
|
||||||
return error.Duplicate;
|
return error.Duplicate;
|
||||||
}
|
|
||||||
res2.value_ptr.* = group;
|
res2.value_ptr.* = group;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,32 +97,28 @@ const Corpus = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var groupsOfMember = try username2groups.getOrPut(memberName.*);
|
var groupsOfMember = try username2groups.getOrPut(memberName.*);
|
||||||
if (!groupsOfMember.found_existing) {
|
if (!groupsOfMember.found_existing)
|
||||||
groupsOfMember.value_ptr.* = ArrayListUnmanaged(*const Group){};
|
groupsOfMember.value_ptr.* = ArrayListUnmanaged(*const Group){};
|
||||||
}
|
|
||||||
try groupsOfMember.value_ptr.*.append(allocator, group);
|
try groupsOfMember.value_ptr.*.append(allocator, group);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = try groupname2users.getOrPut(group.name);
|
var result = try groupname2users.getOrPut(group.name);
|
||||||
if (result.found_existing) {
|
if (result.found_existing)
|
||||||
return error.Duplicate;
|
return error.Duplicate;
|
||||||
}
|
|
||||||
result.value_ptr.* = members;
|
result.value_ptr.* = members;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
var it = groupname2users.valueIterator();
|
var it = groupname2users.valueIterator();
|
||||||
while (it.next()) |groupUsers| {
|
while (it.next()) |groupUsers|
|
||||||
sort.sort(*const User, groupUsers.items, {}, cmpUserPtr);
|
sort.sort(*const User, groupUsers.items, {}, cmpUserPtr);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
{
|
{
|
||||||
var it = username2groups.valueIterator();
|
var it = username2groups.valueIterator();
|
||||||
while (it.next()) |userGroups| {
|
while (it.next()) |userGroups|
|
||||||
sort.sort(*const Group, userGroups.items, {}, cmpGroupPtr);
|
sort.sort(*const Group, userGroups.items, {}, cmpGroupPtr);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return Corpus{
|
return Corpus{
|
||||||
.arena = arena,
|
.arena = arena,
|
||||||
|
@ -155,14 +145,16 @@ fn cmpUser(_: void, a: User, b: User) bool {
|
||||||
var utf8_b = (unicode.Utf8View.init(b.name) catch unreachable).iterator();
|
var utf8_b = (unicode.Utf8View.init(b.name) catch unreachable).iterator();
|
||||||
while (utf8_a.nextCodepoint()) |codepoint_a| {
|
while (utf8_a.nextCodepoint()) |codepoint_a| {
|
||||||
if (utf8_b.nextCodepoint()) |codepoint_b| {
|
if (utf8_b.nextCodepoint()) |codepoint_b| {
|
||||||
if (codepoint_a != codepoint_b) {
|
if (codepoint_a == codepoint_b) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
return codepoint_a < codepoint_b;
|
return codepoint_a < codepoint_b;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
|
||||||
// a is a prefix of b. It is thus shorter.
|
// a is a prefix of b. It is thus shorter.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// b is a prefix of a
|
// b is a prefix of a
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -141,9 +141,8 @@ pub const ShellWriter = struct {
|
||||||
|
|
||||||
var it = self.counts.iterator();
|
var it = self.counts.iterator();
|
||||||
while (it.next()) |entry| {
|
while (it.next()) |entry| {
|
||||||
if (entry.value_ptr.* == 1) {
|
if (entry.value_ptr.* == 1)
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
const score = entry.key_ptr.*.len * entry.value_ptr.*;
|
const score = entry.key_ptr.*.len * entry.value_ptr.*;
|
||||||
try deque.add(KV{ .shell = entry.key_ptr.*, .score = score });
|
try deque.add(KV{ .shell = entry.key_ptr.*, .score = score });
|
||||||
}
|
}
|
||||||
|
|
25
src/user.zig
25
src/user.zig
|
@ -124,12 +124,10 @@ fn packedUser(immutable: bool) type {
|
||||||
// blobLength returns the length of the blob storing string values.
|
// blobLength returns the length of the blob storing string values.
|
||||||
fn blobLength(self: *const Inner) usize {
|
fn blobLength(self: *const Inner) usize {
|
||||||
var result: usize = self.homeLen() + self.gecosLen();
|
var result: usize = self.homeLen() + self.gecosLen();
|
||||||
if (!self.name_is_a_suffix) {
|
if (!self.name_is_a_suffix)
|
||||||
result += self.nameLen();
|
result += self.nameLen();
|
||||||
}
|
if (self.shell_here)
|
||||||
if (self.shell_here) {
|
|
||||||
result += self.shellLen();
|
result += self.shellLen();
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -161,9 +159,8 @@ fn packedUser(immutable: bool) type {
|
||||||
|
|
||||||
const nextStart = pad.roundUp(usize, alignmentBits, endBlob);
|
const nextStart = pad.roundUp(usize, alignmentBits, endBlob);
|
||||||
var next: ?Bytes = null;
|
var next: ?Bytes = null;
|
||||||
if (nextStart < bytes.len) {
|
if (nextStart < bytes.len)
|
||||||
next = bytes[nextStart..];
|
next = bytes[nextStart..];
|
||||||
}
|
|
||||||
|
|
||||||
return Entry{
|
return Entry{
|
||||||
.user = Self{
|
.user = Self{
|
||||||
|
@ -189,10 +186,7 @@ fn packedUser(immutable: bool) type {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn iterator(section: Bytes, idxFn: Idx2ShellProto) Iterator {
|
pub fn iterator(section: Bytes, idxFn: Idx2ShellProto) Iterator {
|
||||||
return Iterator{
|
return Iterator{ .section = section, .shellIndex = idxFn };
|
||||||
.section = section,
|
|
||||||
.shellIndex = idxFn,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// packTo packs the User record and copies it to the given byte slice.
|
// packTo packs the User record and copies it to the given byte slice.
|
||||||
|
@ -234,15 +228,11 @@ fn packedUser(immutable: bool) type {
|
||||||
try arr.*.appendSlice(innerBytes[0..InnerSize]);
|
try arr.*.appendSlice(innerBytes[0..InnerSize]);
|
||||||
try arr.*.appendSlice(user.home);
|
try arr.*.appendSlice(user.home);
|
||||||
|
|
||||||
if (!inner.name_is_a_suffix) {
|
if (!inner.name_is_a_suffix)
|
||||||
try arr.*.appendSlice(user.name);
|
try arr.*.appendSlice(user.name);
|
||||||
}
|
|
||||||
try arr.*.appendSlice(user.gecos);
|
try arr.*.appendSlice(user.gecos);
|
||||||
|
if (inner.shell_here)
|
||||||
if (inner.shell_here) {
|
|
||||||
try arr.*.appendSlice(user.shell);
|
try arr.*.appendSlice(user.shell);
|
||||||
}
|
|
||||||
|
|
||||||
try pad.arrayList(arr, alignmentBits);
|
try pad.arrayList(arr, alignmentBits);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -369,9 +359,8 @@ test "construct PackedUser section" {
|
||||||
.home = "/",
|
.home = "/",
|
||||||
.shell = "/",
|
.shell = "/",
|
||||||
} };
|
} };
|
||||||
for (users) |user| {
|
for (users) |user|
|
||||||
try PackedUserConst.packTo(&buf, user, testShellIndex);
|
try PackedUserConst.packTo(&buf, user, testShellIndex);
|
||||||
}
|
|
||||||
|
|
||||||
var i: u29 = 0;
|
var i: u29 = 0;
|
||||||
var it1 = PackedUserConst.iterator(buf.items, testShell);
|
var it1 = PackedUserConst.iterator(buf.items, testShell);
|
||||||
|
|
Loading…
Reference in New Issue