summaryrefslogtreecommitdiff
path: root/src/simd/index_of.zig
blob: cea549b9509b0d6c0084d959bc6008c075e3a763 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const std = @import("std");
const builtin = @import("builtin");
const options = @import("build_options");

extern "c" fn ghostty_simd_index_of(
    needle: u8,
    input: [*]const u8,
    count: usize,
) usize;

pub fn indexOf(input: []const u8, needle: u8) ?usize {
    if (comptime options.simd) {
        const result = ghostty_simd_index_of(needle, input.ptr, input.len);
        return if (result == input.len) null else result;
    }

    return indexOfScalar(input, needle);
}

fn indexOfScalar(input: []const u8, needle: u8) ?usize {
    return std.mem.indexOfScalar(u8, input, needle);
}

test "indexOf" {
    const testing = std.testing;
    try testing.expect(indexOf("hello", ' ') == null);
    try testing.expectEqual(@as(usize, 2), indexOf("hi lo", ' ').?);
    try testing.expectEqual(@as(usize, 5), indexOf(
        \\XXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        \\XXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXXXX
    , ' ').?);
    try testing.expectEqual(@as(usize, 53), indexOf(
        \\XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        \\XXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXXXX
    , ' ').?);
}