package Sequenic.P2; import java.util.*; /** * Provides a bunch of methods to format a string. */ public class StringFormater { static public String mkSpace(int n) { String s = ""; for (; 0 < n; n--) { s = s + " "; } return s; } /** * Pad the string on the left to the given width. */ static public String padLeft(String s, int N) { return (mkSpace(N - s.length()) + s); } /** * Pad the string on the right to the given width. */ static public String padRight(String s, int N) { return (s + mkSpace(N - s.length())); } /** * Reformat s to possibly multi lines such that each is at most of * the specified width. The formatting is token-based. */ static public List alignLeft(String s, int width) { List result = new LinkedList(); StringTokenizer scanner = new StringTokenizer(s, " \t\n", false); int k = 0; String tok = null; String line = ""; if (scanner.hasMoreElements()) { line = scanner.nextToken(); k = line.length(); } while (scanner.hasMoreElements()) { tok = scanner.nextToken(); //System.out.println(">: " + k) ; if (k < width) { line = line + " " + tok; k = k + tok.length() + 1; } else { result.add(line); line = tok; k = line.length(); } } result.add(line); return result; } static private String indent(String[] lines, int n) { String space = mkSpace(n) ; String result = "" ; int i=0 ; int N = lines.length ; if (N>0) { result = space + lines[0] ; i++ ; } else return result ; while (i lines = alignLeft(s, width); String[] lines_ = new String[lines.size()] ; for (int i=0; i