Skip to content

Commit 27f5364

Browse files
committed
added int8[] and int8[]s into BINFILE function
1 parent e7a4486 commit 27f5364

File tree

5 files changed

+78
-11
lines changed

5 files changed

+78
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
- MAVEN: added `ignoreMissingSources` boolean parameter, allows to skip preprocessing if source folders not found or not provided [#12](https://github.com/raydac/java-comment-preprocessor/issues/12)
1515
- MAVEN: added `skip` boolean parameter, it allows to skip execution, also it is possible to use `-Djcp.preprocess.skip=true` [#13](https://github.com/raydac/java-comment-preprocessor/issues/13)
1616
- CORE: added function `BOOL is(STR,ANY)` to check existence of variable for its name and compare its value with etalon (through string conversion, it will ensure true for `true` and `"true"` case) [#10](https://github.com/raydac/java-comment-preprocessor/issues/10)
17-
- CORE: improved the BINFILE function to support byte arrays in Golang, added types `uint8[]` and `uint8[]s`
17+
- CORE: improved the BINFILE function to support byte arrays in Golang, added types `uint8[]`, `uint8[]s`, `int8[]` and `int8[]s` (`s` means splitting to lines to avoid too long text line)
1818

1919
- **6.1.0**
2020
- added `--es` option to enable spaces between comment chars and directives [#9](https://github.com/raydac/java-comment-preprocessor/issues/9), in ANT and MAVEN plugins it is boolean parameter named `allowWhitespace`, __NB! by default it is turned off for back compatibility!__

changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- MAVEN: added 'ignoreMissingSources' boolean parameter, allows to skip preprocessing if source folders not found or not provided #12
55
- MAVEN: added 'skip'boolean parameter, it allows to skip execution, also it is possible to use `-Djcp.preprocess.skip=true` #13
66
- CORE: added function `BOOL is(STR,ANY)` to check existence of variable for its name and compare its value with etalon (through string conversion, it will ensure true for `true` and `"true"` case), #10
7-
- CORE: improved the BINFILE function to support byte arrays in Golang, added types "uint8[]" and "uint8[]s"
7+
- CORE: improved the BINFILE function to support byte arrays in Golang, added types `uint8[]`, `uint8[]s`, `int8[]` and `int8[]s` (`s` means splitting to lines to avoid too long text line)
88

99
6.1.0 (03 jul 2016)
1010
- implemented request #9, added support of whitespace between directive and comment, in command line it is `--es` option and in MAVEN and ANT it is boolean parameter `allowWhitespace`, by default it is turned off

src/main/java/com/igormaznitsa/jcp/expression/functions/FunctionBINFILE.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ private enum Type {
4646
BYTEARRAY("byte[]"),
4747
BYTEARRAY_SPLITTED("byte[]s"),
4848
UINT8("uint8[]"),
49-
UINT8_SPLITTED("uint8[]s");
49+
UINT8_SPLITTED("uint8[]s"),
50+
INT8("int8[]"),
51+
INT8_SPLITTED("int8[]s");
5052

5153
private final String name;
5254

@@ -159,6 +161,14 @@ public Value executeStrStr(@Nonnull final PreprocessorContext context, @Nonnull
159161
result = convertToUINT8(theFile, 80, endOfLine);
160162
}
161163
break;
164+
case INT8: {
165+
result = convertToINT8(theFile, -1, endOfLine);
166+
}
167+
break;
168+
case INT8_SPLITTED: {
169+
result = convertToINT8(theFile, 80, endOfLine);
170+
}
171+
break;
162172
default:
163173
throw new Error("Unexpected type : " + type);
164174
}
@@ -195,6 +205,33 @@ private String convertToUINT8(@Nonnull final File file, final int lineLength, @N
195205
return result.toString();
196206
}
197207

208+
@Nonnull
209+
private String convertToINT8(@Nonnull final File file, final int lineLength, @Nonnull final String endOfLine) throws IOException {
210+
final StringBuilder result = new StringBuilder(512);
211+
final byte[] array = FileUtils.readFileToByteArray(file);
212+
213+
int endLinePos = lineLength;
214+
215+
boolean addNextLine = false;
216+
217+
for (final byte b : array) {
218+
if (addNextLine) {
219+
addNextLine = false;
220+
result.append(endOfLine);
221+
}
222+
if (result.length() > 0) {
223+
result.append(',');
224+
}
225+
result.append(Integer.toString(b));
226+
if (lineLength > 0 && result.length() >= endLinePos) {
227+
addNextLine = true;
228+
endLinePos = result.length() + lineLength;
229+
}
230+
}
231+
232+
return result.toString();
233+
}
234+
198235
@Nonnull
199236
private String convertToJBytes(@Nonnull final File file, final int lineLength, @Nonnull final String endOfLine) throws IOException {
200237
final StringBuilder result = new StringBuilder(512);

src/test/java/com/igormaznitsa/jcp/expression/functions/FunctionBINFILETest.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,16 @@ public void testExecution_UINT8ArrayEncoding() throws Exception {
6666
final PreprocessorContext context = preparePreprocessorContext(getCurrentTestFolder());
6767
context.setLocalVariable("hello_world", Value.valueOf("Hello World!"));
6868
final Value result = Expression.evalExpression("binfile(\"./eval/TestBinLong.txt\",\"uint8[]\")", context);
69-
assertEquals("72,101,108,108,111,32,80,114,101,112,114,111,99,101,115,115,111,114,33,32,105,116,32,105,115,32,118,101,114,121,32,118,101,114,121,32,118,101,114,121,32,108,111,110,103,32,108,105,110,101,32,116,111,32,102,105,108,108,32,116,104,101,32,102,105,108,101,33,32,115,111,109,101,116,105,109,101,32,105,116,32,105,115,32,117,115,101,102,117,108,32,102,111,114,32,116,101,115,116,115,33,10,97,108,115,32,73,32,100,101,99,105,100,101,100,32,116,111,32,97,100,100,32,111,110,101,32,109,111,114,101,32,108,105,110,101,46", result.asString().trim());
69+
assertEquals("208,186,208,176,208,186,208,184,208,181,32,209,130,208,190,32,209,128,209,131,209,129,209,129,208,186,208,184,208,181,32,208,177,209,131,208,186,208,178,209,139,10,72,101,108,108,111,32,80,114,101,112,114,111,99,101,115,115,111,114,33,32,105,116,32,105,115,32,118,101,114,121,32,118,101,114,121,32,118,101,114,121,32,108,111,110,103,32,108,105,110,101,32,116,111,32,102,105,108,108,32,116,104,101,32,102,105,108,101,33,32,115,111,109,101,116,105,109,101,32,105,116,32,105,115,32,117,115,101,102,117,108,32,102,111,114,32,116,101,115,116,115,33,10,97,108,115,32,73,32,100,101,99,105,100,101,100,32,116,111,32,97,100,100,32,111,110,101,32,109,111,114,101,32,108,105,110,101,46", result.asString().trim());
70+
assertDestinationFolderEmpty();
71+
}
72+
73+
@Test
74+
public void testExecution_INT8ArrayEncoding() throws Exception {
75+
final PreprocessorContext context = preparePreprocessorContext(getCurrentTestFolder());
76+
context.setLocalVariable("hello_world", Value.valueOf("Hello World!"));
77+
final Value result = Expression.evalExpression("binfile(\"./eval/TestBinLong.txt\",\"int8[]\")", context);
78+
assertEquals("-48,-70,-48,-80,-48,-70,-48,-72,-48,-75,32,-47,-126,-48,-66,32,-47,-128,-47,-125,-47,-127,-47,-127,-48,-70,-48,-72,-48,-75,32,-48,-79,-47,-125,-48,-70,-48,-78,-47,-117,10,72,101,108,108,111,32,80,114,101,112,114,111,99,101,115,115,111,114,33,32,105,116,32,105,115,32,118,101,114,121,32,118,101,114,121,32,118,101,114,121,32,108,111,110,103,32,108,105,110,101,32,116,111,32,102,105,108,108,32,116,104,101,32,102,105,108,101,33,32,115,111,109,101,116,105,109,101,32,105,116,32,105,115,32,117,115,101,102,117,108,32,102,111,114,32,116,101,115,116,115,33,10,97,108,115,32,73,32,100,101,99,105,100,101,100,32,116,111,32,97,100,100,32,111,110,101,32,109,111,114,101,32,108,105,110,101,46", result.asString().trim());
7079
assertDestinationFolderEmpty();
7180
}
7281

@@ -76,13 +85,33 @@ public void testExecution_UINT8ArrayEncodingSplitted() throws Exception {
7685
context.setLocalVariable("hello_world", Value.valueOf("Hello World!"));
7786
final Value result = Expression.evalExpression("binfile(\"./eval/TestBinLong.txt\",\"uint8[]s\")", context);
7887
final String eof = System.getProperty("line.separator");
79-
assertEquals("72,101,108,108,111,32,80,114,101,112,114,111,99,101,115,115,111,114,33,32,105,116" + eof
80-
+ ",32,105,115,32,118,101,114,121,32,118,101,114,121,32,118,101,114,121,32,108,111" + eof
81-
+ ",110,103,32,108,105,110,101,32,116,111,32,102,105,108,108,32,116,104,101,32,102" + eof
82-
+ ",105,108,101,33,32,115,111,109,101,116,105,109,101,32,105,116,32,105,115,32,117" + eof
83-
+ ",115,101,102,117,108,32,102,111,114,32,116,101,115,116,115,33,10,97,108,115,32,73" + eof
84-
+ ",32,100,101,99,105,100,101,100,32,116,111,32,97,100,100,32,111,110,101,32,109,111" + eof
85-
+ ",114,101,32,108,105,110,101,46", result.asString().trim());
88+
assertEquals("208,186,208,176,208,186,208,184,208,181,32,209,130,208,190,32,209,128,209,131,209" + eof
89+
+ ",129,209,129,208,186,208,184,208,181,32,208,177,209,131,208,186,208,178,209,139" + eof
90+
+ ",10,72,101,108,108,111,32,80,114,101,112,114,111,99,101,115,115,111,114,33,32,105" + eof
91+
+ ",116,32,105,115,32,118,101,114,121,32,118,101,114,121,32,118,101,114,121,32,108" + eof
92+
+ ",111,110,103,32,108,105,110,101,32,116,111,32,102,105,108,108,32,116,104,101,32" + eof
93+
+ ",102,105,108,101,33,32,115,111,109,101,116,105,109,101,32,105,116,32,105,115,32" + eof
94+
+ ",117,115,101,102,117,108,32,102,111,114,32,116,101,115,116,115,33,10,97,108,115" + eof
95+
+ ",32,73,32,100,101,99,105,100,101,100,32,116,111,32,97,100,100,32,111,110,101,32" + eof
96+
+ ",109,111,114,101,32,108,105,110,101,46", result.asString().trim());
97+
assertDestinationFolderEmpty();
98+
}
99+
100+
@Test
101+
public void testExecution_INT8ArrayEncodingSplitted() throws Exception {
102+
final PreprocessorContext context = preparePreprocessorContext(getCurrentTestFolder());
103+
context.setLocalVariable("hello_world", Value.valueOf("Hello World!"));
104+
final Value result = Expression.evalExpression("binfile(\"./eval/TestBinLong.txt\",\"int8[]s\")", context);
105+
final String eof = System.getProperty("line.separator");
106+
assertEquals("-48,-70,-48,-80,-48,-70,-48,-72,-48,-75,32,-47,-126,-48,-66,32,-47,-128,-47,-125" + eof
107+
+ ",-47,-127,-47,-127,-48,-70,-48,-72,-48,-75,32,-48,-79,-47,-125,-48,-70,-48,-78,-47" + eof
108+
+ ",-117,10,72,101,108,108,111,32,80,114,101,112,114,111,99,101,115,115,111,114,33" + eof
109+
+ ",32,105,116,32,105,115,32,118,101,114,121,32,118,101,114,121,32,118,101,114,121" + eof
110+
+ ",32,108,111,110,103,32,108,105,110,101,32,116,111,32,102,105,108,108,32,116,104" + eof
111+
+ ",101,32,102,105,108,101,33,32,115,111,109,101,116,105,109,101,32,105,116,32,105" + eof
112+
+ ",115,32,117,115,101,102,117,108,32,102,111,114,32,116,101,115,116,115,33,10,97,108" + eof
113+
+ ",115,32,73,32,100,101,99,105,100,101,100,32,116,111,32,97,100,100,32,111,110,101" + eof
114+
+ ",32,109,111,114,101,32,108,105,110,101,46", result.asString().trim());
86115
assertDestinationFolderEmpty();
87116
}
88117

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
какие то русские буквы
12
Hello Preprocessor! it is very very very long line to fill the file! sometime it is useful for tests!
23
als I decided to add one more line.

0 commit comments

Comments
 (0)