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
/**
* 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.
* @param theID the string identifier for this transliterator
@ -300,11 +312,7 @@ void Transliterator::transliterate(Replaceable& text,
*/
void Transliterator::finishTransliteration(Replaceable& text,
UTransPosition& index) const {
if (index.contextStart < 0 ||
index.start < index.contextStart ||
index.limit < index.start ||
index.contextLimit < index.limit ||
text.length() < index.contextLimit) {
if (!positionIsValid(index, text.length())) {
return;
}
@ -326,11 +334,7 @@ void Transliterator::_transliterate(Replaceable& text,
return;
}
if (index.contextStart < 0 ||
index.start < index.contextStart ||
index.limit < index.start ||
index.contextLimit < index.limit ||
text.length() < index.contextLimit) {
if (!positionIsValid(index, text.length())) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
@ -354,6 +358,7 @@ void Transliterator::_transliterate(Replaceable& text,
filteredTransliterate(text, index, TRUE);
#if 0
// TODO
// I CAN'T DO what I'm attempting below now that the Kleene star
// 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
* 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
* override this method.
*

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/Transliterator.java,v $
* $Date: 2001/11/19 19:27:51 $
* $Revision: 1.60 $
* $Date: 2001/11/19 20:53:06 $
* $Revision: 1.61 $
*
*****************************************************************************************
*/
@ -242,7 +242,7 @@ import com.ibm.util.Utility;
* <p>Copyright &copy; IBM Corporation 1999. All rights reserved.
*
* @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 {
/**
@ -331,6 +331,27 @@ public abstract class Transliterator {
this.start = start;
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,
String insertion) {
if (index.contextStart < 0 ||
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());
}
index.validate(text.length());
// int originalStart = index.contextStart;
if (insertion != null) {
@ -542,6 +552,7 @@ public abstract class Transliterator {
filteredTransliterate(text, index, true);
// TODO
// This doesn't work once we add quantifier support. Need to rewrite
// this code to support quantifiers and 'use maximum backup <n>;'.
//
@ -596,19 +607,7 @@ public abstract class Transliterator {
*/
public final void finishTransliteration(Replaceable text,
Position index) {
if (index.contextStart < 0 ||
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());
}
index.validate(text.length());
filteredTransliterate(text, index, false);
}
@ -1571,9 +1570,6 @@ public abstract class Transliterator {
* <code>getInstance("B-A")</code>, or <code>null</code> if that
* 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
* override this method.
*
@ -1735,10 +1731,13 @@ public abstract class Transliterator {
String line = null;
try {
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) {
break;
}
try {
// Skip over whitespace
int pos = 0;
while (pos < line.length() &&
@ -1762,8 +1761,17 @@ public abstract class Transliterator {
// pos colon c2
colon = line.indexOf(':', pos);
int c2 = line.indexOf(':', colon+1);
int dir = line.substring(c2+1).equals("FORWARD") ?
FORWARD : REVERSE;
int dir;
switch (line.charAt(c2+1)) {
case 'F':
dir = FORWARD;
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
@ -1776,6 +1784,9 @@ public abstract class Transliterator {
// Unknown type
throw new RuntimeException("Can't parse line: " + line);
}
} catch (StringIndexOutOfBoundsException e) {
throw new RuntimeException("Can't parse line: " + line);
}
}
specialInverses = new Hashtable();

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/text/Attic/Transliterator.java,v $
* $Date: 2001/11/19 19:27:51 $
* $Revision: 1.60 $
* $Date: 2001/11/19 20:53:06 $
* $Revision: 1.61 $
*
*****************************************************************************************
*/
@ -242,7 +242,7 @@ import com.ibm.util.Utility;
* <p>Copyright &copy; IBM Corporation 1999. All rights reserved.
*
* @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 {
/**
@ -331,6 +331,27 @@ public abstract class Transliterator {
this.start = start;
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,
String insertion) {
if (index.contextStart < 0 ||
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());
}
index.validate(text.length());
// int originalStart = index.contextStart;
if (insertion != null) {
@ -542,6 +552,7 @@ public abstract class Transliterator {
filteredTransliterate(text, index, true);
// TODO
// This doesn't work once we add quantifier support. Need to rewrite
// this code to support quantifiers and 'use maximum backup <n>;'.
//
@ -596,19 +607,7 @@ public abstract class Transliterator {
*/
public final void finishTransliteration(Replaceable text,
Position index) {
if (index.contextStart < 0 ||
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());
}
index.validate(text.length());
filteredTransliterate(text, index, false);
}
@ -1571,9 +1570,6 @@ public abstract class Transliterator {
* <code>getInstance("B-A")</code>, or <code>null</code> if that
* 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
* override this method.
*
@ -1735,10 +1731,13 @@ public abstract class Transliterator {
String line = null;
try {
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) {
break;
}
try {
// Skip over whitespace
int pos = 0;
while (pos < line.length() &&
@ -1762,8 +1761,17 @@ public abstract class Transliterator {
// pos colon c2
colon = line.indexOf(':', pos);
int c2 = line.indexOf(':', colon+1);
int dir = line.substring(c2+1).equals("FORWARD") ?
FORWARD : REVERSE;
int dir;
switch (line.charAt(c2+1)) {
case 'F':
dir = FORWARD;
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
@ -1776,6 +1784,9 @@ public abstract class Transliterator {
// Unknown type
throw new RuntimeException("Can't parse line: " + line);
}
} catch (StringIndexOutOfBoundsException e) {
throw new RuntimeException("Can't parse line: " + line);
}
}
specialInverses = new Hashtable();