Type ‣ Array ᴾᴴᴾ - chung-leong/zigar GitHub Wiki

JavaScript | PHP


In the Zig language, arrays can contain only a single type of values. Their lengths are fixed at compile time.

const std = @import("std");

pub fn print4(values: [4]i32) void {
    std.debug.print("{any}\n", .{values});
}
<?php

$m = zigar_use(__DIR__ . "/array-example-1.zig");
$m->print4([ 123, 234, 345, 456 ]);
{ 123, 234, 345, 456 }

Array objects support bracket operator and iterator:

pub fn get4(offset: i32) [4]i32 {
    var values: [4]i32 = undefined;
    for (&values, 0..) |*value_ptr, index| {
        value_ptr.* = @as(i32, @intCast(index)) + offset;
    }
    return values;
}
<?php

$m = zigar_use(__DIR__ . '/array-example-2.zig');

$array = $m->get4(80);
echo $array[3], "\n";
foreach ($array as $value) {
    echo $value, "\n";
}
print_r($array);
83
80
81
82
83
[4]i32 Object
(
    [0] => 80
    [1] => 81
    [2] => 82
    [3] => 83
)

Obtaining strings

u8 and u16 arrays have the special property __string, which yields an array's content as UTF-8/16 encoded text.

const std = @import("std");

const hello = "Привіт";

pub fn getHello8() [hello.len]u8 {
    var bytes: [hello.len]u8 = undefined;
    for (&bytes, 0..) |*byte_ptr, index| {
        byte_ptr.* = hello[index];
    }
    return bytes;
}

pub fn getHello16() [hello.len / 2]u16 {
    var codepoints: [hello.len / 2]u16 = undefined;
    var view = std.unicode.Utf8View.initUnchecked(hello);
    var iterator = view.iterator();
    for (&codepoints) |*codepoint_ptr| {
        if (iterator.nextCodepoint()) |cp| {
            codepoint_ptr.* = @truncate(cp);
        }
    }
    return codepoints;
}
<?php

$m = zigar_use(__DIR__ . '/array-example-3.zig');

echo $m->getHello8()->__string, "\n";
echo $m->getHello16()->__string, "\n";
Привіт
Привіт

PHP automatically casts values to string when they're interpolated into a double quoted string. The above example can be written more simply as the following:

<?php

$m = zigar_use(__DIR__ . '/array-example-3.zig');

echo "{$m->getHello8()}\n";
echo "{$m->getHello16()}\n";

Obtaining typed arrays

Arrays of integers and floats also have the special property typedArray, which produces an object mimicking the behavior of JavaScript's typed arrays:

pub fn get4Big(offset: i64) [4]i64 {
    var values: [4]i64 = undefined;
    for (&values, 0..) |*value_ptr, index| {
        value_ptr.* = @as(i64, @intCast(index)) + offset;
    }
    return values;
}
<?php

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

$array = $m->get4Big(50);
print_r($array);
print_r($array->__typed_array);
$array->__typed_array[3] = 1000;
print_r($array);
[4]i64 Object
(
    [0] => 50
    [1] => 51
    [2] => 52
    [3] => 53
)
Int64Array Object
(
    [0] => 50
    [1] => 51
    [2] => 52
    [3] => 53
)
[4]i64 Object
(
    [0] => 50
    [1] => 51
    [2] => 52
    [3] => 1000
)

As can be seen in the above example, changing the typed array also changes the data of the array object.


Types | Vector | Pointer