Java Reference
In-Depth Information
outCol += consumedSpaces ;
consumedSpaces = 0 ;
}
continue
continue ;
}
// We're at a non-space; if we're just past a tab stop, we need
// to put the "leftover" spaces back out, since we consumed
// them above.
while
while ( inCol - 1 > outCol ) {
Debug . println ( "pad" , "Padding space at " + inCol );
sb . append ( ' ' );
outCol ++;
}
// Now we have a plain character to output.
sb . append ( ch );
outCol ++;
}
// If line ended with trailing (or only!) spaces, preserve them.
for
for ( int
int i = 0 ; i < consumedSpaces ; i ++) {
Debug . println ( "trail" , "Padding space at end # " + i );
sb . append ( ' ' );
}
return
return sb . toString ();
}
}
This code was patterned after a program in Kernighan and Plauger's classic work, Software
Tools . While their version was in a language called RatFor (Rational Fortran), my version
has since been through several translations. Their version actually worked one character at a
time, and for a long time I tried to preserve this overall structure. Eventually, I rewrote it to
be a line-at-a-time program.
The program that goes in the opposite direction—putting tabs in rather than taking them
out—is the DeTab class shown in Example 3-8 ; only the core methods are shown.
Example 3-8. DeTab.java
public
public class
class DeTab
DeTab {
Tabs ts ;
public
public static
static void
void main ( String [] argv ) throws
throws IOException {
DeTab dt = new
new DeTab ( 8 );
Search WWH ::




Custom Search