[OT] Is this more readable, or just way too verbose?

simendsjo simen.endsjo at pandavre.com
Tue Aug 10 04:54:51 PDT 2010


Lutger wrote:
> simendsjo wrote:
> 
(...)
> 
> The CR and LF constants are a bit too much, probably because they don't really 
> abstract over the literals which I can actually parse faster. The isCR and isLF 
> are nice however. Taking it a step further:
> 
> bool canSplit = inPattern(c,"\r\n");
> if (canSplit)
> {
>   ...
> 
> You have increased the nesting of ifs to 3 inside a for-loop.Personally I don't 
> read deep nesting very well. To go for readability I would use a small function 
> for the entire expression: 
> 
> if( s[i..$].startsWithCRLF() ) // same as startsWithCRLF(s[i..$])
> {
>   i++;
>   istart++;
> }
> 
> or use std.algorithm: if ( s[i..$].startsWith("\r\n") )
>  
(...)
Nice. I didn't increase the if nesting though.

Something like this then?

S[] mysplitlines(S)(S s)
{
     size_t istart;

     auto result = Appender!(S[])();

     foreach (i; 0 .. s.length)
     {
         immutable c = s[i];
         immutable isEOL = inPattern(c, "\r\n");
         if (isEOL)
         {
             auto beforeEOL = s[istart .. i];
             result.put(beforeEOL);

             auto rest = s[i .. $];
             immutable isCRLF = rest.startsWith("\r\n");

             istart = i + 1; // consume first EOL character
             if (isCRLF) // skip \n too
             {
                 i++;
                 istart++;
             }
         }
     }

     // The last line might not end with EOL
     immutable lineNotEmpty = (istart != s.length);
     if (lineNotEmpty)
     {
         auto lastLine = s[istart .. $];
         result.put(lastLine);
     }

     return result.data;
}


More information about the Digitalmars-d-learn mailing list