Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e4756ef

Browse files
committed
added way for JBBPUtils#findMaxStaticArraySize to define different expected sizes for non-static struct arrays
1 parent f7e63d0 commit e4756ef

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

‎jbbp/src/main/java/com/igormaznitsa/jbbp/utils/JBBPUtils.java‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.List;
4141
import java.util.Locale;
4242
import java.util.concurrent.atomic.AtomicLong;
43+
import java.util.function.BiFunction;
4344

4445
/**
4546
* Misc auxiliary methods to be used in the framework.
@@ -1132,14 +1133,32 @@ public static void traceData(final InputStream inStream,
11321133

11331134
/**
11341135
* Allows to calculate maximum static array size provided by script. It doesn't calculate any expressions, so that <b>byte [1000*1000] a;</b> will not be detected.
1136+
* <b>Default size of non-static struct arrays will be recognized as 1.</b>
11351137
*
11361138
* @param script script to be processed, must not be null
11371139
* @param customFieldTypeProcessor custom field type processor if needed, can be null if no custom types in use
11381140
* @return calculated biggest static array size with embedded structure awareness
11391141
* @since 3.0.0
11401142
*/
11411143
public static long findMaxStaticArraySize(final String script,
1142-
final JBBPCustomFieldTypeProcessor customFieldTypeProcessor) {
1144+
final JBBPCustomFieldTypeProcessor customFieldTypeProcessor) {
1145+
return findMaxStaticArraySize(script, customFieldTypeProcessor, (fieldName, wholeStream) -> 1);
1146+
}
1147+
1148+
/**
1149+
* Allows to calculate maximum static array size provided by script. It doesn't calculate any expressions, so that <b>byte [1000*1000] a;</b> will not be detected.
1150+
*
1151+
* @param script script to be processed, must not be null
1152+
* @param customFieldTypeProcessor custom field type processor if needed, can be null if no custom types in use
1153+
* @param expectedStructArraySizeSupplier supplier of default size for structures which size is noe static but calculable,
1154+
* it is a function which gets name info for named structure fields or null for anonymous fields
1155+
* and the flag that a whole stream should be read, as result it should return integer value of approximate expected size.
1156+
* @return calculated biggest static array size with embedded structure awareness
1157+
* @since 3.0.1
1158+
*/
1159+
public static long findMaxStaticArraySize(final String script,
1160+
final JBBPCustomFieldTypeProcessor customFieldTypeProcessor,
1161+
final BiFunction<JBBPNamedFieldInfo, Boolean, Integer> expectedStructArraySizeSupplier) {
11431162

11441163
final AtomicLong maxFound = new AtomicLong();
11451164
final JBBPCompiledBlock compiledBlock =
@@ -1227,18 +1246,20 @@ public void visitVarField(int offsetInCompiledBlock, JBBPNamedFieldInfo nullable
12271246
}
12281247

12291248
@Override
1230-
public void visitStructureStart(int offsetInCompiledBlock,
1231-
JBBPByteOrder byteOrder,
1232-
boolean readWholeStream,
1233-
JBBPNamedFieldInfo nullableNameFieldInfo,
1234-
JBBPIntegerValueEvaluator nullableArraySize) {
1249+
public void visitStructureStart(finalint offsetInCompiledBlock,
1250+
finalJBBPByteOrder byteOrder,
1251+
finalboolean readWholeStream,
1252+
finalJBBPNamedFieldInfo nullableNameFieldInfo,
1253+
finalJBBPIntegerValueEvaluator nullableArraySize) {
12351254
if (readWholeStream) {
1236-
structSizeStack.add(1);
1255+
structSizeStack.add(
1256+
expectedStructArraySizeSupplier.apply(nullableNameFieldInfo, readWholeStream));
12371257
} else {
12381258
final Integer staticSize =
12391259
extractStaticArraySize(offsetInCompiledBlock, nullableArraySize);
12401260
if (staticSize == null) {
1241-
structSizeStack.add(1);
1261+
structSizeStack.add(
1262+
expectedStructArraySizeSupplier.apply(nullableNameFieldInfo, readWholeStream));
12421263
} else {
12431264
processSize(staticSize);
12441265
structSizeStack.add(staticSize);

‎jbbp/src/test/java/com/igormaznitsa/jbbp/utils/JBBPUtilsTest.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,35 @@
4545

4646
public class JBBPUtilsTest {
4747

48+
@Test
49+
public void testFindMaxStaticArraySize_CustomDefaultSize() {
50+
assertEquals(0L, findMaxStaticArraySize("byte [_] a;", null, (name, whole) -> 1000));
51+
assertEquals(1L, findMaxStaticArraySize("byte [1] a;", null, (name, whole) -> 1000));
52+
assertEquals(0L, findMaxStaticArraySize("byte a; byte [a] b;", null, (name, whole) -> 1000));
53+
assertEquals(112L,
54+
findMaxStaticArraySize("byte a; byte [a] b; int [112];", null, (name, whole) -> 1000));
55+
assertEquals(1120L,
56+
findMaxStaticArraySize("byte a; byte [a] b; some [10] { int [112]; }", null,
57+
(name, whole) -> {
58+
assertNotNull(name);
59+
assertFalse(whole);
60+
return 1000;
61+
}));
62+
assertEquals(112000L,
63+
findMaxStaticArraySize("byte a; byte [a] b; some [_] { int [112]; }", null,
64+
(name, whole) -> {
65+
assertNotNull(name);
66+
assertTrue(whole);
67+
return 1000;
68+
}));
69+
assertEquals(112000L,
70+
findMaxStaticArraySize("byte a; byte [a] b; [_] { int [112]; }", null, (name, whole) -> {
71+
assertNull(name);
72+
assertTrue(whole);
73+
return 1000;
74+
}));
75+
}
76+
4877
@Test
4978
public void testFindMaxStaticArraySize() {
5079
assertEquals(0L, findMaxStaticArraySize("byte [_] a;", null));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /