Fix bug624: Illegal Character Exception (#1454)

* - added code to suppress the terminator if the capacity of the version is less than 4 bit
- added test case

* - Removed code in MinimalEncoder that added Mode.TERMINATOR (is taken care of in Encoder.terminateBits)
- Removed the corresponding test case

* Updated test cases

* Improved documentation

* Changed documentation to not use an example with an unsupported character encoding

* Improved wording of comment

* - Simplified code
- Added space after comma in several places

* Added support for more character sets supported by CharacterSetECI

* Syntactic enhancements

* Changed instantiation of generic types to diamond style

* Updated documentation of the QR_COMPACT hint to explain the impact of setting the CHARACTER_SET hint .

* Changed whitespace

* Removed comment

* Fixed typos in comments

* Added text cases for KANJI and Shift_JS encoding

* Improved comments on Japanese language test cases

* Fixed bug

* Compacted code slightly

* Whitespace changes

* Deepend indent
This commit is contained in:
AlexGeller1 2021-11-11 15:23:33 +01:00 committed by GitHub
parent 6c034f9775
commit c25029d29a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 24 deletions

View file

@ -295,32 +295,34 @@ public final class HighLevelEncoder {
int[] intCharCounts = new int[6];
byte[] mins = new byte[6];
findMinimums(charCounts, intCharCounts, Integer.MAX_VALUE, mins);
int minCount = getMinimumCount(mins);
if (intCharCounts[ASCII_ENCODATION] < intCharCounts[BASE256_ENCODATION]
&& intCharCounts[ASCII_ENCODATION] < intCharCounts[C40_ENCODATION]
&& intCharCounts[ASCII_ENCODATION] < intCharCounts[TEXT_ENCODATION]
&& intCharCounts[ASCII_ENCODATION] < intCharCounts[X12_ENCODATION]
&& intCharCounts[ASCII_ENCODATION] < intCharCounts[EDIFACT_ENCODATION]) {
if (intCharCounts[ASCII_ENCODATION] < min(intCharCounts[BASE256_ENCODATION],
intCharCounts[C40_ENCODATION], intCharCounts[TEXT_ENCODATION], intCharCounts[X12_ENCODATION],
intCharCounts[EDIFACT_ENCODATION])) {
return ASCII_ENCODATION;
}
if (intCharCounts[BASE256_ENCODATION] < intCharCounts[ASCII_ENCODATION]
|| (mins[C40_ENCODATION] + mins[TEXT_ENCODATION] + mins[X12_ENCODATION] + mins[EDIFACT_ENCODATION]) == 0) {
if (intCharCounts[BASE256_ENCODATION] < intCharCounts[ASCII_ENCODATION] ||
intCharCounts[BASE256_ENCODATION] + 1 < min(intCharCounts[C40_ENCODATION],
intCharCounts[TEXT_ENCODATION], intCharCounts[X12_ENCODATION], intCharCounts[EDIFACT_ENCODATION])) {
return BASE256_ENCODATION;
}
if (minCount == 1 && mins[EDIFACT_ENCODATION] > 0) {
if (intCharCounts[EDIFACT_ENCODATION] + 1 < min(intCharCounts[BASE256_ENCODATION],
intCharCounts[C40_ENCODATION] , intCharCounts[TEXT_ENCODATION] , intCharCounts[X12_ENCODATION],
intCharCounts[ASCII_ENCODATION])) {
return EDIFACT_ENCODATION;
}
if (minCount == 1 && mins[TEXT_ENCODATION] > 0) {
if (intCharCounts[TEXT_ENCODATION] + 1 < min(intCharCounts[BASE256_ENCODATION],
intCharCounts[C40_ENCODATION] , intCharCounts[EDIFACT_ENCODATION] , intCharCounts[X12_ENCODATION],
intCharCounts[ASCII_ENCODATION])) {
return TEXT_ENCODATION;
}
if (minCount == 1 && mins[X12_ENCODATION] > 0) {
if (intCharCounts[X12_ENCODATION] + 1 < min(intCharCounts[BASE256_ENCODATION],
intCharCounts[C40_ENCODATION] , intCharCounts[EDIFACT_ENCODATION] , intCharCounts[TEXT_ENCODATION],
intCharCounts[ASCII_ENCODATION])) {
return X12_ENCODATION;
}
if (intCharCounts[C40_ENCODATION] + 1 < intCharCounts[ASCII_ENCODATION]
&& intCharCounts[C40_ENCODATION] + 1 < intCharCounts[BASE256_ENCODATION]
&& intCharCounts[C40_ENCODATION] + 1 < intCharCounts[EDIFACT_ENCODATION]
&& intCharCounts[C40_ENCODATION] + 1 < intCharCounts[TEXT_ENCODATION]) {
if (intCharCounts[C40_ENCODATION] + 1 < min(intCharCounts[ASCII_ENCODATION],
intCharCounts[BASE256_ENCODATION] , intCharCounts[EDIFACT_ENCODATION] , intCharCounts[TEXT_ENCODATION])) {
if (intCharCounts[C40_ENCODATION] < intCharCounts[X12_ENCODATION]) {
return C40_ENCODATION;
}
@ -343,6 +345,14 @@ public final class HighLevelEncoder {
}
}
private static int min(int f1, int f2, int f3, int f4, int f5) {
return Math.min(min(f1, f2, f3, f4),f5);
}
private static int min(int f1, int f2, int f3, int f4) {
return Math.min(f1, Math.min(f2, Math.min(f3, f4)));
}
private static int findMinimums(float[] charCounts, int[] intCharCounts, int min, byte[] mins) {
Arrays.fill(mins, (byte) 0);
for (int i = 0; i < 6; i++) {

View file

@ -160,7 +160,7 @@ public final class HighLevelEncodeTestCase extends Assert {
assertEquals("239 91 11 91 11 91 11 254 67 129", visualized);
visualized = encodeHighLevel("aimaimaim{txt}\u0004");
assertEquals("239 91 11 91 11 91 11 16 218 236 107 181 69 254 129 237", visualized);
assertEquals("239 91 11 91 11 91 11 254 124 117 121 117 126 5 129 237", visualized);
}
@Test
@ -236,10 +236,10 @@ public final class HighLevelEncodeTestCase extends Assert {
assertEquals("33 153 235 36 129", visualized);
visualized = encodeHighLevel("\u00ABäöüé\u00BB 234"); //Mixed Base256 + ASCII
assertEquals("231 51 108 59 226 126 1 104 99 153 53 129", visualized);
assertEquals("231 50 108 59 226 126 1 104 33 153 53 129", visualized);
visualized = encodeHighLevel("\u00ABäöüé\u00BB 23£ 1234567890123456789");
assertEquals("231 55 108 59 226 126 1 104 99 10 161 167 185 142 164 186 208"
assertEquals("231 54 108 59 226 126 1 104 99 10 161 167 33 142 164 186 208"
+ " 220 142 164 186 208 58 129 59 209 104 254 150 45", visualized);
visualized = encodeHighLevel(createBinaryMessage(20));
@ -307,19 +307,19 @@ public final class HighLevelEncodeTestCase extends Assert {
//EDIFACT encoding correctly
String visualized = encodeHighLevel("CREX-TAN:h");
assertEquals("240 13 33 88 181 64 78 124 59 105", visualized);
assertEquals("68 83 70 89 46 85 66 79 59 105", visualized);
visualized = encodeHighLevel("CREX-TAN:hh");
assertEquals("240 13 33 88 181 64 78 124 59 105 105 129", visualized);
assertEquals("68 83 70 89 46 85 66 79 59 105 105 129", visualized);
visualized = encodeHighLevel("CREX-TAN:hhh");
assertEquals("240 13 33 88 181 64 78 124 59 105 105 105", visualized);
assertEquals("68 83 70 89 46 85 66 79 59 105 105 105", visualized);
}
@Test
public void testX12Unlatch() {
String visualized = encodeHighLevel("*DTCP01");
assertEquals("238 9 10 104 141 254 50 129", visualized);
assertEquals("43 69 85 68 81 131 129 56", visualized);
}
@Test
@ -334,7 +334,7 @@ public final class HighLevelEncodeTestCase extends Assert {
//of an encoding problem of the character 0x0060 in Java source code.
String visualized = encodeHighLevel("fiykmj*Rh2`,e6");
assertEquals("239 122 87 154 40 7 171 115 207 12 130 71 155 254 129 237", visualized);
assertEquals("103 106 122 108 110 107 43 83 105 51 97 45 102 55 129 237", visualized);
}
@ -350,7 +350,7 @@ public final class HighLevelEncodeTestCase extends Assert {
public void testEncodingWithStartAsX12AndLatchToEDIFACTInTheMiddle() {
String visualized = encodeHighLevel("*MEMANT-1F-MESTECH");
assertEquals("238 10 99 164 204 254 240 82 220 70 180 209 83 80 80 200", visualized);
assertEquals("240 168 209 77 4 229 45 196 107 77 21 53 5 12 135 192", visualized);
}
private static String encodeHighLevel(String msg) {