Allocating a wstring on the stack (no GC)?

Benjamin Thaut via Digitalmars-d digitalmars-d at puremagic.com
Wed May 7 13:20:09 PDT 2014


Am 07.05.2014 20:41, schrieb Maxime Chevalier-Boisvert:
>
> I do need it to be dynamically sized. I also want to avoid copying my
> string data if possible. Basically, I just want to create a wstring
> "view" on an existing "raw" buffer that exists in memory somewhere,
> based on a pointer to this buffer and its length.
>

If you just need a view of the raw buffer that already exists, why don't 
you slice it directly? Is it neccessary that you make a copy of it?

void[] rawBuffer = ...;
size_t offset = ...;

assert(rawBuffer.length >= offset + stringLength * wchar.sizeof, "out of 
bounds access");
const(wchar)[] stringView = (cast(const(wchar)*)rawBuffer.ptr + 
offset)[0..stringLength];

Kind Regards
Benjamin Thaut


More information about the Digitalmars-d mailing list