getNumberOfC40Words static method
The number of characters encoded is returned in characterLength. The number of characters encoded is also minimal in the sense that the algorithm stops as soon as a character encoding fills a C40 word competely (three C40 values). An exception is at the end of the string where two C40 values are allowed (according to the spec the third c40 value is filled with 0 (Shift 1) in this case).
Implementation
static int getNumberOfC40Words(
Input input,
int from,
bool c40,
List<int> characterLength,
) {
int thirdsCount = 0;
for (int i = from; i < input.length; i++) {
if (input.isECI(i)) {
characterLength[0] = 0;
return 0;
}
final ci = input.charAt(i);
if (c40 && HighLevelEncoder.isNativeC40(ci) ||
!c40 && HighLevelEncoder.isNativeText(ci)) {
thirdsCount++; //native
} else if (!MinimalEncoder.isExtendedASCII(ci, input.fnc1Character)) {
thirdsCount += 2; //shift
} else {
final asciiValue = ci & 0xff;
if (asciiValue >= 128 &&
(c40 && HighLevelEncoder.isNativeC40((asciiValue - 128)) ||
!c40 && HighLevelEncoder.isNativeText((asciiValue - 128)))) {
thirdsCount += 3; // shift, Upper shift
} else {
thirdsCount += 4; // shift, Upper shift, shift
}
}
if (thirdsCount % 3 == 0 ||
((thirdsCount - 2) % 3 == 0 && i + 1 == input.length)) {
characterLength[0] = i - from + 1;
return (thirdsCount / 3.0).ceil();
}
}
characterLength[0] = 0;
return 0;
}