forked from maplelang/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.m
43 lines (34 loc) · 838 Bytes
/
str.m
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
37
38
39
40
41
42
43
/*
* Copyright (c) 2022-2023, Mate Kukri
* SPDX-License-Identifier: GPL-2.0-only
*
* Description: String utilities
*/
import slice::Slice
import vec::Vec
struct Str(vec: Vec<Uint8>)
function (!Str) new() -> Str {
Str(Vec::new())
}
function (s: *mut Str) push(byte: Uint8) {
s.vec.push(byte)
}
struct StrView(slice: Slice<Uint8>)
function (!StrView) from_lit<ArrayType>(a: *ArrayType) -> StrView {
StrView(Slice::from_array(a))
}
function (!StrView) from_str<ArrayType>(s: *Str) -> StrView {
StrView(Slice::from_vec(&s.vec))
}
function (a: StrView) eq(b: StrView) -> Bool {
if a.slice.length != b.slice.length { return false }
let mut i = 0;
while i < a.slice.length {
if *a.slice.at(i) != *b.slice.at(i) { return false }
i += 1;
}
true
}
function (a: StrView) ne(b: StrView) -> Bool {
!a.eq(b)
}