Modules

Byte_Search

moduleSource
macOSLinuxWindowswasm

#import "Extensions/Byte_Search";

Fast searches for bytes and substrings, with vector instructions. A jaic extension, not official Jai: other Jai compilers have no such module, and it may change between jaic releases. See docs/extensions/byte-search.md.

ExampleOpen in playground ↗
Byte_Search :: #import "Extensions/Byte_Search";

text := "id,name;note\nneedle in a/haystack";
at := Byte_Search.find(text, #char "\n");          // the first newline, or -1
at  = Byte_Search.find_any(text, #char ",", #char ";");
at  = Byte_Search.find(text, "needle");              // the first "needle", or -1
at  = Byte_Search.find_last(text, #char "/");

// A needle searched for many times: its analysis is done once.
finder := Byte_Search.make_finder("needle");
at = Byte_Search.find(*finder, text, start = at + 1);
Show moreShow less

Every search takes start and returns a position in the whole text, or -1. Every search is linear in the text, including substring search with a needle built to defeat it.

Procedures#

find4 overloads
(text: string, byte: u8, start := 0) -> int

The first byte at or after start, or -1.

find_any2 overloads
(text: string, a: u8, b: u8, start := 0) -> int

The first of either byte at or after start, or -1.

find_last3 overloads
(text: string, byte: u8, end := -1) -> int

The last byte before end (-1: the end of the text), or -1.

find_last_any2 overloads
(text: string, a: u8, b: u8, end := -1) -> int

The last of either byte before end, or -1.

(text: string, byte: u8) -> int

How many times byte occurs in the text.

(first: *Byte_Set, second: *Byte_Set, gap: int, text: string, start := 0) -> int

The first i at or after start where text[i] is in first and text[i + gap] is in second, or -1. Two rare-ish bytes at a fixed distance pick out far fewer places than either alone.

(first: *Byte_Set, second: *Byte_Set, third: *Byte_Set, gap_2: int, gap_3: int, text: string, start := 0) -> int

The same with three sets: text[i] in first, text[i + gap_2] in second, text[i + gap_3] in third, with gap_3 the largest gap.

Types#

Byte_Setstruct
:: struct

A set of bytes to search for, any number of them. Make it once with make_byte_set.

Finderstruct
:: struct

A needle prepared for searching. It points into the needle, which must outlive it.