ICU-5736 Updated Utility#splitString(String, String) to treat the target string as string literal, not regular expression. Replaced TAB with SPs.

X-SVN-Rev: 22123
This commit is contained in:
Yoshito Umaoka 2007-07-24 16:15:22 +00:00
parent 8f18898777
commit 699e263039
2 changed files with 77 additions and 74 deletions

View file

@ -931,9 +931,9 @@ public class TestMessageFormat extends com.ibm.icu.dev.test.TestFmwk {
"{'", "{'",
"{'a", "{'a",
"{'a{}'a}'a", "{'a{}'a}''a",
"'}'", "'}'",
"'} '{'}'", "'} '{'}''",
"'} {{{''", "'} {{{'''",
"'}'", "'}'",
"'} '{'}'", "'} '{'}''",
"'} {{{''", "'} {{{'''",
};
for (int i = 0; i < patterns.length; i += 2) {
assertEquals("[" + (i/2) + "] \"" + patterns[i] + "\"", patterns[i+1], MessageFormat.autoQuoteApostrophe(patterns[i]));
@ -1131,13 +1131,13 @@ public class TestMessageFormat extends com.ibm.icu.dev.test.TestFmwk {
// Test named arguments.
MessageFormat mf = new MessageFormat("Number of files in folder {folder}: {numfiles}");
if (!mf.usesNamedArguments()) {
errln("message format 1 should have used named arguments");
}
if (!mf.usesNamedArguments()) {
errln("message format 1 should have used named arguments");
}
mf = new MessageFormat("Wavelength: {\u028EValue\uFF14}");
if (!mf.usesNamedArguments()) {
errln("message format 2 should have used named arguments");
}
if (!mf.usesNamedArguments()) {
errln("message format 2 should have used named arguments");
}
// Test argument names with invalid start characters.
try {
@ -1167,62 +1167,62 @@ public class TestMessageFormat extends com.ibm.icu.dev.test.TestFmwk {
}
public void testNumericFormatWithMap() {
MessageFormat mf = new MessageFormat("X:{2} Y:{1}");
if (mf.usesNamedArguments()) {
errln("should not use named arguments");
}
MessageFormat mf = new MessageFormat("X:{2} Y:{1}");
if (mf.usesNamedArguments()) {
errln("should not use named arguments");
}
Map map12 = new HashMap();
map12.put("1", "one");
map12.put("2", "two");
Map map12 = new HashMap();
map12.put("1", "one");
map12.put("2", "two");
String target = "X:two Y:one";
String result = mf.format(map12);
if (!target.equals(result)) {
errln("expected '" + target + "' but got '" + result + "'");
}
String target = "X:two Y:one";
String result = mf.format(map12);
if (!target.equals(result)) {
errln("expected '" + target + "' but got '" + result + "'");
}
try {
Map mapResult = mf.parseToMap(target);
if (!map12.equals(mapResult)) {
errln("expected " + map12 + " but got " + mapResult);
}
} catch (ParseException e) {
errln("unexpected exception: " + e.getMessage());
}
Map map10 = new HashMap();
map10.put("1", "one");
map10.put("0", "zero");
target = "X:{2} Y:one";
result = mf.format(map10);
if (!target.equals(result)) {
errln("expected '" + target + "' but got '" + result + "'");
}
try {
Map mapResult = mf.parseToMap(target);
if (!map12.equals(mapResult)) {
errln("expected " + map12 + " but got " + mapResult);
}
} catch (ParseException e) {
errln("unexpected exception: " + e.getMessage());
}
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
Map fmtMap = new HashMap();
fmtMap.put("1", dateFormat);
fmtMap.put("2", timeFormat);
mf.setFormatsByArgumentName(fmtMap);
Date date = new Date(661439820000L);
Map map10 = new HashMap();
map10.put("1", "one");
map10.put("0", "zero");
target = "X:{2} Y:one";
result = mf.format(map10);
if (!target.equals(result)) {
errln("expected '" + target + "' but got '" + result + "'");
}
try {
result = mf.format(map12); // should fail, wrong argument type
fail("expected exception but got '" + result + "'");
} catch (IllegalArgumentException e) {
// expect this
}
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
Map fmtMap = new HashMap();
fmtMap.put("1", dateFormat);
fmtMap.put("2", timeFormat);
mf.setFormatsByArgumentName(fmtMap);
Date date = new Date(661439820000L);
Map argMap = new HashMap();
argMap.put("1", date);
argMap.put("2", date);
target = "X:5:17:00 AM Y:Dec 17, 1990";
result = mf.format(argMap);
if (!target.equals(result)) {
errln("expected '" + target + "' but got '" + result + "'");
}
try {
result = mf.format(map12); // should fail, wrong argument type
fail("expected exception but got '" + result + "'");
} catch (IllegalArgumentException e) {
// expect this
}
Map argMap = new HashMap();
argMap.put("1", date);
argMap.put("2", date);
target = "X:5:17:00 AM Y:Dec 17, 1990";
result = mf.format(argMap);
if (!target.equals(result)) {
errln("expected '" + target + "' but got '" + result + "'");
}
}
// This tests nested Formats inside PluralFormat.

View file

@ -165,14 +165,14 @@ public final class Utility {
}
public final static boolean arrayRegionMatches(byte[] source, int sourceStart,
byte[] target, int targetStart, int len){
int sourceEnd = sourceStart + len;
int delta = targetStart - sourceStart;
for (int i = sourceStart; i < sourceEnd; i++) {
if (source[i] != target[i + delta])
return false;
}
return true;
}
int sourceEnd = sourceStart + len;
int delta = targetStart - sourceStart;
for (int i = sourceStart; i < sourceEnd; i++) {
if (source[i] != target[i + delta])
return false;
}
return true;
}
///CLOVER:ON
/**
@ -1848,7 +1848,7 @@ public final class Utility {
//#ifndef FOUNDATION
return buf.indexOf(s);
//#else
//## return buf.toString().indexOf(s);
//## return buf.toString().indexOf(s);
//#endif
}
@ -1857,7 +1857,7 @@ public final class Utility {
//#ifndef FOUNDATION
return buf.lastIndexOf(s);
//#else
//## return buf.toString().lastIndexOf(s);
//## return buf.toString().lastIndexOf(s);
//#endif
}
@ -1866,7 +1866,7 @@ public final class Utility {
//#ifndef FOUNDATION
return buf.indexOf(s, i);
//#else
//## return buf.toString().indexOf(s, i);
//## return buf.toString().indexOf(s, i);
//#endif
}
@ -1875,7 +1875,7 @@ public final class Utility {
//#ifndef FOUNDATION
return buf.lastIndexOf(s, i);
//#else
//## return buf.toString().lastIndexOf(s, i);
//## return buf.toString().lastIndexOf(s, i);
//#endif
}
@ -1903,10 +1903,13 @@ public final class Utility {
//#endif
}
// !!! 1.3 compatability
private static final String REGEX_SPECIALS = ".^$[]*+?|()";
// !!! 1.3 compatibility
// Note: target is not a string literal, not a regular expression.
public static String[] splitString(String src, String target) {
//#ifndef FOUNDATION
return src.split(target);
return src.split("\\Q" + target + "\\E");
//#else
//## int i = src.indexOf(target);
//## if (i == -1) {
@ -1926,7 +1929,7 @@ public final class Utility {
//#endif
}
// !!! 1.3 compatability
// !!! 1.3 compatibility
/**
* Split the string at runs of ascii whitespace characters.
*/