Skip to content
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

feat(typedarray): Make TypedArray DataView private #123

Merged
merged 1 commit into from
Oct 23, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/strange-paws-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zarrita/typedarray": patch
---

feat: Make unicode string array data view private
28 changes: 14 additions & 14 deletions packages/typedarray/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class ByteStringArray {
}

export class UnicodeStringArray {
_data: Int32Array;
#data: Int32Array;
chars: number;
#encode_buffer: Int32Array;

Expand All @@ -171,57 +171,57 @@ export class UnicodeStringArray {
) {
this.chars = chars;
if (typeof x === "number") {
this._data = new Int32Array(x * chars);
this.#data = new Int32Array(x * chars);
} else if (x instanceof ArrayBuffer) {
if (length) length *= chars;
this._data = new Int32Array(x, byteOffset, length);
this.#data = new Int32Array(x, byteOffset, length);
} else {
const values = x;
const d = new UnicodeStringArray(chars, 1);
this._data = new Int32Array((function* () {
this.#data = new Int32Array((function* () {
for (let str of values) {
d.set(0, str);
yield* d._data;
yield* d.#data;
}
})());
}
this.#encode_buffer = new Int32Array(chars);
}

get BYTES_PER_ELEMENT() {
return this._data.BYTES_PER_ELEMENT * this.chars;
return this.#data.BYTES_PER_ELEMENT * this.chars;
}

get byteLength() {
return this._data.byteLength;
return this.#data.byteLength;
}

get byteOffset() {
return this._data.byteOffset;
return this.#data.byteOffset;
}

/** @type {ArrayBuffer} */
get buffer() {
return this._data.buffer;
return this.#data.buffer;
}

/** @type {number} */
get length() {
return this._data.length / this.chars;
return this.#data.length / this.chars;
}

get(idx: number) {
const offset = this.chars * idx;
let result = "";
for (let i = 0; i < this.chars; i++) {
result += String.fromCodePoint(this._data[offset + i]);
result += String.fromCodePoint(this.#data[offset + i]);
}
return result.replace(/\u0000/g, "");
}

set(idx: number, value: string) {
const offset = this.chars * idx;
const view = this._data.subarray(offset, offset + this.chars);
const view = this.#data.subarray(offset, offset + this.chars);
view.fill(0); // clear current
for (let i = 0; i < this.chars; i++) {
view[i] = value.codePointAt(i) ?? 0;
Expand All @@ -232,9 +232,9 @@ export class UnicodeStringArray {
// encode once
this.set(0, value);
// copy the encoded values to all other elements
let encoded = this._data.subarray(0, this.chars);
let encoded = this.#data.subarray(0, this.chars);
for (let i = 1; i < this.length; i++) {
this._data.set(encoded, i * this.chars);
this.#data.set(encoded, i * this.chars);
}
}

Expand Down