List of usage examples for org.apache.commons.lang.text StrBuilder clear
public StrBuilder clear()
From source file:me.taylorkelly.mywarp.bukkit.util.FormattingUtils.java
/** * Joins the given parts, separated by blanks. The resulting string will be wrapped into multiple lines so that each * line is at most as wide as the given width. Each new line will start with the given prefix. *//from w ww. ja va 2s. co m * @param parts the parts * @param newLinePrefix the prefix for created new lines * @param wrappedWidth the width of each line * @return the wrapped string */ private static String wrappedJoin(Iterable<String> parts, String newLinePrefix, int wrappedWidth) { StrBuilder ret = new StrBuilder(); StrBuilder line = new StrBuilder(); for (String part : parts) { // if the word is longer than the maximum length, add chars as long // as possible than make a new line if (getWidth(part) > wrappedWidth) { for (char c : part.toCharArray()) { // if the maximum width is reached, make a new line if (getWidth(line.toString()) + getWidth(c) > wrappedWidth) { ret.appendln(line.toString()); line.clear(); line.append(newLinePrefix); } line.append(c); } // if the world AND the needed black is longer than the maximum // width make a new line and insert the word there } else { // if the maximum width is reached, make a new line if (getWidth(line.toString()) + getWidth(part) + getWidth(' ') > wrappedWidth) { ret.appendln(line.toString()); line.clear(); line.append(newLinePrefix); } // a blank is only needed if the line is not empty AND the last // char is not a a blank (e.g. from prefix) if (!line.isEmpty() && !line.rightString(1).equals(" ")) { line.append(' '); } line.append(part); } } // commit the line if (!line.isEmpty()) { ret.append(line); } return ret.toString(); }
From source file:mitm.djigzo.web.render.impl.AbstractInlineAddClassPattern.java
private void applyPattern(StrBuilder builder) { Pattern pattern = getPattern(); if (pattern != null) { Matcher matcher = pattern.matcher(builder.toString()); StrBuilder copy = null;//from www . j a v a 2 s . co m /* * Because we will modify the StrBuilder (replacing matches etc.) we need * to keep track of changes with respect to indexes. If there is a match * we will create a copy of the line and do the replacing on that line. We * however need to correct the indexes because we are adding or removing * characters to the copy. */ int indexCorrection = 0; while (matcher.find()) { if (copy == null) { copy = new StrBuilder(builder.toString()); } String replaceWith = "<span class=\"" + className + "\">" + matcher.group() + "</span>"; copy.replace(matcher.start() + indexCorrection, matcher.end() + indexCorrection, replaceWith); indexCorrection = indexCorrection + replaceWith.length() - matcher.group().length(); } if (copy != null) { /* * Content has changed so replace it */ builder.clear(); builder.append(copy.toString()); } } }