interfacing with C: strings and byte vectors

yawniek via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 11 02:32:54 PDT 2016


my C library works a lot with strings defined in C as:

struct vec_t {
     char *base;
     size_t len;
}

is there a easy way to feed regular D strings to functions that 
accept vec_t*
without creating a vec_t every time
or do i write wrappers for these functions and if so, what is the 
most elegant way
to build them?

so far i defined vec_t as:

struct vec_t {
     char *base;
     size_t len;

     this(string s) { base = s.ptr; len = s.lenght; }

nothrow @nogc inout(char)[] toString() inout @property { return 
base[0 .. len]; }

     nothrow @nogc @property const(char)[]  toSlice()
     {
         return cast(string)  base[0..len];
     }
     alias toString this;
}

but i guess there is a more elegant way?!


More information about the Digitalmars-d-learn mailing list