mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-21 10:41:42 +00:00
Implement String#padStart/padEnd; Refactor internal string copy, compare and repeat (#171)
This commit is contained in:
@ -4,7 +4,9 @@ import {
|
||||
EMPTY,
|
||||
clamp,
|
||||
allocate,
|
||||
compareUTF16,
|
||||
compareUnsafe,
|
||||
repeatUnsafe,
|
||||
copyUnsafe,
|
||||
isWhiteSpaceOrLineTerminator,
|
||||
CharCode,
|
||||
parse
|
||||
@ -73,9 +75,8 @@ export class String {
|
||||
|
||||
charCodeAt(pos: i32): i32 {
|
||||
assert(this !== null);
|
||||
if (<u32>pos >= <u32>this.length) {
|
||||
return -1; // (NaN)
|
||||
}
|
||||
if (<u32>pos >= <u32>this.length) return -1; // (NaN)
|
||||
|
||||
return load<u16>(
|
||||
changetype<usize>(this) + (<usize>pos << 1),
|
||||
HEADER_SIZE
|
||||
@ -84,9 +85,8 @@ export class String {
|
||||
|
||||
codePointAt(pos: i32): i32 {
|
||||
assert(this !== null);
|
||||
if (<u32>pos >= <u32>this.length) {
|
||||
return -1; // (undefined)
|
||||
}
|
||||
if (<u32>pos >= <u32>this.length) return -1; // (undefined)
|
||||
|
||||
var first = <i32>load<u16>(
|
||||
changetype<usize>(this) + (<usize>pos << 1),
|
||||
HEADER_SIZE
|
||||
@ -111,24 +111,14 @@ export class String {
|
||||
concat(other: String): String {
|
||||
assert(this !== null);
|
||||
if (other === null) other = changetype<String>("null");
|
||||
|
||||
var thisLen: isize = this.length;
|
||||
var otherLen: isize = other.length;
|
||||
var outLen: usize = thisLen + otherLen;
|
||||
if (outLen == 0) return EMPTY;
|
||||
var out = allocate(outLen);
|
||||
|
||||
memory.copy(
|
||||
changetype<usize>(out) + HEADER_SIZE,
|
||||
changetype<usize>(this) + HEADER_SIZE,
|
||||
thisLen << 1
|
||||
);
|
||||
|
||||
memory.copy(
|
||||
changetype<usize>(out) + HEADER_SIZE + (thisLen << 1),
|
||||
changetype<usize>(other) + HEADER_SIZE,
|
||||
otherLen << 1
|
||||
);
|
||||
|
||||
copyUnsafe(out, 0, this, 0, thisLen);
|
||||
copyUnsafe(out, thisLen, other, 0, otherLen);
|
||||
return out;
|
||||
}
|
||||
|
||||
@ -139,11 +129,7 @@ export class String {
|
||||
var searchLength: isize = searchString.length;
|
||||
var start: isize = end - searchLength;
|
||||
if (start < 0) return false;
|
||||
return !compareUTF16(
|
||||
changetype<usize>(this) + (start << 1),
|
||||
changetype<usize>(searchString),
|
||||
searchLength
|
||||
);
|
||||
return !compareUnsafe(this, start, searchString, 0, searchLength);
|
||||
}
|
||||
|
||||
@operator("==")
|
||||
@ -154,11 +140,7 @@ export class String {
|
||||
var leftLength = left.length;
|
||||
if (leftLength != right.length) return false;
|
||||
|
||||
return !compareUTF16(
|
||||
changetype<usize>(left),
|
||||
changetype<usize>(right),
|
||||
leftLength
|
||||
);
|
||||
return !compareUnsafe(left, 0, right, 0, leftLength);
|
||||
}
|
||||
|
||||
@operator("!=")
|
||||
@ -177,11 +159,7 @@ export class String {
|
||||
if (!rightLength) return true;
|
||||
|
||||
var length = <usize>min<i32>(leftLength, rightLength);
|
||||
return compareUTF16(
|
||||
changetype<usize>(left),
|
||||
changetype<usize>(right),
|
||||
length
|
||||
) > 0;
|
||||
return compareUnsafe(left, 0, right, 0, length) > 0;
|
||||
}
|
||||
|
||||
@operator(">=")
|
||||
@ -196,11 +174,7 @@ export class String {
|
||||
if (!rightLength) return true;
|
||||
|
||||
var length = <usize>min<i32>(leftLength, rightLength);
|
||||
return compareUTF16(
|
||||
changetype<usize>(left),
|
||||
changetype<usize>(right),
|
||||
length
|
||||
) >= 0;
|
||||
return compareUnsafe(left, 0, right, 0, length) >= 0;
|
||||
}
|
||||
|
||||
@operator("<")
|
||||
@ -214,11 +188,7 @@ export class String {
|
||||
if (!leftLength) return true;
|
||||
|
||||
var length = <usize>min<i32>(leftLength, rightLength);
|
||||
return compareUTF16(
|
||||
changetype<usize>(left),
|
||||
changetype<usize>(right),
|
||||
length
|
||||
) < 0;
|
||||
return compareUnsafe(left, 0, right, 0, length) < 0;
|
||||
}
|
||||
|
||||
@operator("<=")
|
||||
@ -233,11 +203,7 @@ export class String {
|
||||
if (!leftLength) return true;
|
||||
|
||||
var length = <usize>min<i32>(leftLength, rightLength);
|
||||
return compareUTF16(
|
||||
changetype<usize>(left),
|
||||
changetype<usize>(right),
|
||||
length
|
||||
) <= 0;
|
||||
return compareUnsafe(left, 0, right, 0, length) <= 0;
|
||||
}
|
||||
|
||||
includes(searchString: String, position: i32 = 0): bool {
|
||||
@ -247,6 +213,7 @@ export class String {
|
||||
indexOf(searchString: String, fromIndex: i32 = 0): i32 {
|
||||
assert(this !== null);
|
||||
if (searchString === null) searchString = changetype<String>("null");
|
||||
|
||||
var searchLen: isize = searchString.length;
|
||||
if (!searchLen) return 0;
|
||||
var len: isize = this.length;
|
||||
@ -254,13 +221,7 @@ export class String {
|
||||
var start = clamp<isize>(fromIndex, 0, len);
|
||||
len -= searchLen;
|
||||
for (let k: isize = start; k <= len; ++k) {
|
||||
if (!compareUTF16(
|
||||
changetype<usize>(this) + (k << 1),
|
||||
changetype<usize>(searchString),
|
||||
searchLen
|
||||
)) {
|
||||
return <i32>k;
|
||||
}
|
||||
if (!compareUnsafe(this, k, searchString, 0, searchLen)) return <i32>k;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@ -268,21 +229,14 @@ export class String {
|
||||
lastIndexOf(searchString: String, fromIndex: i32 = i32.MAX_VALUE): i32 {
|
||||
assert(this !== null);
|
||||
if (searchString === null) searchString = changetype<String>("null");
|
||||
|
||||
var len: isize = this.length;
|
||||
var searchLen: isize = searchString.length;
|
||||
if (!searchLen) return len;
|
||||
if (!len) return -1;
|
||||
var start = clamp<isize>(fromIndex, 0, len - searchLen);
|
||||
|
||||
// TODO: multiple char codes
|
||||
for (let k = start; k >= 0; --k) {
|
||||
if (!compareUTF16(
|
||||
changetype<usize>(this) + (k << 1),
|
||||
changetype<usize>(searchString),
|
||||
searchLen
|
||||
)) {
|
||||
return <i32>k;
|
||||
}
|
||||
if (!compareUnsafe(this, k, searchString, 0, searchLen)) return <i32>k;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@ -296,12 +250,7 @@ export class String {
|
||||
var start = clamp<isize>(pos, 0, len);
|
||||
var searchLength: isize = searchString.length;
|
||||
if (searchLength + start > len) return false;
|
||||
|
||||
return !compareUTF16(
|
||||
changetype<usize>(this) + (start << 1),
|
||||
changetype<usize>(searchString),
|
||||
searchLength
|
||||
);
|
||||
return !compareUnsafe(this, start, searchString, 0, searchLength);
|
||||
}
|
||||
|
||||
substr(start: i32, length: i32 = i32.MAX_VALUE): String {
|
||||
@ -309,19 +258,11 @@ export class String {
|
||||
var intStart: isize = start;
|
||||
var end: isize = length;
|
||||
var size: isize = this.length;
|
||||
if (intStart < 0) {
|
||||
intStart = max<isize>(size + intStart, 0);
|
||||
}
|
||||
if (intStart < 0) intStart = max<isize>(size + intStart, 0);
|
||||
var resultLength = clamp<isize>(end, 0, size - intStart);
|
||||
if (resultLength <= 0) {
|
||||
return EMPTY;
|
||||
}
|
||||
if (resultLength <= 0) return EMPTY;
|
||||
var out = allocate(resultLength);
|
||||
memory.copy(
|
||||
changetype<usize>(out) + HEADER_SIZE,
|
||||
changetype<usize>(this) + HEADER_SIZE + (intStart << 1),
|
||||
<usize>resultLength << 1
|
||||
);
|
||||
copyUnsafe(out, 0, this, intStart, resultLength);
|
||||
return out;
|
||||
}
|
||||
|
||||
@ -333,18 +274,10 @@ export class String {
|
||||
var from = min<i32>(finalStart, finalEnd);
|
||||
var to = max<i32>(finalStart, finalEnd);
|
||||
len = to - from;
|
||||
if (!len) {
|
||||
return EMPTY;
|
||||
}
|
||||
if (!from && to == this.length) {
|
||||
return this;
|
||||
}
|
||||
if (!len) return EMPTY;
|
||||
if (!from && to == this.length) return this;
|
||||
var out = allocate(len);
|
||||
memory.copy(
|
||||
changetype<usize>(out) + HEADER_SIZE,
|
||||
changetype<usize>(this) + HEADER_SIZE + (from << 1),
|
||||
len << 1
|
||||
);
|
||||
copyUnsafe(out, 0, this, from, len);
|
||||
return out;
|
||||
}
|
||||
|
||||
@ -369,18 +302,10 @@ export class String {
|
||||
) {
|
||||
++start, --length;
|
||||
}
|
||||
if (!length) {
|
||||
return EMPTY;
|
||||
}
|
||||
if (!start && length == this.length) {
|
||||
return this;
|
||||
}
|
||||
if (!length) return EMPTY;
|
||||
if (!start && length == this.length) return this;
|
||||
var out = allocate(length);
|
||||
memory.copy(
|
||||
changetype<usize>(out) + HEADER_SIZE,
|
||||
changetype<usize>(this) + HEADER_SIZE + (start << 1),
|
||||
length << 1
|
||||
);
|
||||
copyUnsafe(out, 0, this, start, length);
|
||||
return out;
|
||||
}
|
||||
|
||||
@ -396,19 +321,11 @@ export class String {
|
||||
) {
|
||||
++start;
|
||||
}
|
||||
if (!start) {
|
||||
return this;
|
||||
}
|
||||
if (!start) return this;
|
||||
var outLen = len - start;
|
||||
if (!outLen) {
|
||||
return EMPTY;
|
||||
}
|
||||
if (!outLen) return EMPTY;
|
||||
var out = allocate(outLen);
|
||||
memory.copy(
|
||||
changetype<usize>(out) + HEADER_SIZE,
|
||||
changetype<usize>(this) + HEADER_SIZE + (start << 1),
|
||||
outLen << 1
|
||||
);
|
||||
copyUnsafe(out, 0, this, start, outLen);
|
||||
return out;
|
||||
}
|
||||
|
||||
@ -423,18 +340,50 @@ export class String {
|
||||
) {
|
||||
--len;
|
||||
}
|
||||
if (len <= 0) {
|
||||
return EMPTY;
|
||||
}
|
||||
if (<i32>len == this.length) {
|
||||
return this;
|
||||
}
|
||||
if (len <= 0) return EMPTY;
|
||||
if (<i32>len == this.length) return this;
|
||||
var out = allocate(len);
|
||||
memory.copy(
|
||||
changetype<usize>(out) + HEADER_SIZE,
|
||||
changetype<usize>(this) + HEADER_SIZE,
|
||||
len << 1
|
||||
);
|
||||
copyUnsafe(out, 0, this, 0, len);
|
||||
return out;
|
||||
}
|
||||
|
||||
padStart(targetLength: i32, padString: String = changetype<String>(" ")): String {
|
||||
assert(this !== null);
|
||||
var length = this.length;
|
||||
var padLen = padString.length;
|
||||
if (targetLength < length || !padLen) return this;
|
||||
var len = targetLength - length;
|
||||
var out = allocate(targetLength);
|
||||
if (len > padLen) {
|
||||
let count = (len - 1) / padLen;
|
||||
let base = count * padLen;
|
||||
let rest = len - base;
|
||||
repeatUnsafe(out, 0, padString, count);
|
||||
if (rest) copyUnsafe(out, base, padString, 0, rest);
|
||||
} else {
|
||||
copyUnsafe(out, 0, padString, 0, len);
|
||||
}
|
||||
if (length) copyUnsafe(out, len, this, 0, length);
|
||||
return out;
|
||||
}
|
||||
|
||||
padEnd(targetLength: i32, padString: String = changetype<String>(" ")): String {
|
||||
assert(this !== null);
|
||||
var length = this.length;
|
||||
var padLen = padString.length;
|
||||
if (targetLength < length || !padLen) return this;
|
||||
var len = targetLength - length;
|
||||
var out = allocate(targetLength);
|
||||
if (length) copyUnsafe(out, 0, this, 0, length);
|
||||
if (len > padLen) {
|
||||
let count = (len - 1) / padLen;
|
||||
let base = count * padLen;
|
||||
let rest = len - base;
|
||||
repeatUnsafe(out, length, padString, count);
|
||||
if (rest) copyUnsafe(out, base + length, padString, 0, rest);
|
||||
} else {
|
||||
copyUnsafe(out, length, padString, 0, len);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@ -451,20 +400,7 @@ export class String {
|
||||
if (count === 1) return this;
|
||||
|
||||
var result = allocate(length * count);
|
||||
var strLen = length << 1;
|
||||
|
||||
/*
|
||||
* TODO possible improvments: reuse existing result for exponentially concats like:
|
||||
* 'a' + 'a' => 'aa' + 'aa' => 'aaaa' + 'aaaa' etc
|
||||
*/
|
||||
for (let offset = 0, len = strLen * count; offset < len; offset += strLen) {
|
||||
memory.copy(
|
||||
changetype<usize>(result) + HEADER_SIZE + offset,
|
||||
changetype<usize>(this) + HEADER_SIZE,
|
||||
strLen
|
||||
);
|
||||
}
|
||||
|
||||
repeatUnsafe(result, 0, this, count);
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -551,24 +487,19 @@ export function parseI64(str: String, radix: i32 = 0): i64 {
|
||||
// FIXME: naive implementation
|
||||
export function parseFloat(str: String): f64 {
|
||||
var len: i32 = str.length;
|
||||
if (!len) {
|
||||
return NaN;
|
||||
}
|
||||
if (!len) return NaN;
|
||||
|
||||
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
|
||||
var code = <i32>load<u16>(ptr, HEADER_SIZE);
|
||||
|
||||
// determine sign
|
||||
var sign: f64;
|
||||
if (code == CharCode.MINUS) {
|
||||
if (!--len) {
|
||||
return NaN;
|
||||
}
|
||||
if (!--len) return NaN;
|
||||
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
|
||||
sign = -1;
|
||||
} else if (code == CharCode.PLUS) {
|
||||
if (!--len) {
|
||||
return NaN;
|
||||
}
|
||||
if (!--len) return NaN;
|
||||
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
|
||||
sign = 1;
|
||||
} else {
|
||||
@ -588,9 +519,7 @@ export function parseFloat(str: String): f64 {
|
||||
assert(false); // TODO
|
||||
}
|
||||
code -= CharCode._0;
|
||||
if (<u32>code > 9) {
|
||||
break;
|
||||
}
|
||||
if (<u32>code > 9) break;
|
||||
num += <f64>code * fac;
|
||||
fac *= 0.1;
|
||||
ptr += 2;
|
||||
@ -598,9 +527,7 @@ export function parseFloat(str: String): f64 {
|
||||
break;
|
||||
}
|
||||
code -= CharCode._0;
|
||||
if (<u32>code >= 10) {
|
||||
break;
|
||||
}
|
||||
if (<u32>code >= 10) break;
|
||||
num = (num * 10) + code;
|
||||
ptr += 2;
|
||||
}
|
||||
|
Reference in New Issue
Block a user