Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#

Untabify and Tabify

Rate me:
Please Sign up or sign in to vote.
3.31/5 (7 votes)
16 Jan 2009CPOL3 min read 41.6K   121   3   3
Untabify and Tabify Extension Methods for intending code.

Background

In the never-ending debate over whether to use SPACEs or TABs for indenting code, I prefer SPACEs. Having mentioned that I had written an Untabify utility, I was asked for an article. I have now rewritten the code in C#, and added a companion Tabify utility for readers who are in that camp. I implemented the methods as Extension Methods. I'm not a fan of Extension Methods, but this seemed like a reasonable use for them.

Another implementation choice was to keep the general layout of the two methods similar. At first, I wrote Untabify with a switch, but the switch was problematic in Tabify, so I changed both to use nested ifs.

Both methods use a StringBuilder, and stop processing the string as soon as a non-whitespace character or the end of the line is reached.

Untabify method

The Untabify method iterates the string, appending any SPACEs it encounters, and replacing any TAB characters with an appropriate number of SPACEs to reach the next tab stop. If the caller specifies four SPACEs per indent and a string begins with [ SPACE SPACE TAB ], then the TAB will be replaced by two SPACEs.

The caller may also specify zero SPACEs per indent to simply remove TABs from the leading whitespace.

C#
public static string
Untabify
(
    this string String
,
    byte        SpacesPerIndent
)
{
    /* Let it throw the Exception */
    System.Text.StringBuilder result = 
       new System.Text.StringBuilder ( String.Length ) ;
 
    int offset = 0 ;
 
    while ( offset < String.Length )
    {
        if ( String [ offset ] == ' ' )
        {
            result.Append ( ' ' ) ;
 
            offset++ ;
        }
        else
        {
            if ( String [ offset ] == '\t' )
            {
                if ( SpacesPerIndent > 0 )
                {
                    for ( int i = result.Length % SpacesPerIndent ; 
                          i < SpacesPerIndent ; i++ )
                    {
                        result.Append ( ' ' ) ;
                    }
                }
 
                offset++ ;
            }
            else
            {
                break ;
            }
        }
    }
 
    result.Append ( String.Substring ( offset ) ) ;
 
    return ( result.ToString() ) ;
}

Tabify method

The Tabify method iterates the string, appending any TABs it encounters and removing any SPACE characters. If the caller specifies four SPACEs per indent and a string begins with [ SPACE SPACE TAB ], then the two SPACEs will be removed.

A contiguous sequence of SPACEs of length equal to SpacesPerIndent will cause a TAB to be appended. If the caller specifies four SPACEs per indent and a string begins with [ SPACE SPACE SPACE SPACE ], then the four SPACEs will be replaced by a TAB.

The caller may also specify zero SPACEs per indent to simply remove SPACEs from the leading whitespace.

(See below for information on TabifyMode.)

C#
public static string
Tabify
(
    this string String
,
    byte        SpacesPerIndent
,
    TabifyMode  Mode
)
{
    /* Let it throw the Exception */
    System.Text.StringBuilder result = 
       new System.Text.StringBuilder ( String.Length ) ;
 
    int offset = 0 ;
    int spaces = 0 ;
 
    while ( offset < String.Length )
    {
        if ( String [ offset ] == ' ' )
        {
            if ( ++spaces == SpacesPerIndent )
            {
                result.Append ( '\t' ) ;
 
                spaces = 0 ;
            }
 
            offset++ ;
        }
        else
        {
            if ( String [ offset ] == '\t' )
            {
                result.Append ( '\t' ) ;
 
                spaces = 0 ;
 
                offset++ ;
            }
            else
            {
                break ;
            }
        }
    }
 
    switch ( Mode )
    {
        case TabifyMode.Retain :
        {
            while ( spaces-- > 0 )
            {
                result.Append ( ' ' ) ;
            }
 
            break ;
        }
 
        case TabifyMode.Extend :
        {
            result.Append ( '\t' ) ;
 
            break ;
        }
    }
 
    result.Append ( String.Substring ( offset ) ) ;
 
    return ( result.ToString() ) ;
}

TabifyMode enumeration

The TabifyMode enumeration controls what Untabify does with "extra" SPACEs.

If the caller specifies four SPACEs per indent and a string begins with [ SPACE SPACE SPACE SPACE SPACE SPACE NON-WHITESPACE ], the first four SPACEs cause one TAB to be appended, but what does the caller want to do with the other two?

Visual Studio's tabify feature will leave the two SPACEs in place, so that's the default behavior (Retain). The caller may specify Truncate to remove them, or Extend to append a TAB in their place.

C#
public enum TabifyMode
{
    Retain
,
    Truncate
,
    Extend
}

Using the code

Using these methods is quite simple; they're Extension Methods, so add an appropriate using directive and use them as if they belong to the string class. I prefer to put each Extension Method (or group of overloaded ones) I write in its own namespace, so my using directives specify exactly which methods I'm importing.

C#
using PIEBALD.Lib.LibExt.Untabify ;
using PIEBALD.Lib.LibExt.Tabify ;
 
string s = "  \t" ;
 
s.Untabify ( 4 ) ; // yields "    "
s.Tabify ( 4 ) ;   // yields "\t"

Untabify utility

This is a very simple console application that reads the lines in a file, right-trims and untabifies each, then writes each out to another file.

It can be built with csc Untabify.cs LibExt.Untabify.cs.

C#
namespace Untabify
{
    using PIEBALD.Lib.LibExt.Untabify ;
 
    public static class Untabify
    {
        [System.STAThreadAttribute()]
        public static int
        Main
        (
            string[] args
        )
        {
            int result = 0 ;
 
            try
            {
                if ( args.Length == 3 )
                {
                    using ( System.IO.TextReader tr = 
                              new System.IO.StreamReader ( args [ 0 ] ) )
                    {
                        using ( System.IO.TextWriter tw = 
                                   new System.IO.StreamWriter ( args [ 1 ] ) )
                        {
                            byte spt = byte.Parse ( args [ 2 ] ) ;
 
                            string line ;
 
                            while ( ( line = tr.ReadLine() ) != null )
                            {
                                tw.WriteLine ( line.TrimEnd().Untabify ( spt ) ) ;
                            }
                        }
                    }
                }
                else
                {
                    System.Console.WriteLine ( "Syntax: Untabify" + 
                                   " infile outfile spacesperindent" ) ;
                }
            }
            catch ( System.Exception err )
            {
                while ( err != null )
                {
                    System.Console.WriteLine ( err ) ;

                    err = err.InnerException ;
                }
            }
 
            return ( result ) ;
        }
    }
}

Tabify utility

This is a very simple console application that reads the lines in a file, right-trims and tabifies each, then writes each out to another file.

It can be built with csc Tabify.cs LibExt.Tabify.cs.

C#
namespace Tabify
{
    using PIEBALD.Lib.LibExt.Tabify ;
 
    public static class Tabify
    {
        [System.STAThreadAttribute()]
        public static int
        Main
        (
            string[] args
        )
        {
            int result = 0 ;
 
            try
            {
                if ( args.Length == 3 )
                {
                    using ( System.IO.TextReader tr = 
                               new System.IO.StreamReader ( args [ 0 ] ) )
                    {
                        using ( System.IO.TextWriter tw = 
                                   new System.IO.StreamWriter ( args [ 1 ] ) )
                        {
                            byte spt = byte.Parse ( args [ 2 ] ) ;
 
                            string line ;
 
                            while ( ( line = tr.ReadLine() ) != null )
                            {
                                tw.WriteLine ( line.TrimEnd().Tabify ( spt ) ) ;
                            }
                        }
                    }
                }
                else
                {
                    System.Console.WriteLine ( "Syntax: Tabify" + 
                                   " infile outfile spacesperindent" ) ;
                }
            }
            catch ( System.Exception err )
            {
                while ( err != null )
                {
                    System.Console.WriteLine ( err ) ;
 
                    err = err.InnerException ;
                }
            }
 
            return ( result ) ;
        }
    }
}

History

  • 2009-01-14 - First submitted.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
BSCS 1992 Wentworth Institute of Technology

Originally from the Boston (MA) area. Lived in SoCal for a while. Now in the Phoenix (AZ) area.

OpenVMS enthusiast, ISO 8601 evangelist, photographer, opinionated SOB, acknowledged pedant and contrarian

---------------

"I would be looking for better tekkies, too. Yours are broken." -- Paul Pedant

"Using fewer technologies is better than using more." -- Rico Mariani

"Good code is its own best documentation. As you’re about to add a comment, ask yourself, ‘How can I improve the code so that this comment isn’t needed?’" -- Steve McConnell

"Every time you write a comment, you should grimace and feel the failure of your ability of expression." -- Unknown

"If you need help knowing what to think, let me know and I'll tell you." -- Jeffrey Snover [MSFT]

"Typing is no substitute for thinking." -- R.W. Hamming

"I find it appalling that you can become a programmer with less training than it takes to become a plumber." -- Bjarne Stroustrup

ZagNut’s Law: Arrogance is inversely proportional to ability.

"Well blow me sideways with a plastic marionette. I've just learned something new - and if I could award you a 100 for that post I would. Way to go you keyboard lovegod you." -- Pete O'Hanlon

"linq'ish" sounds like "inept" in German -- Andreas Gieriet

"Things would be different if I ran the zoo." -- Dr. Seuss

"Wrong is evil, and it must be defeated." –- Jeff Ello

"A good designer must rely on experience, on precise, logical thinking, and on pedantic exactness." -- Nigel Shaw

“It’s always easier to do it the hard way.” -- Blackhart

“If Unix wasn’t so bad that you can’t give it away, Bill Gates would never have succeeded in selling Windows.” -- Blackhart

"Use vertical and horizontal whitespace generously. Generally, all binary operators except '.' and '->' should be separated from their operands by blanks."

"Omit needless local variables." -- Strunk... had he taught programming

Comments and Discussions

 
GeneralMy vote of 3 Pin
euggg123456785-Oct-11 13:52
euggg123456785-Oct-11 13:52 
Generalliteral strings Pin
Luc Pattyn16-Jan-09 5:43
sitebuilderLuc Pattyn16-Jan-09 5:43 
GeneralRe: literal strings Pin
PIEBALDconsult16-Jan-09 6:01
mvePIEBALDconsult16-Jan-09 6:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.