Narrow string is not a random access range

mist none at none.none
Wed Oct 24 03:42:59 PDT 2012


On Tuesday, 23 October 2012 at 17:36:53 UTC, Simen Kjaeraas wrote:
> On 2012-10-23, 19:21, mist wrote:
>
>> Hm, and all phobos functions should operate on narrow strings 
>> as if they where not random-acessible? I am thinking about 
>> something like commonPrefix from std.algorithm, which operates 
>> on code points for strings.
>
> Preferably, yes. If there are performance (or other) benefits 
> from
> operating on code units, and it's just as safe, then operating 
> on code
> units is ok.

Probably I don't undertsand it fully, but D approach has always 
been "safe first, fast with some additional syntax". Back to 
commonPrefix and take:

==========================
import std.stdio, std.traits, std.algorithm, std.range;

void main()
{
	auto beer = "Пиво";
	auto r1 = beer.take(2);
	auto pony = "Пони";
	auto r2 = commonPrefix(beer, pony);
	writeln(r1);
	writeln(r2);
}
==========================

First one returns 2 symbols. Second one - 3 code points and 
broken string. There is no way such incosistency by-default in 
standard library is understandable by a newbie.


More information about the Digitalmars-d-learn mailing list