Skip to content

Commit

Permalink
fix radix10 issue in toRadixString (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
desmonddak authored Jan 14, 2025
1 parent a9d7410 commit 10f213b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
26 changes: 10 additions & 16 deletions lib/src/values/logic_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -665,11 +665,14 @@ abstract class LogicValue implements Comparable<LogicValue> {
final radixString =
toBigInt().toUnsigned(width).toRadixString(radix).toUpperCase();
reversedStr = _reverse(radixString);
} else {
if (radix == 10) {
throw LogicValueConversionException(
'Cannot support decimal strings with invalid bits');
} else if (radix == 10) {
final span = (width * math.log(2) / math.log(radix)).floor();
if (toRadixString().contains(RegExp('[xX]'))) {
reversedStr = 'X' * span;
} else {
reversedStr = 'Z' * span;
}
} else {
final span = (math.log(radix) / math.log(2)).ceil();
final extendedStr =
LogicValue.of(this, width: span * (width / span).ceil());
Expand Down Expand Up @@ -800,23 +803,14 @@ abstract class LogicValue implements Comparable<LogicValue> {
if (RegExp('[xXzZ]').hasMatch(leadChar)) {
shorter = span - 1;
} else {
if (radix == 10) {
shorter = binaryLength -
BigInt.parse(compressedStr, radix: 10)
.toRadixString(2)
.length;
} else {
shorter = span -
BigInt.parse(leadChar, radix: radix)
.toRadixString(2)
.length;
}
shorter = span -
BigInt.parse(leadChar, radix: radix).toRadixString(2).length;
}
} else {
shorter = 0;
}
}
if (binaryLength - shorter > specifiedLength) {
if ((radix != 10) & (binaryLength - shorter > specifiedLength)) {
throw LogicValueConstructionException(
'ofRadixString: cannot represent '
'$compressedStr in $specifiedLength');
Expand Down
19 changes: 19 additions & 0 deletions test/logic_value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2140,6 +2140,25 @@ void main() {
LogicValue.ofRadixString(lv.toRadixString(radix: i)), equals(lv));
}
});
test('radixString decimal case', () {
{
final lv = LogicValue.ofRadixString("12'bzz_zzz1_1011");
final ds = lv.toRadixString(radix: 10);
final dlv = LogicValue.ofRadixString(ds);
final ds2 = dlv.toRadixString(radix: 10);
expect(ds, equals(ds2));
expect(ds, equals("12'dZZZ"));
}
{
final lv = LogicValue.ofRadixString("12'bzz_zzx1_1011");
final ds = lv.toRadixString(radix: 10);
final dlv = LogicValue.ofRadixString(ds);
final ds2 = dlv.toRadixString(radix: 10);
expect(ds, equals(ds2));
expect(ds, equals("12'dXXX"));
}
});

test('radixString small leading radix character', () {
final lv = LogicValue.ofRadixString("10'b10_1010_0111");
expect(lv.toRadixString(radix: 4), equals("10'q2_2213"));
Expand Down

0 comments on commit 10f213b

Please sign in to comment.