Edge constructor

Edge(
  1. Mode mode,
  2. int fromPosition,
  3. int charsetEncoderIndex,
  4. int characterLength,
  5. Edge? previous,
  6. Version version,
  7. MinimalEncoder context,
)

Implementation

Edge(
  this.mode,
  this.fromPosition,
  int charsetEncoderIndex,
  this.characterLength,
  this.previous,
  Version version,
  MinimalEncoder context,
)   : charsetEncoderIndex = mode == Mode.byte || previous == null
          ? charsetEncoderIndex
          : previous.charsetEncoderIndex,
      super(context) {
  int size = previous?.cachedTotalSize ?? 0;

  final needECI = mode == Mode.byte &&
          (previous == null &&
              this.charsetEncoderIndex !=
                  0) || // at the beginning and charset is not ISO-8859-1
      (previous != null &&
          this.charsetEncoderIndex != (previous?.charsetEncoderIndex ?? 0));

  if (mode != previous?.mode || needECI) {
    size += 4 + mode.getCharacterCountBits(version);
  }
  switch (mode) {
    case Mode.kanji:
      size += 13;
      break;
    case Mode.alphanumeric:
      size += characterLength == 1 ? 6 : 11;
      break;
    case Mode.numeric:
      size += characterLength == 1
          ? 4
          : characterLength == 2
              ? 7
              : 10;
      break;
    case Mode.byte:
      size += 8 *
          context.encoders
              .encode(
                context.stringToEncode.substring(
                  fromPosition,
                  fromPosition + characterLength,
                ),
                charsetEncoderIndex,
              )
              .length;
      if (needECI) {
        // the ECI assignment numbers for ISO-8859-x, UTF-8 and UTF-16 are all 8 bit long
        size += 4 + 8;
      }
      break;
  }
  cachedTotalSize = size;
}