What it is doing basically is reading an std::string
from a given pointer.
I was told that using a StringBuilder
as I am is not the best way to achieve this, so I would like to know how this would be best written.
As additional information, that is a pointer of a std::string
and I've made that because I was originally curious if there was a way to read a std::string
in C# without having to create a bridge using C++/CLI.
How could I optimize this bit of code for best performance?
public static string ReadStdString(IntPtr address)
{
var capacity = Marshal.ReadInt32(address + 24);
var length = Marshal.ReadInt32(address + 20);
var startStr = address + 4;
if (capacity > 15)
startStr = (IntPtr)Marshal.ReadInt32(address + 4);
var result = new StringBuilder();
for (var i = 0; i < length; i++)
{
result.Append((char)Marshal.ReadByte(startStr, i));
}
return result.ToString();
}
address
is supposed to be a C-style string? A null-terminated array of characters? – nhgrif Jul 17 at 0:00