-
Notifications
You must be signed in to change notification settings - Fork 0
/
dodgy.mojo
74 lines (51 loc) · 1.78 KB
/
dodgy.mojo
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from list_iterator import ListIterator
from memory import memcpy
@value
@register_passable("trivial")
struct DodgyString:
"""
A string that is dodgy because it is not null-terminated.
"""
var data: Pointer[Int8]
var size: Int
fn __init__(value: StringLiteral) -> DodgyString:
let l = len(value)
let s = String(value)
let p = Pointer[Int8].alloc(l)
for i in range(l):
p.store(i, s._buffer[i])
return DodgyString(p, l)
fn __init__(value: String) -> DodgyString:
let l = len(value)
let p = Pointer[Int8].alloc(l)
for i in range(l):
p.store(i, value._buffer[i])
return DodgyString(p, l)
fn __init__(value: StringRef) -> DodgyString:
let l = len(value)
let s = String(value)
let p = Pointer[Int8].alloc(l)
for i in range(l):
p.store(i, s._buffer[i])
return DodgyString(p, l)
fn __eq__(self, other: DodgyString) -> Bool:
if self.size != other.size:
return False
for i in range(self.size):
if self.data.load(i) != other.data.load(i):
return False
return True
fn __ne__(self, other: DodgyString) -> Bool:
return not self.__eq__(other)
fn __iter__(self) -> ListIterator[Int8]:
return ListIterator[Int8](self.data, self.size)
fn to_string(self) -> String:
let ptr = Pointer[Int8]().alloc(self.size)
memcpy(ptr, self.data, self.size)
return String(ptr, self.size)
fn to_string_ref(self) -> StringRef:
let ptr = Pointer[Int8]().alloc(self.size)
memcpy(ptr, self.data, self.size)
return StringRef(
ptr.bitcast[__mlir_type.`!pop.scalar<si8>`]().address, self.size
)