Bug in countUntil?

monarch_dodra monarchdodra at gmail.com
Fri Oct 12 12:27:08 PDT 2012


On Friday, 12 October 2012 at 19:17:13 UTC, Jonathan M Davis 
wrote:
> On Friday, October 12, 2012 21:02:47 monarch_dodra wrote:
>> I was looking in countUntil to fix another issue, and I think 
>> the
>> string support is broken
>> 
>> This program:
>> //----
>> import std.algorithm;
>> import std.stdio;
>> 
>> void main()
>> {
>>      "日本語".countUntil('本').writeln();
>> }
>> //----
>> 
>> Will produce "3".
>> 
>> ...
>> 
>> I'd have straight up said it was a bug, but the implementation
>> goes out of its way to special case narrow strings, when the
>> default implementation would have produced the right result
>> anyway. So I was thinking it is somehow by design...?
>> 
>> Am I missing something, or is it just implementation sillyness?
>
> Many algorithms special case narrow strings for efficiency. 
> However, in this
> case, it looks just plain wrong. countUntil is supposed to 
> return the number
> of elements (i.e. code points in this case), but it looks like 
> it's returning
> the number of code units. So, I'd say that it's definitely 
> wrong. If you want
> code units, then use std.string.indexOf. countUntil is supposed 
> to return the
> number of code points.
>
> - Jonathan M Davis

yeah, that's what I thought, but wanted it double checked. I'll 
take care of it then.


More information about the Digitalmars-d mailing list