Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Type Function

SplitIterator

Public Type Function
std.mem.SplitIterator
pub fn SplitIterator(comptime T: type, comptime delimiter_type: DelimiterType) type

Iterator type for splitting operations, including empty sequences between delimiters.

Fields

buffer: []const T
index: ?usize
delimiter: switch (delimiter_type) { .sequence, .any => []const T, .scalar => T, }

Types

Functions

pub fn first(self: *Self) []const T

Returns a slice of the first field. Call this only to get the first field and then use next to get all subsequent fields. Asserts that iteration has not begun.

pub fn next(self: *Self) ?[]const T

Returns a slice of the next field, or null if splitting is complete.

pub fn peek(self: *const Self) ?[]const T

Returns a slice of the next field, or null if splitting is complete. This method does not alter self.index.

pub fn rest(self: Self) []const T

Returns a slice of the remaining bytes. Does not affect iterator state.

pub fn reset(self: *Self) void

Resets the iterator to the initial slice.

File

mem.zig:3401
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
pub fn SplitIterator(comptime T: type, comptime delimiter_type: DelimiterType) type {
    return struct {
        buffer: []const T,
        index: ?usize,
        delimiter: switch (delimiter_type) {
            .sequence, .any => []const T,
            .scalar => T,
        },

        const Self = @This();

        /// Returns a slice of the first field.
        /// Call this only to get the first field and then use `next` to get all subsequent fields.
        /// Asserts that iteration has not begun.
        pub fn first(self: *Self) []const T {
            assert(self.index.? == 0);
            return self.next().?;
        }

        /// Returns a slice of the next field, or null if splitting is complete.
        pub fn next(self: *Self) ?[]const T {
            const start = self.index orelse return null;
            const end = if (switch (delimiter_type) {
                .sequence => findPos(T, self.buffer, start, self.delimiter),
                .any => findAnyPos(T, self.buffer, start, self.delimiter),
                .scalar => findScalarPos(T, self.buffer, start, self.delimiter),
            }) |delim_start| blk: {
                self.index = delim_start + switch (delimiter_type) {
                    .sequence => self.delimiter.len,
                    .any, .scalar => 1,
                };
                break :blk delim_start;
            } else blk: {
                self.index = null;
                break :blk self.buffer.len;
            };
            return self.buffer[start..end];
        }

        /// Returns a slice of the next field, or null if splitting is complete.
        /// This method does not alter self.index.
        pub fn peek(self: *const Self) ?[]const T {
            const start = self.index orelse return null;
            const end = if (switch (delimiter_type) {
                .sequence => findPos(T, self.buffer, start, self.delimiter),
                .any => findAnyPos(T, self.buffer, start, self.delimiter),
                .scalar => findScalarPos(T, self.buffer, start, self.delimiter),
            }) |delim_start| delim_start else self.buffer.len;
            return self.buffer[start..end];
        }

        /// Returns a slice of the remaining bytes. Does not affect iterator state.
        pub fn rest(self: Self) []const T {
            const end = self.buffer.len;
            const start = self.index orelse end;
            return self.buffer[start..end];
        }

        /// Resets the iterator to the initial slice.
        pub fn reset(self: *Self) void {
            self.index = 0;
        }
    };
}