Skip to content

Commit

Permalink
mem.tokenize -> mem.tokenizeScalar
Browse files Browse the repository at this point in the history
  • Loading branch information
jedisct1 committed Jun 23, 2024
1 parent b9ae251 commit 50c5fa9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
19 changes: 13 additions & 6 deletions src/clap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn countParams(str: []const u8) usize {
@setEvalBranchQuota(std.math.maxInt(u32));

var res: usize = 0;
var it = mem.split(u8, str, "\n");
var it = mem.splitScalar(u8, str, '\n');
while (it.next()) |line| {
const trimmed = mem.trimLeft(u8, line, " \t");
if (mem.startsWith(u8, trimmed, "-") or
Expand Down Expand Up @@ -895,8 +895,15 @@ fn Arguments(
comptime value_parsers: anytype,
comptime multi_arg_kind: MultiArgKind,
) type {
var fields: [params.len]builtin.Type.StructField = undefined;
var fields_len: usize = 0;
for (params) |param| {
const longest = param.names.longest();
if (longest.kind == .positional)
continue;
fields_len += 1;
}

var fields: [fields_len]builtin.Type.StructField = undefined;
var i: usize = 0;
for (params) |param| {
const longest = param.names.longest();
Expand Down Expand Up @@ -926,7 +933,7 @@ fn Arguments(

return @Type(.{ .Struct = .{
.layout = .auto,
.fields = fields[0..i],
.fields = &fields,
.decls = &.{},
.is_tuple = false,
} });
Expand Down Expand Up @@ -1163,7 +1170,7 @@ pub fn help(

var first_line = true;
var res: usize = std.math.maxInt(usize);
var it = mem.tokenize(u8, description, "\n");
var it = mem.tokenizeScalar(u8, description, '\n');
while (it.next()) |line| : (first_line = false) {
const trimmed = mem.trimLeft(u8, line, " ");
const indent = line.len - trimmed.len;
Expand All @@ -1188,7 +1195,7 @@ pub fn help(
};

const description = param.id.description();
var it = mem.split(u8, description, "\n");
var it = mem.splitScalar(u8, description, '\n');
var first_line = true;
var non_emitted_newlines: usize = 0;
var last_line_indentation: usize = 0;
Expand Down Expand Up @@ -1235,7 +1242,7 @@ pub fn help(
try description_writer.newline();
}

var words = mem.tokenize(u8, line, " ");
var words = mem.tokenizeScalar(u8, line, ' ');
while (words.next()) |word|
try description_writer.writeWord(word);

Expand Down
8 changes: 4 additions & 4 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub const Signature = struct {
pub fn decode(child_allocator: mem.Allocator, lines_str: []const u8) !Signature {
var arena = heap.ArenaAllocator.init(child_allocator);
errdefer arena.deinit();
var it = mem.tokenize(u8, lines_str, "\n");
var it = mem.tokenizeScalar(u8, lines_str, '\n');
const untrusted_comment = it.next() orelse return error.InvalidEncoding;
var bin1: [74]u8 = undefined;
try base64.standard.Decoder.decode(&bin1, it.next() orelse return error.InvalidEncoding);
Expand Down Expand Up @@ -99,12 +99,12 @@ pub const PublicKey = struct {
}

pub fn decodeFromSsh(pks: []PublicKey, lines: []const u8) ![]PublicKey {
var lines_it = mem.tokenize(u8, lines, "\n");
var lines_it = mem.tokenizeScalar(u8, lines, '\n');
var i: usize = 0;
while (lines_it.next()) |line| {
var pk = PublicKey{ .key_id = mem.zeroes([8]u8), .key = undefined };

var it = mem.tokenize(u8, line, " ");
var it = mem.tokenizeScalar(u8, line, ' ');
const header = it.next() orelse return error.InvalidEncoding;
if (!mem.eql(u8, key_type, header)) {
return error.InvalidEncoding;
Expand Down Expand Up @@ -138,7 +138,7 @@ pub const PublicKey = struct {
pub fn decode(pks: []PublicKey, lines_str: []const u8) ![]PublicKey {
if (decodeFromSsh(pks, lines_str)) |pks_| return pks_ else |_| {}

var it = mem.tokenize(u8, lines_str, "\n");
var it = mem.tokenizeScalar(u8, lines_str, '\n');
_ = it.next() orelse return error.InvalidEncoding;
const pk = try decodeFromBase64(it.next() orelse return error.InvalidEncoding);
pks[0] = pk;
Expand Down

0 comments on commit 50c5fa9

Please sign in to comment.