HDFGroup / HDFGroup/hdf5

Obtain length of variable length string (and other types)

Open
#2,193 2 comments 0 reactions 1 assignee Claimed by @bmribler View on GitHub
Component - C Library
Dominant language
C
Stars
988
Forks
355
Avg merge
4d 2h
Merged PRs (30d)
12

Description

It would be a useful addition to the API to

- be able to query the length of variable length strings (and other data) before reading
- use that information to allocate the correct size buffers to pass into the read method (as per get_name).

Then we could eliminate and/or have better control of memory allocations.

For example when reading a fixed length string I can do this:

```
#if NET7_0_OR_GREATER
if (storageSize < 256)
{
Span buffer = stackalloc byte[storageSize + 1];
return ReadString(buffer);
}
else
{
using var spanOwner = SpanOwner.Allocate(storageSize + 1);
return ReadString(spanOwner.Span);
}

string ReadString(Span buffer)
{
read(attribute, type, buffer).ThrowIfError();

var nullTerminatorIndex = MemoryExtensions.IndexOf(buffer, (byte)0);
nullTerminatorIndex = nullTerminatorIndex < 0 ? storageSize : nullTerminatorIndex;
return Encoding.UTF8.GetString(buffer[0..nullTerminatorIndex]);
}
#else
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.