February 15, 2012 Re: Stride | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | "Jonathan M Davis" <jmdavisProg@gmx.com> wrote in message news:mailman.306.1329166430.20196.digitalmars-d-learn@puremagic.com... > Ideally perhaps, but I expect that that's not true, because operator overloading is done via lowering. > > foo() ~ bar() > > would become > > opBinary!"~"(foo(), bar()); > While your point is still correct, this will generally be lowered to foo().opBinary!"~"(bar()) or bar().opBinaryRight!"~"(foo()) Both of which do have a defined order of evaluation. |
February 15, 2012 Re: Stride | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Murphy | On Wednesday, February 15, 2012 17:32:07 Daniel Murphy wrote:
> "Jonathan M Davis" <jmdavisProg@gmx.com> wrote in message news:mailman.306.1329166430.20196.digitalmars-d-learn@puremagic.com...
>
> > Ideally perhaps, but I expect that that's not true, because operator overloading is done via lowering.
> >
> > foo() ~ bar()
> >
> > would become
> >
> > opBinary!"~"(foo(), bar());
>
> While your point is still correct, this will generally be lowered to
>
> foo().opBinary!"~"(bar())
>
> or
>
> bar().opBinaryRight!"~"(foo())
>
> Both of which do have a defined order of evaluation.
Ah, good point.
- Jonathan M Davis
|
March 14, 2012 Re: Stride | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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"
|
Copyright © 1999-2021 by the D Language Foundation