internal structure of char[]
Jarrett Billingsley
kb3ctd2 at yahoo.com
Fri Jan 18 05:56:33 PST 2008
"Bjoern" <nanali at nospam-wanadoo.fr> wrote in message
news:fmq5d6$2p8$1 at digitalmars.com...
> Hi,
> Does somebody know the internal structure of char[] ?
>
> Not only that I have to do a lot of converting on the DLL (D) side, I also
> have to convert the strings on the parrent side (4GL)
>
> I hope I can use this info to simplyfy for example code like this :
>
> alias extern(Windows) void function(char* token, size_t siz = 0)
> DisplayCallBack;
>
> export extern(Windows)
> bool TextDelimit(DisplayCallBack cb, char* source, char* delim)
> {
> char[] _source = source[0 .. strlen(source)].dup;
> char[] _delim = delim[0 .. strlen(delim)].dup;
>
> // split into an array
> char[][] elements = Text.delimit (_source, _delim);
> foreach (char[] element; elements)
> {
> char* s = element.ptr;
> cb(s, strlen(s));
> }
> return true;
> }
>
> Well it works fine, but I want it smarter (if possible)
> Thanks in advance, Bjoern
OK, so your function is doing probably a lot of unnecessary copying. You
shouldn't have to .dup those strings, just slice source[0 .. strlen(source)]
and now that string points to the same place the source string is. AFAIK
Text.delimit does not modify the inputs, it just gives you an array of
slices into it (aren't slices great?). Lastly, doing strlen(s) in that loop
shouldn't work (I'm surprised it does?!), since all the items of elements
are slices, not zero-terminated strings.
Finally you shouldn't need to know anything about the internal structure of
char[] to do this.
export extern(Windows)
bool TextDelimit(DisplayCallBack cb, char* source, char* delim)
{
char[] _source = source[0 .. strlen(source)];
char[] _delim = delim[0 .. strlen(delim)];
foreach(element; Text.delimit(_source, _delim))
cb(element.ptr, element.length);
return true;
}
More information about the Digitalmars-d-learn
mailing list