www.digitalmars.com         C & C++   DMDScript  

D - String splitting and printf

reply Paul Stanton <Paul_member pathlink.com> writes:
ok, i am definitly missing something here, any help is appreciated
the output from the following program reads:
 garry barry harry
 barry harry
 harry
 0
 5
the 0 and the 5 lead me to believe that the first element in the resulting list is equal to "garry", but why then does printf print "garry barry harry" instead of "garry"? how do i extract "garry" on it's own? ---------------------------------------------------------- import string; int main(char[][] args) { char[][] result = split("garry barry harry"); for(int i = 0; i < result.length; i++) printf("%s\n", (char*)result[i]); printf("%i\n", cmp(result[0], "garry")); printf("%i\n", result[0].length); return 0; } ----------------------------------------------------------
Jan 27 2003
next sibling parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
I implement the following function in all of my programs.  Maybe there's
a standard D equivalent?

char *p(char[] str)
{
    return (char*)(str~\0);
}

D arrays aren't null terminated, so I pass ALL of my D strings (other
than fixed constant strings) through p() before sending them off to
printf().  It's a little ugly, but it's the best I know for now.

--
The Villagers are Online! http://villagersonline.com

.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]
Jan 27 2003
parent Burton Radons <loth users.sourceforge.net> writes:
Russ Lewis wrote:
 I implement the following function in all of my programs.  Maybe there's
 a standard D equivalent?
 
 char *p(char[] str)
 {
     return (char*)(str~\0);
 }
 
 D arrays aren't null terminated, so I pass ALL of my D strings (other
 than fixed constant strings) through p() before sending them off to
 printf().  It's a little ugly, but it's the best I know for now.
char *toStringz(char[]);
Jan 27 2003
prev sibling parent reply Ilya Minkov <midiclub 8ung.at> writes:
Look.

This has to do with copy-on-write convention, which is faster than C way.
In D, an array is not copied on slicing, exactly the case here. Array 
dimension is written instead. But C doesn't know D array dimension 
information, and still searches for terminating \0, which is right where 
it was, at the end.

go to:
http://www.digitalmars.com/d/interface.html

and look closely at "Calling printf()", it states clearly that you 
should use "%.*s" instead of "%s" to accomodate for a different memory 
model. It should not be a problem after a D-native printf is written, 
and i guess Burton has already adressed this.

Appending \0 to strings would also let you do without this printf format 
accomodation.

-i.



Paul Stanton wrote:
 ok, i am definitly missing something here, any help is appreciated
 the output from the following program reads:
 
garry barry harry
barry harry
harry
0
5
the 0 and the 5 lead me to believe that the first element in the resulting list is equal to "garry", but why then does printf print "garry barry harry" instead of "garry"? how do i extract "garry" on it's own? ---------------------------------------------------------- import string; int main(char[][] args) { char[][] result = split("garry barry harry"); for(int i = 0; i < result.length; i++) printf("%s\n", (char*)result[i]); printf("%i\n", cmp(result[0], "garry")); printf("%i\n", result[0].length); return 0; } ----------------------------------------------------------
Jan 27 2003
parent Paul Stanton <Paul_member pathlink.com> writes:
This is also a problem for windows.MessageBoxA and probably the rest of the
message box procedures. I havent played enough to know how much more of the
phobos API is defective. Any one have a clew when phobos gets updated?

In article <b14ga4$2nh9$1 digitaldaemon.com>, Ilya Minkov says...

and look closely at "Calling printf()", it states clearly that you 
should use "%.*s" instead of "%s" to accomodate for a different memory 
model.
Jan 31 2003