From 943b8564c9408d6a2ea092cc49a21121864d20f9 Mon Sep 17 00:00:00 2001 From: Alan Liu Date: Tue, 25 Apr 2000 17:17:37 +0000 Subject: [PATCH] Add Replaceable.copy to retain out-of-band info during reordering. X-SVN-Rev: 1244 --- icu4j/src/com/ibm/icu/text/Replaceable.java | 28 +++++++++-- .../com/ibm/icu/text/ReplaceableString.java | 36 +++++++++----- .../com/ibm/icu/text/TransliterationRule.java | 49 ++++++++++++++----- icu4j/src/com/ibm/text/Replaceable.java | 28 +++++++++-- icu4j/src/com/ibm/text/ReplaceableString.java | 36 +++++++++----- .../src/com/ibm/text/TransliterationRule.java | 49 ++++++++++++++----- 6 files changed, 172 insertions(+), 54 deletions(-) diff --git a/icu4j/src/com/ibm/icu/text/Replaceable.java b/icu4j/src/com/ibm/icu/text/Replaceable.java index ac42783d70..55cd092e53 100755 --- a/icu4j/src/com/ibm/icu/text/Replaceable.java +++ b/icu4j/src/com/ibm/icu/text/Replaceable.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/Replaceable.java,v $ - * $Date: 2000/03/10 04:07:22 $ - * $Revision: 1.2 $ + * $Date: 2000/04/25 17:17:37 $ + * $Revision: 1.3 $ * ***************************************************************************************** */ @@ -23,7 +23,7 @@ package com.ibm.text; *

Copyright © IBM Corporation 1999. All rights reserved. * * @author Alan Liu - * @version $RCSfile: Replaceable.java,v $ $Revision: 1.2 $ $Date: 2000/03/10 04:07:22 $ + * @version $RCSfile: Replaceable.java,v $ $Revision: 1.3 $ $Date: 2000/04/25 17:17:37 $ */ public interface Replaceable { /** @@ -86,4 +86,26 @@ public interface Replaceable { int charsStart, int charsLen); // Note: We use length rather than limit to conform to StringBuffer // and System.arraycopy. + + /** + * Copy a substring of this object, retaining attribute (out-of-band) + * information. This method is used to duplicate or reorder substrings. + * The destination index must not overlap the source range. + * Implementations that do not care about maintaining out-of-band + * information during copying may use the naive implementation: + * + *

 char[] text = new char[limit - start];
+     * getChars(start, limit, text, 0);
+     * replace(dest, dest, text, 0, limit - start);
+ * + * @param start the beginning index, inclusive; 0 <= start <= + * limit. + * @param limit the ending index, exclusive; start <= limit <= + * length(). + * @param dest the destination index. The characters from + * start..limit-1 will be copied to dest. + * Implementations of this method may assume that dest <= start || + * dest >= limit. + */ + void copy(int start, int limit, int dest); } diff --git a/icu4j/src/com/ibm/icu/text/ReplaceableString.java b/icu4j/src/com/ibm/icu/text/ReplaceableString.java index 0b226af8f5..1a99786b3d 100755 --- a/icu4j/src/com/ibm/icu/text/ReplaceableString.java +++ b/icu4j/src/com/ibm/icu/text/ReplaceableString.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/ReplaceableString.java,v $ - * $Date: 2000/03/10 04:07:22 $ - * $Revision: 1.2 $ + * $Date: 2000/04/25 17:17:37 $ + * $Revision: 1.3 $ * ***************************************************************************************** */ @@ -24,7 +24,7 @@ package com.ibm.text; * * @see Replaceable * @author Alan Liu - * @version $RCSfile: ReplaceableString.java,v $ $Revision: 1.2 $ $Date: 2000/03/10 04:07:22 $ + * @version $RCSfile: ReplaceableString.java,v $ $Revision: 1.3 $ $Date: 2000/04/25 17:17:37 $ */ public class ReplaceableString implements Replaceable { private StringBuffer buf; @@ -67,16 +67,6 @@ public class ReplaceableString implements Replaceable { return buf.toString(); } - /** - * Return the internal storage of this object. Note! Any - * changes made to the returned object affect this object's - * contents, and vice versa. - * @return internal buffer used by this object - */ - public StringBuffer getStringBuffer() { - return buf; - } - /** * Return the number of characters contained in this object. * Replaceable API. @@ -168,4 +158,24 @@ public class ReplaceableString implements Replaceable { buf.append(tail); } } + + /** + * Copy a substring of this object, retaining attribute (out-of-band) + * information. This method is used to duplicate or reorder substrings. + * The destination index must not overlap the source range. + * + * @param start the beginning index, inclusive; 0 <= start <= + * limit. + * @param limit the ending index, exclusive; start <= limit <= + * length(). + * @param dest the destination index. The characters from + * start..limit-1 will be copied to dest. + * Implementations of this method may assume that dest <= start || + * dest >= limit. + */ + public void copy(int start, int limit, int dest) { + char[] text = new char[limit - start]; + getChars(start, limit, text, 0); + replace(dest, dest, text, 0, limit - start); + } } diff --git a/icu4j/src/com/ibm/icu/text/TransliterationRule.java b/icu4j/src/com/ibm/icu/text/TransliterationRule.java index 3815b33dab..657e836e99 100755 --- a/icu4j/src/com/ibm/icu/text/TransliterationRule.java +++ b/icu4j/src/com/ibm/icu/text/TransliterationRule.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/TransliterationRule.java,v $ - * $Date: 2000/04/25 01:42:58 $ - * $Revision: 1.19 $ + * $Date: 2000/04/25 17:17:37 $ + * $Revision: 1.20 $ * ***************************************************************************************** */ @@ -44,7 +44,7 @@ import com.ibm.util.Utility; *

Copyright © IBM Corporation 1999. All rights reserved. * * @author Alan Liu - * @version $RCSfile: TransliterationRule.java,v $ $Revision: 1.19 $ $Date: 2000/04/25 01:42:58 $ + * @version $RCSfile: TransliterationRule.java,v $ $Revision: 1.20 $ $Date: 2000/04/25 17:17:37 $ */ class TransliterationRule { /** @@ -259,28 +259,52 @@ class TransliterationRule { */ public int replace(Replaceable text, int offset, RuleBasedTransliterator.Data data) { - String out; if (segments == null) { - out = output; + text.replace(offset, offset + keyLength, output); + return output.length() - keyLength; } else { + /* When there are segments to be copied, use the Replaceable.copy() + * API in order to retain out-of-band data. Copy everything to the + * point after the key, then delete the key. That is, copy things + * into offset + keyLength, then replace offset .. offset + + * keyLength with the empty string. + * + * Minimize the number of calls to Replaceable.replace() and + * Replaceable.copy(). + */ int textStart = offset - anteContextLength; + int dest = offset + keyLength; // copy new text to here StringBuffer buf = new StringBuffer(); for (int i=0; i 0) { + text.replace(dest, dest, buf.toString()); + dest += buf.length(); + buf.setLength(0); } + // Copy segment with out-of-band data + b *= 2; + text.copy(textStart + segments[b], + textStart + segments[b+1], dest); + dest += segments[b+1] - segments[b]; } + } - out = buf.toString(); + // Insert any accumulated straight text. + if (buf.length() > 0) { + text.replace(dest, dest, buf.toString()); + dest += buf.length(); + } + // Delete the key + text.replace(offset, offset + keyLength, ""); + return dest - (offset + keyLength) - keyLength; } - text.replace(offset, offset + keyLength, out); - return out.length() - keyLength; } /** @@ -493,6 +517,9 @@ class TransliterationRule { /** * $Log: TransliterationRule.java,v $ + * Revision 1.20 2000/04/25 17:17:37 alan + * Add Replaceable.copy to retain out-of-band info during reordering. + * * Revision 1.19 2000/04/25 01:42:58 alan * Allow arbitrary length variable values. Clean up Data API. Update javadocs. * diff --git a/icu4j/src/com/ibm/text/Replaceable.java b/icu4j/src/com/ibm/text/Replaceable.java index 479b3c44fe..d3b31707e4 100755 --- a/icu4j/src/com/ibm/text/Replaceable.java +++ b/icu4j/src/com/ibm/text/Replaceable.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/text/Attic/Replaceable.java,v $ - * $Date: 2000/03/10 04:07:22 $ - * $Revision: 1.2 $ + * $Date: 2000/04/25 17:17:37 $ + * $Revision: 1.3 $ * ***************************************************************************************** */ @@ -23,7 +23,7 @@ package com.ibm.text; *

Copyright © IBM Corporation 1999. All rights reserved. * * @author Alan Liu - * @version $RCSfile: Replaceable.java,v $ $Revision: 1.2 $ $Date: 2000/03/10 04:07:22 $ + * @version $RCSfile: Replaceable.java,v $ $Revision: 1.3 $ $Date: 2000/04/25 17:17:37 $ */ public interface Replaceable { /** @@ -86,4 +86,26 @@ public interface Replaceable { int charsStart, int charsLen); // Note: We use length rather than limit to conform to StringBuffer // and System.arraycopy. + + /** + * Copy a substring of this object, retaining attribute (out-of-band) + * information. This method is used to duplicate or reorder substrings. + * The destination index must not overlap the source range. + * Implementations that do not care about maintaining out-of-band + * information during copying may use the naive implementation: + * + *

 char[] text = new char[limit - start];
+     * getChars(start, limit, text, 0);
+     * replace(dest, dest, text, 0, limit - start);
+ * + * @param start the beginning index, inclusive; 0 <= start <= + * limit. + * @param limit the ending index, exclusive; start <= limit <= + * length(). + * @param dest the destination index. The characters from + * start..limit-1 will be copied to dest. + * Implementations of this method may assume that dest <= start || + * dest >= limit. + */ + void copy(int start, int limit, int dest); } diff --git a/icu4j/src/com/ibm/text/ReplaceableString.java b/icu4j/src/com/ibm/text/ReplaceableString.java index 5228495cd2..b383c2a5f2 100755 --- a/icu4j/src/com/ibm/text/ReplaceableString.java +++ b/icu4j/src/com/ibm/text/ReplaceableString.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/text/Attic/ReplaceableString.java,v $ - * $Date: 2000/03/10 04:07:22 $ - * $Revision: 1.2 $ + * $Date: 2000/04/25 17:17:37 $ + * $Revision: 1.3 $ * ***************************************************************************************** */ @@ -24,7 +24,7 @@ package com.ibm.text; * * @see Replaceable * @author Alan Liu - * @version $RCSfile: ReplaceableString.java,v $ $Revision: 1.2 $ $Date: 2000/03/10 04:07:22 $ + * @version $RCSfile: ReplaceableString.java,v $ $Revision: 1.3 $ $Date: 2000/04/25 17:17:37 $ */ public class ReplaceableString implements Replaceable { private StringBuffer buf; @@ -67,16 +67,6 @@ public class ReplaceableString implements Replaceable { return buf.toString(); } - /** - * Return the internal storage of this object. Note! Any - * changes made to the returned object affect this object's - * contents, and vice versa. - * @return internal buffer used by this object - */ - public StringBuffer getStringBuffer() { - return buf; - } - /** * Return the number of characters contained in this object. * Replaceable API. @@ -168,4 +158,24 @@ public class ReplaceableString implements Replaceable { buf.append(tail); } } + + /** + * Copy a substring of this object, retaining attribute (out-of-band) + * information. This method is used to duplicate or reorder substrings. + * The destination index must not overlap the source range. + * + * @param start the beginning index, inclusive; 0 <= start <= + * limit. + * @param limit the ending index, exclusive; start <= limit <= + * length(). + * @param dest the destination index. The characters from + * start..limit-1 will be copied to dest. + * Implementations of this method may assume that dest <= start || + * dest >= limit. + */ + public void copy(int start, int limit, int dest) { + char[] text = new char[limit - start]; + getChars(start, limit, text, 0); + replace(dest, dest, text, 0, limit - start); + } } diff --git a/icu4j/src/com/ibm/text/TransliterationRule.java b/icu4j/src/com/ibm/text/TransliterationRule.java index b78cfed5e8..3873751828 100755 --- a/icu4j/src/com/ibm/text/TransliterationRule.java +++ b/icu4j/src/com/ibm/text/TransliterationRule.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/text/Attic/TransliterationRule.java,v $ - * $Date: 2000/04/25 01:42:58 $ - * $Revision: 1.19 $ + * $Date: 2000/04/25 17:17:37 $ + * $Revision: 1.20 $ * ***************************************************************************************** */ @@ -44,7 +44,7 @@ import com.ibm.util.Utility; *

Copyright © IBM Corporation 1999. All rights reserved. * * @author Alan Liu - * @version $RCSfile: TransliterationRule.java,v $ $Revision: 1.19 $ $Date: 2000/04/25 01:42:58 $ + * @version $RCSfile: TransliterationRule.java,v $ $Revision: 1.20 $ $Date: 2000/04/25 17:17:37 $ */ class TransliterationRule { /** @@ -259,28 +259,52 @@ class TransliterationRule { */ public int replace(Replaceable text, int offset, RuleBasedTransliterator.Data data) { - String out; if (segments == null) { - out = output; + text.replace(offset, offset + keyLength, output); + return output.length() - keyLength; } else { + /* When there are segments to be copied, use the Replaceable.copy() + * API in order to retain out-of-band data. Copy everything to the + * point after the key, then delete the key. That is, copy things + * into offset + keyLength, then replace offset .. offset + + * keyLength with the empty string. + * + * Minimize the number of calls to Replaceable.replace() and + * Replaceable.copy(). + */ int textStart = offset - anteContextLength; + int dest = offset + keyLength; // copy new text to here StringBuffer buf = new StringBuffer(); for (int i=0; i 0) { + text.replace(dest, dest, buf.toString()); + dest += buf.length(); + buf.setLength(0); } + // Copy segment with out-of-band data + b *= 2; + text.copy(textStart + segments[b], + textStart + segments[b+1], dest); + dest += segments[b+1] - segments[b]; } + } - out = buf.toString(); + // Insert any accumulated straight text. + if (buf.length() > 0) { + text.replace(dest, dest, buf.toString()); + dest += buf.length(); + } + // Delete the key + text.replace(offset, offset + keyLength, ""); + return dest - (offset + keyLength) - keyLength; } - text.replace(offset, offset + keyLength, out); - return out.length() - keyLength; } /** @@ -493,6 +517,9 @@ class TransliterationRule { /** * $Log: TransliterationRule.java,v $ + * Revision 1.20 2000/04/25 17:17:37 alan + * Add Replaceable.copy to retain out-of-band info during reordering. + * * Revision 1.19 2000/04/25 01:42:58 alan * Allow arbitrary length variable values. Clean up Data API. Update javadocs. *