import std::io; import core::swar; # Words of bytes, and the text functions that go by them, give the same results # wherever they run: in either compiler's machine, in an executable, and in # constants worked out while compiling. A mask made of masks by operators is a # mask, with a mask's methods. const text: strview = "Words of bytes:\tsearched eight at a time, 20261016, \"quoted\\n\" and done. \n"; def place(found: maybe): u64 { if found is some(at) { return at; } return 999; } const found_quote: u64 = place(text.find("\"quoted")); const date: u64 = place(swar::load(text, 42).digits()); const spaces: u64 = text.count_of(&' '); def show_mask(name: strview, mask: swar::mask8): void { print($"{name}:"); for k in 0..8u { print((mask & (0x80u << (k * 8))) != 0 ? "1" : "0"); } println($" first {mask.first()} last {mask.last()} count {mask.count()}"); } def main(): void { # Each test of each word of the text. var at: u64 = 0; while at < text.length { val word := swar::load_last(text, at); println($"{at}: {word}"); show_mask(" space", word.equal(' ')); show_mask(" zero", word.zero()); show_mask(" below 0x20", word.below(0x20)); show_mask(" above 'r'", word.above('r')); show_mask(" digit", word.within('0', '9')); show_mask(" ascii", word.ascii()); # Operators keep the mask, so its methods are called on what they give. val letters := word.within('a', 'z') | word.within('A', 'Z'); println($" letters first {letters.first()}, rest first {letters.rest().first()}, not first {(~letters & 0x8080808080808080).first()}"); println($" upper {word.upper()} lower {word.lower()} spread {letters.spread()}"); at += 8; } # The text functions. println($"find {place(text.find("eight"))} {place(text.find("time,"))} {place(text.find("absent"))} {place(text.find("."))}"); println($"find_any {place(text.find_any(",:"))} {place(text.find_any("\t\""))} {place(text.find_any("!?;#@"))}"); println($"index_of {place(text.index_of(&'q'))} last {place(text.last_index_of(&'e'))} count {text.count_of(&'e')}"); println($"ascii {text.is_ascii()} {"caf\xC3\xA9".is_ascii()} upper {text.upper().view().substr(0, 15)} lower {text.lower().view().substr(0, 15)}"); println($"digits {place(swar::load(text, 42).digits())} {place(swar::load(text, 41).digits())}"); println($"constants {found_quote} {date} {spaces}, running {place(text.find("\"quoted"))} {place(swar::load(text, 42).digits())} {text.count_of(&' ')}"); }