tolf and detab

Norbert Nemec Norbert at Nemec-online.de
Sun Aug 8 01:31:32 PDT 2010


I usually do the same thing with a shell pipe
	expand | sed 's/ *$//;s/\r$//;s/\r/\n/'


On 07/08/10 02:34, Walter Bright wrote:
> I wrote these two trivial utilities for the purpose of canonicalizing
> source code before checkins and to deal with FreeBSD's inability to deal
> with CRLF line endings, and because I can never figure out the right
> settings for git to make it do the canonicalization.
>
> tolf - converts LF, CR, and CRLF line endings to LF.
>
> detab - converts all tabs to the correct number of spaces. Assumes tabs
> are 8 column tabs. Removes trailing whitespace from lines.
>
> Posted here just in case someone wonders what they are.
> ---------------------------------------------------------
> /* 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];
> }
> -----------------------------------------------------
> /* Replace line endings with LF
> */
>
> 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;
>
> for (size_t i = 0; i < input.length; i++)
> {
> auto c = input[i];
>
> switch (c)
> {
> case '\r':
> c = '\n';
> break;
>
> case '\n':
> if (i && input[i - 1] == '\r')
> continue;
> break;
>
> case 0:
> continue;
>
> default:
> break;
> }
> output ~= c;
> j++;
> }
> return output[0 .. j];
> }
> ------------------------------------------



More information about the Digitalmars-d mailing list