Skip to content

Commit

Permalink
minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
esaulpaugh committed Aug 19, 2024
1 parent c107e09 commit 9604408
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
24 changes: 10 additions & 14 deletions src/main/java/com/esaulpaugh/headlong/rlp/Notation.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,38 +207,34 @@ public String toString() {
*/
public static List<Object> parse(String notation) {
List<Object> topLevelObjects = new ArrayList<>(); // a sequence (as in RLPEncoder.sequence)
parse(notation, 0, notation.length(), topLevelObjects, 0);
parse(notation, 0, topLevelObjects, 0);
return topLevelObjects;
}

private static final int MAX_DEPTH = 768;

private static int parse(final String notation, int i, final int end, final List<Object> parent, int depth) {
while (i < end) {
switch (notation.charAt(i)) {
private static int parse(final String notation, int i, final List<Object> parent, final int depth) {
do {
switch (notation.charAt(i++)) {
case BEGIN_STRING:
final int datumStart = i + 1;
final int datumEnd = notation.indexOf(END_STRING, datumStart);
final int datumEnd = notation.indexOf(END_STRING, i);
if (datumEnd < 0) {
throw new IllegalArgumentException("unterminated string @ " + datumStart);
throw new IllegalArgumentException("unterminated string @ " + i);
}
parent.add(FastHex.decode(notation, datumStart, datumEnd - datumStart));
parent.add(FastHex.decode(notation, i, datumEnd - i));
i = datumEnd + 1;
continue;
case BEGIN_LIST:
if (depth >= MAX_DEPTH) {
throw new IllegalArgumentException("exceeds max depth of " + MAX_DEPTH);
}
List<Object> childList = new ArrayList<>();
i = parse(notation, i + 1, end, childList, depth + 1);
i = parse(notation, i, childList, depth + 1);
parent.add(childList);
continue;
case END_LIST:
return i + 1;
default:
i++;
case END_LIST: return i;
}
}
} while (i < notation.length());
return Integer.MAX_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ boolean test(boolean function) {
System.out.println(r + " " + e + (r.equals(e) ? "" : " ****"));
}
for (; i < encodingTokens.length; i++) {
System.out.println("----------------------------------------------------------------" + " " + encodingTokens[i]);
System.out.println("---------------------------------------------------------------- " + encodingTokens[i]);
}
}
// return false;
Expand Down

0 comments on commit 9604408

Please sign in to comment.