2015-06-09 20:02:02 -04:00
|
|
|
import vector
|
2015-06-28 20:25:27 -04:00
|
|
|
import util
|
2015-06-09 20:02:02 -04:00
|
|
|
import mem
|
2015-03-11 01:58:10 -04:00
|
|
|
|
2015-06-08 21:47:02 -04:00
|
|
|
fun string(in:char*):string {
|
|
|
|
|
var out:string = in
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-05 00:34:24 -04:00
|
|
|
obj string (Object) {
|
2015-05-09 06:24:56 -04:00
|
|
|
var data: vector::vector<char>;
|
|
|
|
|
fun construct(): string* {
|
2015-03-11 01:58:10 -04:00
|
|
|
data.construct();
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2015-05-09 06:24:56 -04:00
|
|
|
fun construct(str: char*): string* {
|
2015-03-11 01:58:10 -04:00
|
|
|
data.construct();
|
|
|
|
|
while(*str) {
|
|
|
|
|
data.addEnd(*str);
|
|
|
|
|
str += 1;
|
|
|
|
|
}
|
2015-06-01 01:43:23 -04:00
|
|
|
// no null terminator
|
2015-03-11 01:58:10 -04:00
|
|
|
return this;
|
|
|
|
|
}
|
2015-06-01 01:43:23 -04:00
|
|
|
fun construct(vec: vector::vector<char>): string* {
|
|
|
|
|
data.copy_construct(&vec);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2015-06-28 20:25:27 -04:00
|
|
|
fun construct(str: string): string* {
|
|
|
|
|
return construct(str.data);
|
|
|
|
|
}
|
2015-06-01 01:43:23 -04:00
|
|
|
|
|
|
|
|
fun copy_construct(old: string*): void {
|
|
|
|
|
data.copy_construct(&old->data)
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-07 19:54:08 -04:00
|
|
|
fun copy_construct(old: char**): void {
|
|
|
|
|
construct(*old)
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-27 18:06:02 -04:00
|
|
|
fun operator=(str: char*): void {
|
|
|
|
|
destruct();
|
|
|
|
|
construct(str)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun operator=(str: string): void {
|
|
|
|
|
destruct();
|
|
|
|
|
data.copy_construct(&str.data)
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 01:43:23 -04:00
|
|
|
fun destruct():void {
|
|
|
|
|
data.destruct()
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-08 21:47:02 -04:00
|
|
|
fun operator[](index: int): char { return data[index]; }
|
2015-06-14 18:13:52 -04:00
|
|
|
fun slice(first: int, second: int): string {
|
|
|
|
|
var new.construct(data.slice(first,second)): string
|
|
|
|
|
return new
|
|
|
|
|
}
|
2015-06-27 11:46:31 -04:00
|
|
|
fun operator[]=(index: int, toSet: char) {
|
|
|
|
|
data[index] = toSet
|
|
|
|
|
}
|
|
|
|
|
fun set(index: int, toSet: char) {
|
|
|
|
|
data.set(index, toSet)
|
|
|
|
|
}
|
2015-06-08 21:47:02 -04:00
|
|
|
fun length():int { return data.size; }
|
|
|
|
|
|
2015-06-28 20:25:27 -04:00
|
|
|
fun operator==(other: string): bool {
|
|
|
|
|
return length() == other.length() && !util::range(length()).any_true(fun(i: int):bool { return data[i] != other[i]; } )
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 01:43:23 -04:00
|
|
|
fun operator+(str: char*): string {
|
|
|
|
|
var newStr.construct(str):string
|
|
|
|
|
var ret.construct(data + newStr.data):string
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-28 20:25:27 -04:00
|
|
|
fun operator+(str: string): string {
|
|
|
|
|
var newStr.construct(str):string
|
|
|
|
|
var ret.construct(data + newStr.data):string
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-04 03:21:36 -04:00
|
|
|
fun operator+=(character: char): void {
|
|
|
|
|
data += character
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 01:43:23 -04:00
|
|
|
fun operator+=(str: char*): void {
|
|
|
|
|
var newStr.construct(str):string
|
|
|
|
|
data += newStr.data
|
|
|
|
|
}
|
2015-03-11 01:58:10 -04:00
|
|
|
|
2015-06-28 20:25:27 -04:00
|
|
|
fun operator+=(str: string): void {
|
|
|
|
|
var newStr.construct(str):string
|
|
|
|
|
data += newStr.data
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-09 06:24:56 -04:00
|
|
|
fun toCharArray(): char* {
|
2015-06-01 01:43:23 -04:00
|
|
|
var out: char* = mem::new<char>(data.size+1);
|
2015-05-09 06:24:56 -04:00
|
|
|
for (var i: int = 0; i < data.size; i++;)
|
2015-03-11 01:58:10 -04:00
|
|
|
out[i] = data.get(i);
|
2015-06-01 01:43:23 -04:00
|
|
|
// null terminator
|
|
|
|
|
out[data.size] = 0
|
2015-03-11 01:58:10 -04:00
|
|
|
return out;
|
|
|
|
|
}
|
2015-07-04 03:21:36 -04:00
|
|
|
|
|
|
|
|
fun lines(): vector::vector<string> {
|
|
|
|
|
var out.construct(): vector::vector<string>
|
|
|
|
|
var current = string("")
|
|
|
|
|
for (var i = 0; i < data.size; i++;) {
|
|
|
|
|
if (data[i] == '\n') {
|
|
|
|
|
out.add(current)
|
|
|
|
|
current = string("")
|
|
|
|
|
} else {
|
|
|
|
|
current += data[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out.add(current)
|
|
|
|
|
return out
|
|
|
|
|
}
|
2015-03-11 01:58:10 -04:00
|
|
|
};
|
|
|
|
|
|