Regan Heath | 2 Oct 16:55 2012
Picon

zero-terminated strings, string literals, etc

Recent discussions on the zero terminated string problems and  
inconsistency of string literals has me, again, wondering why D doesn't  
have a 'type' to represent C's zero terminated strings.  It seems to me  
that having a type, and typing C functions with it would solve a lot of  
problems.

The compiler could/would then error if people attempted to pass a D string  
without converting it correctly.

The compiler would create literals with or without \0 as required by the  
'type' being assigned, parameter passed, etc.

The conversion function from a D string to a C string would return the new  
type.

A %sz format specifier could be added to writef which would be able to  
type check the argument.

As the only way to get a variable of the new type would be from a literal,  
conversion or C function call so we could be sure it was in fact \0  
terminated(*), and so..

An implicit conversion between a C string and a D string (slice using  
strlen) would be possible, and safe.  (Though, not at zero runtime cost)

Existing (correct) code would continue to compile, by this I mean:
  - passing literals
  - calling a conversion function for each D string argument

But code which passes D string variables to C functions without conversion  
(Continue reading)

David | 2 Oct 17:29 2012
Picon

Re: zero-terminated strings, string literals, etc

Am 02.10.2012 16:55, schrieb Regan Heath:
> Recent discussions on the zero terminated string problems and
> inconsistency of string literals has me, again, wondering why D doesn't
> have a 'type' to represent C's zero terminated strings.  It seems to me
> that having a type, and typing C functions with it would solve a lot of
> problems.

You have basically a type only used for 0-terminated strings, char*, in 
D you use normally string, and if you wanna represent binary data you 
use ubyte[], I've never used char* except for interfacing with C. I 
would prefer a library soulution, some kind of Struct which is 
implicitly convertable to char* (0-terminates) and also to string (not 
0-terminated), not sure how to implement that though.


Gmane