Skip to content

Commit 79c8e28

Browse files
committed
reordered flag constants in compiler to prepare future changes
1 parent 1679e9c commit 79c8e28

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/main/java/com/igormaznitsa/jbbp/compiler/JBBPCompiler.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,30 +142,34 @@ private StructStackItem(final int namedFieldCounter, final int startStructureOff
142142
public static final int CODE_RESET_COUNTER = 0x0E;
143143

144144
/**
145-
* The Byte code shows that field should be processed by custom field type processor.
145+
* The Byte code shows that field should be processed by custom field type
146+
* processor.
146147
*/
147148
public static final int CODE_CUSTOMTYPE = 0x0F;
148149

149150
/**
150151
* The Byte-Code Flag shows that the field is a named one.
151152
*/
152153
public static final int FLAG_NAMED = 0x10;
153-
/**
154-
* The Byte-Code Flag shows that the field is an array which size is defined
155-
* by an expression or the array is unsized and must be read till the end of a
156-
* stream.
157-
*/
158-
public static final int FLAG_EXPRESSION_OR_WHOLESTREAM = 0x20;
154+
159155
/**
160156
* The Byte-Code Flag shows that the field is an array but it must be omitted
161157
* for unlimited field arrays.
162158
*/
163-
public static final int FLAG_ARRAY = 0x40;
159+
public static final int FLAG_ARRAY = 0x20;
160+
164161
/**
165162
* The Byte-Code Flag shows that a multi-byte field must be decoded as
166163
* Little-endian one.
167164
*/
168-
public static final int FLAG_LITTLE_ENDIAN = 0x80;
165+
public static final int FLAG_LITTLE_ENDIAN = 0x40;
166+
167+
/**
168+
* The Byte-Code Flag shows that the field is an array which size is defined
169+
* by an expression or the array is unsized and must be read till the end of a
170+
* stream.
171+
*/
172+
public static final int FLAG_EXPRESSION_OR_WHOLESTREAM = 0x80;
169173

170174
public static JBBPCompiledBlock compile(final String script) throws IOException {
171175
return compile(script, null);
@@ -175,7 +179,8 @@ public static JBBPCompiledBlock compile(final String script) throws IOException
175179
* Compile a text script into its byte code representation/
176180
*
177181
* @param script a text script to be compiled, must not be null.
178-
* @param customTypeFieldProcessor processor to process custom type fields, can be null
182+
* @param customTypeFieldProcessor processor to process custom type fields,
183+
* can be null
179184
* @return a compiled block for the script.
180185
* @throws IOException it will be thrown for an inside IO error.
181186
* @throws JBBPException it will be thrown for any logical or work exception
@@ -215,7 +220,7 @@ public static JBBPCompiledBlock compile(final String script, final JBBPCustomFie
215220
boolean extraFieldPresented = false;
216221
int extraField = -1;
217222
int customTypeFieldIndex = -1;
218-
223+
219224
// check that the field is not in the current structure which is a whole stream one
220225
if ((code & 0xF) != CODE_STRUCT_END && fieldUnrestrictedArrayOffset >= 0 && (structureStack.isEmpty() || structureStack.get(structureStack.size() - 1).startStructureOffset != fieldUnrestrictedArrayOffset)) {
221226
throw new JBBPCompilationException("Attempt to read after a 'till-the-end' field", token);
@@ -397,8 +402,8 @@ public static JBBPCompiledBlock compile(final String script, final JBBPCustomFie
397402
if (extraFieldPresented) {
398403
offset += writePackedInt(out, extraField);
399404
}
400-
401-
if (customTypeFieldIndex>=0){
405+
406+
if (customTypeFieldIndex >= 0) {
402407
offset += writePackedInt(out, customTypeFieldIndex);
403408
}
404409

@@ -506,7 +511,8 @@ private static int writePackedInt(final OutputStream out, final int value) throw
506511
* The Method prepares a byte-code for a token field type and modifiers.
507512
*
508513
* @param token a token to be processed, must not be null
509-
* @param customTypeFieldProcessor custom type field processor for the parser, it can be null
514+
* @param customTypeFieldProcessor custom type field processor for the parser,
515+
* it can be null
510516
* @return the prepared byte code for the token
511517
*/
512518
private static int prepareCodeForToken(final JBBPToken token, final JBBPCustomFieldTypeProcessor customTypeFieldProcessor) {

src/test/java/com/igormaznitsa/jbbp/compiler/JBBPCompilerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void testCompile_StructForWholeStreamAsSecondField() throws Exception {
4242
final JBBPCompiledBlock block = JBBPCompiler.compile("byte;test [_] {byte;}");
4343
assertEquals(5, block.getCompiledData().length);
4444
assertEquals(JBBPCompiler.CODE_BYTE, block.getCompiledData()[0]);
45-
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_EXPRESSION_OR_WHOLESTREAM | JBBPCompiler.FLAG_NAMED, block.getCompiledData()[1]);
45+
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_EXPRESSION_OR_WHOLESTREAM | JBBPCompiler.FLAG_NAMED, block.getCompiledData()[1] & 0xFF);
4646
assertEquals(JBBPCompiler.CODE_BYTE, block.getCompiledData()[2]);
4747
assertEquals(JBBPCompiler.CODE_STRUCT_END, block.getCompiledData()[3]);
4848
assertEquals(1, block.getCompiledData()[4]);
@@ -64,7 +64,7 @@ public void testCompile_WholeStreamArrayInsideStructure() throws Exception {
6464
final JBBPCompiledBlock block = JBBPCompiler.compile("test {byte [_];}");
6565
assertEquals(4, block.getCompiledData().length);
6666
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_NAMED, block.getCompiledData()[0]);
67-
assertEquals(JBBPCompiler.CODE_BYTE | JBBPCompiler.FLAG_EXPRESSION_OR_WHOLESTREAM, block.getCompiledData()[1]);
67+
assertEquals(JBBPCompiler.CODE_BYTE | JBBPCompiler.FLAG_EXPRESSION_OR_WHOLESTREAM, block.getCompiledData()[1] & 0xFF);
6868
assertEquals(JBBPCompiler.CODE_STRUCT_END, block.getCompiledData()[2]);
6969
assertEquals(0, block.getCompiledData()[3]);
7070
}
@@ -74,7 +74,7 @@ public void testCompile_WholeStreamStructureArrayInsideStructure() throws Except
7474
final JBBPCompiledBlock block = JBBPCompiler.compile("test { whole[_]{ byte;}}");
7575
assertEquals(7, block.getCompiledData().length);
7676
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_NAMED, block.getCompiledData()[0]);
77-
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_NAMED | JBBPCompiler.FLAG_EXPRESSION_OR_WHOLESTREAM, block.getCompiledData()[1]);
77+
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_NAMED | JBBPCompiler.FLAG_EXPRESSION_OR_WHOLESTREAM, block.getCompiledData()[1] & 0xFF);
7878
assertEquals(JBBPCompiler.CODE_BYTE, block.getCompiledData()[2]);
7979
assertEquals(JBBPCompiler.CODE_STRUCT_END, block.getCompiledData()[3]);
8080
assertEquals(1, block.getCompiledData()[4]);
@@ -502,7 +502,7 @@ public void testCompile_ArrayWithUndefinedLength() throws Exception {
502502
assertEquals(1, compiled.getCompiledData().length);
503503
assertNotNull(field);
504504
assertEquals(0, field.getFieldOffsetInCompiledBlock());
505-
assertEquals(JBBPCompiler.CODE_BYTE | JBBPCompiler.FLAG_NAMED | JBBPCompiler.FLAG_EXPRESSION_OR_WHOLESTREAM, compiled.getCompiledData()[0]);
505+
assertEquals(JBBPCompiler.CODE_BYTE | JBBPCompiler.FLAG_NAMED | JBBPCompiler.FLAG_EXPRESSION_OR_WHOLESTREAM, compiled.getCompiledData()[0] & 0xFF);
506506
}
507507

508508
@Test

0 commit comments

Comments
 (0)