Xojo.Core.MutableMemoryBlock and trailing NULs

When getting back CStrings via declares some time the API of a library asks for a buffer large enough to receive the largest possible value. Since TextEncoding.UTF8.ConvertDataToText(buffer) does not have something like a stripTrailingNULs option, I remove them this way:

Dim i As Integer = maxSize - 1 Do Until buffer.Int8Value(i) <> 0 buffer.Remove(i, 1) i = i - 1 Loop
Is there a faster way to do that?

[quote=223214:@Eli Ott]When getting back CStrings via declares some time the API of a library asks for a buffer large enough to receive the largest possible value. Since TextEncoding.UTF8.ConvertDataToText(buffer) does not have something like a stripTrailingNULs option, I remove them this way:

Dim i As Integer = maxSize - 1 Do Until buffer.Int8Value(i) <> 0 buffer.Remove(i, 1) i = i - 1 Loop
Is there a faster way to do that?[/quote]

Just an offhand idea… Can’t you just find the first NUL and then resize the buffer to be that length?

I can do that, but as most of the strings returned by this library are filling more than 50% of the buffer, it is slower to start the loop at the beginning. And in both cases (0 to maxSize and maxSize to 0) removing the elements on each iteration is faster than using buffer.Left(i - 1) when the first non-NUL byte is found.

I’m going to test if the tail can be cut off after the conversion to Text (though I suspect this will be even slower).

[quote=223242:@Eli Ott]I can do that, but as most of the strings returned by this library are filling more than 50% of the buffer, it is slower to start the loop at the beginning. And in both cases (0 to maxSize and maxSize to 0) removing the elements on each iteration is faster than using buffer.Left(i - 1) when the first non-NUL byte is found.

I’m going to test if the tail can be cut off after the conversion to Text (though I suspect this will be even slower).[/quote]

Roughly how large are your buffers?

The longest string I received back from this library was about 100 characters long.

This seems to be the fastest:

// buffer is MutableMemoryBlock Dim i As Integer = maxSize - 1 Do Until buffer.Int8Value(i) <> 0 i = i - 1 Loop buffer.Remove(i + 1, maxSize - i - 1) Return TextEncoding.UTF8.ConvertDataToText(buffer)