/// import { BLOCK, BLOCK_OVERHEAD, BLOCK_MAXSIZE } from "./rt/common"; import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./util/string"; import { E_INVALIDLENGTH } from "./util/error"; import { ArrayBufferView } from "./arraybuffer"; import { idof } from "./builtins"; @sealed export abstract class String { @lazy static readonly MAX_LENGTH: i32 = BLOCK_MAXSIZE >>> alignof(); static fromCharCode(unit: i32, surr: i32 = -1): string { var out: usize; if (~surr) { out = __alloc(4, idof()); store(out, unit); store(out, surr, 2); } else { out = __alloc(2, idof()); store(out, unit); } return changetype(out); // retains } static fromCodePoint(code: i32): string { assert(code <= 0x10FFFF); var sur = code > 0xFFFF; var out = __alloc((i32(sur) + 1) << 1, idof()); if (!sur) { store(out, code); } else { code -= 0x10000; let hi: u32 = (code >>> 10) + 0xD800; let lo: u32 = (code & 0x3FF) + 0xDC00; store(out, (hi << 16) | lo); } return changetype(out); // retains } get length(): i32 { return changetype(changetype(this) - BLOCK_OVERHEAD).rtSize >> 1; } @operator("[]") charAt(pos: i32): String { assert(this !== null); if (pos >= this.length) return changetype(""); var out = __alloc(2, idof()); store(out, load(changetype(this) + (pos << 1))); return changetype(out); // retains } charCodeAt(pos: i32): i32 { if (pos >= this.length) return -1; // (NaN) return load(changetype(this) + (pos << 1)); } codePointAt(pos: i32): i32 { if (pos >= this.length) return -1; // (undefined) var first = load(changetype(this) + (pos << 1)); if (first < 0xD800 || first > 0xDBFF || pos + 1 == this.length) return first; var second = load(changetype(this) + ((pos + 1) << 1)); if (second < 0xDC00 || second > 0xDFFF) return first; return ((first - 0xD800) << 10) + (second - 0xDC00) + 0x10000; } @operator("+") private static __concat(left: String, right: String): String { return select(left, changetype("null"), left !== null).concat(right); } concat(other: String): String { if (other === null) other = changetype("null"); var thisSize: isize = this.length << 1; var otherSize: isize = other.length << 1; var outSize: usize = thisSize + otherSize; if (outSize == 0) return changetype(""); var out = changetype(__alloc(outSize, idof())); // retains memory.copy(changetype(out), changetype(this), thisSize); memory.copy(changetype(out) + thisSize, changetype(other), otherSize); return out; } endsWith(search: String, end: i32 = String.MAX_LENGTH): bool { assert(this !== null); if (search === null) return false; end = min(max(end, 0), this.length); var searchLength = search.length; var searchStart = end - searchLength; if (searchStart < 0) return false; // @ts-ignore: string <-> String return !compareImpl(this, searchStart, search, 0, searchLength); } @operator("==") private static __eq(left: String | null, right: String | null): bool { if (left === right) return true; if (left === null || right === null) return false; var leftLength = left.length; if (leftLength != right.length) return false; // @ts-ignore: string <-> String return !compareImpl(left, 0, right, 0, leftLength); } @operator.prefix("!") private static __not(str: String | null): bool { return str === null || !str.length; } @operator("!=") private static __ne(left: String | null, right: String | null): bool { return !this.__eq(left, right); } @operator(">") private static __gt(left: String | null, right: String | null): bool { if (left === right || left === null || right === null) return false; var leftLength = left.length; var rightLength = right.length; if (!leftLength) return false; if (!rightLength) return true; // @ts-ignore: string <-> String return compareImpl(left, 0, right, 0, min(leftLength, rightLength)) > 0; } @operator(">=") private static __gte(left: String, right: String): bool { return !this.__lt(left, right); } @operator("<") private static __lt(left: String, right: String): bool { if (left === right || left === null || right === null) return false; var leftLength = left.length; var rightLength = right.length; if (!rightLength) return false; if (!leftLength) return true; // @ts-ignore: string <-> String return compareImpl(left, 0, right, 0, min(leftLength, rightLength)) < 0; } @operator("<=") private static __lte(left: String, right: String): bool { return !this.__gt(left, right); } includes(search: String, start: i32 = 0): bool { return this.indexOf(search, start) != -1; } indexOf(search: String, start: i32 = 0): i32 { var searchLen = search.length; if (!searchLen) return 0; var len = this.length; if (!len) return -1; var searchStart = min(max(start, 0), len); for (len -= searchLen; searchStart <= len; ++searchStart) { // @ts-ignore: string <-> String if (!compareImpl(this, searchStart, search, 0, searchLen)) return searchStart; } return -1; } lastIndexOf(search: String, start: i32 = i32.MAX_VALUE): i32 { var searchLen = search.length; if (!searchLen) return this.length; var len = this.length; if (!len) return -1; var searchStart = min(max(start, 0), len - searchLen); for (; searchStart >= 0; --searchStart) { // @ts-ignore: string <-> String if (!compareImpl(this, searchStart, search, 0, searchLen)) return searchStart; } return -1; } startsWith(search: String, start: i32 = 0): bool { assert(this !== null); if (search === null) search = changetype("null"); var len = this.length; var searchStart = min(max(start, 0), len); var searchLength = search.length; if (searchLength + searchStart > len) return false; // @ts-ignore: string <-> String return !compareImpl(this, searchStart, search, 0, searchLength); } substr(start: i32, length: i32 = i32.MAX_VALUE): String { // legacy 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 = min(max(end, 0), size - intStart); if (resultLength <= 0) return changetype(""); var out = __alloc(resultLength << 1, idof()); memory.copy(out, changetype(this) + intStart, resultLength); return changetype(out); // retains } substring(start: i32, end: i32 = i32.MAX_VALUE): String { assert(this !== null); var len: isize = this.length; var finalStart = min(max(start, 0), len); var finalEnd = min(max(end, 0), len); var fromPos = min(finalStart, finalEnd) << 1; var toPos = max(finalStart, finalEnd) << 1; len = toPos - fromPos; if (!len) return changetype(""); if (!fromPos && toPos == this.length << 1) return this; var out = __alloc(len, idof()); memory.copy(out, changetype(this) + fromPos, len); return changetype(out); // retains } trim(): String { assert(this !== null); var length = this.length; var size: usize = length << 1; while ( size && isWhiteSpaceOrLineTerminator( load(changetype(this) + size) ) ) { size -= 2; } var offset: usize = 0; while ( offset < size && isWhiteSpaceOrLineTerminator( load(changetype(this) + offset) ) ) { offset += 2; size -= 2; } if (!size) return changetype(""); if (!start && size == length << 1) return this; var out = __alloc(size, idof()); memory.copy(out, changetype(this) + offset, size); return changetype(out); // retains } @inline trimLeft(): String { return this.trimStart(); } @inline trimRight(): String { return this.trimEnd(); } trimStart(): String { assert(this !== null); var size = this.length << 1; var offset: usize = 0; while ( offset < size && isWhiteSpaceOrLineTerminator( load(changetype(this) + offset) ) ) { offset += 2; } if (!offset) return this; size -= offset; if (!size) return changetype(""); var out = __alloc(size, idof()); memory.copy(out, changetype(this) + offset, size); return changetype(out); // retains } trimEnd(): String { assert(this !== null); var originalSize = this.length << 1; var size = originalSize; while ( size && isWhiteSpaceOrLineTerminator( load(changetype(this) + size) ) ) { size -= 2; } if (!size) return changetype(""); if (size == originalSize) return this; var out = __alloc(size, idof()); memory.copy(out, changetype(this), size); return changetype(out); // retains } padStart(length: i32, pad: string = " "): String { assert(this !== null); var thisSize = this.length << 1; var targetSize = length << 1; var padSize = pad.length << 1; if (targetSize < thisSize || !padSize) return this; var prependSize = targetSize - thisSize; var out = __alloc(targetSize, idof()); if (prependSize > padSize) { let repeatCount = (prependSize - 2) / padSize; let restBase = repeatCount * padSize; let restSize = prependSize - restBase; memory.repeat(out, changetype(pad), padSize, repeatCount); memory.copy(out + restBase, changetype(pad), restSize); } else { memory.copy(out, changetype(pad), prependSize); } memory.copy(out + prependSize, changetype(this), thisSize); return changetype(out); // retains } padEnd(length: i32, pad: string = " "): String { assert(this !== null); var thisSize = this.length << 1; var targetSize = length << 1; var padSize = pad.length << 1; if (targetSize < thisSize || !padSize) return this; var appendSize = targetSize - thisSize; var out = __alloc(targetSize, idof()); memory.copy(out, changetype(this), thisSize); if (appendSize > padSize) { let repeatCount = (appendSize - 2) / padSize; let restBase = repeatCount * padSize; let restSize = appendSize - restBase; memory.repeat(out + thisSize, changetype(pad), padSize, repeatCount); memory.copy(out + thisSize + restBase, changetype(pad), restSize); } else { memory.copy(out + thisSize, changetype(pad), appendSize); } return changetype(out); // retains } repeat(count: i32 = 0): String { assert(this !== null); var length = this.length; // Most browsers can't handle strings 1 << 28 chars or longer if (count < 0 || length * count > (1 << 28)) { throw new RangeError(E_INVALIDLENGTH); } if (count == 0 || !length) return changetype(""); if (count == 1) return this; var out = __alloc((length * count) << 1, idof()); memory.repeat(out, changetype(this), length << 1, count); return changetype(out); // retains } slice(start: i32, end: i32 = i32.MAX_VALUE): String { var len = this.length; start = start < 0 ? max(start + len, 0) : min(start, len); end = end < 0 ? max(end + len, 0) : min(end, len); len = end - start; if (len <= 0) return changetype(""); var out = __alloc(len << 1, idof()); memory.copy(out, changetype(this) + (start << 1), len << 1); return changetype(out); // retains } split(separator: String | null = null, limit: i32 = i32.MAX_VALUE): String[] { assert(this !== null); if (!limit) return changetype>(__allocArray(0, alignof(), idof>())); // retains if (separator === null) return [this]; var length: isize = this.length; var sepLen: isize = separator.length; if (limit < 0) limit = i32.MAX_VALUE; if (!sepLen) { if (!length) return changetype>(__allocArray(0, alignof(), idof>())); // retains // split by chars length = min(length, limit); let result = __allocArray(length, alignof(), idof>()); let resultStart = changetype(result).dataStart; for (let i: isize = 0; i < length; ++i) { let charStr = __alloc(2, idof()); store(charStr, load(changetype(this) + (i << 1))); store(resultStart + (i << alignof()), charStr); // result[i] = charStr if (isManaged()) __retain(charStr); } return changetype>(result); // retains } else if (!length) { let result = __allocArray(1, alignof(), idof>()); store(changetype(result).dataStart, changetype("")); // static "" return changetype>(result); // retains } var result = changetype>(__allocArray(0, alignof(), idof>())); // retains var end = 0, start = 0, i = 0; while ((end = this.indexOf(separator, start)) != -1) { let len = end - start; if (len > 0) { let out = __alloc(len << 1, idof()); memory.copy(out, changetype(this) + (start << 1), len << 1); result.push(changetype(out)); } else { result.push(changetype("")); } if (++i == limit) return changetype>(result); // retains start = end + sepLen; } if (!start) { // also means: loop above didn't do anything result.push(this); return changetype>(result); // retains } var len = length - start; if (len > 0) { let out = __alloc(len << 1, idof()); memory.copy(out, changetype(this) + (start << 1), len << 1); result.push(changetype(out)); // retains } else { result.push(changetype("")); // static "" } return changetype>(result); // retains // releases result } toString(): String { return this; } get lengthUTF8(): i32 { var len = 1; // null terminated var pos: usize = 0; var end = this.length; while (pos < end) { let c = load(changetype(this) + (pos << 1)); if (c < 128) { len += 1; ++pos; } else if (c < 2048) { len += 2; ++pos; } else { if ( (c & 0xFC00) == 0xD800 && pos + 1 < end && (load(changetype(this) + ((pos + 1) << 1)) & 0xFC00) == 0xDC00 ) { len += 4; pos += 2; } else { len += 3; ++pos; } } } return len; } static fromUTF8(ptr: usize, len: usize): String { if (len < 1) return changetype(""); var ptrPos = 0; var buf = __alloc(len << 1, 0); var bufPos = 0; while (ptrPos < len) { let cp = load(ptr + ptrPos++); if (cp < 128) { store(buf + bufPos, cp); bufPos += 2; } else if (cp > 191 && cp < 224) { assert(ptrPos + 1 <= len); store(buf + bufPos, (cp & 31) << 6 | load(ptr + ptrPos++) & 63); bufPos += 2; } else if (cp > 239 && cp < 365) { assert(ptrPos + 3 <= len); cp = ( (cp & 7) << 18 | (load(ptr + ptrPos++) & 63) << 12 | (load(ptr + ptrPos++) & 63) << 6 | load(ptr + ptrPos++) & 63 ) - 0x10000; store(buf + bufPos, 0xD800 + (cp >> 10)); bufPos += 2; store(buf + bufPos, 0xDC00 + (cp & 1023)); bufPos += 2; } else { assert(ptrPos + 2 <= len); store(buf + bufPos, (cp & 15) << 12 | (load(ptr + ptrPos++) & 63) << 6 | load(ptr + ptrPos++) & 63 ); bufPos += 2; } } assert(ptrPos == len); var out = __alloc(bufPos, idof()); memory.copy(out, buf, bufPos); __free(buf); return changetype(out); // retains } toUTF8(): usize { var buf = __alloc(this.lengthUTF8, 0); var pos: usize = 0; var end = this.length; var off: usize = 0; while (pos < end) { let c1 = load(changetype(this) + (pos << 1)); if (c1 < 128) { store(buf + off, c1); ++off; ++pos; } else if (c1 < 2048) { let ptr = buf + off; store(ptr, c1 >> 6 | 192); store(ptr, c1 & 63 | 128, 1); off += 2; ++pos; } else { let ptr = buf + off; if ((c1 & 0xFC00) == 0xD800 && pos + 1 < end) { let c2 = load(changetype(this) + ((pos + 1) << 1)); if ((c2 & 0xFC00) == 0xDC00) { c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF); store(ptr, c1 >> 18 | 240); store(ptr, c1 >> 12 & 63 | 128, 1); store(ptr, c1 >> 6 & 63 | 128, 2); store(ptr, c1 & 63 | 128, 3); off += 4; pos += 2; continue; } } store(ptr, c1 >> 12 | 224); store(ptr, c1 >> 6 & 63 | 128, 1); store(ptr, c1 & 63 | 128, 2); off += 3; ++pos; } } store(buf + off, 0); return buf; } } // @ts-ignore: nolib export type string = String; export function parseInt(str: String, radix: i32 = 0): f64 { // @ts-ignore: string <-> String return parse(str, radix); } export function parseI32(str: String, radix: i32 = 0): i32 { // @ts-ignore: string <-> String return parse(str, radix); } export function parseI64(str: String, radix: i32 = 0): i64 { // @ts-ignore: string <-> String return parse(str, radix); } // FIXME: naive implementation export function parseFloat(str: String): f64 { var len: i32 = str.length; if (!len) return NaN; var ptr = changetype(str); var code = load(ptr); // determine sign var sign: f64; // trim white spaces while (isWhiteSpaceOrLineTerminator(code)) { code = load(ptr += 2); --len; } if (code == CharCode.MINUS) { if (!--len) return NaN; code = load(ptr += 2); sign = -1; } else if (code == CharCode.PLUS) { if (!--len) return NaN; code = load(ptr += 2); sign = 1; } else { sign = 1; } // calculate value var num: f64 = 0; while (len--) { code = load(ptr); if (code == CharCode.DOT) { ptr += 2; let fac: f64 = 0.1; // precision :( while (len--) { code = load(ptr); if (code == CharCode.E || code == CharCode.e) { assert(false); // TODO } code -= CharCode._0; if (code > 9) break; num += code * fac; fac *= 0.1; ptr += 2; } break; } code -= CharCode._0; if (code >= 10) break; num = (num * 10) + code; ptr += 2; } return sign * num; }