185 lines
4.9 KiB
Plaintext
185 lines
4.9 KiB
Plaintext
import vector
|
|
import util
|
|
import mem
|
|
import serialize
|
|
|
|
fun to_string(in: int): string {
|
|
var dest = mem::new<char>(mem::sizeof<int>() * 8)
|
|
defer mem::delete(dest)
|
|
__if_comp__ __C__ {
|
|
simple_passthrough(dest = dest, in = in::) """
|
|
sprintf(dest, "%d", in);
|
|
"""
|
|
}
|
|
var ret = string(dest)
|
|
return ret
|
|
}
|
|
|
|
fun string(in:*char):string {
|
|
var out.construct(in):string
|
|
return out
|
|
}
|
|
fun string(in:char):string {
|
|
var out.construct():string
|
|
out += in
|
|
return out
|
|
}
|
|
fun string():string {
|
|
return string("")
|
|
}
|
|
|
|
obj string (Object, Serializable) {
|
|
var data: vector::vector<char>;
|
|
fun construct(): *string {
|
|
data.construct();
|
|
return this;
|
|
}
|
|
fun construct(str: *char): *string {
|
|
data.construct();
|
|
while(*str) {
|
|
data.addEnd(*str);
|
|
str += 1;
|
|
}
|
|
// no null terminator
|
|
return this;
|
|
}
|
|
fun construct(vec: ref vector::vector<char>): *string {
|
|
data.copy_construct(&vec);
|
|
return this;
|
|
}
|
|
fun construct(str: ref string): *string {
|
|
return construct(str.data);
|
|
}
|
|
|
|
fun copy_construct(old: *string): void {
|
|
data.copy_construct(&old->data)
|
|
}
|
|
|
|
fun copy_construct(old: **char): void {
|
|
construct(*old)
|
|
}
|
|
|
|
fun operator=(str: *char): void {
|
|
destruct();
|
|
construct(str)
|
|
}
|
|
|
|
fun operator=(str: ref string): void {
|
|
destruct();
|
|
data.copy_construct(&str.data)
|
|
}
|
|
|
|
fun destruct():void {
|
|
data.destruct()
|
|
}
|
|
|
|
fun serialize(): vector::vector<char> {
|
|
return serialize::serialize(data)
|
|
}
|
|
fun unserialize(it: ref vector::vector<char>, pos: int): int {
|
|
/*construct()*/
|
|
/*util::unpack(data, pos) = serialize::unserialize<vector::vector<char>>(it, pos)*/
|
|
return data.unserialize(it, pos)
|
|
}
|
|
|
|
fun operator[](index: int): ref char { return data[index]; }
|
|
fun slice(first: int, second: int): string {
|
|
var new.construct(data.slice(first,second)): string
|
|
return new
|
|
}
|
|
fun set(index: int, toSet: char) {
|
|
data.set(index, toSet)
|
|
}
|
|
fun length():int { return data.size; }
|
|
|
|
fun operator!=(other: ref string): bool return !(*this ==other)
|
|
fun operator!=(other: *char): bool return !(*this==other)
|
|
fun operator==(other: ref string): bool {
|
|
// you were too good for this world
|
|
//return length() == other.length() && !util::range(length()).any_true(fun(i: int):bool { return data[i] != other[i]; } )
|
|
if (data.size != other.data.size)
|
|
return false
|
|
for (var i = 0; i < data.size; i++;)
|
|
if (data.data[i] != other.data.data[i])
|
|
return false
|
|
return true
|
|
}
|
|
fun operator==(other: *char): bool {
|
|
var str.construct(other) : string
|
|
return *this == str
|
|
}
|
|
|
|
fun operator+(integer: int): string return *this + to_string(integer);
|
|
|
|
fun operator+(str: *char): string {
|
|
var newStr.construct(str):string
|
|
var ret.construct(data + newStr.data):string
|
|
return ret
|
|
}
|
|
|
|
fun operator+(str: ref string): string {
|
|
var newStr.construct(str):string
|
|
var ret.construct(data + newStr.data):string
|
|
return ret
|
|
}
|
|
|
|
fun operator+=(integer: int) *this += to_string(integer);
|
|
|
|
fun operator+=(character: char): void {
|
|
data += character
|
|
}
|
|
|
|
fun operator+=(str: *char): void {
|
|
var newStr.construct(str):string
|
|
data += newStr.data
|
|
}
|
|
|
|
fun operator+=(str: ref string): void {
|
|
//var newStr.construct(str):string
|
|
//data += newStr.data
|
|
data += str.data
|
|
}
|
|
|
|
fun toCharArray(): *char {
|
|
var out: *char = mem::new<char>(data.size+1);
|
|
for (var i: int = 0; i < data.size; i++;)
|
|
out[i] = data.get(i);
|
|
// null terminator
|
|
out[data.size] = 0
|
|
return out;
|
|
}
|
|
|
|
fun split(delim: *char): vector::vector<string> return split(string(delim))
|
|
fun split(delim: string): vector::vector<string> {
|
|
var out.construct(): vector::vector<string>
|
|
var current = string("")
|
|
for (var i = 0; i < data.size; i++;) {
|
|
if (i < data.size-delim.length() && slice(i, i+delim.length()) == delim) {
|
|
out.add(current)
|
|
current = string("")
|
|
i += delim.length()-1
|
|
} else {
|
|
current += data[i]
|
|
}
|
|
}
|
|
out.add(current)
|
|
return out
|
|
}
|
|
fun lines(): vector::vector<string> return split('\n')
|
|
fun split(delim: char): vector::vector<string> {
|
|
var out.construct(): vector::vector<string>
|
|
var current = string("")
|
|
for (var i = 0; i < data.size; i++;) {
|
|
if (data[i] == delim) {
|
|
out.add(current)
|
|
current = string("")
|
|
} else {
|
|
current += data[i]
|
|
}
|
|
}
|
|
out.add(current)
|
|
return out
|
|
}
|
|
};
|
|
|