Skip to content

Commit

Permalink
Made analyze --parametertab work with long parameters, cf #38.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengtmartensson committed Nov 16, 2018
1 parent 83b3534 commit eb2c160
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/main/java/org/harctoolbox/irp/IrpTransmogrifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -882,14 +882,10 @@ private void analyze(Analyzer analyzer, String[] names) throws NoDecoderMatchExc
Protocol protocol = protocols.get(i);
NameEngine definitions = protocol.getDefinitions();
for (Map.Entry<String, Expression> definition : definitions) {
try {
String name = definition.getKey();
int length = protocol.guessParameterLength(name);
long num = definition.getValue().toLong();
out.print("\t" + IrCoreUtils.formatIntegerWithLeadingZeros(num, commandAnalyze.radix, length));
} catch (NameUnassignedException ex) {
throw new ThisCannotHappenException(ex);
}
String name = definition.getKey();
int length = protocol.guessParameterLength(name);
Number num = definition.getValue().toNumber();
out.print("\t" + num.formatIntegerWithLeadingZeros(commandAnalyze.radix, length));
}
out.println();
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/harctoolbox/irp/Number.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ public static java.lang.Number parse(String str, int radix) {
}
}

private static String pad(String rawString, int length, double noBits) {
StringBuilder str = new StringBuilder(rawString);
int effectiveLength = (int) Math.ceil(length/noBits);
while (str.length() < effectiveLength)
str.insert(0, '0');
return str.toString();
}

private java.lang.Number data;

public Number(java.lang.Number n) {
Expand Down Expand Up @@ -201,4 +209,9 @@ public PrimaryItem leftHandSide() {
public PrimaryItem substituteConstantVariables(Map<String, Long> constantVariables) {
return this;
}

public String formatIntegerWithLeadingZeros(int radix, int length) {
return data instanceof Long ? IrCoreUtils.formatIntegerWithLeadingZeros(data.longValue(), radix, length)
: pad(((BigInteger) data).toString(radix), length, Math.log(radix)/Math.log(2.0));
}
}

0 comments on commit eb2c160

Please sign in to comment.