Hi there, I want to call a C function that upcases a string. I have something working, I just want to check in here to see if there's a better approach that I'm missing. I ask because std.string.toStringZ()
returns an immutable char *
.
As far as I can tell, I have two options:
- Make the extern definition accept immutable.
- Cast to
char *
.
I opted for 2 because it seems that 1 would be confusing - the definition says immutable, but it mutates the string.
Anyway, is this the D way to mutate a string from C, or is there another approach I'm unaware of?
extern (C) void upcase(char *);
import std.stdio;
import std.string;
void main() {
auto s = "hello d";
auto cs = cast (char *) std.string.toStringz(s);
upcase(cs);
writeln(std.string.fromStringz(cs));
}
It also works with:
extern (C) void upcase(immutable char *);
import std.stdio;
import std.string;
void main() {
auto s = "hello d";
auto cs = std.string.toStringz(s);
upcase(cs);
writeln(std.string.fromStringz(cs));
}
but it seems that "immutable" is a lie in that case.