Skip to content

Add concat3 helper for string/util #1432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 45 additions & 13 deletions std/assembly/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { ipow32 } from "../math";
// All tables are stored as two staged lookup tables (static tries)
// because the full range of Unicode symbols can't be efficiently
// represented as-is in memory (see Unicode spec ch 5, p.196):
// https://www.unicode.org/versions/Unicode12.0.0/ch05.pdf
// https://www.unicode.org/versions/Unicode13.0.0/ch05.pdf
// Tables have been generated using these forked musl tools:
// https://github.com/MaxGraey/musl-chartable-tools/tree/case-ignorable

// Lookup table to check if a character is alphanumeric or not
// See: https://git.musl-libc.org/cgit/musl/tree/src/ctype/alpha.h
// size: 3904 bytes
// size: 4032 bytes (compressed to ~3500 after binaryen)
// @ts-ignore
@inline @lazy const ALPHA_TABLE = memory.data<u8>([
18,17,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,17,34,35,36,17,37,38,39,40,
Expand Down Expand Up @@ -191,7 +191,7 @@ import { ipow32 } from "../math";
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,3
]);

// size: 1568 bytes (compressed to ~1380 bytes after binaryen)
// size: 1568 bytes (compressed to ~1300 bytes after binaryen)
// @ts-ignore: decorator
@lazy @inline const CASED = memory.data<u8>([
18,19,20,21,22,23,16,16,16,16,16,16,16,16,16,16,
Expand Down Expand Up @@ -274,7 +274,7 @@ import { ipow32 } from "../math";
0,0,0,0,0,0,0,0
]);

// size: 2976 bytes (compressed to ~2050 bytes after binaryen)
// size: 2976 bytes (compressed to ~2000 bytes after binaryen)
// @ts-ignore: decorator
@lazy @inline const CASE_IGNORABLES = memory.data<u8>([
18,16,19,20,21,22,23,24,25,26,27,28,29,30,31,32,
Expand Down Expand Up @@ -983,40 +983,72 @@ export function joinStringArray(dataStart: usize, length: i32, separator: string
// @ts-ignore: type
if (value !== null) estLen += value.length;
}
var offset = 0;
var sepLen = separator.length;
var result = __alloc((estLen + sepLen * lastIndex) << 1, idof<string>());
var offset: usize = 0;
var sepLen = <usize>separator.length << 1;
var result = __alloc((estLen << 1) + sepLen * lastIndex, idof<string>());
for (let i = 0; i < lastIndex; ++i) {
value = load<string>(dataStart + (<usize>i << alignof<string>()));
if (value !== null) {
let valueLen = value.length;
let valueLen = <usize>value.length << 1;
memory.copy(
result + (<usize>offset << 1),
result + offset,
changetype<usize>(value),
<usize>valueLen << 1
valueLen
);
offset += valueLen;
}
if (sepLen) {
memory.copy(
result + (<usize>offset << 1),
result + offset,
changetype<usize>(separator),
<usize>sepLen << 1
sepLen
);
offset += sepLen;
}
}
value = load<string>(dataStart + (<usize>lastIndex << alignof<string>()));
if (value !== null) {
memory.copy(
result + (<usize>offset << 1),
result + offset,
changetype<usize>(value),
<usize>value.length << 1
);
}
return changetype<string>(result); // retains
}

export function concat3(a: string, b: string, c: string): string {
var bytesLenA = <usize>a.length << 1;
var bytesLenB = <usize>b.length << 1;
var bytesLenC = <usize>c.length << 1;
var result = __alloc(bytesLenA + bytesLenB + bytesLenC, idof<string>());
var offset: usize = 0;
if (bytesLenA) {
memory.copy(
result,
changetype<usize>(a),
bytesLenA
);
offset += bytesLenA;
}
if (bytesLenB) {
memory.copy(
result + offset,
changetype<usize>(b),
bytesLenB
);
offset += bytesLenB;
}
if (bytesLenC) {
memory.copy(
result + offset,
changetype<usize>(c),
bytesLenC
);
}
return changetype<string>(result); // retains
}

export function joinReferenceArray<T>(dataStart: usize, length: i32, separator: string): string {
var lastIndex = length - 1;
if (lastIndex < 0) return "";
Expand Down
18 changes: 6 additions & 12 deletions tests/compiler/std/array.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -16072,15 +16072,17 @@
local.set $10
local.get $2
call $~lib/string/String#get:length
i32.const 1
i32.shl
local.set $11
local.get $5
i32.const 1
i32.shl
local.get $11
local.get $3
i32.mul
i32.add
i32.const 1
i32.shl
i32.const 1
call $~lib/rt/tlsf/__alloc
local.set $12
i32.const 0
Expand Down Expand Up @@ -16117,16 +16119,14 @@
if
local.get $6
call $~lib/string/String#get:length
i32.const 1
i32.shl
local.set $9
local.get $12
local.get $10
i32.const 1
i32.shl
i32.add
local.get $6
local.get $9
i32.const 1
i32.shl
call $~lib/memory/memory.copy
local.get $10
local.get $9
Expand All @@ -16137,13 +16137,9 @@
if
local.get $12
local.get $10
i32.const 1
i32.shl
i32.add
local.get $2
local.get $11
i32.const 1
i32.shl
call $~lib/memory/memory.copy
local.get $10
local.get $11
Expand Down Expand Up @@ -16182,8 +16178,6 @@
if
local.get $12
local.get $10
i32.const 1
i32.shl
i32.add
local.get $6
local.get $6
Expand Down