Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/org/jcodings/MultiByteEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,15 @@ protected final int mbnMbcCaseFold(int flag, byte[]bytes, IntHolder pp, int end,
pp.value++;
return 1;
} else {
int len = length(bytes, p, end);
for (int i=0; i<len; i++) {
// The logical end can split a multibyte character. Use its nominal
// length, limited only by the backing array's physical boundary.
int len = Math.max(length(bytes[p]), 1);
int copyLen = Math.min(len, bytes.length - p);
for (int i=0; i<copyLen; i++) {
lower[lowerP++] = bytes[p++];
}
pp.value += len;
return len; /* return byte length of converted to lower char */
pp.value += copyLen;
return copyLen; /* return byte length of converted to lower char */
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_10Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_13Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_14Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_15Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_16Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_1Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_2Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_3Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_4Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/ISO8859_9Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/Windows_1250Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == ISOEncoding.SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/Windows_1252Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == ISOEncoding.SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/Windows_1254Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == ISOEncoding.SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/jcodings/specific/Windows_1257Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public int caseMap(IntHolder flagP, byte[] bytes, IntHolder pp, int end, byte[]
int toStart = toP;
int flags = flagP.value;

while (pp.value < end && toP < toEnd) {
while (pp.value < end && toEnd - toP > 1) {
int code = bytes[pp.value++] & 0xff;
if (code == ISOEncoding.SHARP_s) {
if ((flags & Config.CASE_UPCASE) != 0) {
Expand Down
43 changes: 43 additions & 0 deletions test/org/jcodings/specific/TestCaseMap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jcodings.specific;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
Expand Down Expand Up @@ -82,6 +84,47 @@ public void testISOCaseMap() throws Exception {
}
}

@Test
public void testSharpSWithLimitedOutput() {
Encoding[] encodings = {
ISO8859_1Encoding.INSTANCE, ISO8859_2Encoding.INSTANCE,
ISO8859_3Encoding.INSTANCE, ISO8859_4Encoding.INSTANCE,
ISO8859_9Encoding.INSTANCE, ISO8859_10Encoding.INSTANCE,
ISO8859_13Encoding.INSTANCE, ISO8859_14Encoding.INSTANCE,
ISO8859_15Encoding.INSTANCE, ISO8859_16Encoding.INSTANCE,
Windows_1250Encoding.INSTANCE, Windows_1252Encoding.INSTANCE,
Windows_1254Encoding.INSTANCE, Windows_1257Encoding.INSTANCE
};
int[] flags = {
Config.CASE_UPCASE, Config.CASE_FOLD,
Config.CASE_UPCASE | Config.CASE_TITLECASE
};
byte[][] expected = {
{'S', 'S'}, {'s', 's'}, {'S', 's'}
};

for (Encoding enc : encodings) {
for (int i = 0; i < flags.length; i++) {
byte[] input = {(byte)0xdf};
IntHolder inputP = new IntHolder();
IntHolder flagP = new IntHolder();
flagP.value = flags[i];
byte[] output = new byte[1];
assertEquals(0, enc.caseMap(flagP, input, inputP, input.length,
output, 0, output.length));
assertEquals(0, inputP.value);

inputP.value = 0;
flagP.value = flags[i];
output = new byte[2];
assertEquals(2, enc.caseMap(flagP, input, inputP, input.length,
output, 0, output.length));
assertArrayEquals(expected[i], output);
assertEquals(1, inputP.value);
}
}
}

@Test
public void testGeorgian() throws Exception {
Encoding enc = UTF8Encoding.INSTANCE;
Expand Down
26 changes: 26 additions & 0 deletions test/org/jcodings/specific/TestEmacsMule.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.jcodings.specific;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;

import org.jcodings.Encoding;
import org.jcodings.IntHolder;
import org.jcodings.exception.ErrorCodes;
import org.junit.Test;

Expand Down Expand Up @@ -42,4 +44,28 @@ public void testCodeToMbcLengthMatchesCharLength() {
assertEquals(c.length, enc.codeToMbc(code, out, 0));
}
}

@Test
public void testCaseFoldWhenLogicalEndSplitsCharacter() {
Encoding enc = EmacsMuleEncoding.INSTANCE;
byte[] input = {(byte)0x86, (byte)0xcd};
byte[] folded = new byte[4];
IntHolder p = new IntHolder();

assertEquals(2, enc.mbcCaseFold(0, input, p, 1, folded));
assertEquals(2, p.value);
assertArrayEquals(input, new byte[]{folded[0], folded[1]});
}

@Test
public void testCaseFoldDoesNotReadPastBackingArray() {
Encoding enc = EmacsMuleEncoding.INSTANCE;
byte[] input = {(byte)0x86};
byte[] folded = new byte[4];
IntHolder p = new IntHolder();

assertEquals(1, enc.mbcCaseFold(0, input, p, 1, folded));
assertEquals(1, p.value);
assertEquals(input[0], folded[0]);
}
}
Loading