ASF Bugzilla – Attachment 30991 Details for
Bug 55731
[PATCH] StringBuilder logic in DataFormatter.cleanFormatForNumber
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Fix StringBuilder logic in DataFormatter.cleanFormatForNumber
patch.diff (text/plain), 6.46 KB, created by
Eric Peters
on 2013-11-01 18:39:46 UTC
(
hide
)
Description:
Fix StringBuilder logic in DataFormatter.cleanFormatForNumber
Filename:
MIME Type:
Creator:
Eric Peters
Created:
2013-11-01 18:39:46 UTC
Size:
6.46 KB
patch
obsolete
>From 2a5cc57d8d3fae04e681c1ea584eddd28ca107a5 Mon Sep 17 00:00:00 2001 >From: Eric Peters <eric@peters.org> >Date: Thu, 31 Oct 2013 17:47:54 -0700 >Subject: [PATCH] Fix StringBuilder logic in > DataFormatter.cleanFormatForNumber > >--- > .../org/apache/poi/ss/usermodel/DataFormatter.java | 39 +++++++++++++++++++--- > 1 file changed, 34 insertions(+), 5 deletions(-) > >diff --git a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java >index 273dd89..ef8d512 100644 >--- a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java >+++ b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java >@@ -40,6 +40,9 @@ import java.util.Map; > import java.util.regex.Matcher; > import java.util.regex.Pattern; > >+import org.apache.poi.util.POILogFactory; >+import org.apache.poi.util.POILogger; >+ > > /** > * DataFormatter contains methods for formatting the value stored in an >@@ -100,6 +103,8 @@ import java.util.regex.Pattern; > * </ul> > */ > public class DataFormatter { >+ private static final POILogger logger = POILogFactory.getLogger(DataFormatter.class); >+ > private static final String defaultFractionWholePartFormat = "#"; > private static final String defaultFractionFractionPartFormat = "#/##"; > /** Pattern to find a number format: "0" or "#" */ >@@ -259,17 +264,23 @@ public class DataFormatter { > } > > private Format getFormat(double cellValue, int formatIndex, String formatStrIn) { >+ logger.log(POILogger.DEBUG, "Enter DataFormatter.getFormat("+cellValue+", "+formatIndex+", "+formatStrIn+")"); >+ > // // Might be better to separate out the n p and z formats, falling back to p when n and z are not set. > // // That however would require other code to be re factored. > // String[] formatBits = formatStrIn.split(";"); > // int i = cellValue > 0.0 ? 0 : cellValue < 0.0 ? 1 : 2; > // String formatStr = (i < formatBits.length) ? formatBits[i] : formatBits[0]; >- >+ > String formatStr = formatStrIn; >+ > // Excel supports positive/negative/zero, but java > // doesn't, so we need to do it specially > final int firstAt = formatStr.indexOf(';'); > final int lastAt = formatStr.lastIndexOf(';'); >+ >+ >+ > // p and p;n are ok by default. p;n;z and p;n;z;s need to be fixed. > if (firstAt != -1 && firstAt != lastAt) { > final int secondAt = formatStr.indexOf(';', firstAt + 1); >@@ -293,6 +304,8 @@ public class DataFormatter { > formatStr = formatStr.replaceAll("#", ""); > } > >+ logger.log(POILogger.DEBUG, "DataFormatter.getFormat("+cellValue+", +"+formatIndex+", "+formatStrIn+"), Target formatStr: "+formatStr ); >+ > // See if we already have it cached > Format format = formats.get(formatStr); > if (format != null) { >@@ -328,6 +341,8 @@ public class DataFormatter { > } > > private Format createFormat(double cellValue, int formatIndex, String sFormat) { >+ logger.log(POILogger.DEBUG, "Enter DataFormatter.createFormat("+cellValue+", +"+formatIndex+", "+sFormat+")"); >+ > String formatStr = sFormat; > > // Remove colour formatting if present >@@ -556,6 +571,7 @@ public class DataFormatter { > } > > private String cleanFormatForNumber(String formatStr) { >+ logger.log(POILogger.DEBUG, "Enter DataFormatter.cleanFormatForNumber("+formatStr+")"); > StringBuffer sb = new StringBuffer(formatStr); > > if (emulateCsv) { >@@ -563,7 +579,8 @@ public class DataFormatter { > // Full-column-width padding "*" are removed. > // Not processing fractions at this time. Replace ? with space. > // This matches CSV output. >- for (int i = 0; i < sb.length(); i++) { >+ int length = sb.length(); >+ for (int i = 0; i < length; i++) { > char c = sb.charAt(i); > if (c == '_' || c == '*' || c == '?') { > if (i > 0 && sb.charAt((i - 1)) == '\\') { >@@ -580,9 +597,12 @@ public class DataFormatter { > sb.setCharAt(i + 1, ' '); > } else { > sb.deleteCharAt(i + 1); >+ lenght--; > } > // Remove the character too > sb.deleteCharAt(i); >+ length--; >+ i--; > } > } > } >@@ -591,8 +611,11 @@ public class DataFormatter { > // remove those as we don't do spacing > // If they requested full-column-width > // padding, with "*", remove those too >- for (int i = 0; i < sb.length(); i++) { >+ >+ int length = sb.length(); >+ for (int i = 0; i < length; i++) { > char c = sb.charAt(i); >+ logger.log(POILogger.DEBUG, "DataFormatter.cleanFormatForNumber("+formatStr+"), else/foreach character at: "+i+", value: "+c+", current StringBuffer: "+sb.toString()+", sb size: "+sb.length()); > if (c == '_' || c == '*') { > if (i > 0 && sb.charAt((i - 1)) == '\\') { > // It's escaped, don't worry >@@ -601,11 +624,16 @@ public class DataFormatter { > if (i < sb.length() - 1) { > // Remove the character we're supposed > // to match the space of / pad to the >- // column width with >+ // column width with & adjust length/index >+ > sb.deleteCharAt(i + 1); >+ length--; > } > // Remove the _ too >+ // Adjust length + counter > sb.deleteCharAt(i); >+ length--; >+ i--; > } > } > } >@@ -630,8 +658,9 @@ public class DataFormatter { > } > > private Format createNumberFormat(String formatStr, double cellValue) { >+ logger.log(POILogger.DEBUG, "Enter DataFormatter.createNumberFormat("+formatStr+", +"+cellValue+")"); > final String format = cleanFormatForNumber(formatStr); >- >+ logger.log(POILogger.DEBUG, "DataFormatter.createNumberFormat("+formatStr+", +"+cellValue+"): cleanFormatForNumber(formatStr) => "+format); > try { > DecimalFormat df = new DecimalFormat(format, decimalSymbols); > setExcelStyleRoundingMode(df); >-- >1.7.11.1 >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 55731
:
30989
|
30991
|
30992
|
31149