-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.vek
More file actions
202 lines (186 loc) · 4.23 KB
/
string.vek
File metadata and controls
202 lines (186 loc) · 4.23 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
builtin string {
pub fn to_cstr(self) -> cstr;
pub unsafe fn from_cstr(source: cstr) -> string;
pub inline fn is_empty(self) -> bool {
return self.length() == 0;
}
pub fn starts_with(self, prefix: string) -> bool {
if prefix.length() > self.length() {
return false;
}
let mut i: usize = 0;
while i < prefix.length() {
if !self[i].equals(prefix[i]) {
return false;
}
i += 1;
}
return true;
}
pub fn ends_with(self, suffix: string) -> bool {
if suffix.length() > self.length() {
return false;
}
let offset: usize = self.length() - suffix.length();
let mut i: usize = 0;
while i < suffix.length() {
if !self[offset + i].equals(suffix[i]) {
return false;
}
i += 1;
}
return true;
}
pub fn contains(self, needle: string) -> bool {
if needle.length() > self.length() {
return false;
}
if needle.length() == 0 {
return true;
}
let limit: usize = self.length() - needle.length() + 1;
let mut i: usize = 0;
while i < limit {
let mut j: usize = 0;
let mut matched: bool = true;
while j < needle.length() {
if !self[i + j].equals(needle[j]) {
matched = false;
break;
}
j += 1;
}
if matched {
return true;
}
i += 1;
}
return false;
}
pub fn count(self, needle: string) -> usize {
if needle.length() == 0 {
return 0;
}
if needle.length() > self.length() {
return 0;
}
let limit: usize = self.length() - needle.length() + 1;
let mut count: usize = 0;
let mut i: usize = 0;
while i < limit {
let mut j: usize = 0;
let mut matched: bool = true;
while j < needle.length() {
if !self[i + j].equals(needle[j]) {
matched = false;
break;
}
j += 1;
}
if matched {
count += 1;
i += needle.length();
} else {
i += 1;
}
}
return count;
}
pub fn find(self, needle: string) -> usize? {
if needle.length() == 0 {
return 0;
}
if needle.length() > self.length() {
return null;
}
let limit: usize = self.length() - needle.length() + 1;
let mut i: usize = 0;
while i < limit {
let mut j: usize = 0;
let mut matched: bool = true;
while j < needle.length() {
if !self[i + j].equals(needle[j]) {
matched = false;
break;
}
j += 1;
}
if matched {
return i;
}
i += 1;
}
return null;
}
pub fn replace(mut self, needle: string, replacement: string, amount: usize? = null) -> usize {
if needle.length() == 0 {
return 0;
}
if needle.length() > self.length() {
return 0;
}
if amount != null && amount == 0 {
return 0;
}
let len: usize = self.length();
let needle_len: usize = needle.length();
let mut result: string = "";
let mut count: usize = 0;
let mut i: usize = 0;
while i < len {
let mut matched: bool = false;
if i + needle_len <= len && (amount == null || count < amount) {
let mut j: usize = 0;
matched = true;
while j < needle_len {
if !self[i + j].equals(needle[j]) {
matched = false;
break;
}
j += 1;
}
}
if matched {
result += replacement;
count += 1;
i += needle_len;
} else {
result += self[i];
i += 1;
}
}
self = result;
return count;
}
satisfies Length {
fn length(self) -> usize;
}
satisfies Equal<string> {
fn equals(self, other: string) -> bool;
}
satisfies Order<string> {
fn compare(self, other: string) -> Ordering;
}
satisfies Hash {
fn hash(self) -> u64;
}
satisfies Add<string, string> {
fn add(self, rhs: string) -> string;
}
satisfies Index<usize, string> {
fn index_get(self, index: usize) -> string;
}
satisfies Clone {
fn clone(self) -> string;
}
satisfies Default {
inline fn default() -> Self {
return "";
}
}
satisfies Format {
inline fn format(self) -> string {
return self.clone();
}
}
}