All pastes #49059 Raw Edit

UglyFormat

public java v1 · immutable
#49059 ·published 2006-04-11 19:48 UTC
rendered paste body
    /**     * Convert the passed text so that it fits to the passed     * number of columns. As well, insert the passed number of      * spaced at the beginning of lines.     */    private static String formatFixedWidth(String s, int width, int initialSpaces)    {        StringBuffer result = new StringBuffer(s.length()+10);        int curColumn = 0;        StringBuffer spaces = new StringBuffer(initialSpaces);        if ( width < 0 )            width = 1;        if ( initialSpaces > width )            initialSpaces = width - 1;                for(int i = 0; i < initialSpaces; i++)            spaces.append(' ');                int curChar = 0;        while( curChar < s.length() )        {            if (0 == curColumn )            {                // Insert initial spaces                result.append(spaces);                curColumn += initialSpaces;            }            while(curColumn < width && curChar < s.length())            {                result.append(s.charAt(curChar));                curChar++;                if ( s.charAt(curChar-1) == '\n' )                {                    curColumn = 0;                    break;                }                curColumn++;            }            if ( curColumn >= width )            {                boolean wordShifted = false;                // If we are breaking up a word, just put it onto a new line,                // unless the word makes up the whole line.                               if ( !Character.isWhitespace( s.charAt(curChar-1) ) )                {                    // Find word beginning                    int beg, end;                    for( beg = curChar-1; beg >= curChar - curColumn && s.charAt(beg) != ' '; beg--)                    { }                                    if ( beg >= curChar - curColumn)                    {                        // Found a space at beg! Check for end                        for( end = curChar; end < s.length() && s.charAt(end) != ' '; end++ )                        { }                                                if ( end < s.length() )                        {                            // Found a space at end!                            // Check if length is small enough for it to be delegated to a                             // new line.                            if ( end - beg - 1  < width - initialSpaces )                            {                                // Put word onto next line. Go back in output buffer                                // and place a newline at the space before the word,                                // and remove the current newline                                result.setCharAt(result.length() - (curChar - beg), '\n');                                result.insert(result.length() - (curChar - beg)+1, spaces);                                curColumn = curChar - beg - 1 + initialSpaces;                                wordShifted = true;                            }                        }                    }                }                    // Only add a newline if we didn't already add one by shifting the word                // to a new line                if ( !wordShifted )                {                    result.append('\n');                    curColumn = 0;                }            }        }        return result.toString();            }