From 29a020c198e39dac0e860c154803acb722442165 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Wed, 16 Nov 2011 15:32:20 +0000 Subject: [PATCH] ICU-8899 LocaleIDParser#append should not rely on exceptions. X-SVN-Rev: 30972 --- .../src/com/ibm/icu/impl/LocaleIDParser.java | 52 +++++++++---------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/LocaleIDParser.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/LocaleIDParser.java index 6a4d392fff8..72d8a31119b 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/LocaleIDParser.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/LocaleIDParser.java @@ -1,6 +1,6 @@ /* ****************************************************************************** -* Copyright (C) 2003-2009, International Business Machines Corporation and * +* Copyright (C) 2003-2011, International Business Machines Corporation and * * others. All Rights Reserved. * ****************************************************************************** */ @@ -59,25 +59,26 @@ public final class LocaleIDParser { } // utilities for working on text in the buffer + + private void resizeBuffer(int newSize) { + if (newSize > 1024) { + // if buffer is too large, something is wrong. die. + throw new IndexOutOfBoundsException("Buffer has grown too large."); + } + + char[] nbuffer = new char[newSize]; + System.arraycopy(buffer, 0, nbuffer, 0, Math.min(buffer.length, newSize)); + buffer = nbuffer; + } /** * Append c to the buffer. */ private void append(char c) { - try { - buffer[blen] = c; - } - catch (IndexOutOfBoundsException e) { - if (buffer.length > 512) { - // something is seriously wrong, let this go - throw e; - } - char[] nbuffer = new char[buffer.length * 2]; - System.arraycopy(buffer, 0, nbuffer, 0, buffer.length); - nbuffer[blen] = c; - buffer = nbuffer; + if (blen >= buffer.length) { + resizeBuffer(buffer.length * 2); } - ++blen; + buffer[blen++] = c; } private void addSeparator() { @@ -106,8 +107,12 @@ public final class LocaleIDParser { * Append the string to the buffer. */ private void append(String s) { - for (int i = 0; i < s.length(); ++i) { - append(s.charAt(i)); + int slen = s.length(); + if (blen + slen > buffer.length) { + resizeBuffer(buffer.length * 2 + slen); + } + for (int i = 0; i < slen; ++i) { + buffer[blen++] = s.charAt(i); } } @@ -136,8 +141,7 @@ public final class LocaleIDParser { * Advance index until the next terminator or id separator, and leave it there. */ private void skipUntilTerminatorOrIDSeparator() { - while (!isTerminatorOrIDSeparator(next())) { - } + while (!isTerminatorOrIDSeparator(next())); --index; } @@ -148,13 +152,6 @@ public final class LocaleIDParser { return index >= id.length || isTerminator(id[index]); } - /* - * Returns true if the character is an id separator (underscore or hyphen). - */ - /* private boolean isIDSeparator(char c) { - return c == UNDERSCORE || c == HYPHEN; - }*/ - /** * Returns true if the character is a terminator (keyword separator, dot, or DONE). * Dot is a terminator because of the POSIX form, where dot precedes the codepage. @@ -168,8 +165,7 @@ public final class LocaleIDParser { * Returns true if the character is a terminator or id separator. */ private boolean isTerminatorOrIDSeparator(char c) { - return c == KEYWORD_SEPARATOR || c == UNDERSCORE || c == HYPHEN || - c == DONE || c == DOT; + return c == UNDERSCORE || c == HYPHEN || isTerminator(c); } /** @@ -738,4 +734,4 @@ public final class LocaleIDParser { } } } -} \ No newline at end of file +} -- 2.40.0