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);
|
||||
var source = c.cmph_io_vector_adapter(cvector, len);
|
||||
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_b(config, 7);
|
||||
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;
|
||||
|
||||
for (buf) |b, i| {
|
||||
if (i == maxVarintLen64) {
|
||||
if (i == maxVarintLen64)
|
||||
// Catch byte reads past maxVarintLen64.
|
||||
// See issue https://golang.org/issues/41185
|
||||
return error.Overflow;
|
||||
}
|
||||
|
||||
if (b < 0x80) {
|
||||
if (i == maxVarintLen64 - 1 and b > 1) {
|
||||
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);
|
||||
s = try std.math.add(u6, s, 7);
|
||||
|
|
|
@ -64,9 +64,8 @@ const PackedGroup = struct {
|
|||
const nextStart = pad.roundUp(usize, alignmentBits, endBlob);
|
||||
|
||||
var next: ?[]const u8 = null;
|
||||
if (nextStart < bytes.len) {
|
||||
if (nextStart < bytes.len)
|
||||
next = bytes[nextStart..];
|
||||
}
|
||||
|
||||
return Entry{
|
||||
.group = PackedGroup{
|
||||
|
@ -79,9 +78,8 @@ const PackedGroup = struct {
|
|||
|
||||
const packErr = validate.InvalidRecord || Allocator.Error;
|
||||
fn validateUtf8(s: []const u8) InvalidRecord!void {
|
||||
if (!std.unicode.utf8ValidateSlice(s)) {
|
||||
if (!std.unicode.utf8ValidateSlice(s))
|
||||
return error.InvalidRecord;
|
||||
}
|
||||
}
|
||||
|
||||
pub const Iterator = struct {
|
||||
|
@ -174,15 +172,13 @@ test "construct PackedGroups" {
|
|||
}
|
||||
|
||||
var i: u29 = 0;
|
||||
{
|
||||
var it = PackedGroup.iterator(buf.items);
|
||||
while (it.next()) |group| : (i += 1) {
|
||||
try testing.expectEqual(groups[i].gid, group.gid());
|
||||
try testing.expectEqualStrings(groups[i].name, group.name());
|
||||
try testing.expectEqual(groups[i].members_offset, group.membersOffset());
|
||||
}
|
||||
try testing.expectEqual(groups.len, i);
|
||||
var it = PackedGroup.iterator(buf.items);
|
||||
while (it.next()) |group| : (i += 1) {
|
||||
try testing.expectEqual(groups[i].gid, group.gid());
|
||||
try testing.expectEqualStrings(groups[i].name, group.name());
|
||||
try testing.expectEqual(groups[i].members_offset, group.membersOffset());
|
||||
}
|
||||
try testing.expectEqual(groups.len, i);
|
||||
}
|
||||
|
||||
test "Group.clone" {
|
||||
|
|
|
@ -44,12 +44,10 @@ const Corpus = struct {
|
|||
|
||||
var users = try allocator.alloc(User, usersConst.len);
|
||||
var groups = try allocator.alloc(Group, groupsConst.len);
|
||||
for (usersConst) |_, i| {
|
||||
users[i] = try usersConst[i].clone(allocator);
|
||||
}
|
||||
for (groupsConst) |_, i| {
|
||||
groups[i] = try groupsConst[i].clone(allocator);
|
||||
}
|
||||
for (usersConst) |*user, i|
|
||||
users[i] = try user.clone(allocator);
|
||||
for (groupsConst) |*group, i|
|
||||
groups[i] = try group.clone(allocator);
|
||||
|
||||
sort.sort(User, users, {}, cmpUser);
|
||||
sort.sort(Group, groups, {}, cmpGroup);
|
||||
|
@ -60,29 +58,25 @@ const Corpus = struct {
|
|||
var gid2group = AutoHashMap(u32, *const Group).init(allocator);
|
||||
for (users) |*user| {
|
||||
var res1 = try name2user.getOrPut(user.name);
|
||||
if (res1.found_existing) {
|
||||
if (res1.found_existing)
|
||||
return error.Duplicate;
|
||||
}
|
||||
res1.value_ptr.* = user;
|
||||
|
||||
var res2 = try uid2user.getOrPut(user.uid);
|
||||
if (res2.found_existing) {
|
||||
if (res2.found_existing)
|
||||
return error.Duplicate;
|
||||
}
|
||||
|
||||
res2.value_ptr.* = user;
|
||||
}
|
||||
|
||||
for (groups) |*group| {
|
||||
var res1 = try name2group.getOrPut(group.name);
|
||||
if (res1.found_existing) {
|
||||
if (res1.found_existing)
|
||||
return error.Duplicate;
|
||||
}
|
||||
res1.value_ptr.* = group;
|
||||
var res2 = try gid2group.getOrPut(group.gid);
|
||||
if (res2.found_existing) {
|
||||
if (res2.found_existing)
|
||||
return error.Duplicate;
|
||||
}
|
||||
res2.value_ptr.* = group;
|
||||
}
|
||||
|
||||
|
@ -103,31 +97,27 @@ const Corpus = struct {
|
|||
}
|
||||
|
||||
var groupsOfMember = try username2groups.getOrPut(memberName.*);
|
||||
if (!groupsOfMember.found_existing) {
|
||||
if (!groupsOfMember.found_existing)
|
||||
groupsOfMember.value_ptr.* = ArrayListUnmanaged(*const Group){};
|
||||
}
|
||||
try groupsOfMember.value_ptr.*.append(allocator, group);
|
||||
}
|
||||
|
||||
var result = try groupname2users.getOrPut(group.name);
|
||||
if (result.found_existing) {
|
||||
if (result.found_existing)
|
||||
return error.Duplicate;
|
||||
}
|
||||
result.value_ptr.* = members;
|
||||
}
|
||||
|
||||
{
|
||||
var it = groupname2users.valueIterator();
|
||||
while (it.next()) |groupUsers| {
|
||||
while (it.next()) |groupUsers|
|
||||
sort.sort(*const User, groupUsers.items, {}, cmpUserPtr);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var it = username2groups.valueIterator();
|
||||
while (it.next()) |userGroups| {
|
||||
while (it.next()) |userGroups|
|
||||
sort.sort(*const Group, userGroups.items, {}, cmpGroupPtr);
|
||||
}
|
||||
}
|
||||
|
||||
return Corpus{
|
||||
|
@ -155,13 +145,15 @@ fn cmpUser(_: void, a: User, b: User) bool {
|
|||
var utf8_b = (unicode.Utf8View.init(b.name) catch unreachable).iterator();
|
||||
while (utf8_a.nextCodepoint()) |codepoint_a| {
|
||||
if (utf8_b.nextCodepoint()) |codepoint_b| {
|
||||
if (codepoint_a != codepoint_b) {
|
||||
if (codepoint_a == codepoint_b) {
|
||||
continue;
|
||||
} else {
|
||||
return codepoint_a < codepoint_b;
|
||||
}
|
||||
} else {
|
||||
// a is a prefix of b. It is thus shorter.
|
||||
return false;
|
||||
}
|
||||
|
||||
// a is a prefix of b. It is thus shorter.
|
||||
return false;
|
||||
}
|
||||
// b is a prefix of a
|
||||
return true;
|
||||
|
|
|
@ -141,9 +141,8 @@ pub const ShellWriter = struct {
|
|||
|
||||
var it = self.counts.iterator();
|
||||
while (it.next()) |entry| {
|
||||
if (entry.value_ptr.* == 1) {
|
||||
if (entry.value_ptr.* == 1)
|
||||
continue;
|
||||
}
|
||||
const score = entry.key_ptr.*.len * entry.value_ptr.*;
|
||||
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.
|
||||
fn blobLength(self: *const Inner) usize {
|
||||
var result: usize = self.homeLen() + self.gecosLen();
|
||||
if (!self.name_is_a_suffix) {
|
||||
if (!self.name_is_a_suffix)
|
||||
result += self.nameLen();
|
||||
}
|
||||
if (self.shell_here) {
|
||||
if (self.shell_here)
|
||||
result += self.shellLen();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
@ -161,9 +159,8 @@ fn packedUser(immutable: bool) type {
|
|||
|
||||
const nextStart = pad.roundUp(usize, alignmentBits, endBlob);
|
||||
var next: ?Bytes = null;
|
||||
if (nextStart < bytes.len) {
|
||||
if (nextStart < bytes.len)
|
||||
next = bytes[nextStart..];
|
||||
}
|
||||
|
||||
return Entry{
|
||||
.user = Self{
|
||||
|
@ -189,10 +186,7 @@ fn packedUser(immutable: bool) type {
|
|||
};
|
||||
|
||||
pub fn iterator(section: Bytes, idxFn: Idx2ShellProto) Iterator {
|
||||
return Iterator{
|
||||
.section = section,
|
||||
.shellIndex = idxFn,
|
||||
};
|
||||
return Iterator{ .section = section, .shellIndex = idxFn };
|
||||
}
|
||||
|
||||
// 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(user.home);
|
||||
|
||||
if (!inner.name_is_a_suffix) {
|
||||
if (!inner.name_is_a_suffix)
|
||||
try arr.*.appendSlice(user.name);
|
||||
}
|
||||
try arr.*.appendSlice(user.gecos);
|
||||
|
||||
if (inner.shell_here) {
|
||||
if (inner.shell_here)
|
||||
try arr.*.appendSlice(user.shell);
|
||||
}
|
||||
|
||||
try pad.arrayList(arr, alignmentBits);
|
||||
}
|
||||
|
||||
|
@ -369,9 +359,8 @@ test "construct PackedUser section" {
|
|||
.home = "/",
|
||||
.shell = "/",
|
||||
} };
|
||||
for (users) |user| {
|
||||
for (users) |user|
|
||||
try PackedUserConst.packTo(&buf, user, testShellIndex);
|
||||
}
|
||||
|
||||
var i: u29 = 0;
|
||||
var it1 = PackedUserConst.iterator(buf.items, testShell);
|
||||
|
|
Loading…
Reference in New Issue