5

I have array of bytes which is defined as pointer + size:

  size_t size;   // size in bytes
  void   *data;  // NOT zero-terminated string

How do I construct, preferably zero-copy, 'string' from it?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Brazhnyk Yuriy
  • 464
  • 4
  • 10

1 Answers1

7

This assumes that data points to immutable memory:

string s = (cast(immutable(char)*)data)[0..size];

If it doesn't, a char[] would be more appropriate instead of a string, or you can make an immutable copy with .idup.

Vladimir Panteleev
  • 24,651
  • 6
  • 70
  • 114