ICU-1533 incorporate Mark's comments

X-SVN-Rev: 6992
This commit is contained in:
Alan Liu 2001-11-19 20:53:06 +00:00
parent 117df8c791
commit 4f8c05c3af
4 changed files with 169 additions and 145 deletions

View File

@ -101,6 +101,18 @@ U_NAMESPACE_BEGIN
*/ */
const char Transliterator::fgClassID = 0; // Value is irrelevant const char Transliterator::fgClassID = 0; // Value is irrelevant
/**
* Return TRUE if the given UTransPosition is valid for text of
* the given length.
*/
inline UBool positionIsValid(UTransPosition& index, int32_t len) {
return !(index.contextStart < 0 ||
index.start < index.contextStart ||
index.limit < index.start ||
index.contextLimit < index.limit ||
len < index.contextLimit);
}
/** /**
* Default constructor. * Default constructor.
* @param theID the string identifier for this transliterator * @param theID the string identifier for this transliterator
@ -300,11 +312,7 @@ void Transliterator::transliterate(Replaceable& text,
*/ */
void Transliterator::finishTransliteration(Replaceable& text, void Transliterator::finishTransliteration(Replaceable& text,
UTransPosition& index) const { UTransPosition& index) const {
if (index.contextStart < 0 || if (!positionIsValid(index, text.length())) {
index.start < index.contextStart ||
index.limit < index.start ||
index.contextLimit < index.limit ||
text.length() < index.contextLimit) {
return; return;
} }
@ -326,11 +334,7 @@ void Transliterator::_transliterate(Replaceable& text,
return; return;
} }
if (index.contextStart < 0 || if (!positionIsValid(index, text.length())) {
index.start < index.contextStart ||
index.limit < index.start ||
index.contextLimit < index.limit ||
text.length() < index.contextLimit) {
status = U_ILLEGAL_ARGUMENT_ERROR; status = U_ILLEGAL_ARGUMENT_ERROR;
return; return;
} }
@ -354,6 +358,7 @@ void Transliterator::_transliterate(Replaceable& text,
filteredTransliterate(text, index, TRUE); filteredTransliterate(text, index, TRUE);
#if 0 #if 0
// TODO
// I CAN'T DO what I'm attempting below now that the Kleene star // I CAN'T DO what I'm attempting below now that the Kleene star
// operator is supported. For example, in the rule // operator is supported. For example, in the rule

View File

@ -709,9 +709,6 @@ public:
* <code>getInstance("B-A")</code>, or <code>null</code> if that * <code>getInstance("B-A")</code>, or <code>null</code> if that
* call fails. * call fails.
* *
* <p>This method does not take filtering into account. The
* returned transliterator will have no filter.
*
* <p>Subclasses with knowledge of their inverse may wish to * <p>Subclasses with knowledge of their inverse may wish to
* override this method. * override this method.
* *

View File

@ -5,8 +5,8 @@
******************************************************************************* *******************************************************************************
* *
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/Transliterator.java,v $ * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/Transliterator.java,v $
* $Date: 2001/11/19 19:27:51 $ * $Date: 2001/11/19 20:53:06 $
* $Revision: 1.60 $ * $Revision: 1.61 $
* *
***************************************************************************************** *****************************************************************************************
*/ */
@ -242,7 +242,7 @@ import com.ibm.util.Utility;
* <p>Copyright &copy; IBM Corporation 1999. All rights reserved. * <p>Copyright &copy; IBM Corporation 1999. All rights reserved.
* *
* @author Alan Liu * @author Alan Liu
* @version $RCSfile: Transliterator.java,v $ $Revision: 1.60 $ $Date: 2001/11/19 19:27:51 $ * @version $RCSfile: Transliterator.java,v $ $Revision: 1.61 $ $Date: 2001/11/19 20:53:06 $
*/ */
public abstract class Transliterator { public abstract class Transliterator {
/** /**
@ -331,6 +331,27 @@ public abstract class Transliterator {
this.start = start; this.start = start;
this.limit = limit; this.limit = limit;
} }
/**
* Check all bounds. If they are invalid, throw an exception.
* @param length the length of the string this object applies to
* @exception IllegalArgumentException if any indices are out
* of bounds
*/
public final void validate(int length) {
if (contextStart < 0 ||
start < contextStart ||
limit < start ||
contextLimit < limit ||
length < contextLimit) {
throw new IllegalArgumentException("Invalid Position {cs=" +
contextStart + ", s=" +
start + ", l=" +
limit + ", cl=" +
contextLimit + "}, len=" +
length);
}
}
} }
/** /**
@ -511,18 +532,7 @@ public abstract class Transliterator {
*/ */
public final void transliterate(Replaceable text, Position index, public final void transliterate(Replaceable text, Position index,
String insertion) { String insertion) {
if (index.contextStart < 0 || index.validate(text.length());
index.start < index.contextStart ||
index.limit < index.start ||
index.contextLimit < index.limit ||
text.length() < index.contextLimit) {
throw new IllegalArgumentException("Invalid index {" +
index.contextStart + ", " +
index.start + ", " +
index.limit + ", " +
index.contextLimit + "}, len=" +
text.length());
}
// int originalStart = index.contextStart; // int originalStart = index.contextStart;
if (insertion != null) { if (insertion != null) {
@ -542,6 +552,7 @@ public abstract class Transliterator {
filteredTransliterate(text, index, true); filteredTransliterate(text, index, true);
// TODO
// This doesn't work once we add quantifier support. Need to rewrite // This doesn't work once we add quantifier support. Need to rewrite
// this code to support quantifiers and 'use maximum backup <n>;'. // this code to support quantifiers and 'use maximum backup <n>;'.
// //
@ -596,19 +607,7 @@ public abstract class Transliterator {
*/ */
public final void finishTransliteration(Replaceable text, public final void finishTransliteration(Replaceable text,
Position index) { Position index) {
if (index.contextStart < 0 || index.validate(text.length());
index.start < index.contextStart ||
index.limit < index.start ||
index.contextLimit < index.limit ||
text.length() < index.contextLimit) {
throw new IllegalArgumentException("Invalid index {" +
index.contextStart + ", " +
index.start + ", " +
index.limit + ", " +
index.contextLimit + "}, len=" +
text.length());
}
filteredTransliterate(text, index, false); filteredTransliterate(text, index, false);
} }
@ -1571,9 +1570,6 @@ public abstract class Transliterator {
* <code>getInstance("B-A")</code>, or <code>null</code> if that * <code>getInstance("B-A")</code>, or <code>null</code> if that
* call fails. * call fails.
* *
* <p>This method does not take filtering into account. The
* returned transliterator will have no filter.
*
* <p>Subclasses with knowledge of their inverse may wish to * <p>Subclasses with knowledge of their inverse may wish to
* override this method. * override this method.
* *
@ -1735,45 +1731,60 @@ public abstract class Transliterator {
String line = null; String line = null;
try { try {
line = r.readLine(); line = r.readLine();
} catch (java.io.IOException e) {} } catch (java.io.IOException e) {
throw new RuntimeException("Can't read Transliterator_index.txt");
}
if (line == null) { if (line == null) {
break; break;
} }
// Skip over whitespace try {
int pos = 0; // Skip over whitespace
while (pos < line.length() && int pos = 0;
Character.isWhitespace(line.charAt(pos))) { while (pos < line.length() &&
++pos; Character.isWhitespace(line.charAt(pos))) {
} ++pos;
// Ignore blank lines and comments }
if (pos == line.length() || line.charAt(pos) == '#') { // Ignore blank lines and comments
continue; if (pos == line.length() || line.charAt(pos) == '#') {
} continue;
// Parse colon-delimited line }
int colon = line.indexOf(':', pos); // Parse colon-delimited line
String ID = line.substring(pos, colon); int colon = line.indexOf(':', pos);
pos = colon+1; String ID = line.substring(pos, colon);
colon = line.indexOf(':', pos); pos = colon+1;
String type = line.substring(pos, colon);
pos = colon+1;
if (type.equals("file") || type.equals("internal")) {
// Rest of line is <resource>:<encoding>:<direction>
// pos colon c2
colon = line.indexOf(':', pos); colon = line.indexOf(':', pos);
int c2 = line.indexOf(':', colon+1); String type = line.substring(pos, colon);
int dir = line.substring(c2+1).equals("FORWARD") ? pos = colon+1;
FORWARD : REVERSE;
registry.put(ID, if (type.equals("file") || type.equals("internal")) {
line.substring(pos, colon), // resource // Rest of line is <resource>:<encoding>:<direction>
line.substring(colon+1, c2), // encoding // pos colon c2
dir, colon = line.indexOf(':', pos);
!type.equals("internal")); int c2 = line.indexOf(':', colon+1);
} else if (type.equals("alias")) { int dir;
// Rest of line is the <getInstanceArg> switch (line.charAt(c2+1)) {
registry.put(ID, line.substring(pos), true); case 'F':
} else { dir = FORWARD;
// Unknown type break;
case 'R':
dir = REVERSE;
break;
default:
throw new RuntimeException("Can't parse line: " + line);
}
registry.put(ID,
line.substring(pos, colon), // resource
line.substring(colon+1, c2), // encoding
dir,
!type.equals("internal"));
} else if (type.equals("alias")) {
// Rest of line is the <getInstanceArg>
registry.put(ID, line.substring(pos), true);
} else {
// Unknown type
throw new RuntimeException("Can't parse line: " + line);
}
} catch (StringIndexOutOfBoundsException e) {
throw new RuntimeException("Can't parse line: " + line); throw new RuntimeException("Can't parse line: " + line);
} }
} }

View File

@ -5,8 +5,8 @@
******************************************************************************* *******************************************************************************
* *
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/text/Attic/Transliterator.java,v $ * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/text/Attic/Transliterator.java,v $
* $Date: 2001/11/19 19:27:51 $ * $Date: 2001/11/19 20:53:06 $
* $Revision: 1.60 $ * $Revision: 1.61 $
* *
***************************************************************************************** *****************************************************************************************
*/ */
@ -242,7 +242,7 @@ import com.ibm.util.Utility;
* <p>Copyright &copy; IBM Corporation 1999. All rights reserved. * <p>Copyright &copy; IBM Corporation 1999. All rights reserved.
* *
* @author Alan Liu * @author Alan Liu
* @version $RCSfile: Transliterator.java,v $ $Revision: 1.60 $ $Date: 2001/11/19 19:27:51 $ * @version $RCSfile: Transliterator.java,v $ $Revision: 1.61 $ $Date: 2001/11/19 20:53:06 $
*/ */
public abstract class Transliterator { public abstract class Transliterator {
/** /**
@ -331,6 +331,27 @@ public abstract class Transliterator {
this.start = start; this.start = start;
this.limit = limit; this.limit = limit;
} }
/**
* Check all bounds. If they are invalid, throw an exception.
* @param length the length of the string this object applies to
* @exception IllegalArgumentException if any indices are out
* of bounds
*/
public final void validate(int length) {
if (contextStart < 0 ||
start < contextStart ||
limit < start ||
contextLimit < limit ||
length < contextLimit) {
throw new IllegalArgumentException("Invalid Position {cs=" +
contextStart + ", s=" +
start + ", l=" +
limit + ", cl=" +
contextLimit + "}, len=" +
length);
}
}
} }
/** /**
@ -511,18 +532,7 @@ public abstract class Transliterator {
*/ */
public final void transliterate(Replaceable text, Position index, public final void transliterate(Replaceable text, Position index,
String insertion) { String insertion) {
if (index.contextStart < 0 || index.validate(text.length());
index.start < index.contextStart ||
index.limit < index.start ||
index.contextLimit < index.limit ||
text.length() < index.contextLimit) {
throw new IllegalArgumentException("Invalid index {" +
index.contextStart + ", " +
index.start + ", " +
index.limit + ", " +
index.contextLimit + "}, len=" +
text.length());
}
// int originalStart = index.contextStart; // int originalStart = index.contextStart;
if (insertion != null) { if (insertion != null) {
@ -542,6 +552,7 @@ public abstract class Transliterator {
filteredTransliterate(text, index, true); filteredTransliterate(text, index, true);
// TODO
// This doesn't work once we add quantifier support. Need to rewrite // This doesn't work once we add quantifier support. Need to rewrite
// this code to support quantifiers and 'use maximum backup <n>;'. // this code to support quantifiers and 'use maximum backup <n>;'.
// //
@ -596,19 +607,7 @@ public abstract class Transliterator {
*/ */
public final void finishTransliteration(Replaceable text, public final void finishTransliteration(Replaceable text,
Position index) { Position index) {
if (index.contextStart < 0 || index.validate(text.length());
index.start < index.contextStart ||
index.limit < index.start ||
index.contextLimit < index.limit ||
text.length() < index.contextLimit) {
throw new IllegalArgumentException("Invalid index {" +
index.contextStart + ", " +
index.start + ", " +
index.limit + ", " +
index.contextLimit + "}, len=" +
text.length());
}
filteredTransliterate(text, index, false); filteredTransliterate(text, index, false);
} }
@ -1571,9 +1570,6 @@ public abstract class Transliterator {
* <code>getInstance("B-A")</code>, or <code>null</code> if that * <code>getInstance("B-A")</code>, or <code>null</code> if that
* call fails. * call fails.
* *
* <p>This method does not take filtering into account. The
* returned transliterator will have no filter.
*
* <p>Subclasses with knowledge of their inverse may wish to * <p>Subclasses with knowledge of their inverse may wish to
* override this method. * override this method.
* *
@ -1735,45 +1731,60 @@ public abstract class Transliterator {
String line = null; String line = null;
try { try {
line = r.readLine(); line = r.readLine();
} catch (java.io.IOException e) {} } catch (java.io.IOException e) {
throw new RuntimeException("Can't read Transliterator_index.txt");
}
if (line == null) { if (line == null) {
break; break;
} }
// Skip over whitespace try {
int pos = 0; // Skip over whitespace
while (pos < line.length() && int pos = 0;
Character.isWhitespace(line.charAt(pos))) { while (pos < line.length() &&
++pos; Character.isWhitespace(line.charAt(pos))) {
} ++pos;
// Ignore blank lines and comments }
if (pos == line.length() || line.charAt(pos) == '#') { // Ignore blank lines and comments
continue; if (pos == line.length() || line.charAt(pos) == '#') {
} continue;
// Parse colon-delimited line }
int colon = line.indexOf(':', pos); // Parse colon-delimited line
String ID = line.substring(pos, colon); int colon = line.indexOf(':', pos);
pos = colon+1; String ID = line.substring(pos, colon);
colon = line.indexOf(':', pos); pos = colon+1;
String type = line.substring(pos, colon);
pos = colon+1;
if (type.equals("file") || type.equals("internal")) {
// Rest of line is <resource>:<encoding>:<direction>
// pos colon c2
colon = line.indexOf(':', pos); colon = line.indexOf(':', pos);
int c2 = line.indexOf(':', colon+1); String type = line.substring(pos, colon);
int dir = line.substring(c2+1).equals("FORWARD") ? pos = colon+1;
FORWARD : REVERSE;
registry.put(ID, if (type.equals("file") || type.equals("internal")) {
line.substring(pos, colon), // resource // Rest of line is <resource>:<encoding>:<direction>
line.substring(colon+1, c2), // encoding // pos colon c2
dir, colon = line.indexOf(':', pos);
!type.equals("internal")); int c2 = line.indexOf(':', colon+1);
} else if (type.equals("alias")) { int dir;
// Rest of line is the <getInstanceArg> switch (line.charAt(c2+1)) {
registry.put(ID, line.substring(pos), true); case 'F':
} else { dir = FORWARD;
// Unknown type break;
case 'R':
dir = REVERSE;
break;
default:
throw new RuntimeException("Can't parse line: " + line);
}
registry.put(ID,
line.substring(pos, colon), // resource
line.substring(colon+1, c2), // encoding
dir,
!type.equals("internal"));
} else if (type.equals("alias")) {
// Rest of line is the <getInstanceArg>
registry.put(ID, line.substring(pos), true);
} else {
// Unknown type
throw new RuntimeException("Can't parse line: " + line);
}
} catch (StringIndexOutOfBoundsException e) {
throw new RuntimeException("Can't parse line: " + line); throw new RuntimeException("Can't parse line: " + line);
} }
} }