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-08-26 03:45:34 -04:00
|
|
|
import serialize
|
2016-04-30 15:38:28 -04:00
|
|
|
import io
|
2015-03-11 01:58:10 -04:00
|
|
|
|
2016-04-30 15:38:28 -04:00
|
|
|
fun to_string(in: uchar): string
|
|
|
|
|
return to_string_num(in)
|
|
|
|
|
fun to_string(in: short): string
|
|
|
|
|
return to_string_num(in)
|
|
|
|
|
fun to_string(in: ushort): string
|
|
|
|
|
return to_string_num(in)
|
|
|
|
|
fun to_string(in: int): string
|
|
|
|
|
return to_string_num(in)
|
|
|
|
|
fun to_string(in: uint): string
|
|
|
|
|
return to_string_num(in)
|
|
|
|
|
fun to_string(in: long): string
|
|
|
|
|
return to_string_num(in)
|
|
|
|
|
fun to_string(in: ulong): string
|
|
|
|
|
return to_string_num(in)
|
|
|
|
|
|
2016-05-13 03:10:36 -04:00
|
|
|
fun string_to_int(it: string): int {
|
|
|
|
|
var is_negative = false
|
|
|
|
|
if (it[0] == '-') {
|
|
|
|
|
is_negative = true
|
|
|
|
|
it = it.slice(1,-1)
|
|
|
|
|
}
|
|
|
|
|
var result = 0
|
|
|
|
|
var power = 1
|
|
|
|
|
while (it.length()) {
|
|
|
|
|
result += power * (it.last() - '0')
|
|
|
|
|
it = it.slice(0,-2)
|
|
|
|
|
power *= 10
|
|
|
|
|
}
|
|
|
|
|
if (is_negative)
|
|
|
|
|
return -result
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-30 15:38:28 -04:00
|
|
|
fun to_string_num<T>(in: T): string {
|
|
|
|
|
if (in == 0)
|
|
|
|
|
return string("0")
|
|
|
|
|
var ret = string()
|
|
|
|
|
var pos = 0
|
|
|
|
|
var is_neg = false
|
|
|
|
|
if (in > 0) {
|
|
|
|
|
pos = in
|
|
|
|
|
} else {
|
|
|
|
|
pos = -in
|
|
|
|
|
is_neg = true
|
|
|
|
|
}
|
|
|
|
|
while (pos) {
|
|
|
|
|
ret = string((pos%10 + '0') cast char) + ret
|
|
|
|
|
pos = pos / 10
|
|
|
|
|
}
|
|
|
|
|
if (is_neg)
|
|
|
|
|
return string("-") + ret
|
2015-08-12 23:15:41 -04:00
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 05:14:26 -04:00
|
|
|
fun operator+(first: *char, second: ref string): string {
|
|
|
|
|
return string(first) + second
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-04 17:02:51 -04:00
|
|
|
fun string(in:*char):string {
|
2015-08-04 14:57:56 -04:00
|
|
|
var out.construct(in):string
|
2015-06-08 21:47:02 -04:00
|
|
|
return out
|
|
|
|
|
}
|
2015-08-11 01:07:16 -04:00
|
|
|
fun string(in:char):string {
|
|
|
|
|
var out.construct():string
|
|
|
|
|
out += in
|
|
|
|
|
return out
|
|
|
|
|
}
|
2015-08-26 03:45:34 -04:00
|
|
|
fun string():string {
|
|
|
|
|
return string("")
|
|
|
|
|
}
|
2015-06-08 21:47:02 -04:00
|
|
|
|
2015-08-26 03:45:34 -04:00
|
|
|
obj string (Object, Serializable) {
|
2015-05-09 06:24:56 -04:00
|
|
|
var data: vector::vector<char>;
|
2015-07-04 17:02:51 -04:00
|
|
|
fun construct(): *string {
|
2015-03-11 01:58:10 -04:00
|
|
|
data.construct();
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2015-07-04 17:02:51 -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-08-04 14:57:56 -04:00
|
|
|
fun construct(vec: ref vector::vector<char>): *string {
|
2015-06-01 01:43:23 -04:00
|
|
|
data.copy_construct(&vec);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2015-08-04 14:57:56 -04:00
|
|
|
fun construct(str: ref string): *string {
|
2015-06-28 20:25:27 -04:00
|
|
|
return construct(str.data);
|
|
|
|
|
}
|
2015-06-01 01:43:23 -04:00
|
|
|
|
2015-07-04 17:02:51 -04:00
|
|
|
fun copy_construct(old: *string): void {
|
2015-06-01 01:43:23 -04:00
|
|
|
data.copy_construct(&old->data)
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-04 17:02:51 -04:00
|
|
|
fun copy_construct(old: **char): void {
|
2015-06-07 19:54:08 -04:00
|
|
|
construct(*old)
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-04 17:02:51 -04:00
|
|
|
fun operator=(str: *char): void {
|
2015-06-27 18:06:02 -04:00
|
|
|
destruct();
|
|
|
|
|
construct(str)
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-04 14:57:56 -04:00
|
|
|
fun operator=(str: ref string): void {
|
2016-05-05 04:51:10 -04:00
|
|
|
/*destruct();*/
|
|
|
|
|
/*data.copy_construct(&str.data)*/
|
|
|
|
|
data = str.data
|
2015-06-27 18:06:02 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-01 01:43:23 -04:00
|
|
|
fun destruct():void {
|
|
|
|
|
data.destruct()
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 03:45:34 -04:00
|
|
|
fun serialize(): vector::vector<char> {
|
|
|
|
|
return serialize::serialize(data)
|
|
|
|
|
}
|
|
|
|
|
fun unserialize(it: ref vector::vector<char>, pos: int): int {
|
2015-08-29 21:45:55 -04:00
|
|
|
/*construct()*/
|
|
|
|
|
/*util::unpack(data, pos) = serialize::unserialize<vector::vector<char>>(it, pos)*/
|
|
|
|
|
return data.unserialize(it, pos)
|
2015-08-26 03:45:34 -04:00
|
|
|
}
|
|
|
|
|
|
2015-07-15 00:53:53 -04:00
|
|
|
fun operator[](index: int): ref 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 set(index: int, toSet: char) {
|
|
|
|
|
data.set(index, toSet)
|
|
|
|
|
}
|
2015-06-08 21:47:02 -04:00
|
|
|
fun length():int { return data.size; }
|
|
|
|
|
|
2015-12-06 15:15:33 -05:00
|
|
|
fun operator!=(other: ref string): bool return !(*this ==other)
|
|
|
|
|
fun operator!=(other: *char): bool return !(*this==other)
|
2015-08-04 14:57:56 -04:00
|
|
|
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
|
2015-06-28 20:25:27 -04:00
|
|
|
}
|
2015-07-05 02:34:45 -04:00
|
|
|
fun operator==(other: *char): bool {
|
|
|
|
|
var str.construct(other) : string
|
|
|
|
|
return *this == str
|
|
|
|
|
}
|
2015-06-28 20:25:27 -04:00
|
|
|
|
2016-01-10 18:26:31 -05:00
|
|
|
fun operator+(integer: int): string return *this + to_string(integer);
|
|
|
|
|
|
2015-07-04 17:02:51 -04:00
|
|
|
fun operator+(str: *char): string {
|
2015-06-01 01:43:23 -04:00
|
|
|
var newStr.construct(str):string
|
|
|
|
|
var ret.construct(data + newStr.data):string
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-04 14:57:56 -04:00
|
|
|
fun operator+(str: ref string): string {
|
2016-05-05 04:51:10 -04:00
|
|
|
/*var newStr.construct(str):string*/
|
|
|
|
|
/*var ret.construct(data + newStr.data):string*/
|
|
|
|
|
var ret.construct(data + str.data):string
|
2015-06-28 20:25:27 -04:00
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-10 18:26:31 -05:00
|
|
|
fun operator+=(integer: int) *this += to_string(integer);
|
|
|
|
|
|
2015-07-04 03:21:36 -04:00
|
|
|
fun operator+=(character: char): void {
|
|
|
|
|
data += character
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-04 17:02:51 -04:00
|
|
|
fun operator+=(str: *char): void {
|
2015-06-01 01:43:23 -04:00
|
|
|
var newStr.construct(str):string
|
|
|
|
|
data += newStr.data
|
|
|
|
|
}
|
2015-03-11 01:58:10 -04:00
|
|
|
|
2015-08-04 14:57:56 -04:00
|
|
|
fun operator+=(str: ref string): void {
|
|
|
|
|
//var newStr.construct(str):string
|
|
|
|
|
//data += newStr.data
|
|
|
|
|
data += str.data
|
2015-06-28 20:25:27 -04:00
|
|
|
}
|
|
|
|
|
|
2015-07-04 17:02:51 -04:00
|
|
|
fun toCharArray(): *char {
|
|
|
|
|
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;
|
|
|
|
|
}
|
2016-02-06 23:09:46 -05:00
|
|
|
fun getBackingMemory(): *char return data.getBackingMemory();
|
2015-07-04 03:21:36 -04:00
|
|
|
|
2016-02-05 13:56:29 -05:00
|
|
|
fun split(delim: *char): vector::vector<string> return split(string(delim))
|
|
|
|
|
fun split(delim: string): vector::vector<string> {
|
2015-07-04 03:21:36 -04:00
|
|
|
var out.construct(): vector::vector<string>
|
|
|
|
|
var current = string("")
|
|
|
|
|
for (var i = 0; i < data.size; i++;) {
|
2016-02-05 13:56:29 -05:00
|
|
|
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
|
|
|
|
|
}
|
2016-05-13 03:10:36 -04:00
|
|
|
fun last(): char return data.last()
|
2016-02-05 13:56:29 -05:00
|
|
|
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) {
|
2015-07-04 03:21:36 -04:00
|
|
|
out.add(current)
|
|
|
|
|
current = string("")
|
|
|
|
|
} else {
|
|
|
|
|
current += data[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out.add(current)
|
|
|
|
|
return out
|
|
|
|
|
}
|
2016-05-05 04:51:10 -04:00
|
|
|
fun join(to_join: ref vector::vector<string>): string {
|
2016-03-28 17:12:53 -04:00
|
|
|
var to_ret = to_join.first()
|
|
|
|
|
for (var i = 1; i < to_join.size; i++;)
|
|
|
|
|
to_ret += *this + to_join[i]
|
|
|
|
|
return to_ret
|
|
|
|
|
}
|
2016-02-15 23:12:56 -05:00
|
|
|
fun for_each(func: fun(char):void) {
|
|
|
|
|
data.for_each(func)
|
|
|
|
|
}
|
2015-03-11 01:58:10 -04:00
|
|
|
};
|
|
|
|
|
|