All of lore.kernel.org
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: Jeff Garzik <jeff@garzik.org>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [SCRIPT] chomp: trim trailing whitespace
Date: Sun, 28 May 2006 02:24:35 -0700	[thread overview]
Message-ID: <44796C53.5090408@zytor.com> (raw)
In-Reply-To: <44783B08.1040803@garzik.org>

[-- Attachment #1: Type: text/plain, Size: 907 bytes --]

Jeff Garzik wrote:
> H. Peter Anvin wrote:
>> Jeff Garzik wrote:
>>>
>>> Attached to this email is chomp.pl, a Perl script which removes 
>>> trailing whitespace from several files.  I've had this for years, as 
>>> trailing whitespace is one of my pet peeves.
>>>
>>> Now that git-applymbox complains loudly whenever a patch adds 
>>> trailing whitespace, I figured this script may be useful to others.
>>>
>>
>> This is the script I use for the same purpose.  It's a bit more 
>> sophisticated, in that it detects and avoids binary files, and doesn't 
>> throw an error if it encounters a directory (which can happen if you 
>> give it a wildcard.)
> 
> Chewing the EOF blanks is nice.  The only nit I have is that your script 
> rewrites the file even if nothing was changed.
> 

Ah, good point.  Attached version fixes that.  It still doesn't break 
hard links, which may be a desirable feature.

	-hpa

[-- Attachment #2: cleanfile --]
[-- Type: text/plain, Size: 1418 bytes --]

#!/usr/bin/perl
#
# Clean a text file of stealth whitespace
#

use bytes;

$name = 'cleanfile';

foreach $f ( @ARGV ) {
    print STDERR "$name: $f\n";

    if (! -f $f) {
	print STDERR "$f: not a file\n";
	next;
    }
    
    if (!open(FILE, '+<', $f)) {
	print STDERR "$name: Cannot open file: $f: $!\n";
	next;
    }

    binmode FILE;

    # First, verify that it is not a binary file
    $is_binary = 0;

    while (read(FILE, $data, 65536) > 0) {
	if ($data =~ /\0/) {
	    $is_binary = 1;
	    last;
	}
    }

    if ($is_binary) {
	print STDERR "$name: $f: binary file\n";
	next;
    }

    seek(FILE, 0, 0);

    $in_bytes = 0;
    $out_bytes = 0;
    $blank_bytes = 0;

    @blanks = ();
    @lines  = ();

    while ( defined($line = <FILE>) ) {
	$in_bytes += length($line);
	$line =~ s/[ \t\r\n]*$/\n/;

	if ( $line eq "\n" ) {
	    push(@blanks, $line);
	    $blank_bytes += length($line);
	} else {
	    push(@lines, @blanks);
	    $out_bytes += $blank_bytes;
	    push(@lines, $line);
	    $out_bytes += length($line);
	    @blanks = ();
	    $blank_bytes = 0;
	}
    }

    # Any blanks at the end of the file are discarded

    if ($in_bytes != $out_bytes) {
	# Only write to the file if changed
	seek(FILE, 0, 0);
	print FILE @lines;

	if ( !defined($where = tell(FILE)) ||
	     !truncate(FILE, $where) ) {
	    die "$name: Failed to truncate modified file: $f: $!\n";
	}
    }

    close(FILE);
}

  reply	other threads:[~2006-05-28  9:25 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-05-27  2:27 [SCRIPT] chomp: trim trailing whitespace Jeff Garzik
2006-05-27  4:17 ` H. Peter Anvin
2006-05-27 11:42   ` Jeff Garzik
2006-05-28  9:24     ` H. Peter Anvin [this message]
2006-05-27 10:15 ` Jan Engelhardt
2006-05-27 10:24   ` Thomas Glanzmann
2006-05-27 10:36     ` Neil Brown
2006-05-27 11:32   ` Jeff Garzik
2006-05-27 11:48     ` Dmitry Fedorov
2006-05-27 12:42     ` Jan Engelhardt
2006-05-28  8:33       ` Keith Owens
2006-05-27 15:28 ` Martin Langhoff
2006-05-27 16:13   ` Linus Torvalds
2006-05-28 10:00     ` Johannes Schindelin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=44796C53.5090408@zytor.com \
    --to=hpa@zytor.com \
    --cc=jeff@garzik.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.