View previous topic :: View next topic |
Author |
Message |
JJR
Joined: 22 Feb 2004 Posts: 1104
|
Posted: Sun Jan 08, 2006 2:40 pm Post subject: Converting C strings? |
|
|
Kris,
How do I print a C string (char*) using Mango.io? It doesn't appear you have any toString method available to convert C type strings ( Can't output a C string with Stdout or print ). Ares doesn't have any of those either. I'm avoiding Phobos, but it appears to be the only library that supports that.
Thanks.
-JJR |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Sun Jan 08, 2006 2:58 pm Post subject: |
|
|
That's a good question ... how would you like to see it supported?
We could add a function to return an array slice from the C string? We could add explicit support to the (printf) formatting routine ~ perhaps a ?S (uppercase)?
The gnarly way to do it is this: Println (cString [0 .. strlen(cString)]); |
|
Back to top |
|
|
JJR
Joined: 22 Feb 2004 Posts: 1104
|
Posted: Sun Jan 08, 2006 3:22 pm Post subject: |
|
|
Yes... I would like to see it. It's pretty simple to implement, I guess.
?S sounds good.
It actually looks like Socket.d uses the strlen() method (I thought of that... but I didn't think it looked too pretty). That will do for now, but I would be nice to have some smooth integration with the current io methods: both Stdout and print?
Code: | char* msg = "Hello there!";
Stdout (msg) (CR); |
Formatting would be a bonus.
Thanks,
John |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Sun Jan 08, 2006 3:41 pm Post subject: |
|
|
Thinking about it some more, I'm a bit loath to add support into Reader/Writer for char* + wchar* + dchar*. I like to keep those guys completely symmetric in terms of data types, and the pointer types stand out a bit. Also, it's not entirely clear if Stdout should write a C string or just a 32bit pointer address?
I wonder if, in the above case, it would not be better to provide a c.string module with various appropriate functions exposed therein?
On the other hand, the printf formatting should probably handle C-style char*, wchar*, and dchar* correctly in conjunction with a ?s ~ its job is ostensibly to format things ... there's not much of an excuse there
Thus, you'd be able to use C strings directly with Print, Println, Error, Stdout.format(), and the Sprint variations? |
|
Back to top |
|
|
JJR
Joined: 22 Feb 2004 Posts: 1104
|
Posted: Sun Jan 08, 2006 6:58 pm Post subject: |
|
|
Actually, you're right. I don't really need to have it integrated into Stdout. It's overkill and could lead to more trouble than it's worth. A simple solution is just to make use of a global toString method that does the job (like phobos). I've added the basic functions to my project toolbox.
Code: | char *p = "Hello!";
Stdout (.toString(p)) (CR);
|
was all I really needed. If you could add a couple of global methods to Mango, I think that would work: one for char* toStringz(char[]) and the other char[] toString(char*). Or is this still not a good idea for Mango?
Maybe they would be better placed in Ares. It would be nice to have something available so I won't have to carry the functions to each project I work on. I sure miss these functions from Phobos. They are very basic and important when it comes to interfacing with C code out there.
Of course, I'm not sure if this means that we would need similar methods for dchar* and wchar*. I don't need those, anyway. Of course, maybe I will eventually need them, and just don't realize it.
-JJR |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Sun Jan 08, 2006 9:38 pm Post subject: |
|
|
Mango doesn't currently have a home for C mini-functions, so I'll look into how to do this cleanly. It may turn out that Ares is the right place for this, but we'll see on Tuesday. I will add the char*, wchar* and dchar* support to the formatting routines ~ and thanks for the prodding |
|
Back to top |
|
|
JJR
Joined: 22 Feb 2004 Posts: 1104
|
Posted: Mon Jan 09, 2006 3:49 pm Post subject: |
|
|
Sounds great, Kris. You're quite right.
Thanks for listening to my confused dithering!
-JJR |
|
Back to top |
|
|
|