Thread overview
byte* and int length -> byte[]
Aug 15, 2011
mimocrocodil
Aug 15, 2011
David Nadlinger
Aug 15, 2011
Jimmy Cao
Aug 15, 2011
Mafi
Aug 15, 2011
Vijay Nayar
August 15, 2011
I obtain immutable(byte)* and int where contained length of bytes block from C library.

Can I convert this into byte[] without explict copying etc.

Something like:

byte* p; // bytes
int size; // size of bytes block

byte[] b;
b.length = size;
b.ptr = p;

// now b contains bytes from library
August 15, 2011
On 8/15/11 7:16 PM, mimocrocodil wrote:
> Can I convert this into byte[] without explict copying etc.
> […]
> byte* p; // bytes
> int size; // size of bytes block

Simply slice the pointer: p[0 .. size]

David
August 15, 2011
On Mon, Aug 15, 2011 at 12:16 PM, mimocrocodil <4denizzz@gmail.com> wrote:

> I obtain immutable(byte)* and int where contained length of bytes block
> from C library.
>
> Can I convert this into byte[] without explict copying etc.
>
> Something like:
>
> byte* p; // bytes
> int size; // size of bytes block
>
> byte[] b;
> b.length = size;
> b.ptr = p;
>
> // now b contains bytes from library
>


Yes:

byte[] b = p[0 .. size];


August 15, 2011
On Mon, 15 Aug 2011 13:16:54 -0400, mimocrocodil <4denizzz@gmail.com> wrote:

> I obtain immutable(byte)* and int where contained length of bytes block from C library.
>
> Can I convert this into byte[] without explict copying etc.
>
> Something like:
>
> byte* p; // bytes
> int size; // size of bytes block
>
> byte[] b;
> b.length = size;
> b.ptr = p;
>
> // now b contains bytes from library

It's even easier:

auto b = p[0..size]; // b is of type byte[]

I.e. you can use a slice operation on a pointer to create a correctly-typed slice.

Keep in mind, the size is the number of *elements* for the slice, not the number of *bytes*.  In your case it happens to be identical, but for larger element types it would be different.

-Steve
August 15, 2011
Am 15.08.2011 19:16, schrieb mimocrocodil:
> I obtain immutable(byte)* and int where contained length of bytes block from C library.
>
> Can I convert this into byte[] without explict copying etc.
>
> Something like:
>
> byte* p; // bytes
> int size; // size of bytes block
>
> byte[] b;
> b.length = size;
> b.ptr = p;
>
> // now b contains bytes from library

Pointers support indexing (like C) but additionally also slicing so if you have a byte* b and a int/long/size_t (or similiar) len then
b[0.. len]
is a slice starting b conatining len elements of type typeof(*b)[].
August 15, 2011
On Mon, 15 Aug 2011 17:16:54 +0000, mimocrocodil wrote:

> I obtain immutable(byte)* and int where contained length of bytes block
> from C library.
> 
> Can I convert this into byte[] without explict copying etc.
> 
> Something like:
> 
> byte* p; // bytes
> int size; // size of bytes block
> 
> byte[] b;
> b.length = size;
> b.ptr = p;
> 
> // now b contains bytes from library

The question presented here really has two parts:
  1) Can you declare a new symbol without duplicating the original data?
  2) Can a mutable symbol reference an immutable pointer?

The short answer to (1) is yes, if you do not need mutability.
The short answer to (2) is no.

The following small program demonstrates this:

import std.stdio;

void main() {
    // Reference data to play with.
    immutable(byte)[] data = [0, 1, 2, 3, 4, 5, 6, 7];

    // These two lines simulate having an immutable byte pointer.
    immutable(byte)* ptr = data.ptr;
    auto ptr_len = data.length;

    // Making an immutable copy is easy, 'auto' works here too.
    immutable(byte)[] immutableCopy = ptr[2 .. 6];

    // Error!  Cannot implicitly convert to immutable.
    //byte[] badMutableCopy = ptr[2 .. 6];
    byte[] goodMutableCopy = immutableCopy.dup;

    // Make a simple edit to our mutable copy.
    foreach (ref b; goodMutableCopy) {
        b++;
    }

    // Output is: 2,3 3,4 4,5 5,6
    for (auto i=0; i < immutableCopy.length; i++) {
        writeln(immutableCopy[i], ',', goodMutableCopy[i]);
    }
}