Skip to content
This repository was archived by the owner on Jul 16, 2022. It is now read-only.

Commit cac15eb

Browse files
authored
Merge pull request #50 from eb4j/topic/miurahr/ignore-comment-line
Loader: ignore comment line
2 parents 6e56fb9 + 4e1470c commit cac15eb

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/main/java/io/github/eb4j/dsl/impl/EntriesLoaderImpl.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class EntriesLoaderImpl implements AutoCloseable {
4545
private final byte[] lf;
4646
private final byte[] tab;
4747
private final byte[] space;
48+
private final byte[] commentStart;
4849

4950
public EntriesLoaderImpl(final Path path, final boolean isDictZip, final Charset charset, final byte[] eol)
5051
throws IOException {
@@ -64,6 +65,7 @@ public EntriesLoaderImpl(final Path path, final boolean isDictZip, final Charset
6465
lf = "\n".getBytes(charset);
6566
tab = "\t".getBytes(charset);
6667
space = " ".getBytes(charset);
68+
commentStart = "{{".getBytes(charset);
6769
}
6870

6971
public void close() throws IOException {
@@ -81,13 +83,19 @@ public List<DslIndex.Entry> load() throws IOException {
8183
String headWords;
8284
cardStart = entryStartSearch();
8385
while (true) {
86+
// skip comment
87+
long next = skipComment();
8488
// check multiple head words
8589
long headWordLen = eolSearch();
8690
if (headWordLen == -1) {
8791
break;
8892
}
8993
while (!isSpaceOrTab()) {
90-
headWordLen += eolSearch();
94+
next = eolSearch();
95+
if (next == -1) {
96+
break;
97+
}
98+
headWordLen += next;
9199
}
92100
seek(cardStart);
93101
byte[] headWordBytes = new byte[(int) headWordLen];
@@ -151,6 +159,33 @@ private long position() throws IOException {
151159
}
152160
}
153161

162+
private long skipComment() throws IOException {
163+
byte[] b = new byte[commentStart.length];
164+
InputStream is;
165+
if (isDictZip) {
166+
is = dzis;
167+
} else {
168+
is = rais;
169+
}
170+
is.mark(commentStart.length);
171+
if (is.read(b) > 0) {
172+
if (Arrays.equals(commentStart, b)) {
173+
// a line looks comment
174+
long next = eolSearch();
175+
if (next == -1) {
176+
return -1;
177+
}
178+
// we should check end of comment, but now we ignore line.
179+
return next;
180+
}
181+
is.reset();
182+
return 0;
183+
}
184+
// got EOF
185+
return -1;
186+
187+
}
188+
154189
private long entryStartSearch() throws IOException {
155190
byte[] b = new byte[eol.length];
156191
InputStream is;

src/test/java/io/github/eb4j/dsl/DslDictionaryTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,14 @@ void loadUTF16LE_BOM_CRLF_EL_DZ() throws URISyntaxException, IOException {
283283
"[m2]to [ref]abandon \\[price\\] control[/ref][/m]\n" +
284284
"[m2]to [ref]abandon a right[/ref][/m]\n", entry.getValue());
285285
}
286+
287+
@Test
288+
void loadUtf8_Comment() throws URISyntaxException, IOException {
289+
URL utf8 = this.getClass().getResource("/utf8_comment.dsl");
290+
DslDictionary dictionary = DslDictionary.loadDictionary(new File(utf8.toURI()));
291+
DumpDslVisitor dumper = new DumpDslVisitor();
292+
DslResult results = dictionary.lookup("About");
293+
Map.Entry<String, String> entry = results.getEntries(dumper).get(0);
294+
assertEquals("Version: 0.0\n", entry.getValue());
295+
}
286296
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#NAME "test (En-Ru)"
2+
#INDEX_LANGUAGE "English"
3+
#CONTENTS_LANGUAGE "Russian"
4+
5+
ABC
6+
[m1]\[[t]ABC[/t]\] [p]n[/p][/m]
7+
Foo
8+
[m1]\[[t]Boo[/t]\] [p]pl[/p][/m]
9+
10+
{{ The End }}
11+
12+
{{ Техническая часть }}
13+
14+
About
15+
**
16+
Version: 0.0
17+
18+
{{ Техническая часть }}

0 commit comments

Comments
 (0)