View mode: basic / threaded / horizontal-split · Log in · Help
October 01, 2012
Re: Idea: Introduce zero-terminated string specifier
On Sunday, 30 September 2012 at 20:27:16 UTC, Andrej Mitrovic 
wrote:
> On 9/30/12, deadalnix <deadalnix@gmail.com> wrote:
>> If you know that a string is 0 terminated, you can easily 
>> create a slice
>> from it as follow :
>>
>> char* myZeroTerminatedString;
>> char[]  myZeroTerminatedString[0 .. 
>> strlen(myZeroTerminatedString)];
>>
>> It is clean and avoid to modify the stdlib in an unsafe way.
>>
>
> What does that have to do with writef()? You can call 
> to!string, but
> that's beside the point. The point was getting rid of this 
> verbosity
> when using C APIs.

You should anyway wrap those APIs not to pollute D call with 
lower level APIs.

As such I don't find the verbosity, as you put it, that much of 
an issue.

Then again, I favor the Pascal family of languages for systems 
programming.

--
Paulo
October 01, 2012
Re: Idea: Introduce zero-terminated string specifier
On Monday, 1 October 2012 at 06:58:41 UTC, Paulo Pinto wrote:
> You should anyway wrap those APIs not to pollute D call with 
> lower level APIs.

I have to agree, esp when it applies to pointers.

We should not forget that one of the objectives of D is to make 
coding "safe" by getting rid of the need to use pointers and 
other unsafe features. It encourages safe practice by making safe 
practice much easier to do than using unsafe practice. It however 
allows unsafe practice where necessary, but the programmer has to 
intentionally do something extra to make that happen.

I think the suggestion of introducing a null string specifier 
fundamentally goes against the objectives of D, and if introduced 
will unltimately degrade the quality of the language.

--rt
October 01, 2012
Re: Idea: Introduce zero-terminated string specifier
Adam D. Ruppe wrote:
> On Saturday, 29 September 2012 at 02:11:12 UTC, Alex Rønne Petersen wrote:
>> While the idea is reasonable, the problem then becomes that if you
>> accidentally pass a non-zero terminated char* to %sz, all hell breaks
>> loose just like with printf.
>
> That's the same risk with to!string(), yes? We aren't really losing
> anything by adding it.
>
> Also this reminds me of the utter uselessness of the current behavior of
> "%s" and a pointer - it prints the address.

Why not specialize current "%s" for character pointer types so it will 
print null terminated strings? It's always possible to cast to void* to 
print an address.
October 01, 2012
Re: Idea: Introduce zero-terminated string specifier
On Monday, 1 October 2012 at 09:17:52 UTC, Piotr Szturmaj wrote:
> Adam D. Ruppe wrote:
>> On Saturday, 29 September 2012 at 02:11:12 UTC, Alex Rønne 
>> Petersen wrote:
>> Also this reminds me of the utter uselessness of the current 
>> behavior of
>> "%s" and a pointer - it prints the address.
>
> Why not specialize current "%s" for character pointer types so 
> it will print null terminated strings? It's always possible to 
> cast to void* to print an address.

It's not safe to assume that pointers to characters are generally 
null terminated.
October 01, 2012
Re: Idea: Introduce zero-terminated string specifier
Jakob Ovrum wrote:
> On Monday, 1 October 2012 at 09:17:52 UTC, Piotr Szturmaj wrote:
>> Adam D. Ruppe wrote:
>>> On Saturday, 29 September 2012 at 02:11:12 UTC, Alex Rønne Petersen
>>> wrote:
>>> Also this reminds me of the utter uselessness of the current behavior of
>>> "%s" and a pointer - it prints the address.
>>
>> Why not specialize current "%s" for character pointer types so it will
>> print null terminated strings? It's always possible to cast to void*
>> to print an address.
>
> It's not safe to assume that pointers to characters are generally null
> terminated.

Yes, but programmer should know what he's passing anyway.
October 01, 2012
Re: Idea: Introduce zero-terminated string specifier
On Monday, October 01, 2012 11:18:16 Piotr Szturmaj wrote:
> Adam D. Ruppe wrote:
> > On Saturday, 29 September 2012 at 02:11:12 UTC, Alex Rønne Petersen wrote:
> >> While the idea is reasonable, the problem then becomes that if you
> >> accidentally pass a non-zero terminated char* to %sz, all hell breaks
> >> loose just like with printf.
> > 
> > That's the same risk with to!string(), yes? We aren't really losing
> > anything by adding it.
> > 
> > Also this reminds me of the utter uselessness of the current behavior of
> > "%s" and a pointer - it prints the address.
> 
> Why not specialize current "%s" for character pointer types so it will
> print null terminated strings? It's always possible to cast to void* to
> print an address.

Honestly? One of Phobos' best features is the fact that %s works for 
_everything_. Specializing it for _anything_ would be horrible. It would also 
break a _ton_ of code. Who even uses %d, %f, etc. if they don't need to use 
format specifiers? It's just way simpler to always use %s.

I'm not completely against the idea of %zs, but I confess that I have to 
wonder what someone is doing if they really need to print zero-terminated 
strings all that often in D for anything other than quick debugging (in which 
case to!string works just fine), since only stuff directly interacting with C 
code will even care. And if it's really that big a deal, and you're constantly 
interacting with C code like that, you can always use the appropriate C 
function - printf - and then it's a non-issue.

- Jonathan M Davis
October 01, 2012
Re: Idea: Introduce zero-terminated string specifier
On Monday, 1 October 2012 at 09:42:08 UTC, Piotr Szturmaj wrote:
> Jakob Ovrum wrote:
>> On Monday, 1 October 2012 at 09:17:52 UTC, Piotr Szturmaj 
>> wrote:
>>> Adam D. Ruppe wrote:
>>>> On Saturday, 29 September 2012 at 02:11:12 UTC, Alex Rønne 
>>>> Petersen
>>>> wrote:
>>>> Also this reminds me of the utter uselessness of the current 
>>>> behavior of
>>>> "%s" and a pointer - it prints the address.
>>>
>>> Why not specialize current "%s" for character pointer types 
>>> so it will
>>> print null terminated strings? It's always possible to cast 
>>> to void*
>>> to print an address.
>>
>> It's not safe to assume that pointers to characters are 
>> generally null
>> terminated.
>
> Yes, but programmer should know what he's passing anyway.

The thinking "the programmer should" only works in one man teams.

As soon as you start having teams with disparate programming 
knowledge among team members, you can forget everything about 
"the programmer should".

..
Paulo
October 01, 2012
Re: Idea: Introduce zero-terminated string specifier
Le 30/09/2012 21:58, Vladimir Panteleev a écrit :
> On Sunday, 30 September 2012 at 18:31:00 UTC, deadalnix wrote:
>> If you know that a string is 0 terminated, you can easily create a
>> slice from it as follow :
>>
>> char* myZeroTerminatedString;
>> char[] myZeroTerminatedString[0 .. strlen(myZeroTerminatedString)];
>>
>> It is clean and avoid to modify the stdlib in an unsafe way.
>
> That's what to!string already does.

How does to!string know that the string is 0 terminated ?
October 01, 2012
Re: Idea: Introduce zero-terminated string specifier
Paulo Pinto wrote:
> On Monday, 1 October 2012 at 09:42:08 UTC, Piotr Szturmaj wrote:
>> Jakob Ovrum wrote:
>>> On Monday, 1 October 2012 at 09:17:52 UTC, Piotr Szturmaj wrote:
>>>> Adam D. Ruppe wrote:
>>>>> On Saturday, 29 September 2012 at 02:11:12 UTC, Alex Rønne Petersen
>>>>> wrote:
>>>>> Also this reminds me of the utter uselessness of the current
>>>>> behavior of
>>>>> "%s" and a pointer - it prints the address.
>>>>
>>>> Why not specialize current "%s" for character pointer types so it will
>>>> print null terminated strings? It's always possible to cast to void*
>>>> to print an address.
>>>
>>> It's not safe to assume that pointers to characters are generally null
>>> terminated.
>>
>> Yes, but programmer should know what he's passing anyway.
>
> The thinking "the programmer should" only works in one man teams.
>
> As soon as you start having teams with disparate programming knowledge
> among team members, you can forget everything about "the programmer
> should".

I experienced such team at my previous work and I know what you mean. My 
original thoughts was based on telling writef that I want print a 
null-terminated string rather than address. to!string will surely work, 
but it implies double iteration, one in to!string to calculate length 
(seeking for 0 char) and one in writef (printing). With long strings 
this is suboptimal. What about something like this:

struct CString(T)
    if (isSomeChar!T)
{
    T* str;
}

@property
auto cstring(S : T*, T)(S str)
    if (isSomeChar!T)
{
    return CString!T(str);
}

string test = "abc";
immutable(char)* p = test.ptr;

writefln("%s", p.cstring); // prints "abc"

Here the char pointer type is "annotated" as null terminated string and 
writefln can use this information.
October 01, 2012
Re: Idea: Introduce zero-terminated string specifier
On Monday, 1 October 2012 at 10:56:36 UTC, deadalnix wrote:
> Le 30/09/2012 21:58, Vladimir Panteleev a écrit :
>> On Sunday, 30 September 2012 at 18:31:00 UTC, deadalnix wrote:
>>> If you know that a string is 0 terminated, you can easily 
>>> create a
>>> slice from it as follow :
>>>
>>> char* myZeroTerminatedString;
>>> char[] myZeroTerminatedString[0 .. 
>>> strlen(myZeroTerminatedString)];
>>>
>>> It is clean and avoid to modify the stdlib in an unsafe way.
>>
>> That's what to!string already does.
>
> How does to!string know that the string is 0 terminated ?

By convention (it doesn't).
1 2 3 4 5 6
Top | Discussion index | About this forum | D home