Polyfill move_memory and set_memory and remove Heap

This commit is contained in:
dcodeIO
2018-01-14 02:30:20 +01:00
parent 2c009c67d3
commit ad469ca445
33 changed files with 8529 additions and 5057 deletions

View File

@ -1,38 +1,37 @@
const EMPTY: String = changetype<String>("");
export class String {
// [key: number]: string;
private ptr: usize;
private __memory: usize;
readonly length: i32;
constructor(ptr: usize, lenght: i32) {
this.ptr = ptr;
this.length = lenght;
constructor(ptr: usize, length: i32) {
if (length < 0)
throw new RangeError("invalid length");
this.__memory = ptr;
this.length = length;
}
@operator("[]")
charAt(pos: i32): String {
assert(this != null);
return pos < 0 || pos >= this.length ? EMPTY
: new String(this.ptr + (<usize>pos << 1), 1);
if (<u32>pos >= this.length)
return changetype<String>("");
return new String(this.__memory + (<usize>pos << 1), 1);
}
charCodeAt(pos: i32): i32 {
assert(this != null);
return pos < 0 || pos >= this.length ? -1 // NaN
: load<u16>(this.ptr + (<usize>pos << 1));
if (<u32>pos >= this.length)
return -1; // NaN
return load<u16>(this.__memory + (<usize>pos << 1));
}
codePointAt(pos: i32): i32 {
assert(this != null);
if (pos < 0 || pos >= this.length)
if (<u32>pos >= this.length)
return -1; // undefined
var first = <i32>load<u16>(this.ptr + (<usize>pos << 1));
var first = <i32>load<u16>(this.__memory + (<usize>pos << 1));
if (first < 0xD800 || first > 0xDBFF || pos + 1 == this.length)
return first;
var second = <i32>load<u16>(this.ptr + ((<usize>pos + 1) << 1));
var second = <i32>load<u16>(this.__memory + ((<usize>pos + 1) << 1));
if (second < 0xDC00 || second > 0xDFFF)
return first;
return ((first - 0xD800) << 10) + (second - 0xDC00) + 0x10000;
@ -45,37 +44,31 @@ export class String {
var thisLen: isize = this.length;
var otherLen: isize = other.length;
var len: usize = thisLen + otherLen;
return new String(
Heap.copy(
Heap.copy(
Heap.allocate(len << 1),
this.ptr,
thisLen << 1
) + (thisLen << 1),
other.ptr,
otherLen << 1
),
<i32>len
);
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, <i32>len);
}
endsWith(searchString: this, endPosition: i32 = 0x7fffffff): bool {
assert(this != null);
assert(searchString != null);
var end: isize = <isize>min<i32>(max<i32>(endPosition, 0), this.length);
var searchLength: isize = searchString.length;
var start: isize = end - searchLength;
if (start < 0)
return false;
return !Heap.compare(this.ptr + (start << 1), searchString.ptr, searchLength << 1);
return !compare_memory(this.__memory + (start << 1), searchString.__memory, searchLength << 1);
}
@operator("==")
private __eq(other: this): bool {
assert(this != null);
assert(other != null);
return this.length != other.length ? false
: !Heap.compare(this.ptr, other.ptr, <usize>this.length);
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, <usize>this.length);
}
includes(searchString: this, position: i32 = 0): bool {
@ -83,14 +76,13 @@ export class String {
}
indexOf(searchString: this, position: i32 = 0): i32 {
assert(this != null);
assert(searchString != null);
var pos: isize = position;
var len: isize = this.length;
var start: isize = min<isize>(max<isize>(pos, 0), len);
var searchLen: isize = searchString.length;
for (var k: usize = start; <isize>k + searchLen <= len; ++k)
if (!Heap.compare(this.ptr + (k << 1), searchString.ptr, searchLen << 1))
if (!compare_memory(this.__memory + (k << 1), searchString.__memory, searchLen << 1))
return <i32>k;
return -1;
}
@ -104,7 +96,7 @@ export class String {
var searchLength: isize = searchString.length;
if (searchLength + start > len)
return false;
return !Heap.compare(this.ptr + (start << 1), searchString.ptr, searchLength << 1);
return !compare_memory(this.__memory + (start << 1), searchString.__memory, searchLength << 1);
}
substr(start: i32, length: i32 = i32.MAX_VALUE): String {
@ -117,7 +109,7 @@ export class String {
var resultLength: isize = min<isize>(max<isize>(end, 0), size - intStart);
if (resultLength < 0)
return EMPTY;
return new String(this.ptr + (intStart << 1), <i32>resultLength);
return new String(this.__memory + (intStart << 1), <i32>resultLength);
}
substring(start: i32, end: i32 = i32.MAX_VALUE): String {
@ -132,46 +124,46 @@ export class String {
return EMPTY;
if (!from && to == this.length)
return this;
return new String(this.ptr + (from << 1), len);
return new String(this.__memory + (from << 1), len);
}
trim(): String {
assert(this != null);
var length: usize = this.length;
while (length && isWhiteSpaceOrLineTerminator(load<u16>(this.ptr + (length << 1))))
while (length && isWhiteSpaceOrLineTerminator(load<u16>(this.__memory + (length << 1))))
--length;
var start: usize = 0;
while (start < length && isWhiteSpaceOrLineTerminator(load<u16>(this.ptr + (start << 1)))) {
while (start < length && isWhiteSpaceOrLineTerminator(load<u16>(this.__memory + (start << 1)))) {
++start; --length;
}
if (!length)
return EMPTY;
if (!start && length == this.length)
return this;
return new String(this.ptr + (start << 1), length);
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<u16>(this.ptr + (start << 1))))
while (start < len && isWhiteSpaceOrLineTerminator(load<u16>(this.__memory + (start << 1))))
++start;
if (!start)
return this;
return new String(this.ptr + (start << 1), <i32>(len - start));
return new String(this.__memory + (start << 1), <i32>(len - start));
}
trimRight(): String {
assert(this != null);
var len: isize = this.length;
while (len > 0 && isWhiteSpaceOrLineTerminator(load<u16>(this.ptr + (len << 1))))
while (len > 0 && isWhiteSpaceOrLineTerminator(load<u16>(this.__memory + (len << 1))))
--len;
if (len <= 0)
return EMPTY;
if (<i32>len == this.length)
return this;
return new String(this.ptr, <i32>len);
return new String(this.__memory, <i32>len);
}
}