I’ve been using the MemoryBlock / BinaryStream method for fast string concatenation. Take the following function as an example:
Function GenerateString() as String Var cResult As New MemoryBlock( 0 ) Var bsResult As New BinaryStream( cResult ) For iNumber as Integer = 1 to 100000 bsResult.Write( Str( iNumber ) ) bsResult.Write( " " ) Next bsResult.Close Return cResult End Function
Obviously the contents of the function is only an example. The real functions are more convoluted but this shows the principal. The problem I’ve got is that the returned string has a null encoding.
I would like to define the encoding to UTF8, which I can’t seem to find a way to do.
Return CType( cResult, String ).DefineEncoding( Encodings.UTF8 )
Works, but I’m worried it will duplicate the MemoryBlock before defining the encoding. Another way is:
Return String( cResult ).DefineEncoding( Encodings.UTF8 )
Would seem like a viable option but it doesn’t work:
Return cResult.StringValue( 0, cResult.Size - 1 ).DefineEncoding( Encodings.UTF8 )
Which I presume doesn’t duplicate but I have to get the length for the string, which could be time consuming. Produces a syntax error anyway.
I’d rather not put the .DefineEncoding( Encodings.UTF8 ) on every call of the function.