Skip to content

Commit 7aa7a85

Browse files
authored
refactored to use Bytes instead of String for file contents, see #98 (#402)
* refactored to use Bytes instead of String for file contents, see #98
1 parent d5d5fc4 commit 7aa7a85

17 files changed

+59
-43
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Fixed BkOpen childs in token tree parser [#398](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/398)
88
- Fixed bad offset crash with C++ build on7 Windows 10 [#398](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/398)
99
- Fixed object declaration handling [#399](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/399)
10+
- Refactored content handling to use Bytes instead of String (should fix [#98](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/98)) [#402](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/402)
1011
- Added unittests for ParserQueue and CheckerPool [#393](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/393)
1112
- Added unittests for TokenTree structure verification [#400](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/400)
1213
- Removed `.` from default settings in SeparatorWrapCheck [#400](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/400)

checkstyle.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
},
172172
{
173173
"props": {
174-
"ignoreNumbers": [-1, 0, 1, 2, 3, 4.0, 5, 8, 13, 21, 34, 100]
174+
"ignoreNumbers": [-1, 0, 1, 2, 3, 4.0, 5, 8, 10, 13, 21, 34, 100]
175175
},
176176
"type": "MagicNumber"
177177
},

src/checkstyle/CheckFile.hx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package checkstyle;
22

3+
import byte.ByteData;
4+
35
typedef CheckFile = {
46
var name:String;
5-
var content:String;
7+
var content:ByteData;
68
var index:Int;
79
}

src/checkstyle/Checker.hx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package checkstyle;
22

3-
import byte.ByteData;
43
import haxe.CallStack;
54
import haxeparser.HaxeParser;
65
import haxeparser.HaxeLexer;
@@ -15,7 +14,6 @@ import checkstyle.token.TokenTreeBuilder;
1514
class Checker {
1615

1716
public var file:CheckFile;
18-
public var bytes:ByteData;
1917
public var lines:Array<String>;
2018
public var tokens:Array<Token>;
2119
public var ast:Ast;
@@ -41,19 +39,19 @@ class Checker {
4139

4240
public function getTokenTree():TokenTree {
4341
if (tokens == null) return null;
44-
if (tokenTree == null) tokenTree = TokenTreeBuilder.buildTokenTree(tokens, bytes);
42+
if (tokenTree == null) tokenTree = TokenTreeBuilder.buildTokenTree(tokens, file.content);
4543
return tokenTree;
4644
}
4745

4846
function makePosIndices() {
49-
var code = file.content;
47+
var code:Bytes = cast file.content;
5048
linesIdx = [];
5149

5250
var last = 0;
5351
var left = false;
5452

5553
for (i in 0...code.length) {
56-
if (code.charAt(i) == "\n") {
54+
if (code.get(i) == 0x0A) {
5755
linesIdx.push({l:last, r:i});
5856
last = i + 1;
5957
left = false;
@@ -71,11 +69,16 @@ class Checker {
7169
}
7270

7371
public function getString(off:Int, off2:Int):String {
74-
return file.content.substr(off, off2 - off);
72+
var code:Bytes = cast file.content;
73+
var len:Int = off2 - off;
74+
if ((off >= code.length) || (off + len > code.length)) return "";
75+
return code.sub(off, off2 - off).toString();
7576
}
7677

7778
function findLineSeparator() {
78-
var code = file.content;
79+
var codeBytes:Bytes = cast file.content;
80+
var code:String = codeBytes.toString();
81+
7982
for (i in 0...code.length) {
8083
var char = code.charAt(i);
8184
if (char == "\r" || char == "\n") {
@@ -92,15 +95,16 @@ class Checker {
9295
}
9396

9497
function makeLines() {
95-
var code = file.content;
96-
lines = code.split(lineSeparator);
98+
var code:Bytes = cast file.content;
99+
var textCode:String = code.toString();
100+
lines = textCode.split(lineSeparator);
97101
}
98102

99103
function makeTokens() {
100104
try {
101105
tokens = [];
102106
tokenTree = null;
103-
var lexer = new HaxeLexer(bytes, file.name);
107+
var lexer = new HaxeLexer(file.content, file.name);
104108
var t:Token = lexer.token(HaxeLexer.tok);
105109

106110
while (t.tok != Eof) {
@@ -128,8 +132,7 @@ class Checker {
128132
}
129133

130134
function makeAST(defines:Array<String>):Ast {
131-
var code = file.content;
132-
var parser = new HaxeParser(byte.ByteData.ofString(code), file.name);
135+
var parser = new HaxeParser(file.content, file.name);
133136
parser.define("cross");
134137
parser.define("scriptable");
135138
parser.define("unsafe");
@@ -176,7 +179,7 @@ class Checker {
176179
public function loadFileContent(checkFile:CheckFile) {
177180
// unittests set content before running Checker
178181
// real checks load content here
179-
if (checkFile.content == null) checkFile.content = File.getContent(checkFile.name);
182+
if (checkFile.content == null) checkFile.content = cast File.getBytes(checkFile.name);
180183
}
181184

182185
public function unloadFileContent(checkFile:CheckFile) {
@@ -185,7 +188,6 @@ class Checker {
185188

186189
public function createContext(checkFile:CheckFile):Bool {
187190
file = checkFile;
188-
bytes = byte.ByteData.ofString(file.content);
189191
ReporterManager.INSTANCE.fileStart(file);
190192
try {
191193
findLineSeparator();

src/checkstyle/checks/block/ConditionalCompilationCheck.hx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class ConditionalCompilationCheck extends Check {
5151
}
5252

5353
function checkMultiLine(tok:TokenTree, linePos:LinePos) {
54-
var line:String = checker.lines[linePos.line];
55-
var prefix:String = line.substr(0, linePos.ofs);
54+
var line:Bytes = Bytes.ofString(checker.lines[linePos.line]);
55+
var prefix:String = line.sub(0, linePos.ofs).toString();
5656
if (checkLine(tok, linePos, line)) return;
5757

5858
switch (policy) {
@@ -68,8 +68,8 @@ class ConditionalCompilationCheck extends Check {
6868
switch (childTok.tok) {
6969
case Sharp("else"), Sharp("elseif"), Sharp("end"):
7070
var childLinePos:LinePos = checker.getLinePos(childTok.pos.min);
71-
var childLine:String = checker.lines[childLinePos.line];
72-
var childPrefix:String = childLine.substr(0, childLinePos.ofs);
71+
var childLine:Bytes = Bytes.ofString(checker.lines[childLinePos.line]);
72+
var childPrefix:String = childLine.sub(0, childLinePos.ofs).toString();
7373
if (checkLine(childTok, childLinePos, childLine)) continue;
7474
if (childPrefix == prefix) continue;
7575
logPos('Indentation of $childTok must match corresponding #if', childTok.pos);
@@ -78,9 +78,9 @@ class ConditionalCompilationCheck extends Check {
7878
}
7979
}
8080

81-
function checkLine(tok:TokenTree, linePos:LinePos, line:String):Bool {
81+
function checkLine(tok:TokenTree, linePos:LinePos, line:Bytes):Bool {
8282
var r:EReg = ~/^[ \t]*$/;
83-
var prefix:String = line.substr(0, linePos.ofs);
83+
var prefix:String = line.sub(0, linePos.ofs).toString();
8484
if (!r.match(prefix)) {
8585
logPos('only whitespace allowed before $tok', tok.pos);
8686
return true;
@@ -89,7 +89,7 @@ class ConditionalCompilationCheck extends Check {
8989
if (expr == null) return false;
9090
var linePosAfter:LinePos = checker.getLinePos(expr.getPos().max);
9191
if (linePosAfter.line == linePos.line) {
92-
var postfix:String = line.substr(linePosAfter.ofs);
92+
var postfix:String = line.sub(linePosAfter.ofs, line.length - linePosAfter.ofs).toString();
9393
if (!r.match(postfix)) {
9494
logPos('only whitespace allowed after $tok', tok.pos);
9595
return true;

src/checkstyle/checks/block/RightCurlyCheck.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ class RightCurlyCheck extends Check {
125125
var linePos:LinePos = checker.getLinePos(curlyPos.max);
126126
var afterCurly:String = "";
127127
if (!eof) {
128-
var afterLine:String = checker.lines[linePos.line];
129-
if (linePos.ofs < afterLine.length) afterCurly = afterLine.substr(linePos.ofs);
128+
var afterLine:Bytes = Bytes.ofString(checker.lines[linePos.line]);
129+
if (linePos.ofs < afterLine.length) afterCurly = afterLine.sub(linePos.ofs, afterLine.length - linePos.ofs).toString();
130130
}
131131
// only else and catch allowed on same line after a right curly
132132
var sameRegex = ~/^\s*(else|catch)/;

src/checkstyle/checks/imports/UnusedImportCheck.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class UnusedImportCheck extends Check {
3636
var stringLiterals:Array<TokenTree> = root.filterCallback(function(token:TokenTree, depth:Int):FilterResult {
3737
switch (token.tok) {
3838
case Const(CString(text)):
39-
if (checker.file.content.substr(token.pos.min, 1) != "'") return GO_DEEPER;
39+
if (checker.getString(token.pos.min, token.pos.min + 1) != "'") return GO_DEEPER;
4040
if (~/\$\{[^\}]+\.[^\}]+\}/.match (text)) return FOUND_GO_DEEPER;
4141
default:
4242
}

src/checkstyle/checks/literal/StringLiteralCheck.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class StringLiteralCheck extends Check {
3838
}
3939

4040
function checkLiteral(s:String, pos:Position) {
41-
var quote:String = checker.file.content.substr(pos.min, 1);
41+
var quote:String = checker.getString(pos.min, pos.min + 1);
4242
var singleQuote:Bool = quote == "'";
4343
switch (policy) {
4444
case ONLY_DOUBLE:

src/checkstyle/checks/whitespace/ArrayAccessCheck.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class ArrayAccessCheck extends Check {
3232
}
3333

3434
if (!spaceInside) {
35-
if (checker.file.content.substr(e2.pos.min - 1, 1) == " ") logPos("Space between [ and index", e.pos);
36-
if (checker.file.content.substr(e2.pos.max, 1) == " ") logPos("Space between index and ]", e.pos);
35+
if (checker.getString(e2.pos.min - 1, e2.pos.min) == " ") logPos("Space between [ and index", e.pos);
36+
if (checker.getString(e2.pos.max, e2.pos.max + 1) == " ") logPos("Space between index and ]", e.pos);
3737
}
3838
default:
3939
}

src/checkstyle/checks/whitespace/WhitespaceAfterCheck.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class WhitespaceAfterCheck extends Check {
8787
if (isPosSuppressed(tok.pos)) continue;
8888
if (TokenTreeCheckUtils.filterOpSub(tok)) continue;
8989

90-
var contentAfter:String = checker.file.content.substr(tok.pos.max, 1);
90+
var contentAfter:String = checker.getString(tok.pos.max, tok.pos.max + 1);
9191
if (~/^(\s|)$/.match(contentAfter)) continue;
9292

9393
logPos('No whitespace after "$tok"', tok.pos);

0 commit comments

Comments
 (0)