tolf and detab

Jonathan M Davis jmdavisProg at gmail.com
Sat Aug 7 21:27:18 PDT 2010


Jonathan M Davis wrote:

> void removeTabs(int tabSize, string fileName)
> {
>     auto file = File(fileName);
>     string[] output;
> 
>     foreach(line; file.byLine())
>     {
>         int lastTab = 0;
> 
>         while(lastTab != -1)
>         {
>             const int tab = line.indexOf('\t');
> 
>             if(tab == -1)
>                 break;
> 
>             const int numSpaces = tabSize - tab % tabSize;
> 
>             line = line[0 .. tab] ~ repeat(" ", numSpaces) ~ line[tab + 1
>             .. $];
> 
>             lastTab = tab + numSpaces;
>         }
> 
>         output ~= line.idup;
>     }
> 
>     std.file.write(fileName, output.join("\n"));
> }

Actually, looking at the code again, that while loop really should be 
while(1) rather than while(lastTab != -1), but it will work the same 
regardless.

- Jonathan M Davis



More information about the Digitalmars-d mailing list