From 22f108c028cff18ff5906e41a59f93d620a3e163 Mon Sep 17 00:00:00 2001 From: Peter Edberg Date: Tue, 24 Sep 2013 04:06:18 +0000 Subject: [PATCH] ICU-10161 (J) Speed up trimMarksFromAffix per Yoshito, skip it entirely for affix length 1 X-SVN-Rev: 34463 --- .../src/com/ibm/icu/text/DecimalFormat.java | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java index e5bef2479df..03855d0e845 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java @@ -2771,23 +2771,31 @@ public class DecimalFormat extends NumberFormat { * Remove bidi marks from affix */ private static final int TRIM_BUFLEN = 32; - private static String trimMarksFromAffix(String affix) { - char[] trimBuf = new char[TRIM_BUFLEN]; - int affixLen = affix.length(); - int affixPos, trimLen = 0; - for (affixPos = 0; affixPos < affixLen; affixPos++) { - char c = affix.charAt(affixPos); - if (!isBidiMark(c)) { - if (trimLen < TRIM_BUFLEN) { - trimBuf[trimLen++] = c; - } else { - trimLen = 0; - break; - } - } - } - return (trimLen > 0)? new String(trimBuf, 0, trimLen): affix; - } + private static String trimMarksFromAffix(String affix) { + boolean hasBidiMark = false; + int idx = 0; + for (; idx < affix.length(); idx++) { + if (isBidiMark(affix.charAt(idx))) { + hasBidiMark = true; + break; + } + } + if (!hasBidiMark) { + return affix; + } + + StringBuilder buf = new StringBuilder(); + buf.append(affix, 0, idx); + idx++; // skip the first Bidi mark + for (; idx < affix.length(); idx++) { + char c = affix.charAt(idx); + if (!isBidiMark(c)) { + buf.append(c); + } + } + + return buf.toString(); + } /** * Return the length matched by the given affix, or -1 if none. Runs of white space in @@ -2804,7 +2812,7 @@ public class DecimalFormat extends NumberFormat { // Affixes here might consist of sign, currency symbol and related spacing, etc. // For more efficiency we should keep lazily-created trimmed affixes around in // instance variables instead of trimming each time they are used (the next step). - String trimmedAffix = trimMarksFromAffix(affix); + String trimmedAffix = (affix.length() > 1)? trimMarksFromAffix(affix): affix; for (int i = 0; i < trimmedAffix.length();) { int c = UTF16.charAt(trimmedAffix, i); int len = UTF16.getCharCount(c); -- 2.40.0