www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - comparing with c strings

reply denis <contact gantsevdenis.fr> writes:
Let's say I have a D application, with some callbacks from C, 
where some arguments to the callbacks are `const char* path`. 
What is the recommended way to compare them to D strings? Without 
making allocations, if that's possible
Nov 23 2023
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Thursday, November 23, 2023 11:29:09 AM MST denis via Digitalmars-d-learn 
wrote:
 Let's say I have a D application, with some callbacks from C,
 where some arguments to the callbacks are `const char* path`.
 What is the recommended way to compare them to D strings? Without
 making allocations, if that's possible
std.string.fromStringz will slice the char* to give you a char[] (using strlen to find the end of the string). Then you can operate on the C string as a char[] - though since it's a slice of the char*, you'll want to dup or idup it if the char[] risks living longer than the char* that it's a slice of. But if all you're doing is comparing it against a D string, then presumably, you don't need to keep the char[] around, and you won't have to allocate a copy. https://dlang.org/phobos/std_string.html#.fromStringz - Jonathan M Davis
Nov 23 2023