Skip to content

Commit

Permalink
Parse HDF5 string padding
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Jun 4, 2024
1 parent 6c1678e commit 67f9753
Show file tree
Hide file tree
Showing 17 changed files with 725 additions and 407 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
"wait-on": "7.2.0"
},
"pnpm": {
"overrides": {
"eslint-config-galex>typescript": "$typescript"
},
"peerDependencyRules": {
"allowedVersions": {
"@phenomnomnominal/tsquery>typescript": "5.x",
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/providers/h5grove/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export interface H5GroveTimeType extends H5GroveBaseType {
export interface H5GroveStringType extends H5GroveBaseType {
class: 3;
cset: number;
strPad?: number; // optional for backwards compatibility with h5grove <= 2.1.0
vlen: boolean;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/app/src/providers/h5grove/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { H5T_CSET, H5T_ORDER } from '@h5web/shared/h5t';
import { H5T_CSET, H5T_ORDER, H5T_STR } from '@h5web/shared/h5t';
import {
arrayType,
bitfieldType,
Expand Down Expand Up @@ -42,13 +42,13 @@ describe('parseDType', () => {
it('should convert string types', () => {
expect(
parseDType({ class: 3, size: 6, cset: 0, vlen: false }),
).toStrictEqual(strType(H5T_CSET.ASCII, 6));
).toStrictEqual(strType(H5T_CSET.ASCII, undefined, 6));
expect(
parseDType({ class: 3, size: 6, cset: 0, vlen: true }),
).toStrictEqual(strType(H5T_CSET.ASCII));
expect(
parseDType({ class: 3, size: 6, cset: 1, vlen: false }),
).toStrictEqual(strType(H5T_CSET.UTF8, 6));
parseDType({ class: 3, size: 6, cset: 1, strPad: 1, vlen: false }),
).toStrictEqual(strType(H5T_CSET.UTF8, H5T_STR.NULLPAD, 6));
expect(
parseDType({ class: 3, size: 6, cset: 1, vlen: true }),
).toStrictEqual(strType(H5T_CSET.UTF8));
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/providers/h5grove/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ export function parseDType(type: H5GroveType): DType {
}

if (h5tClass === H5T_CLASS.STRING) {
const { cset, vlen } = type;
return strType(cset, vlen ? undefined : size);
const { cset, strPad, vlen } = type;
return strType(cset, strPad, vlen ? undefined : size);
}

if (h5tClass === H5T_CLASS.BITFIELD) {
Expand Down
4 changes: 3 additions & 1 deletion packages/app/src/providers/hsds/models.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { H5T_CSET, H5T_STR } from '@h5web/shared/h5t';
import type { Entity } from '@h5web/shared/hdf5-models';

/* --------------------- */
Expand Down Expand Up @@ -104,7 +105,8 @@ export interface HsdsNumericType {

export interface HsdsStringType {
class: 'H5T_STRING';
charSet: `H5T_CSET_${'ASCII' | 'UTF8'}`;
charSet: `H5T_CSET_${keyof typeof H5T_CSET}`;
strPad: `H5T_STR_${keyof typeof H5T_STR}`;
length: number | 'H5T_VARIABLE';
}

Expand Down
10 changes: 7 additions & 3 deletions packages/app/src/providers/hsds/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { H5T_CSET, H5T_ORDER } from '@h5web/shared/h5t';
import { H5T_CSET, H5T_ORDER, H5T_STR } from '@h5web/shared/h5t';
import type { DType } from '@h5web/shared/hdf5-models';
import {
arrayType,
Expand Down Expand Up @@ -52,22 +52,26 @@ describe('convertHsdsType', () => {
const asciiStr: HsdsStringType = {
class: 'H5T_STRING',
charSet: 'H5T_CSET_ASCII',
strPad: 'H5T_STR_NULLTERM',
length: 25,
};

expect(convertHsdsType(asciiStr)).toStrictEqual(
strType(H5T_CSET.ASCII, 25),
strType(H5T_CSET.ASCII, H5T_STR.NULLTERM, 25),
);
});

it('should convert variable-length UTF-8 string type', () => {
const unicodeStr: HsdsStringType = {
class: 'H5T_STRING',
charSet: 'H5T_CSET_UTF8',
strPad: 'H5T_STR_NULLPAD',
length: 'H5T_VARIABLE',
};

expect(convertHsdsType(unicodeStr)).toStrictEqual(strType(H5T_CSET.UTF8));
expect(convertHsdsType(unicodeStr)).toStrictEqual(
strType(H5T_CSET.UTF8, H5T_STR.NULLPAD),
);
});

it('should convert integer types', () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/app/src/providers/hsds/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertArray, isGroup } from '@h5web/shared/guards';
import { H5T_CSET, H5T_ORDER } from '@h5web/shared/h5t';
import { H5T_CSET, H5T_ORDER, H5T_STR } from '@h5web/shared/h5t';
import type {
ArrayShape,
Attribute,
Expand Down Expand Up @@ -138,11 +138,14 @@ export function convertHsdsType(hsdsType: HsdsType): DType {
return convertHsdsCompoundType(hsdsType);

case 'H5T_STRING': {
const { charSet, length } = hsdsType;
const { charSet, strPad, length } = hsdsType;
return strType(
H5T_CSET[
charSet.slice(charSet.lastIndexOf('_') + 1) as keyof typeof H5T_CSET
],
H5T_STR[
strPad.slice(strPad.lastIndexOf('_') + 1) as keyof typeof H5T_STR
],
length === 'H5T_VARIABLE' ? undefined : length,
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/providers/mock/mock-file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { H5T_CSET } from '@h5web/shared/h5t';
import { H5T_CSET, H5T_STR } from '@h5web/shared/h5t';
import type { GroupWithChildren } from '@h5web/shared/hdf5-models';
import {
arrayType,
Expand Down Expand Up @@ -62,7 +62,7 @@ export function makeMockFile(): GroupWithChildren {
}),
scalar('scalar_compound', ['foo', 2], {
type: compoundType({
str: strType(H5T_CSET.ASCII, 3),
str: strType(H5T_CSET.ASCII, H5T_STR.NULLPAD, 3),
int: intType(8),
}),
attributes: [
Expand Down
2 changes: 1 addition & 1 deletion packages/h5wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"dependencies": {
"comlink": "4.4.1",
"h5wasm": "0.7.4",
"h5wasm": "0.7.5",
"nanoid": "5.0.7"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 67f9753

Please sign in to comment.