Is it possible to get the length of a variable length string?


From other posts in this forum I understand that the answer is currently no (in 1.10.x at least).
This seems like an oversight to me. String handling in .NET (and probably other platforms) often needs to know the length of the buffer, presumably to prevent buffer overruns. So when reading a variable length string I currently have to do something like:

// NOTE: no way to retrieve size of variable length buffer.
// Only search for null up to a fixed length.
Span<byte> bytes = new((byte*)buffer[0], H5Global.MaxVariableLengthStringBuffer);
var nullTerminatorIndex = MemoryExtensions.IndexOf(bytes, (byte)0);
if (nullTerminatorIndex != -1)
    return Encoding.UTF8.GetString((byte*)buffer[0], nullTerminatorIndex);

It would be a useful addition to the API to

  1. be able to query the length of variable length strings (and other data) before reading
  2. use that information to allocate the correct size buffers to pass into the read method (as per get_name).
    Then we could eliminate memory allocations in some circumstances using stack allocated memory.


The answer is no if we use the current C-API as a reference. There is no API to query the length of variable-length sequences (including vlen strings). How about creating an enhancement issue on GitHub?



Thanks, done: