[OT] What is more readable?

simendsjo simen.endsjo at pandavre.com
Mon Aug 9 17:20:07 PDT 2010


Continuing "my what is more readable" thread (just shut me up, but I 
don't always agree with i, j, k etc...):

std.string.count:

size_t count(string s, string sub)
{
     size_t i;
     int j;
     int count = 0;

     for (i = 0; i < s.length; i += j + sub.length)
     {
         j = indexOf(s[i .. s.length], sub);
         if (j == -1)
             break;
         count++;
     }
     return count;
}




size_t count(string s, string sub)
{
     int result = 0;

     int subStart = 0;
     for (size_t restStart = 0; restStart < s.length; restStart += 
subStart + sub.length)
     {
         auto rest = s[restStart .. $];
         subStart = indexOf(rest, sub);

         bool notFound = (subStart == -1);
         if (notFound)
             break;

         result++;
     }

     return result;
}


More information about the Digitalmars-d-learn mailing list