encode method

  1. @override
void encode(
  1. EncoderContext context
)
override

Implementation

@override
void encode(EncoderContext context) {
  //step B
  final n = HighLevelEncoder.determineConsecutiveDigitCount(
    context.message,
    context.pos,
  );
  if (n >= 2) {
    context.writeCodeword(
      _encodeASCIIDigits(
        context.message.codeUnitAt(context.pos),
        context.message.codeUnitAt(context.pos + 1),
      ),
    );
    context.pos += 2;
  } else {
    final c = context.currentChar;
    final newMode = HighLevelEncoder.lookAheadTest(
      context.message,
      context.pos,
      encodingMode,
    );
    if (newMode != encodingMode) {
      switch (newMode) {
        case HighLevelEncoder.base256Encodation:
          context.writeCodeword(HighLevelEncoder.latchToBase256);
          context.signalEncoderChange(HighLevelEncoder.base256Encodation);
          return;
        case HighLevelEncoder.c40Encodation:
          context.writeCodeword(HighLevelEncoder.latchToC40);
          context.signalEncoderChange(HighLevelEncoder.c40Encodation);
          return;
        case HighLevelEncoder.x12Encodation:
          context.writeCodeword(HighLevelEncoder.latchToAnsix12);
          context.signalEncoderChange(HighLevelEncoder.x12Encodation);
          break;
        case HighLevelEncoder.textEncodation:
          context.writeCodeword(HighLevelEncoder.latchToText);
          context.signalEncoderChange(HighLevelEncoder.textEncodation);
          break;
        case HighLevelEncoder.edifactEncodation:
          context.writeCodeword(HighLevelEncoder.latchToEdifact);
          context.signalEncoderChange(HighLevelEncoder.edifactEncodation);
          break;
        default:
          throw StateError('Illegal mode: $newMode');
      }
    } else if (HighLevelEncoder.isExtendedASCII(c)) {
      context.writeCodeword(HighLevelEncoder.upperShift);
      context.writeCodeword(c - 128 + 1);
      context.pos++;
    } else {
      context.writeCodeword(c + 1);
      context.pos++;
    }
  }
}