commonLength

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 15 12:10:51 PST 2015


On Thursday, 15 January 2015 at 19:49:14 UTC, Ali Çehreli wrote:
> auto commonPrefixLength(S, T)(S a, T b)
> {
>     import std.range: zip;
>     import std.algorithm: count;
>     return zip(a, b).count!(ab => ab[0] == ab[1]);
> }
>
> unittest
> {
>     assert(commonPrefixLength("açde", "") == 0);
>     assert(commonPrefixLength("açde", "xyz") == 0);
>     assert(commonPrefixLength("açd", "açde") == 3);
>     assert(commonPrefixLength("açdef", "açdef") == 5);
> }

I want to terminate count on first mismatch like

     assert(commonPrefixLength("abc_d", "abc-d") == 3);

Your version will

     assert(commonPrefixLength("abc_d", "abc-d") == 4);


More information about the Digitalmars-d-learn mailing list