raz / sbcl / lib / Bytes.raz

44 lines · 843 B · raw · history · last change 55bc6c0d

1(* Searches of bytes in a []U8. *)
2module Bytes;
3
4(* where b first is in s, or -1 *)
5proc index*(s: []U8; b: U8): INT;
6var i: INT;
7begin
8 for i := 0 to LEN(s) - 1 do
9 if s[i] = b then return i; end;
10 end;
11 return -1;
12end;
13
14(* where b last is in s, or -1 *)
15proc last*(s: []U8; b: U8): INT;
16var i: INT;
17begin
18 for i := LEN(s) - 1 to 0 by -1 do
19 if s[i] = b then return i; end;
20 end;
21 return -1;
22end;
23
24(* how many times b is in s *)
25proc count*(s: []U8; b: U8): INT;
26var i, n: INT;
27begin
28 for i := 0 to LEN(s) - 1 do
29 if s[i] = b then INC(n); end;
30 end;
31 return n;
32end;
33
34(* where sub first starts in s, or -1 *)
35proc find*(s, sub: []U8): INT;
36var i: INT;
37begin
38 for i := 0 to LEN(s) - LEN(sub) do
39 if s[i..i + LEN(sub)] = sub then return i; end;
40 end;
41 return -1;
42end;
43
44end Bytes.