const EMPTY: String = changetype(""); export class String { private __memory: usize; readonly length: i32; constructor(ptr: usize, length: i32) { if (length < 0) throw new RangeError("invalid length"); this.__memory = ptr; this.length = length; } @operator("[]") charAt(pos: i32): String { if (pos >= this.length) return changetype(""); return new String(this.__memory + (pos << 1), 1); } charCodeAt(pos: i32): i32 { if (pos >= this.length) return -1; // NaN return load(this.__memory + (pos << 1)); } codePointAt(pos: i32): i32 { if (pos >= this.length) return -1; // undefined var first = load(this.__memory + (pos << 1)); if (first < 0xD800 || first > 0xDBFF || pos + 1 == this.length) return first; var second = load(this.__memory + ((pos + 1) << 1)); if (second < 0xDC00 || second > 0xDFFF) return first; return ((first - 0xD800) << 10) + (second - 0xDC00) + 0x10000; } @operator("+") concat(other: this): String { assert(this != null); assert(other != null); var thisLen: isize = this.length; var otherLen: isize = other.length; var len: usize = thisLen + otherLen; var newMemory = allocate_memory(len << 1); move_memory(newMemory, this.__memory, thisLen << 1); move_memory(newMemory + (thisLen << 1), other.__memory, otherLen << 1); return new String(newMemory, len); } endsWith(searchString: this, endPosition: i32 = 0x7fffffff): bool { assert(searchString != null); var end: isize = min(max(endPosition, 0), this.length); var searchLength: isize = searchString.length; var start: isize = end - searchLength; if (start < 0) return false; return !compare_memory(this.__memory + (start << 1), searchString.__memory, searchLength << 1); } @operator("==") private __eq(other: this): bool { if (this == null) return other == null; else if (other == null) return false; if (this.length != other.length) return false; return !compare_memory(this.__memory, other.__memory, this.length); } includes(searchString: this, position: i32 = 0): bool { return this.indexOf(searchString, position) != -1; } indexOf(searchString: this, position: i32 = 0): i32 { assert(searchString != null); var pos: isize = position; var len: isize = this.length; var start: isize = min(max(pos, 0), len); var searchLen: isize = searchString.length; for (var k: usize = start; k + searchLen <= len; ++k) if (!compare_memory(this.__memory + (k << 1), searchString.__memory, searchLen << 1)) return k; return -1; } startsWith(searchString: this, position: i32 = 0): bool { assert(this != null); assert(searchString != null); var pos: isize = position; var len: isize = this.length; var start: isize = min(max(position, 0), len); var searchLength: isize = searchString.length; if (searchLength + start > len) return false; return !compare_memory(this.__memory + (start << 1), searchString.__memory, 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; if (intStart < 0) intStart = max(size + intStart, 0); var resultLength: isize = min(max(end, 0), size - intStart); if (resultLength < 0) return EMPTY; return new String(this.__memory + (intStart << 1), resultLength); } substring(start: i32, end: i32 = i32.MAX_VALUE): String { assert(this != null); var len = this.length; var finalStart = min(max(start, 0), len); var finalEnd = min(max(end, 0), len); var from = min(finalStart, finalEnd); var to = max(finalStart, finalEnd); len = to - from; if (!len) return EMPTY; if (!from && to == this.length) return this; return new String(this.__memory + (from << 1), len); } trim(): String { assert(this != null); var length: usize = this.length; while (length && isWhiteSpaceOrLineTerminator(load(this.__memory + (length << 1)))) --length; var start: usize = 0; while (start < length && isWhiteSpaceOrLineTerminator(load(this.__memory + (start << 1)))) { ++start; --length; } if (!length) return EMPTY; if (!start && length == this.length) return this; return new String(this.__memory + (start << 1), length); } trimLeft(): String { assert(this != null); var start: isize = 0; var len: isize = this.length; while (start < len && isWhiteSpaceOrLineTerminator(load(this.__memory + (start << 1)))) ++start; if (!start) return this; return new String(this.__memory + (start << 1), (len - start)); } trimRight(): String { assert(this != null); var len: isize = this.length; while (len > 0 && isWhiteSpaceOrLineTerminator(load(this.__memory + (len << 1)))) --len; if (len <= 0) return EMPTY; if (len == this.length) return this; return new String(this.__memory, len); } } function isWhiteSpaceOrLineTerminator(c: u16): bool { switch (c) { case 10: // case 13: // case 8232: // case 8233: // case 9: // case 11: // case 12: // case 32: // case 160: // case 65279: // return true; default: return false; } } // @binding(CALL, [ STRING, PASS_THRU ], PASS_THRU) export function parseInt(str: string, radix: i32 = 10): f64 { throw new Error("not implemented"); } // @binding(CALL, [ STRING ], PASS_THRU) export function parseFloat(str: string): f64 { throw new Error("not implemented"); }