@Test public void testAdjustSpace() { StringBuilder s1 = new StringBuilder(" Do you like Java language? "); assertEquals("Do you like Java language?", TextHelper.adjustSpace(s1, 30, 30).toString()); assertEquals("Do you like Java language?", TextHelper.adjustSpace(s1, 31, 30).toString()); StringBuilder s2 = new StringBuilder(); assertEquals("", TextHelper.adjustSpace(s2, 30, 30).toString()); StringBuilder s3 = new StringBuilder("The Argparse4j"); assertEquals("The Argparse4j", TextHelper.adjustSpace(s3, 20, 14).toString()); }
public static StringBuilder adjustSpace(StringBuilder sb, int width, int curWidth) { int i, len = sb.length(); int origLen = len; i = 0; while (i < len && sb.charAt(i) == ' ') { ++i; } sb.delete(0, i); len = sb.length(); i = len - 1; while (i >= 0 && sb.charAt(i) == ' ') { --i; } sb.delete(i + 1, len); len = sb.length(); curWidth -= origLen - len; int numWsBlock = 0; boolean cont = false; for (i = 0; i < len; ++i) { if (sb.charAt(i) == ' ') { if (!cont) { cont = true; ++numWsBlock; } } else { cont = false; } } if (numWsBlock == 0) { return sb; } int needWs = width - curWidth; int eachWs = needWs / numWsBlock; int rem = needWs % numWsBlock; int remStart = (numWsBlock - rem + 1) / 2; int remEnd = remStart + rem; cont = false; int b = 0; for (i = 0; i < len; ++i) { if (sb.charAt(i) == ' ') { if (!cont) { cont = true; int add = eachWs + (remStart <= b && b < remEnd ? 1 : 0); for (int j = 0; j < add; ++j) { sb.insert(i, ' '); } len = sb.length(); ++b; } } else { cont = false; } } return sb; }
TextHelper { public static StringBuilder adjustSpace(StringBuilder sb, int width, int curWidth) { int i, len = sb.length(); int origLen = len; i = 0; while (i < len && sb.charAt(i) == ' ') { ++i; } sb.delete(0, i); len = sb.length(); i = len - 1; while (i >= 0 && sb.charAt(i) == ' ') { --i; } sb.delete(i + 1, len); len = sb.length(); curWidth -= origLen - len; int numWsBlock = 0; boolean cont = false; for (i = 0; i < len; ++i) { if (sb.charAt(i) == ' ') { if (!cont) { cont = true; ++numWsBlock; } } else { cont = false; } } if (numWsBlock == 0) { return sb; } int needWs = width - curWidth; int eachWs = needWs / numWsBlock; int rem = needWs % numWsBlock; int remStart = (numWsBlock - rem + 1) / 2; int remEnd = remStart + rem; cont = false; int b = 0; for (i = 0; i < len; ++i) { if (sb.charAt(i) == ' ') { if (!cont) { cont = true; int add = eachWs + (remStart <= b && b < remEnd ? 1 : 0); for (int j = 0; j < add; ++j) { sb.insert(i, ' '); } len = sb.length(); ++b; } } else { cont = false; } } return sb; } }
TextHelper { public static StringBuilder adjustSpace(StringBuilder sb, int width, int curWidth) { int i, len = sb.length(); int origLen = len; i = 0; while (i < len && sb.charAt(i) == ' ') { ++i; } sb.delete(0, i); len = sb.length(); i = len - 1; while (i >= 0 && sb.charAt(i) == ' ') { --i; } sb.delete(i + 1, len); len = sb.length(); curWidth -= origLen - len; int numWsBlock = 0; boolean cont = false; for (i = 0; i < len; ++i) { if (sb.charAt(i) == ' ') { if (!cont) { cont = true; ++numWsBlock; } } else { cont = false; } } if (numWsBlock == 0) { return sb; } int needWs = width - curWidth; int eachWs = needWs / numWsBlock; int rem = needWs % numWsBlock; int remStart = (numWsBlock - rem + 1) / 2; int remEnd = remStart + rem; cont = false; int b = 0; for (i = 0; i < len; ++i) { if (sb.charAt(i) == ' ') { if (!cont) { cont = true; int add = eachWs + (remStart <= b && b < remEnd ? 1 : 0); for (int j = 0; j < add; ++j) { sb.insert(i, ' '); } len = sb.length(); ++b; } } else { cont = false; } } return sb; } private  TextHelper(); }
TextHelper { public static StringBuilder adjustSpace(StringBuilder sb, int width, int curWidth) { int i, len = sb.length(); int origLen = len; i = 0; while (i < len && sb.charAt(i) == ' ') { ++i; } sb.delete(0, i); len = sb.length(); i = len - 1; while (i >= 0 && sb.charAt(i) == ' ') { --i; } sb.delete(i + 1, len); len = sb.length(); curWidth -= origLen - len; int numWsBlock = 0; boolean cont = false; for (i = 0; i < len; ++i) { if (sb.charAt(i) == ' ') { if (!cont) { cont = true; ++numWsBlock; } } else { cont = false; } } if (numWsBlock == 0) { return sb; } int needWs = width - curWidth; int eachWs = needWs / numWsBlock; int rem = needWs % numWsBlock; int remStart = (numWsBlock - rem + 1) / 2; int remEnd = remStart + rem; cont = false; int b = 0; for (i = 0; i < len; ++i) { if (sb.charAt(i) == ' ') { if (!cont) { cont = true; int add = eachWs + (remStart <= b && b < remEnd ? 1 : 0); for (int j = 0; j < add; ++j) { sb.insert(i, ' '); } len = sb.length(); ++b; } } else { cont = false; } } return sb; } private  TextHelper(); static String concat(T a[], int offset, String sep,
            String start, String end); static String concat(T a[], int offset, String sep); static String concat(Collection<T> a, int offset, String sep,
            String start, String end); static String concat(Collection<T> a, int offset, String sep); static String wrap(TextWidthCounter textWidthCounter, String s,
            int width, int initialOffset, String initialIndent,
            String subsequentIndent); static StringBuilder adjustSpace(StringBuilder sb, int width,
            int curWidth); static void printHelp(PrintWriter writer, String title, String help,
            TextWidthCounter textWidthCounter, int width); static String nonNull(String str); static List<String> findPrefix(Collection<String> src, String prefix); }
TextHelper { public static StringBuilder adjustSpace(StringBuilder sb, int width, int curWidth) { int i, len = sb.length(); int origLen = len; i = 0; while (i < len && sb.charAt(i) == ' ') { ++i; } sb.delete(0, i); len = sb.length(); i = len - 1; while (i >= 0 && sb.charAt(i) == ' ') { --i; } sb.delete(i + 1, len); len = sb.length(); curWidth -= origLen - len; int numWsBlock = 0; boolean cont = false; for (i = 0; i < len; ++i) { if (sb.charAt(i) == ' ') { if (!cont) { cont = true; ++numWsBlock; } } else { cont = false; } } if (numWsBlock == 0) { return sb; } int needWs = width - curWidth; int eachWs = needWs / numWsBlock; int rem = needWs % numWsBlock; int remStart = (numWsBlock - rem + 1) / 2; int remEnd = remStart + rem; cont = false; int b = 0; for (i = 0; i < len; ++i) { if (sb.charAt(i) == ' ') { if (!cont) { cont = true; int add = eachWs + (remStart <= b && b < remEnd ? 1 : 0); for (int j = 0; j < add; ++j) { sb.insert(i, ' '); } len = sb.length(); ++b; } } else { cont = false; } } return sb; } private  TextHelper(); static String concat(T a[], int offset, String sep,
            String start, String end); static String concat(T a[], int offset, String sep); static String concat(Collection<T> a, int offset, String sep,
            String start, String end); static String concat(Collection<T> a, int offset, String sep); static String wrap(TextWidthCounter textWidthCounter, String s,
            int width, int initialOffset, String initialIndent,
            String subsequentIndent); static StringBuilder adjustSpace(StringBuilder sb, int width,
            int curWidth); static void printHelp(PrintWriter writer, String title, String help,
            TextWidthCounter textWidthCounter, int width); static String nonNull(String str); static List<String> findPrefix(Collection<String> src, String prefix); static final Locale LOCALE_ROOT; static final String LINESEP; }