Repository navigation
Expand file tree
/
Copy pathlist.mojo
More file actions
74 lines (60 loc) · 2.05 KB
/
Copy pathlist.mojo
File metadata and controls
74 lines (60 loc) · 2.05 KB
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 utils.vector import DynamicVector
from list_iter import ListIter
@value
struct List:
"""This does not have the same behavior as python at all regarding references.
But it helps us code things without having real lists available, and it's memory efficient.
Replace with the list provided by mojo ASAP.
"""
var _internal_string: String
var _end_of_strings: DynamicVector[Int]
fn __init__(inout self):
self._internal_string = ""
self._end_of_strings = DynamicVector[Int]()
fn __init__(inout self, input_value: String):
self._internal_string = ""
self._end_of_strings = DynamicVector[Int]()
for i in range(len(input_value)):
self.append(input_value[i])
fn __copyinit__(inout self, other: Self):
self._internal_string = other._internal_string
self._end_of_strings = other._end_of_strings
fn append(inout self, value: String):
self._internal_string += value
self._end_of_strings.push_back(len(self._internal_string))
fn __getitem__(self, index: Int) raises -> String:
if index >= len(self._end_of_strings):
raise Error("list index out of range")
let start: Int
if index == 0:
start = 0
else:
start = self._end_of_strings[index - 1]
return self._internal_string[start : self._end_of_strings[index]]
fn __getitem__(self: Self, limits: slice) raises -> Self:
var new_list: Self = Self()
for i in range(limits.start, limits.end, limits.step):
new_list.append(self.__getitem__(i))
return new_list
fn __len__(self) -> Int:
return len(self._end_of_strings)
fn size(self) -> Int:
return self.__len__()
fn len(self) -> Int:
return self.__len__()
fn __str__(self: Self) raises -> String:
var result: String = "["
for i in range(self.__len__()):
result += "'" + self.__getitem__(i) + "'"
if i < self.__len__() - 1:
result += ", "
result += "]"
return result
fn __iter__(self: Self) -> ListIter:
return self
fn str(borrowed self) raises -> String:
var result: String = ""
for i in range(self.__len__()):
result += self.__getitem__(i)
if i < self.__len__() - 1: result += "\n"
return result