From 2d8c9cbc9af1e257d004108a0635d64ac8c939cc Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 20 Nov 2017 23:35:36 +0000 Subject: [PATCH] [bug-61792] some changes to avoid iterating over chars of Strings git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1815871 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/HeaderFooterRecord.java | 6 ++-- .../apache/poi/hssf/record/UnknownRecord.java | 6 ++-- .../poi/hssf/record/UserSViewBegin.java | 4 +-- .../apache/poi/hssf/record/UserSViewEnd.java | 6 ++-- .../poi/hssf/record/cf/FontFormatting.java | 5 +-- .../apache/poi/sl/draw/DrawTextParagraph.java | 34 +++++-------------- .../poi/xslf/usermodel/XSLFTextRun.java | 33 +++++++----------- .../org/apache/poi/hwpf/dev/RecordUtil.java | 16 ++++----- 8 files changed, 42 insertions(+), 68 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/HeaderFooterRecord.java b/src/java/org/apache/poi/hssf/record/HeaderFooterRecord.java index af4a385987..92966385e1 100644 --- a/src/java/org/apache/poi/hssf/record/HeaderFooterRecord.java +++ b/src/java/org/apache/poi/hssf/record/HeaderFooterRecord.java @@ -86,10 +86,10 @@ public final class HeaderFooterRecord extends StandardRecord implements Cloneabl } public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); - sb.append("[").append("HEADERFOOTER").append("] (0x"); - sb.append(Integer.toHexString(sid).toUpperCase(Locale.ROOT) + ")\n"); + sb.append('[').append("HEADERFOOTER").append("] (0x"); + sb.append(Integer.toHexString(sid).toUpperCase(Locale.ROOT)).append(")\n"); sb.append(" rawData=").append(HexDump.toHex(_rawData)).append("\n"); sb.append("[/").append("HEADERFOOTER").append("]\n"); return sb.toString(); diff --git a/src/java/org/apache/poi/hssf/record/UnknownRecord.java b/src/java/org/apache/poi/hssf/record/UnknownRecord.java index 59bab98f27..189b582472 100644 --- a/src/java/org/apache/poi/hssf/record/UnknownRecord.java +++ b/src/java/org/apache/poi/hssf/record/UnknownRecord.java @@ -111,10 +111,10 @@ public final class UnknownRecord extends StandardRecord { if (biffName == null) { biffName = "UNKNOWNRECORD"; } - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); - sb.append("[").append(biffName).append("] (0x"); - sb.append(Integer.toHexString(_sid).toUpperCase(Locale.ROOT) + ")\n"); + sb.append('[').append(biffName).append("] (0x"); + sb.append(Integer.toHexString(_sid).toUpperCase(Locale.ROOT)).append(")\n"); if (_rawData.length > 0) { sb.append(" rawData=").append(HexDump.toHex(_rawData)).append("\n"); } diff --git a/src/java/org/apache/poi/hssf/record/UserSViewBegin.java b/src/java/org/apache/poi/hssf/record/UserSViewBegin.java index 1c4895a53e..c32cffbb2b 100644 --- a/src/java/org/apache/poi/hssf/record/UserSViewBegin.java +++ b/src/java/org/apache/poi/hssf/record/UserSViewBegin.java @@ -74,10 +74,10 @@ public final class UserSViewBegin extends StandardRecord { } public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append("[").append("USERSVIEWBEGIN").append("] (0x"); - sb.append(Integer.toHexString(sid).toUpperCase(Locale.ROOT) + ")\n"); + sb.append(Integer.toHexString(sid).toUpperCase(Locale.ROOT)).append(")\n"); sb.append(" rawData=").append(HexDump.toHex(_rawData)).append("\n"); sb.append("[/").append("USERSVIEWBEGIN").append("]\n"); return sb.toString(); diff --git a/src/java/org/apache/poi/hssf/record/UserSViewEnd.java b/src/java/org/apache/poi/hssf/record/UserSViewEnd.java index aa72eacd5a..b3410ebc3e 100644 --- a/src/java/org/apache/poi/hssf/record/UserSViewEnd.java +++ b/src/java/org/apache/poi/hssf/record/UserSViewEnd.java @@ -60,10 +60,10 @@ public final class UserSViewEnd extends StandardRecord { } public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); - sb.append("[").append("USERSVIEWEND").append("] (0x"); - sb.append(Integer.toHexString(sid).toUpperCase(Locale.ROOT) + ")\n"); + sb.append('[').append("USERSVIEWEND").append("] (0x"); + sb.append(Integer.toHexString(sid).toUpperCase(Locale.ROOT)).append(")\n"); sb.append(" rawData=").append(HexDump.toHex(_rawData)).append("\n"); sb.append("[/").append("USERSVIEWEND").append("]\n"); return sb.toString(); diff --git a/src/java/org/apache/poi/hssf/record/cf/FontFormatting.java b/src/java/org/apache/poi/hssf/record/cf/FontFormatting.java index 807d67afd1..e6ea05de23 100644 --- a/src/java/org/apache/poi/hssf/record/cf/FontFormatting.java +++ b/src/java/org/apache/poi/hssf/record/cf/FontFormatting.java @@ -452,7 +452,7 @@ public final class FontFormatting implements Cloneable { public String toString() { - StringBuffer buffer = new StringBuffer(); + StringBuilder buffer = new StringBuilder(); buffer.append(" [Font Formatting]\n"); buffer.append(" .font height = ").append(getFontHeight()).append(" twips\n"); @@ -525,7 +525,8 @@ public final class FontFormatting implements Cloneable { { buffer.append(" .underline type is not modified\n"); } - buffer.append(" .color index = ").append("0x"+Integer.toHexString(getFontColorIndex()).toUpperCase(Locale.ROOT)).append("\n"); + buffer.append(" .color index = ").append("0x") + .append(Integer.toHexString(getFontColorIndex()).toUpperCase(Locale.ROOT)).append('\n'); buffer.append(" [/Font Formatting]\n"); return buffer.toString(); diff --git a/src/java/org/apache/poi/sl/draw/DrawTextParagraph.java b/src/java/org/apache/poi/sl/draw/DrawTextParagraph.java index e93a3a5fb0..1b086430cd 100644 --- a/src/java/org/apache/poi/sl/draw/DrawTextParagraph.java +++ b/src/java/org/apache/poi/sl/draw/DrawTextParagraph.java @@ -50,14 +50,13 @@ import org.apache.poi.sl.usermodel.TextParagraph.BulletStyle; import org.apache.poi.sl.usermodel.TextParagraph.TextAlign; import org.apache.poi.sl.usermodel.TextRun; import org.apache.poi.sl.usermodel.TextRun.FieldType; -import org.apache.poi.sl.usermodel.TextRun.TextCap; import org.apache.poi.sl.usermodel.TextShape; import org.apache.poi.sl.usermodel.TextShape.TextDirection; +import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; import org.apache.poi.util.Units; - public class DrawTextParagraph implements Drawable { private static final POILogger LOG = POILogFactory.getLogger(DrawTextParagraph.class); @@ -379,33 +378,16 @@ public class DrawTextParagraph implements Drawable { Slide slide = (Slide)graphics.getRenderingHint(Drawable.CURRENT_SLIDE); return (slide == null) ? "" : Integer.toString(slide.getSlideNumber()); } - StringBuilder buf = new StringBuilder(); - TextCap cap = tr.getTextCap(); - String tabs = null; - for (char c : tr.getRawText().toCharArray()) { - switch (c) { - case '\t': - if (tabs == null) { - tabs = tab2space(tr); - } - buf.append(tabs); - break; - case '\u000b': - buf.append('\n'); - break; - default: - switch (cap) { - case ALL: c = Character.toUpperCase(c); break; - case SMALL: c = Character.toLowerCase(c); break; - case NONE: break; - } + String txt = tr.getRawText(); + txt.replace("\t", tab2space(tr)).replace("\u000b", "\n"); - buf.append(c); - break; - } + switch (tr.getTextCap()) { + case ALL: txt.toUpperCase(LocaleUtil.getUserLocale()); break; + case SMALL: txt.toLowerCase(LocaleUtil.getUserLocale()); break; + case NONE: break; } - return buf.toString(); + return txt; } /** diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java index ea53360084..6e62e4ae4d 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java @@ -30,6 +30,7 @@ import org.apache.poi.sl.usermodel.PaintStyle; import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint; import org.apache.poi.sl.usermodel.TextRun; import org.apache.poi.util.Beta; +import org.apache.poi.util.LocaleUtil; import org.apache.poi.xslf.model.CharacterPropertyFetcher; import org.apache.poi.xslf.usermodel.XSLFPropertiesDelegate.XSLFFillProperties; import org.apache.xmlbeans.XmlObject; @@ -95,28 +96,18 @@ public class XSLFTextRun implements TextRun { String txt = ((CTRegularTextRun)_r).getT(); - TextCap cap = getTextCap(); - StringBuilder buf = new StringBuilder(); - for(int i = 0; i < txt.length(); i++) { - char c = txt.charAt(i); - if(c == '\t') { - // TODO: finish support for tabs - buf.append(" "); - } else { - switch (cap){ - case ALL: - buf.append(Character.toUpperCase(c)); - break; - case SMALL: - buf.append(Character.toLowerCase(c)); - break; - default: - buf.append(c); - } - } - } + // TODO: finish support for tabs + txt.replace("\t", " "); - return buf.toString(); + switch (getTextCap()) { + case ALL: + txt = txt.toUpperCase(LocaleUtil.getUserLocale()); + break; + case SMALL: + txt = txt.toLowerCase(LocaleUtil.getUserLocale()); + break; + } + return txt; } @Override diff --git a/src/scratchpad/src/org/apache/poi/hwpf/dev/RecordUtil.java b/src/scratchpad/src/org/apache/poi/hwpf/dev/RecordUtil.java index a1b03bb0d5..9cfa1324e9 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/dev/RecordUtil.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/dev/RecordUtil.java @@ -142,7 +142,7 @@ public class RecordUtil public static String getConstName( String parentName, String constName, int padTo ) { - StringBuffer fieldName = new StringBuffer(); + StringBuilder fieldName = new StringBuilder(); toConstIdentifier( parentName, fieldName ); fieldName.append( '_' ); toConstIdentifier( constName, fieldName ); @@ -152,7 +152,7 @@ public class RecordUtil public static String getFieldName( int position, String name, int padTo ) { - StringBuffer fieldName = new StringBuffer( "field_" + position + "_" ); + StringBuilder fieldName = new StringBuilder().append("field_").append(position).append('_'); toIdentifier( name, fieldName ); pad( fieldName, padTo ); @@ -161,7 +161,7 @@ public class RecordUtil public static String getFieldName( String name, int padTo ) { - StringBuffer fieldName = new StringBuffer(); + StringBuilder fieldName = new StringBuilder(); toIdentifier( name, fieldName ); pad( fieldName, padTo ); @@ -170,7 +170,7 @@ public class RecordUtil public static String getFieldName1stCap( String name, int padTo ) { - StringBuffer fieldName = new StringBuffer(); + StringBuilder fieldName = new StringBuilder(); toIdentifier( name, fieldName ); fieldName.setCharAt( 0, Character.toUpperCase( fieldName.charAt( 0 ) ) ); pad( fieldName, padTo ); @@ -180,7 +180,7 @@ public class RecordUtil public static String getType1stCap( String size, String type, int padTo ) { - StringBuffer result = new StringBuffer(); + StringBuilder result = new StringBuilder(); result.append( type ); result = pad( result, padTo ); result.setCharAt( 0, Character.toUpperCase( result.charAt( 0 ) ) ); @@ -188,14 +188,14 @@ public class RecordUtil return result.toString(); } - protected static StringBuffer pad( StringBuffer fieldName, int padTo ) + protected static StringBuilder pad( StringBuilder fieldName, int padTo ) { for ( int i = fieldName.length(); i < padTo; i++ ) fieldName.append( ' ' ); return fieldName; } - private static void toConstIdentifier( String name, StringBuffer fieldName ) + private static void toConstIdentifier( String name, StringBuilder fieldName ) { for ( int i = 0; i < name.length(); i++ ) { @@ -206,7 +206,7 @@ public class RecordUtil } } - private static void toIdentifier( String name, StringBuffer fieldName ) + private static void toIdentifier( String name, StringBuilder fieldName ) { for ( int i = 0; i < name.length(); i++ ) { -- 2.39.5