www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - internal structure of char[]

reply Bjoern <nanali nospam-wanadoo.fr> writes:
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
Jan 18 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Bjoern" <nanali nospam-wanadoo.fr> wrote in message 
news:fmq5d6$2p8$1 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; }
Jan 18 2008
parent Bjoern <nanali nospam-wanadoo.fr> writes:
Thanks, as allways usefull information !

cb(element.ptr, element.length);
//Well at least this I found out by myself; so there is still hope :)

Just in another Thread regarding char* to char[] I read : slice and dup.
Using .dup in this case however, produces wrong results.
Jan 18 2008