utilium 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ /** Utilities for computing checksums */
2
+ /**
3
+ * Computes the CRC32C checksum of a Uint8Array.
4
+ */
5
+ export declare function crc32c(data: Uint8Array): number;
@@ -0,0 +1,20 @@
1
+ /** Utilities for computing checksums */
2
+ // Precompute CRC32C table
3
+ const crc32cTable = new Uint32Array(256);
4
+ for (let i = 0; i < 256; i++) {
5
+ let value = i;
6
+ for (let j = 0; j < 8; j++) {
7
+ value = value & 1 ? 0x82f63b78 ^ (value >>> 1) : value >>> 1;
8
+ }
9
+ crc32cTable[i] = value;
10
+ }
11
+ /**
12
+ * Computes the CRC32C checksum of a Uint8Array.
13
+ */
14
+ export function crc32c(data) {
15
+ let crc = 0xffffffff;
16
+ for (let i = 0; i < data.length; i++) {
17
+ crc = (crc >>> 8) ^ crc32cTable[(crc ^ data[i]) & 0xff];
18
+ }
19
+ return (crc ^ 0xffffffff) >>> 0;
20
+ }
package/dist/struct.d.ts CHANGED
@@ -7,6 +7,10 @@ export * as Struct from './internal/struct.js';
7
7
  * Gets the size in bytes of a type
8
8
  */
9
9
  export declare function sizeof<T extends primitive.Valid | StaticLike | InstanceLike>(type: T): Size<T>;
10
+ /**
11
+ * Returns the offset (in bytes) of a member in a struct.
12
+ */
13
+ export declare function offsetof(type: StaticLike | InstanceLike, memberName: string): number;
10
14
  /**
11
15
  * Aligns a number
12
16
  */
package/dist/struct.js CHANGED
@@ -15,6 +15,18 @@ export function sizeof(type) {
15
15
  const struct = isStatic(type) ? type : type.constructor;
16
16
  return struct[symbol_metadata(struct)][Symbol.struct_metadata].size;
17
17
  }
18
+ /**
19
+ * Returns the offset (in bytes) of a member in a struct.
20
+ */
21
+ export function offsetof(type, memberName) {
22
+ checkStruct(type);
23
+ const struct = isStatic(type) ? type : type.constructor;
24
+ const metadata = struct[symbol_metadata(struct)][Symbol.struct_metadata];
25
+ const member = metadata.members.get(memberName);
26
+ if (!member)
27
+ throw new Error('Struct does not have member: ' + memberName);
28
+ return member.offset;
29
+ }
18
30
  /**
19
31
  * Aligns a number
20
32
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
@@ -0,0 +1,22 @@
1
+ /** Utilities for computing checksums */
2
+
3
+ // Precompute CRC32C table
4
+ const crc32cTable = new Uint32Array(256);
5
+ for (let i = 0; i < 256; i++) {
6
+ let value = i;
7
+ for (let j = 0; j < 8; j++) {
8
+ value = value & 1 ? 0x82f63b78 ^ (value >>> 1) : value >>> 1;
9
+ }
10
+ crc32cTable[i] = value;
11
+ }
12
+
13
+ /**
14
+ * Computes the CRC32C checksum of a Uint8Array.
15
+ */
16
+ export function crc32c(data: Uint8Array): number {
17
+ let crc = 0xffffffff;
18
+ for (let i = 0; i < data.length; i++) {
19
+ crc = (crc >>> 8) ^ crc32cTable[(crc ^ data[i]) & 0xff];
20
+ }
21
+ return (crc ^ 0xffffffff) >>> 0;
22
+ }
package/src/struct.ts CHANGED
@@ -23,6 +23,20 @@ export function sizeof<T extends primitive.Valid | StaticLike | InstanceLike>(ty
23
23
  return struct[symbol_metadata(struct)][Symbol.struct_metadata].size as Size<T>;
24
24
  }
25
25
 
26
+ /**
27
+ * Returns the offset (in bytes) of a member in a struct.
28
+ */
29
+ export function offsetof(type: StaticLike | InstanceLike, memberName: string): number {
30
+ checkStruct(type);
31
+
32
+ const struct = isStatic(type) ? type : type.constructor;
33
+ const metadata = struct[symbol_metadata(struct)][Symbol.struct_metadata];
34
+
35
+ const member = metadata.members.get(memberName);
36
+ if (!member) throw new Error('Struct does not have member: ' + memberName);
37
+ return member.offset;
38
+ }
39
+
26
40
  /**
27
41
  * Aligns a number
28
42
  */