From c65fdec5e21c3a59e0e0343ed4ea2b43df1d1a53 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Fri, 6 Mar 2015 19:46:43 +0000 Subject: [PATCH] ICU-11392 make readLine() easier to use X-SVN-Rev: 37157 --- .../icu/dev/test/collator/CollationTest.java | 73 +++++++------------ 1 file changed, 28 insertions(+), 45 deletions(-) diff --git a/icu4j/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationTest.java b/icu4j/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationTest.java index 018b18a3074..5046d933f31 100644 --- a/icu4j/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationTest.java +++ b/icu4j/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationTest.java @@ -1058,26 +1058,33 @@ public class CollationTest extends TestFmwk { return printSortKey(p); } - private boolean readLine(BufferedReader in) throws IOException { - String line = in.readLine(); - if (line == null) { - 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; + private boolean readNonEmptyLine(BufferedReader in) throws IOException { + for (;;) { + String line = in.readLine(); + if (line == null) { + fileLine = null; + return false; } + 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 prefix, Output s) throws ParseException { @@ -1324,13 +1331,7 @@ public class CollationTest extends TestFmwk { private void buildTailoring(BufferedReader in) throws IOException { StringBuilder rules = new StringBuilder(); - while (readLine(in)) { - if (fileLine.length() == 0) { - continue; - } - if (isSectionStarter(fileLine.charAt(0))) { - break; - } + while (readNonEmptyLine(in) && !isSectionStarter(fileLine.charAt(0))) { rules.append(Utility.unescape(fileLine)); } @@ -1621,13 +1622,7 @@ public class CollationTest extends TestFmwk { String prevFileLine = "(none)"; String prevString = ""; Output sOut = new Output(); - while (readLine(in)) { - if (fileLine.length() == 0) { - continue; - } - if (isSectionStarter(fileLine.charAt(0))) { - break; - } + while (readNonEmptyLine(in) && !isSectionStarter(fileLine.charAt(0))) { // Parse the line even if it will be ignored (when we do not have a Collator) // in order to report syntax issues. int relation; @@ -1677,19 +1672,7 @@ public class CollationTest extends TestFmwk { try { in = TestUtil.getDataReader("collationtest.txt", "UTF-8"); - // read first line and remove BOM if present - 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; - } + while (fileLine != null || readNonEmptyLine(in)) { if (!isSectionStarter(fileLine.charAt(0))) { logln(fileLine); errln("syntax error on line " + fileLineNumber);