Add Replaceable.copy to retain out-of-band info during reordering.

X-SVN-Rev: 1244
This commit is contained in:
Alan Liu 2000-04-25 17:17:37 +00:00
parent 0b9db3eadf
commit 943b8564c9
6 changed files with 172 additions and 54 deletions

View File

@ -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;
* <p>Copyright &copy; 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:
*
* <pre> char[] text = new char[limit - start];
* getChars(start, limit, text, 0);
* replace(dest, dest, text, 0, limit - start);</pre>
*
* @param start the beginning index, inclusive; <code>0 <= start <=
* limit</code>.
* @param limit the ending index, exclusive; <code>start <= limit <=
* length()</code>.
* @param dest the destination index. The characters from
* <code>start..limit-1</code> will be copied to <code>dest</code>.
* Implementations of this method may assume that <code>dest <= start ||
* dest >= limit</code>.
*/
void copy(int start, int limit, int dest);
}

View File

@ -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. <em>Note! Any
* changes made to the returned object affect this object's
* contents, and vice versa.</em>
* @return internal buffer used by this object
*/
public StringBuffer getStringBuffer() {
return buf;
}
/**
* Return the number of characters contained in this object.
* <code>Replaceable</code> 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; <code>0 <= start <=
* limit</code>.
* @param limit the ending index, exclusive; <code>start <= limit <=
* length()</code>.
* @param dest the destination index. The characters from
* <code>start..limit-1</code> will be copied to <code>dest</code>.
* Implementations of this method may assume that <code>dest <= start ||
* dest >= limit</code>.
*/
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);
}
}

View File

@ -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;
* <p>Copyright &copy; 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<output.length(); ++i) {
char c = output.charAt(i);
int b = data.lookupSegmentReference(c);
if (b < 0) {
// Accumulate straight (non-segment) text.
buf.append(c);
} else {
for (int j=textStart + segments[2*b];
j<textStart + segments[2*b+1]; ++j) {
buf.append(text.charAt(j));
// Insert any accumulated straight text.
if (buf.length() > 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.
*

View File

@ -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;
* <p>Copyright &copy; 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:
*
* <pre> char[] text = new char[limit - start];
* getChars(start, limit, text, 0);
* replace(dest, dest, text, 0, limit - start);</pre>
*
* @param start the beginning index, inclusive; <code>0 <= start <=
* limit</code>.
* @param limit the ending index, exclusive; <code>start <= limit <=
* length()</code>.
* @param dest the destination index. The characters from
* <code>start..limit-1</code> will be copied to <code>dest</code>.
* Implementations of this method may assume that <code>dest <= start ||
* dest >= limit</code>.
*/
void copy(int start, int limit, int dest);
}

View File

@ -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. <em>Note! Any
* changes made to the returned object affect this object's
* contents, and vice versa.</em>
* @return internal buffer used by this object
*/
public StringBuffer getStringBuffer() {
return buf;
}
/**
* Return the number of characters contained in this object.
* <code>Replaceable</code> 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; <code>0 <= start <=
* limit</code>.
* @param limit the ending index, exclusive; <code>start <= limit <=
* length()</code>.
* @param dest the destination index. The characters from
* <code>start..limit-1</code> will be copied to <code>dest</code>.
* Implementations of this method may assume that <code>dest <= start ||
* dest >= limit</code>.
*/
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);
}
}

View File

@ -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;
* <p>Copyright &copy; 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<output.length(); ++i) {
char c = output.charAt(i);
int b = data.lookupSegmentReference(c);
if (b < 0) {
// Accumulate straight (non-segment) text.
buf.append(c);
} else {
for (int j=textStart + segments[2*b];
j<textStart + segments[2*b+1]; ++j) {
buf.append(text.charAt(j));
// Insert any accumulated straight text.
if (buf.length() > 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.
*