504 lines
12 KiB
TypeScript
Raw Normal View History

2018-01-28 06:18:27 +01:00
// singleton empty string
const EMPTY: String = changetype<String>("");
2018-01-28 06:18:27 +01:00
// number of bytes preceeding string data
const HEADER_SIZE: usize = 4;
function allocate(length: i32): String {
2018-01-28 06:18:27 +01:00
assert(length > 0); // 0 -> EMPTY
var ptr = allocate_memory(HEADER_SIZE + (<usize>length << 1));
store<i32>(ptr, length);
return changetype<String>(ptr);
}
@sealed
2017-12-16 17:54:53 +01:00
export class String {
readonly length: i32; // capped to [0, 0x7fffffff]
2018-01-06 10:20:38 +01:00
@operator("[]")
charAt(pos: i32): String {
2018-01-28 06:18:27 +01:00
assert(this != null);
if (<u32>pos >= <u32>this.length) {
return EMPTY;
2018-02-25 23:21:32 +01:00
}
2018-01-28 06:18:27 +01:00
var out = allocate(1);
store<u16>(
changetype<usize>(out),
2018-01-28 06:18:27 +01:00
load<u16>(
changetype<usize>(this) + (<usize>pos << 1),
HEADER_SIZE
2018-01-28 06:18:27 +01:00
),
HEADER_SIZE
);
return out;
}
charCodeAt(pos: i32): i32 {
2018-01-28 06:18:27 +01:00
assert(this != null);
if (<u32>pos >= <u32>this.length) {
2018-01-28 06:18:27 +01:00
return -1; // (NaN)
2018-02-25 23:21:32 +01:00
}
2018-01-28 06:18:27 +01:00
return load<u16>(
changetype<usize>(this) + (<usize>pos << 1),
HEADER_SIZE
2018-01-28 06:18:27 +01:00
);
}
codePointAt(pos: i32): i32 {
2018-01-28 06:18:27 +01:00
assert(this != null);
if (<u32>pos >= <u32>this.length) {
2018-01-28 06:18:27 +01:00
return -1; // (undefined)
2018-02-25 23:21:32 +01:00
}
2018-01-28 06:18:27 +01:00
var first = <i32>load<u16>(
changetype<usize>(this) + (<usize>pos << 1),
HEADER_SIZE
2018-01-28 06:18:27 +01:00
);
2018-02-25 23:21:32 +01:00
if (first < 0xD800 || first > 0xDBFF || pos + 1 == this.length) {
return first;
2018-02-25 23:21:32 +01:00
}
2018-01-28 06:18:27 +01:00
var second = <i32>load<u16>(
changetype<usize>(this) + ((<usize>pos + 1) << 1),
HEADER_SIZE
2018-01-28 06:18:27 +01:00
);
2018-02-25 23:21:32 +01:00
if (second < 0xDC00 || second > 0xDFFF) {
return first;
2018-02-25 23:21:32 +01:00
}
return ((first - 0xD800) << 10) + (second - 0xDC00) + 0x10000;
}
@operator("+")
2018-01-28 06:18:27 +01:00
private static __concat(left: String, right: String): String {
if (!changetype<usize>(left)) left = changetype<String>("null");
2018-01-28 06:18:27 +01:00
return left.concat(right);
}
concat(other: String): String {
assert(this != null);
if (other == null) other = changetype<String>("null");
var thisLen: isize = this.length;
var otherLen: isize = other.length;
2018-01-28 06:18:27 +01:00
var outLen: usize = thisLen + otherLen;
if (outLen == 0) return EMPTY;
2018-01-28 06:18:27 +01:00
var out = allocate(outLen);
move_memory(
changetype<usize>(out) + HEADER_SIZE,
changetype<usize>(this) + HEADER_SIZE,
thisLen << 1
);
move_memory(
changetype<usize>(out) + HEADER_SIZE + (thisLen << 1),
changetype<usize>(other) + HEADER_SIZE,
otherLen << 1
);
return out;
}
endsWith(searchString: String, endPosition: i32 = 0x7fffffff): bool {
2018-01-28 06:18:27 +01:00
assert(this != null);
2018-02-25 23:21:32 +01:00
if (searchString == null) {
2018-01-28 06:18:27 +01:00
return false;
2018-02-25 23:21:32 +01:00
}
2018-01-28 06:18:27 +01:00
var end: isize = <isize>min(max(endPosition, 0), this.length);
var searchLength: isize = searchString.length;
var start: isize = end - searchLength;
2018-02-25 23:21:32 +01:00
if (start < 0) {
return false;
2018-02-25 23:21:32 +01:00
}
return !compare_memory(
changetype<usize>(this) + HEADER_SIZE + (start << 1),
changetype<usize>(searchString) + HEADER_SIZE,
searchLength << 1
);
}
@operator("==")
2018-01-28 06:18:27 +01:00
private static __eq(left: String, right: String): bool {
if (!changetype<usize>(left)) return !changetype<usize>(right);
else if (!changetype<usize>(right)) return false;
2018-01-28 06:18:27 +01:00
var leftLength = left.length;
if (leftLength != right.length) return false;
return !compare_memory(
changetype<usize>(left) + HEADER_SIZE,
changetype<usize>(right) + HEADER_SIZE,
2018-01-28 06:18:27 +01:00
(<usize>leftLength << 1)
);
}
includes(searchString: String, position: i32 = 0): bool {
return this.indexOf(searchString, position) != -1;
}
indexOf(searchString: String, position: i32 = 0): i32 {
2018-01-28 06:18:27 +01:00
assert(this != null);
2018-02-25 23:21:32 +01:00
if (searchString == null) {
2018-01-28 06:18:27 +01:00
searchString = changetype<String>("null");
2018-02-25 23:21:32 +01:00
}
var pos: isize = position;
var len: isize = this.length;
var start: isize = min<isize>(max<isize>(pos, 0), len);
2018-01-28 06:18:27 +01:00
var searchLen: isize = <isize>searchString.length;
// TODO: two-way, multiple char codes
for (let k: usize = start; <isize>k + searchLen <= len; ++k) {
if (!compare_memory(
changetype<usize>(this) + HEADER_SIZE + (k << 1),
changetype<usize>(searchString) + HEADER_SIZE,
searchLen << 1)
2018-02-25 23:21:32 +01:00
) {
return <i32>k;
2018-02-25 23:21:32 +01:00
}
}
return -1;
}
startsWith(searchString: String, position: i32 = 0): bool {
assert(this != null);
2018-02-25 23:21:32 +01:00
if (searchString == null) {
2018-01-28 06:18:27 +01:00
searchString = changetype<String>("null");
2018-02-25 23:21:32 +01:00
}
var pos: isize = position;
var len: isize = this.length;
2018-02-25 23:21:32 +01:00
var start: isize = min<isize>(max<isize>(pos, 0), len);
2018-01-28 06:18:27 +01:00
var searchLength: isize = <isize>searchString.length;
2018-02-25 23:21:32 +01:00
if (searchLength + start > len) {
return false;
2018-02-25 23:21:32 +01:00
}
return !compare_memory(
changetype<usize>(this) + HEADER_SIZE + (start << 1),
changetype<usize>(searchString) + HEADER_SIZE,
searchLength << 1
);
}
substr(start: i32, length: i32 = i32.MAX_VALUE): String {
assert(this != null);
var intStart: isize = start;
var end: isize = length;
var size: isize = this.length;
2018-02-25 23:21:32 +01:00
if (intStart < 0) {
intStart = max<isize>(size + intStart, 0);
2018-02-25 23:21:32 +01:00
}
var resultLength: isize = min<isize>(max<isize>(end, 0), size - intStart);
2018-02-25 23:21:32 +01:00
if (resultLength <= 0) {
return EMPTY;
2018-02-25 23:21:32 +01:00
}
var out = allocate(resultLength);
move_memory(
changetype<usize>(out) + HEADER_SIZE,
changetype<usize>(this) + HEADER_SIZE + (intStart << 1),
<usize>resultLength << 1
);
return out;
}
substring(start: i32, end: i32 = i32.MAX_VALUE): String {
assert(this != null);
var len = this.length;
var finalStart = min<i32>(max<i32>(start, 0), len);
var finalEnd = min<i32>(max<i32>(end, 0), len);
var from = min<i32>(finalStart, finalEnd);
var to = max<i32>(finalStart, finalEnd);
len = to - from;
2018-02-25 23:21:32 +01:00
if (!len) {
return EMPTY;
2018-02-25 23:21:32 +01:00
}
if (!from && to == this.length) {
return this;
2018-02-25 23:21:32 +01:00
}
var out = allocate(len);
move_memory(
changetype<usize>(out) + HEADER_SIZE,
changetype<usize>(this) + HEADER_SIZE + (from << 1),
len << 1
);
return out;
}
trim(): String {
assert(this != null);
var length: usize = this.length;
2018-02-25 23:21:32 +01:00
while (
length &&
isWhiteSpaceOrLineTerminator(
load<u16>(changetype<usize>(this) + (length << 1), HEADER_SIZE)
2018-02-25 23:21:32 +01:00
)
) {
--length;
2018-02-25 23:21:32 +01:00
}
var start: usize = 0;
2018-02-25 23:21:32 +01:00
while (
start < length &&
isWhiteSpaceOrLineTerminator(
load<u16>(changetype<usize>(this) + (start << 1), HEADER_SIZE)
2018-02-25 23:21:32 +01:00
)
) {
2018-01-28 06:18:27 +01:00
++start, --length;
2018-02-25 23:21:32 +01:00
}
if (!length) {
return EMPTY;
2018-02-25 23:21:32 +01:00
}
if (!start && length == this.length) {
return this;
2018-02-25 23:21:32 +01:00
}
var out = allocate(length);
move_memory(
changetype<usize>(out) + HEADER_SIZE,
changetype<usize>(this) + HEADER_SIZE + (start << 1),
length << 1
);
return out;
}
trimLeft(): String {
assert(this != null);
var start: isize = 0;
var len: isize = this.length;
2018-02-25 23:21:32 +01:00
while (
start < len &&
isWhiteSpaceOrLineTerminator(
load<u16>(changetype<usize>(this) + (start << 1), HEADER_SIZE)
2018-02-25 23:21:32 +01:00
)
) {
++start;
2018-02-25 23:21:32 +01:00
}
if (!start) {
return this;
2018-02-25 23:21:32 +01:00
}
var outLen = len - start;
2018-02-25 23:21:32 +01:00
if (!outLen) {
return EMPTY;
2018-02-25 23:21:32 +01:00
}
var out = allocate(outLen);
move_memory(
changetype<usize>(out) + HEADER_SIZE,
changetype<usize>(this) + HEADER_SIZE + (start << 1),
outLen << 1
);
return out;
}
trimRight(): String {
assert(this != null);
var len: isize = this.length;
2018-02-25 23:21:32 +01:00
while (
len > 0 &&
isWhiteSpaceOrLineTerminator(
load<u16>(changetype<usize>(this) + (len << 1), HEADER_SIZE)
2018-02-25 23:21:32 +01:00
)
) {
--len;
2018-02-25 23:21:32 +01:00
}
if (len <= 0) {
return EMPTY;
2018-02-25 23:21:32 +01:00
}
if (<i32>len == this.length) {
return this;
2018-02-25 23:21:32 +01:00
}
var out = allocate(len);
move_memory(
changetype<usize>(out) + HEADER_SIZE,
changetype<usize>(this) + HEADER_SIZE,
len << 1
);
return out;
}
}
function isWhiteSpaceOrLineTerminator(c: u16): bool {
switch (c) {
case 10: // <LF>
case 13: // <CR>
case 8232: // <LS>
case 8233: // <PS>
case 9: // <TAB>
case 11: // <VT>
case 12: // <FF>
case 32: // <SP>
case 160: // <NBSP>
case 65279: { // <ZWNBSP>
return true;
}
default: return false;
}
2017-12-16 17:54:53 +01:00
}
2018-01-28 23:42:55 +01:00
const enum CharCode {
PLUS = 0x2B,
MINUS = 0x2D,
2018-01-29 07:42:40 +01:00
DOT = 0x2E,
2018-01-28 23:42:55 +01:00
_0 = 0x30,
_1 = 0x31,
_2 = 0x32,
_3 = 0x33,
_4 = 0x34,
_5 = 0x35,
_6 = 0x36,
_7 = 0x37,
_8 = 0x38,
_9 = 0x39,
A = 0x41,
B = 0x42,
2018-01-29 07:42:40 +01:00
E = 0x45,
2018-01-28 23:42:55 +01:00
O = 0x4F,
X = 0x58,
Z = 0x5a,
a = 0x61,
b = 0x62,
2018-01-29 07:42:40 +01:00
e = 0x65,
2018-01-28 23:42:55 +01:00
o = 0x6F,
x = 0x78,
z = 0x7A
}
export function parseInt(str: String, radix: i32 = 0): f64 {
return parse<f64>(str, radix);
}
export function parseI32(str: String, radix: i32 = 0): i32 {
return parse<i32>(str, radix);
}
export function parseI64(str: String, radix: i32 = 0): i64 {
return parse<i64>(str, radix);
}
function parse<T>(str: String, radix: i32 = 0): T {
var len: i32 = str.length;
2018-02-25 23:21:32 +01:00
if (!len) {
return <T>NaN;
2018-02-25 23:21:32 +01:00
}
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
var code = <i32>load<u16>(ptr, HEADER_SIZE);
2018-01-28 23:42:55 +01:00
// determine sign
var sign: T;
2018-01-28 23:42:55 +01:00
if (code == CharCode.MINUS) {
2018-02-25 23:21:32 +01:00
if (!--len) {
return <T>NaN;
2018-02-25 23:21:32 +01:00
}
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
2018-01-28 23:42:55 +01:00
sign = -1;
} else if (code == CharCode.PLUS) {
2018-02-25 23:21:32 +01:00
if (!--len) {
return <T>NaN;
2018-02-25 23:21:32 +01:00
}
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
2018-01-28 23:42:55 +01:00
sign = 1;
2018-02-25 23:21:32 +01:00
} else {
2018-01-28 23:42:55 +01:00
sign = 1;
2018-02-25 23:21:32 +01:00
}
2018-01-28 23:42:55 +01:00
// determine radix
if (!radix) {
if (code == CharCode._0 && len > 2) {
switch (<i32>load<u16>(ptr + 2, HEADER_SIZE)) {
2018-01-28 23:42:55 +01:00
case CharCode.B:
case CharCode.b: {
2018-01-28 23:42:55 +01:00
ptr += 4; len -= 2;
radix = 2;
break;
}
2018-01-28 23:42:55 +01:00
case CharCode.O:
case CharCode.o: {
2018-01-28 23:42:55 +01:00
ptr += 4; len -= 2;
radix = 8;
break;
}
2018-01-28 23:42:55 +01:00
case CharCode.X:
case CharCode.x: {
2018-01-28 23:42:55 +01:00
ptr += 4; len -= 2;
radix = 16;
break;
}
default: {
2018-01-28 23:42:55 +01:00
radix = 10;
}
2018-01-28 23:42:55 +01:00
}
} else radix = 10;
2018-02-25 23:21:32 +01:00
} else if (radix < 2 || radix > 36) {
return <T>NaN;
2018-02-25 23:21:32 +01:00
}
2018-01-28 23:42:55 +01:00
// calculate value
var num: T = 0;
2018-01-28 23:42:55 +01:00
while (len--) {
code = <i32>load<u16>(ptr, HEADER_SIZE);
2018-02-25 23:21:32 +01:00
if (code >= CharCode._0 && code <= CharCode._9) {
2018-01-28 23:42:55 +01:00
code -= CharCode._0;
2018-02-25 23:21:32 +01:00
} else if (code >= CharCode.A && code <= CharCode.Z) {
2018-01-28 23:42:55 +01:00
code -= CharCode.A - 10;
2018-02-25 23:21:32 +01:00
} else if (code >= CharCode.a && code <= CharCode.z) {
2018-01-28 23:42:55 +01:00
code -= CharCode.a - 10;
2018-02-25 23:21:32 +01:00
} else {
2018-01-29 07:42:40 +01:00
break;
2018-02-25 23:21:32 +01:00
}
if (code >= radix) {
2018-01-29 07:42:40 +01:00
break;
2018-02-25 23:21:32 +01:00
}
2018-01-28 23:42:55 +01:00
num = (num * radix) + code;
ptr += 2;
}
return sign * num;
}
export function parseFloat(str: String): f64 {
2018-01-29 07:42:40 +01:00
var len: i32 = str.length;
2018-02-25 23:21:32 +01:00
if (!len) {
2018-01-29 07:42:40 +01:00
return NaN;
2018-02-25 23:21:32 +01:00
}
2018-01-29 07:42:40 +01:00
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
var code = <i32>load<u16>(ptr, HEADER_SIZE);
2018-01-29 07:42:40 +01:00
// determine sign
var sign: f64;
if (code == CharCode.MINUS) {
2018-02-25 23:21:32 +01:00
if (!--len) {
2018-01-29 07:42:40 +01:00
return NaN;
2018-02-25 23:21:32 +01:00
}
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
2018-01-29 07:42:40 +01:00
sign = -1;
} else if (code == CharCode.PLUS) {
2018-02-25 23:21:32 +01:00
if (!--len) {
2018-01-29 07:42:40 +01:00
return NaN;
2018-02-25 23:21:32 +01:00
}
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
2018-01-29 07:42:40 +01:00
sign = 1;
2018-02-25 23:21:32 +01:00
} else {
2018-01-29 07:42:40 +01:00
sign = 1;
2018-02-25 23:21:32 +01:00
}
2018-01-29 07:42:40 +01:00
// calculate value
var num: f64 = 0;
while (len--) {
code = <i32>load<u16>(ptr, HEADER_SIZE);
2018-01-29 07:42:40 +01:00
if (code == CharCode.DOT) {
ptr += 2;
let fac: f64 = 0.1; // precision :(
2018-01-29 07:42:40 +01:00
while (len--) {
code = <i32>load<u16>(ptr, HEADER_SIZE);
2018-02-25 23:21:32 +01:00
if (code == CharCode.E || code == CharCode.e) {
2018-01-29 07:42:40 +01:00
assert(false); // TODO
2018-02-25 23:21:32 +01:00
}
2018-01-29 07:42:40 +01:00
code -= CharCode._0;
2018-02-25 23:21:32 +01:00
if (<u32>code > 9) {
2018-01-29 07:42:40 +01:00
break;
2018-02-25 23:21:32 +01:00
}
2018-01-29 07:42:40 +01:00
num += <f64>code * fac;
fac *= 0.1;
ptr += 2;
}
break;
}
code -= CharCode._0;
2018-02-25 23:21:32 +01:00
if (<u32>code >= 10) {
2018-01-29 07:42:40 +01:00
break;
2018-02-25 23:21:32 +01:00
}
2018-01-29 07:42:40 +01:00
num = (num * 10) + code;
ptr += 2;
}
return sign * num;
}