[phobos] Annoying whitespace diffs

Walter Bright walter at digitalmars.com
Sun Aug 22 14:20:41 PDT 2010


A lot of checkins to Phobos wind up with a lot of invisible changes, 
because:

1. random addition and subtraction of tabs
2. random appearance and disappearance of trailing whitespace

I'd like to put an end to this, with the following rules:

1. no tabs are allowed
2. no trailing whitespace is allowed

I use this program to do filter my checkins which takes care of it, feel 
free to use it or some equivalent.
--------------------------------------------------------
/* Replace tabs with spaces, and remove trailing whitespace from lines.
 */

import std.file;
import std.path;

int main(string[] args)
{
    foreach (f; args[1 .. $])
    {
        auto input = cast(char[]) std.file.read(f);
        auto output = filter(input);
        if (output != input)
            std.file.write(f, output);
    }
    return 0;
}


char[] filter(char[] input)
{
    char[] output;
    size_t j;

    int column;
    for (size_t i = 0; i < input.length; i++)
    {
        auto c = input[i];

        switch (c)
        {
            case '\t':
                while ((column & 7) != 7)
                {   output ~= ' ';
                    j++;
                    column++;
                }
                c = ' ';
                column++;
                break;

            case '\r':
            case '\n':
                while (j && output[j - 1] == ' ')
                    j--;
                output = output[0 .. j];
                column = 0;
                break;

            default:
                column++;
                break;
        }
        output ~= c;
        j++;
    }
    while (j && output[j - 1] == ' ')
        j--;
    return output[0 .. j];
}



More information about the phobos mailing list