Skip to content

Commit 4a68f31

Browse files
committed
Make JFlex versions as efficient as previous hand-coding
1 parent 5454473 commit 4a68f31

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

opengrok-indexer/src/main/resources/util/LineBreakerScanner.lex

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,29 @@ import java.util.List;
3535
return false;
3636
%eofval}
3737
%eof{
38-
length = yychar;
39-
4038
/*
4139
* Following JFlexXref's custom, an empty file or a file ending with EOL
4240
* produces an additional line of length zero. We also ensure there are two
4341
* entries to describe the boundaries.
4442
*/
45-
if (lastHadEOL || offsets.size() <= 1) {
46-
offsets.add(yychar);
47-
}
43+
offsets.add(yychar);
44+
length = yychar;
4845
%eof}
4946
%{
5047
private int length;
5148

52-
private boolean lastHadEOL;
53-
5449
private List<Integer> offsets;
5550

5651
public int getLength() {
5752
return length;
5853
}
5954

6055
/**
61-
* Sets the required target to write.
56+
* Sets the required target to write, and adds a first offset of 0.
6257
* @param offsets a required instance
6358
*/
6459
public void setTarget(List<Integer> offsets) {
6560
this.length = 0;
66-
this.lastHadEOL = false;
6761
this.offsets = offsets;
6862
offsets.add(0);
6963
}
@@ -84,12 +78,9 @@ import java.util.List;
8478
%include Common.lexh
8579
%%
8680

87-
[^\n\r]* {EOL} {
81+
{EOL} {
8882
offsets.add(yychar + yylength());
89-
lastHadEOL = true;
9083
}
9184

92-
[^\n\r]+ {
93-
offsets.add(yychar + yylength());
94-
lastHadEOL = false;
85+
[^\n\r] {
9586
}

opengrok-indexer/src/main/resources/util/SourceSplitterScanner.lex

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,19 @@ import java.util.List;
3535
return false;
3636
%eofval}
3737
%eof{
38-
length = yychar;
39-
4038
/*
4139
* Following JFlexXref's custom, an empty file or a file ending with EOL
4240
* produces an additional line of length zero.
4341
*/
44-
if (lastHadEOL || lines.size() < 1) {
45-
lines.add("");
46-
}
42+
lines.add(builder.toString());
43+
builder.setLength(0);
44+
45+
length = yychar;
4746
%eof}
4847
%{
49-
private int length;
48+
private final StringBuilder builder = new StringBuilder();
5049

51-
private boolean lastHadEOL;
50+
private int length;
5251

5352
private List<String> lines;
5453

@@ -61,8 +60,8 @@ import java.util.List;
6160
* @param lines a required instance
6261
*/
6362
public void setTarget(List<String> lines) {
63+
this.builder.setLength(0);
6464
this.length = 0;
65-
this.lastHadEOL = false;
6665
this.lines = lines;
6766
}
6867

@@ -82,12 +81,16 @@ import java.util.List;
8281
%include Common.lexh
8382
%%
8483

85-
[^\n\r]* {EOL} {
86-
lines.add(yytext());
87-
lastHadEOL = true;
84+
{EOL} {
85+
for (int i = 0; i < yylength(); ++i) {
86+
builder.append(yycharat(i)); // faster than yytext()
87+
}
88+
lines.add(builder.toString());
89+
builder.setLength(0);
8890
}
8991

90-
[^\n\r]+ {
91-
lines.add(yytext());
92-
lastHadEOL = false;
92+
[^\n\r] {
93+
for (int i = 0; i < yylength(); ++i) {
94+
builder.append(yycharat(i)); // faster than yytext()
95+
}
9396
}

0 commit comments

Comments
 (0)