zigar.function.Generator(T, allocating).pipe(self, iterator) ᴾᴴᴾ - chung-leong/zigar GitHub Wiki

JavaScript | PHP


Obtain contents from an iterator and send them to the PHP side.

Usage:

const std = @import("std");
const zigar = @import("zigar");

var gpa = std.heap.DebugAllocator(.{}).init;
const allocator = gpa.allocator();

const Generator = zigar.function.Generator(?[]const u8);

pub fn start() !void {
    try zigar.thread.use();
}

pub fn stop() void {
    zigar.thread.end();
}

pub fn splitSequence(text: []const u8, delimiter: []const u8, generator: Generator) !void {
    const thread = try std.Thread.spawn(.{}, generateSequence, .{
        text,
        delimiter,
        generator,
    });
    thread.detach();
}

fn generateSequence(text: []const u8, delimiter: []const u8, generator: Generator) void {
    const iter = std.mem.splitSequence(u8, text, delimiter);
    generator.pipe(iter);
}
<?php

$m = zigar_use(__DIR__ . '/generator-example-4.zig');

try {
    $m->start();
    foreach ($m->splitSequence('hello||world||123||chicken', '||') as $s) {
        echo "$s\n";
    }
} finally {
    $m->stop();
}
hello
world
123
chicken

Arguments:

  • self: @This()
  • iterator: anytype
    An iterator producing T or an error union of one.

Return value:

void


Generator(T, allocating)