Edge constructor

Edge(
  1. Input input,
  2. Mode mode,
  3. int fromPosition,
  4. int characterLength,
  5. Edge? previous,
)

Implementation

/* private */ Edge(
  this.input,
  this.mode,
  this.fromPosition,
  this.characterLength,
  this.previous,
) {
  assert(fromPosition + characterLength <= input.length);

  int size = previous?.cachedTotalSize ?? 0;

  final previousMode = this.previousMode;

  /*
  * Switching modes
  * ASCII -> C40: latch 230
  * ASCII -> TEXT: latch 239
  * ASCII -> X12: latch 238
  * ASCII -> EDF: latch 240
  * ASCII -> B256: latch 231
  * C40 -> ASCII: word(c1,c2,c3), 254
  * TEXT -> ASCII: word(c1,c2,c3), 254
  * X12 -> ASCII: word(c1,c2,c3), 254
  * EDIFACT -> ASCII: Unlatch character,0,0,0 or c1,Unlatch character,0,0 or c1,c2,Unlatch character,0 or
  * c1,c2,c3,Unlatch character
  * B256 -> ASCII: without latch after n bytes
  */
  switch (mode) {
    case Mode.ascii:
      size++;
      if (input.isECI(fromPosition) ||
          MinimalEncoder.isExtendedASCII(
            input.charAt(fromPosition),
            input.fnc1Character,
          )) {
        size++;
      }
      if (previousMode == Mode.c40 ||
          previousMode == Mode.text ||
          previousMode == Mode.x12) {
        size++; //unatch 254 to ASCII
      }
      break;
    case Mode.b256:
      size++;
      if (previousMode != Mode.b256) {
        size++; //byte count
      } else if (getB256Size() == 250) {
        size++; //extra byte count
      }
      if (previousMode == Mode.ascii) {
        size++; //latch to B256
      } else if (previousMode == Mode.c40 ||
          previousMode == Mode.text ||
          previousMode == Mode.x12) {
        size += 2; //unlatch to ASCII, latch to B256
      }
      break;
    case Mode.c40:
    case Mode.text:
    case Mode.x12:
      if (mode == Mode.x12) {
        size += 2;
      } else {
        final charLen = [0];
        size += MinimalEncoder.getNumberOfC40Words(
              input,
              fromPosition,
              mode == Mode.c40,
              charLen,
            ) *
            2;
      }

      if (previousMode == Mode.ascii || previousMode == Mode.b256) {
        size++; //additional byte for latch from ASCII to this mode
      } else if (previousMode != mode &&
          (previousMode == Mode.c40 ||
              previousMode == Mode.text ||
              previousMode == Mode.x12)) {
        size += 2; //unlatch 254 to ASCII followed by latch to this mode
      }
      break;
    case Mode.edf:
      size += 3;
      if (previousMode == Mode.ascii || previousMode == Mode.b256) {
        size++; //additional byte for latch from ASCII to this mode
      } else if (previousMode == Mode.c40 ||
          previousMode == Mode.text ||
          previousMode == Mode.x12) {
        size += 2; //unlatch 254 to ASCII followed by latch to this mode
      }
      break;
  }
  cachedTotalSize = size;
}