decodeNumeric function

_ChunkContent decodeNumeric(
  1. _BitStream stream,
  2. int size
)

Implementation

_ChunkContent decodeNumeric(_BitStream stream, int size) {
  List<int> bytes = [];
  String text = "";

  final characterCountSize = [10, 12, 14][size];
  int length = stream.readBits(characterCountSize);
  // Read digits in groups of 3
  while (length >= 3) {
    final num = stream.readBits(10);
    if (num >= 1000) {
      throw Exception("Invalid numeric value above 999");
    }

    final a = (num / 100).floor();
    final b = (num / 10).floor() % 10;
    final c = num % 10;

    bytes.addAll([48 + a, 48 + b, 48 + c]);
    text += a.toString() + b.toString() + c.toString();
    length -= 3;
  }

  // If the number of digits aren't a multiple of 3, the remaining digits are special cased.
  if (length == 2) {
    final num = stream.readBits(7);
    if (num >= 100) {
      throw Exception("Invalid numeric value above 99");
    }

    final a = (num / 10).floor();
    final b = num % 10;

    bytes.addAll([48 + a, 48 + b]);
    text += a.toString() + b.toString();
  } else if (length == 1) {
    final num = stream.readBits(4);
    if (num >= 10) {
      throw Exception("Invalid numeric value above 9");
    }

    bytes.add(48 + num);
    text += num.toString();
  }

  return _ChunkContent(text: text, bytes: bytes);
}