Stride

Chris W. wendlec at cd.ie
Wed Mar 14 05:29:10 PDT 2012


On Tuesday, 14 February 2012 at 21:09:09 UTC, Ali Çehreli wrote:
> On 02/14/2012 12:59 PM, RenatoL wrote:
>> mmmhhh.... this is interesting nevertheless i don't understand 
>> the
>> solution of my problem, and i cannot even understand it's 
>> origin:
>>
>> void main()
>> {
>>     string s1 = "abcd";
>>     s1 = s1[stride(s1,0)..1] ~ 'r' ~ s1[2..$];
>>     writeln(s1);
>> }
>>
>> why there is not way i cannot achive "arcd"?
>
> import std.stdio;
> import std.utf;
>
> void main()
> {
>     string s1 = "abcd";
>
>     immutable firstCharStride = stride(s1, 0);
>     immutable secondCharStride = stride(s1, firstCharStride);
>
>     auto firstPart = s1[0..firstCharStride];
>     auto lastPart = s1[firstCharStride + secondCharStride..$];
>
>     s1 = firstPart ~ 'r' ~ lastPart;
>     writeln(s1);
> }
>
> Ali

I have come across similar problems using stride.

Andrei Alexandrescu writes in his book on D that it is a subtle 
bug, if strings are not sliced using stride(s, index). However, 
if index is the length of another string there might be a 
mismatch, because stride() returns the "number of bytes in the 
UTF-8/16/32 sequence" which may not correspond to length/the 
index needed.

E.g. if you have a string "chat" and you want to slice it as 
follows:

auto word = "chat";
// Do some regex search ...
// m[0] is the matched start sequence "ch"
auto substring = word[m[0].length..$]; // "at"

Using stride() returns 1 and not 2 (length of "ch"), so you'd get 
"hat" instead of "at".

To avoid this and keep it "legal", is it possible to determine a 
string's length with std.utf.count() to determine the length of a 
string via utf code points (or toUCSindex())?

auto substring = word[std.utf.count(m[0])..$]; // "at"



More information about the Digitalmars-d-learn mailing list