formatValue static method

String formatValue(
  1. String value,
  2. SignalValueFormat format,
  3. int width
)

Formats a ROHD radix literal according to format.

Implementation

static String formatValue(String value, SignalValueFormat format, int width) {
  final waveformValue = _waveformValue(value, width);
  if (waveformValue == null) {
    return _canonicalWaveformValue(value, width);
  }
  final (logicValue, canonical) = waveformValue;
  if (format == SignalValueFormat.waveform ||
      _containsUnknownDigits(canonical)) {
    return canonical;
  }
  return switch (format) {
    SignalValueFormat.binary => logicValue.toRadixString(
        leadingZeros: true,
        includeWidth: false,
        sepChar: '',
      ),
    SignalValueFormat.hexadecimal => logicValue.toRadixString(
        radix: 16,
        sepChar: '',
      ),
    SignalValueFormat.unsignedDecimal => logicValue.toRadixString(
        radix: 10,
        includeWidth: false,
        sepChar: '',
      ),
    SignalValueFormat.signedDecimal =>
      logicValue.toBigInt().toSigned(logicValue.width).toString(),
    SignalValueFormat.octal => _formatOctal(logicValue),
    SignalValueFormat.ascii => () {
        final byteCount = (logicValue.width + 7) ~/ 8;
        return String.fromCharCodes(
          List<int>.generate(byteCount, (index) {
            final shift = (byteCount - index - 1) * 8;
            final code =
                ((logicValue.toBigInt() >> shift) & BigInt.from(0xff))
                    .toInt();
            return code >= 0x20 && code <= 0x7e ? code : 0x2e;
          }),
        );
      }(),
    SignalValueFormat.waveform => canonical,
  };
}