Proposal for fixing dchar ranges

Steven Schveighoffer schveiguy at yahoo.com
Mon Mar 10 13:06:25 PDT 2014


On Mon, 10 Mar 2014 14:54:22 -0400, Johannes Pfau <nospam at example.com>  
wrote:

> Am Mon, 10 Mar 2014 13:55:00 -0400
> schrieb "Steven Schveighoffer" <schveiguy at yahoo.com>:
>
>> On Mon, 10 Mar 2014 13:06:08 -0400, Brad Anderson <eco at gnuk.net>
>> wrote:
>>
>> > It seems like this would be an even bigger breaking change than
>> > Walter's proposal though (right or wrong, slicing strings is very
>> > common).
>>
>> You're the second person to mention that, I was not planning on
>> disabling string slicing. Just random access to individual chars, and
>> probably .length.
>>
>> -Steve
>
> Unfortunately slicing by code units is probably the most important
> safety issue with the current implementation: As was mentioned in the
> other thread:
>
> size_t index = str.countUntil('a');
> auto slice = str[0..index];
>
> This can be a safety and security issue. (I realize that this would
> break lots of code so I'm not sure if we should/can fix it. But I think
> this was the most important problem mentioned in the other thread.)

Slicing can never be a code point based operation. It would be too slow  
(read linear complexity). What needs to be broken is the expectation that  
an index is the number of code points or characters in a string. Think of  
an index as a position that has no real meaning except they are ordered in  
the stream. Like a set of ordered numbers, not necessarily consecutive.  
The index 4 may not exist, while 5 does.

At this point, my proposal does not fix that particular problem, but I  
don't think there's any way to fix that "problem" except to train the user  
who wrote it not to do that. However, it does not leave us in a worse  
position.

-Steve


More information about the Digitalmars-d mailing list