开发者

Is there a better way to detab (expand tabs) using Perl?

I wanted to detab my source files. (Please, no flame about WHY I wanted to detab my sources. That's not the point :-) I couldn't find a utility to do that. Eclipse didn't do it for me, so I implemented my own.

I couldn't fit it into a one liner (-e) program. I came with the following, which did the job just fine.

while( <> )
{
    while( /\t/ ) {
        s/^(([^\t]{4})*)\t/$1    /;
        s/^((([^\t]{4})*)[^\t]{1})\t/$1   /;
        s/^((([^\t]{4})*)[^\t]{2})\t/$1  /;
        s/^((([^\t]{4})*)[^\t]{3})\t/$1 /;
    }
    print;
}

However, it makes me wonder if Perl - the champion language of processing text - is the right tool. The code d开发者_开发百科oesn't seem very elegant. If I had to detab source that assume tab=8 spaces, the code would look even worse.

Specifically because I can think of a deterministic state machine with only 4 states to do the job.

I have a feeling that a more elegant solution exists. Am I missing a Perl idiom? In the spirit of TIMTOWTDI I'm curious about the other ways to do it.

u.


What ever happened to the old Unix program "expand"? I used to use that all the time.


I remember a detabify script from one of the O'Reilly books, but I can't seem to find a link now.

I have had to solve this problem as well, and I settled on this concise solution to detabify a line:

1 while $line =~ s/\t/" " x ($tablength - ($-[0] % $tablength))/e ;

In this regular expression $-[0] is the length of the "pre-matched" portion of the line -- the number of characters before the tab character.


As a one-liner:

perl -pe '1 while s/\t/" "x(4-($-[0]%4))/e' input


Whenever I want to expand tabs, I use Text::Tabs.


This can be easily done in vim:

:set expandtab
:retab

http://vim.wikia.com/wiki/Converting_tabs_to_spaces


Can't let vi be all alone here. Emacs:

M-x tabify
M-x untabify


I do this in vim with:

:%s/^V^I/    /g

(That's a literal ^V followed by a literal tab), and then :%gq to fix incorrect spacing. Perl is overkill.


The exact expression is:

1 while $line =~ s/\t/" " x ($tablength+1 - ($-[0] % $tablength))/e ;

And expand is useful for command line not inside a program which may expand or not some lines.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜