ICU-11392 make readLine() easier to use

X-SVN-Rev: 37157
This commit is contained in:
Markus Scherer 2015-03-06 19:46:43 +00:00
parent a2a9fba351
commit c65fdec5e2

View File

@ -1058,26 +1058,33 @@ public class CollationTest extends TestFmwk {
return printSortKey(p); return printSortKey(p);
} }
private boolean readLine(BufferedReader in) throws IOException { private boolean readNonEmptyLine(BufferedReader in) throws IOException {
String line = in.readLine(); for (;;) {
if (line == null) { String line = in.readLine();
fileLine = null; if (line == null) {
return false; fileLine = null;
} return false;
++fileLineNumber;
// Strip trailing comments and spaces
int idx = line.indexOf('#');
if (idx < 0) {
idx = line.length();
}
for (; idx > 0; idx--) {
if (!isSpace(line.charAt(idx -1))) {
break;
} }
if (fileLineNumber == 0 && line.length() != 0 && line.charAt(0) == '\uFEFF') {
line = line.substring(1); // Remove the BOM.
}
++fileLineNumber;
// Strip trailing comments and spaces
int idx = line.indexOf('#');
if (idx < 0) {
idx = line.length();
}
for (; idx > 0; idx--) {
if (!isSpace(line.charAt(idx -1))) {
break;
}
}
if (idx != 0) {
fileLine = idx < line.length() ? line.substring(0, idx) : line;
return true;
}
// Empty line, continue.
} }
fileLine = idx < line.length() ? line.substring(0, idx) : line;
return true;
} }
private int parseString(int start, Output<String> prefix, Output<String> s) throws ParseException { private int parseString(int start, Output<String> prefix, Output<String> s) throws ParseException {
@ -1324,13 +1331,7 @@ public class CollationTest extends TestFmwk {
private void buildTailoring(BufferedReader in) throws IOException { private void buildTailoring(BufferedReader in) throws IOException {
StringBuilder rules = new StringBuilder(); StringBuilder rules = new StringBuilder();
while (readLine(in)) { while (readNonEmptyLine(in) && !isSectionStarter(fileLine.charAt(0))) {
if (fileLine.length() == 0) {
continue;
}
if (isSectionStarter(fileLine.charAt(0))) {
break;
}
rules.append(Utility.unescape(fileLine)); rules.append(Utility.unescape(fileLine));
} }
@ -1621,13 +1622,7 @@ public class CollationTest extends TestFmwk {
String prevFileLine = "(none)"; String prevFileLine = "(none)";
String prevString = ""; String prevString = "";
Output<String> sOut = new Output<String>(); Output<String> sOut = new Output<String>();
while (readLine(in)) { while (readNonEmptyLine(in) && !isSectionStarter(fileLine.charAt(0))) {
if (fileLine.length() == 0) {
continue;
}
if (isSectionStarter(fileLine.charAt(0))) {
break;
}
// Parse the line even if it will be ignored (when we do not have a Collator) // Parse the line even if it will be ignored (when we do not have a Collator)
// in order to report syntax issues. // in order to report syntax issues.
int relation; int relation;
@ -1677,19 +1672,7 @@ public class CollationTest extends TestFmwk {
try { try {
in = TestUtil.getDataReader("collationtest.txt", "UTF-8"); in = TestUtil.getDataReader("collationtest.txt", "UTF-8");
// read first line and remove BOM if present while (fileLine != null || readNonEmptyLine(in)) {
readLine(in);
if (fileLine != null && fileLine.charAt(0) == '\uFEFF') {
fileLine = fileLine.substring(1);
}
while (true) {
if (fileLine == null || fileLine.length() == 0) {
if (!readLine(in)) {
break;
}
continue;
}
if (!isSectionStarter(fileLine.charAt(0))) { if (!isSectionStarter(fileLine.charAt(0))) {
logln(fileLine); logln(fileLine);
errln("syntax error on line " + fileLineNumber); errln("syntax error on line " + fileLineNumber);