www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - std.string.toString(char*) little nuisance

reply Tom <tom nospam.com> writes:
A question,

shouldn't be documented on the 'std.string.toString(char*)', that the 
d-string returned shares the same buffer as the input c-string?

I've spent some (too much) time debugging to find out why the heck all 
my strings were messing up... now I know.

Mentioning this on the docs could have been of so much help, it's not 
that obvious!

Regards,
--
Tom;
Mar 12 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Tom wrote:
 A question,
 
 shouldn't be documented on the 'std.string.toString(char*)', that the 
 d-string returned shares the same buffer as the input c-string?
 
 I've spent some (too much) time debugging to find out why the heck all 
 my strings were messing up... now I know.
 
 Mentioning this on the docs could have been of so much help, it's not 
 that obvious!

I think it's pretty obvious if you read the first paragraph of http://www.digitalmars.com/d/phobos/std_string.html, and consider that a char* c-string is also a 'string' as described in that bit of text. It's not modified, so a slice of the original is returned (as a D char[]). Remember: don't just read the function description, the module description may contain information that applies to all (or a lot of) the functions in a module.
Mar 12 2007
parent reply Tom <tom nospam.com> writes:
Frits van Bommel escribió:
 Tom wrote:
 A question,

 shouldn't be documented on the 'std.string.toString(char*)', that the 
 d-string returned shares the same buffer as the input c-string?

 I've spent some (too much) time debugging to find out why the heck all 
 my strings were messing up... now I know.

 Mentioning this on the docs could have been of so much help, it's not 
 that obvious!

I think it's pretty obvious if you read the first paragraph of http://www.digitalmars.com/d/phobos/std_string.html, and consider that a char* c-string is also a 'string' as described in that bit of text. It's not modified, so a slice of the original is returned (as a D char[]).

It wasn't so obvious for me. I knew the COW thing for char[] but didn't guess that a char* was considered a string in this matters.
 Remember: don't just read the function description, the module 
 description may contain information that applies to all (or a lot of) 
 the functions in a module.

Uhmm, I know :). Not reading wasn't the issue. It was disregarding the fact that in D, a char* is also considered a string. Regards, -- Tom;
Mar 12 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Tom wrote:
 Frits van Bommel escribió:
 Tom wrote:
 shouldn't be documented on the 'std.string.toString(char*)', that the 
 d-string returned shares the same buffer as the input c-string?



 I think it's pretty obvious if you read the first paragraph of 
 http://www.digitalmars.com/d/phobos/std_string.html, and consider that 
 a char* c-string is also a 'string' as described in that bit of text. 
 It's not modified, so a slice of the original is returned (as a D 
 char[]).

It wasn't so obvious for me. I knew the COW thing for char[] but didn't guess that a char* was considered a string in this matters.

 Not reading wasn't the issue. It was disregarding the 
 fact that in D, a char* is also considered a string.

Well, char* isn't typically considered a string in D, but in this case[1] it's treated as one[2]. Also, you clearly referred to it as a (c-)string in your original post ;). [1]: And AFAIK only in this case, outside of *.c.* modules. Well, perhaps you could also count the return value of toStringz... [2]: The description of toString(char*): "Convert C-style 0 terminated _string_ s to char[] string." (emphasis added)
Mar 13 2007