-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstring.elsa
More file actions
55 lines (43 loc) · 935 Bytes
/
string.elsa
File metadata and controls
55 lines (43 loc) · 935 Bytes
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
struct String {
char[] str;
fn CharAt(int i) : char {
return str[i];
}
fn Length() : int {
return str.Length();
}
fn Concat(String other) : String {
var newStr = new String {
str: new char[Length() + other.Length()]
};
for(var i = 0; i < Length(); i++) {
newStr.str.Push(str[i]);
}
for(var i = 0; i < other.Length(); i++) {
newStr.str.Push(other.str[i]);
}
return newStr;
}
fn Substring(int startIndex, int length) : String {
var newStr = new String {
str: new char[length]
};
// TODO: Error checking, validate start and length
int stopIndex = startIndex + length;
for(var i = startIndex; i < stopIndex; i++) {
newStr.str.Push(str[i]);
}
return newStr;
}
fn Equals(String other) : bool {
if(Length() != other.Length()) {
return false;
}
for(var i = 0; i < Length(); i++) {
if(str[i] != other.str[i]) {
return false;
}
}
return true;
}
};