Skip to content

Commit

Permalink
use StringBuilder instead of StringBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Sep 19, 2023
1 parent ea66179 commit 671a992
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ private static void createChildren(
Text textNode = (Text)working;

/*
StringBuffer text = new StringBuffer(textNode.getData());
StringBuilder text = new StringBuilder(textNode.getData());
Node maybeText = textNode;
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected String getExtraBoxDescription() {
}

public String toString() {
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();
String className = getClass().getName();
result.append(className.substring(className.lastIndexOf('.') + 1));
result.append(": ");
Expand Down Expand Up @@ -168,7 +168,7 @@ public String toString() {
return result.toString();
}

protected void appendPositioningInfo(StringBuffer result) {
protected void appendPositioningInfo(StringBuilder result) {
if (getStyle().isRelative()) {
result.append("(relative) ");
}
Expand All @@ -185,7 +185,7 @@ protected void appendPositioningInfo(StringBuffer result) {

@Override
public String dump(LayoutContext c, String indent, int which) {
StringBuffer result = new StringBuffer(indent);
StringBuilder result = new StringBuilder(indent);

ensureChildren(c);

Expand Down Expand Up @@ -1006,8 +1006,8 @@ protected void layoutInlineChildren(
}

private void justifyText() {
for (Iterator<Box> i = getChildIterator(); i.hasNext(); ) {
LineBox line = (LineBox)i.next();
for (Box box : getChildren()) {
LineBox line = (LineBox) box;
line.justify();
}
}
Expand Down Expand Up @@ -1193,8 +1193,8 @@ private void collapseTopMargin(
if (isMayCollapseMarginsWithChildren() && isNoTopPaddingOrBorder(c)) {
ensureChildren(c);
if (getChildrenContentType() == CONTENT_BLOCK) {
for (Iterator<Box> i = getChildIterator(); i.hasNext();) {
BlockBox child = (BlockBox) i.next();
for (Box box : getChildren()) {
BlockBox child = (BlockBox) box;
child.collapseTopMargin(c, false, result);

if (child.isSkipWhenCollapsingMargins()) {
Expand Down Expand Up @@ -1276,8 +1276,8 @@ private void collapseEmptySubtreeMargins(LayoutContext c, MarginCollapseResult r

ensureChildren(c);
if (getChildrenContentType() == CONTENT_BLOCK) {
for (Iterator<Box> i = getChildIterator(); i.hasNext();) {
BlockBox child = (BlockBox) i.next();
for (Box box : getChildren()) {
BlockBox child = (BlockBox) box;
child.collapseEmptySubtreeMargins(c, result);
}
}
Expand All @@ -1301,9 +1301,9 @@ private boolean isVerticalMarginsAdjoin(LayoutContext c) {
if (getChildrenContentType() == CONTENT_INLINE) {
return false;
} else if (getChildrenContentType() == CONTENT_BLOCK) {
for (Iterator<Box> i = getChildIterator(); i.hasNext();) {
BlockBox child = (BlockBox) i.next();
if (child.isSkipWhenCollapsingMargins() || ! child.isVerticalMarginsAdjoin(c)) {
for (Box box : getChildren()) {
BlockBox child = (BlockBox) box;
if (child.isSkipWhenCollapsingMargins() || !child.isVerticalMarginsAdjoin(c)) {
return false;
}
}
Expand Down Expand Up @@ -1613,8 +1613,8 @@ private void calcMinMaxWidthBlockChildren(LayoutContext c) {
int childMinWidth = 0;
int childMaxWidth = 0;

for (Iterator<Box> i = getChildIterator(); i.hasNext();) {
BlockBox child = (BlockBox) i.next();
for (Box box : getChildren()) {
BlockBox child = (BlockBox) box;
child.calcMinMaxWidth(c);
if (child.getMinWidth() > childMinWidth) {
childMinWidth = child.getMinWidth();
Expand Down Expand Up @@ -2094,8 +2094,8 @@ public boolean isContainsInlineContent(LayoutContext c) {
case CONTENT_EMPTY:
return false;
case CONTENT_BLOCK:
for (Iterator<Box> i = getChildIterator(); i.hasNext(); ) {
BlockBox box = (BlockBox)i.next();
for (Box value : getChildren()) {
BlockBox box = (BlockBox) value;
if (box.isContainsInlineContent(c)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected Box() {

protected void dumpBoxes(
LayoutContext c, String indent, List<Box> boxes,
int which, StringBuffer result) {
int which, StringBuilder result) {
for (Iterator<Box> i = boxes.iterator(); i.hasNext(); ) {
Box b = i.next();
result.append(b.dump(c, indent + " ", which));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
package org.xhtmlrenderer.render;

import java.text.BreakIterator;

import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.xhtmlrenderer.css.constants.IdentValue;
Expand All @@ -33,16 +31,18 @@
import org.xhtmlrenderer.layout.breaker.BreakPointsProvider;
import org.xhtmlrenderer.layout.breaker.Breaker;

import java.text.BreakIterator;

/**
* A class which reprsents a portion of an inline element. If an inline element
* does not contain any nested elements, then a single <code>InlineBox</code>
* A class which represents a portion of an inline element. If an inline element
* does not contain any nested elements, then a single {@code InlineBox}
* object will contain the content for the entire element. Otherwise, multiple
* <code>InlineBox</code> objects will be created corresponding to each
* discrete chunk of text appearing in the elment. It is not rendered directly
* {@code InlineBox} objects will be created corresponding to each
* discrete chunk of text appearing in the element. It is not rendered directly
* (and hence does not extend from {@link Box}), but does play an important
* role in layout (for example, when calculating min/max widths). Note that it
* does not contain children. Inline content is stored as a flat list in the
* layout tree. However, <code>InlineBox</code> does contain enough
* layout tree. However, {@code InlineBox} does contain enough
* information to reconstruct the original element nesting and this is, in fact,
* done during inline layout.
*
Expand Down Expand Up @@ -116,18 +116,22 @@ public void setStartsHere(boolean startsHere) {
_startsHere = startsHere;
}

@Override
public CalculatedStyle getStyle() {
return _style;
}

@Override
public void setStyle(CalculatedStyle style) {
_style = style;
}

@Override
public Element getElement() {
return _element;
}

@Override
public void setElement(Element element) {
_element = element;
}
Expand All @@ -154,8 +158,8 @@ private int getTextWidth(LayoutContext c, String s) {
private int getMaxCharWidth(LayoutContext c, String s) {
char[] chars = s.toCharArray();
int result = 0;
for (int i = 0; i < chars.length; i++) {
int width = getTextWidth(c, Character.toString(chars[i]));
for (char aChar : chars) {
int width = getTextWidth(c, Character.toString(aChar));
if (width > result) {
result = width;
}
Expand Down Expand Up @@ -377,6 +381,7 @@ public int getFirstLineWidth() {
return _firstLineWidth;
}

@Override
public String getPseudoElementOrClass() {
return _pseudoElementOrClass;
}
Expand All @@ -386,7 +391,7 @@ public void setPseudoElementOrClass(String pseudoElementOrClass) {
}

public String toString() {
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();
result.append("InlineBox: ");
if (getElement() != null) {
result.append("<");
Expand Down Expand Up @@ -419,7 +424,7 @@ public String toString() {
return result.toString();
}

protected void appendPositioningInfo(StringBuffer result) {
protected void appendPositioningInfo(StringBuilder result) {
if (getStyle().isRelative()) {
result.append("(relative) ");
}
Expand Down Expand Up @@ -468,6 +473,6 @@ public void truncateText() {
}

public Text getTextNode() {
return this._textNode;
return _textNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -84,7 +83,7 @@ public String dump(LayoutContext c, String indent, int which) {
throw new IllegalArgumentException();
}

StringBuffer result = new StringBuffer(indent);
StringBuilder result = new StringBuilder(indent);
result.append(this);
result.append('\n');

Expand Down Expand Up @@ -251,12 +250,11 @@ public void justify() {

private void adjustChildren(JustificationInfo info) {
float adjust = 0.0f;
for (Iterator<Box> i = getChildIterator(); i.hasNext(); ) {
Box b = i.next();
for (Box b : getChildren()) {
b.setX(b.getX() + Math.round(adjust));

if (b instanceof InlineLayoutBox) {
adjust += ((InlineLayoutBox)b).adjustHorizontalPosition(info, adjust);
adjust += ((InlineLayoutBox) b).adjustHorizontalPosition(info, adjust);
}
}

Expand All @@ -280,10 +278,9 @@ private boolean isLastLineWithContent() {
private CharCounts countJustifiableChars() {
CharCounts result = new CharCounts();

for (Iterator<Box> i = getChildIterator(); i.hasNext(); ) {
Box b = i.next();
for (Box b : getChildren()) {
if (b instanceof InlineLayoutBox) {
((InlineLayoutBox)b).countJustifiableChars(result);
((InlineLayoutBox) b).countJustifiableChars(result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class URLUTF8Encoder {

/** Description of the Field */
final static String[] hex = {
private static final String[] hex = {
"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
"%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
"%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
Expand Down Expand Up @@ -89,13 +89,13 @@ public class URLUTF8Encoder {
* @return The encoded string
*/
public static String encode( String s ) {
StringBuffer sbuf = new StringBuffer();
StringBuilder buf = new StringBuilder();
int len = s.length();
for ( int i = 0; i < len; i++ ) {
int ch = s.charAt( i );
append( sbuf, ch );
append( buf, ch );
}
return sbuf.toString();
return buf.toString();
}

/**
Expand All @@ -105,45 +105,37 @@ public static String encode( String s ) {
* @return Returns
*/
public static String encode( char[] chars ) {
StringBuffer sbuf = new StringBuffer();
int len = chars.length;
for ( int i = 0; i < len; i++ ) {
int ch = chars[i];
append( sbuf, ch );
StringBuilder buf = new StringBuilder();
for (int ch : chars) {
append(buf, ch);
}
return sbuf.toString();
return buf.toString();
}

/**
* Description of the Method
*
* @param sbuf PARAM
* @param ch PARAM
*/
private static void append( StringBuffer sbuf, int ch ) {
private static void append( StringBuilder buf, int ch ) {
if ( 'A' <= ch && ch <= 'Z' ) {// 'A'..'Z'
sbuf.append( (char)ch );
buf.append( (char)ch );
} else if ( 'a' <= ch && ch <= 'z' ) {// 'a'..'z'
sbuf.append( (char)ch );
buf.append( (char)ch );
} else if ( '0' <= ch && ch <= '9' ) {// '0'..'9'
sbuf.append( (char)ch );
buf.append( (char)ch );
} else if ( ch == ' ' ) {// space
sbuf.append( '+' );
buf.append( '+' );
} else if ( ch == '-' || ch == '_' // unreserved
|| ch == '.' || ch == '!'
|| ch == '~' || ch == '*'
|| ch == '\'' || ch == '('
|| ch == ')' ) {
sbuf.append( (char)ch );
buf.append( (char)ch );
} else if ( ch <= 0x007f ) {// other ASCII
sbuf.append( hex[ch] );
buf.append( hex[ch] );
} else if ( ch <= 0x07FF ) {// non-ASCII <= 0x7FF
sbuf.append( hex[0xc0 | ( ch >> 6 )] );
sbuf.append( hex[0x80 | ( ch & 0x3F )] );
buf.append( hex[0xc0 | ( ch >> 6 )] );
buf.append( hex[0x80 | ( ch & 0x3F )] );
} else {// 0x7FF < ch <= 0xFFFF
sbuf.append( hex[0xe0 | ( ch >> 12 )] );
sbuf.append( hex[0x80 | ( ( ch >> 6 ) & 0x3F )] );
sbuf.append( hex[0x80 | ( ch & 0x3F )] );
buf.append( hex[0xe0 | ( ch >> 12 )] );
buf.append( hex[0x80 | ( ( ch >> 6 ) & 0x3F )] );
buf.append( hex[0x80 | ( ch & 0x3F )] );
}
}

Expand Down
Loading

0 comments on commit 671a992

Please sign in to comment.