package org.jcodings;
public abstract class CaseFoldMapEncoding extends SingleByteEncoding {
protected final int[][]CaseFoldMap;
protected final boolean foldFlag;
protected CaseFoldMapEncoding(String name, short[]CTypeTable, byte[]LowerCaseTable, int[][]CaseFoldMap) {
this(name, CTypeTable, LowerCaseTable, CaseFoldMap, true);
}
protected CaseFoldMapEncoding(String name, short[]CTypeTable, byte[]LowerCaseTable, int[][]CaseFoldMap, boolean foldFlag) {
super(name, CTypeTable, LowerCaseTable);
this.CaseFoldMap = CaseFoldMap;
this.foldFlag = foldFlag;
}
protected final int applyAllCaseFoldWithMap(int mapSize, int[][]map, boolean essTsettFlag, int flag,
ApplyAllCaseFoldFunction fun, Object arg) {
asciiApplyAllCaseFold(flag, fun, arg);
int[]code = new int[]{0};
for (int i=0; i<mapSize; i++) {
code[0] = map[i][1];
fun.apply(map[i][0], code, 1, arg);
code[0] = map[i][0];
fun.apply(map[i][1], code, 1, arg);
}
if (essTsettFlag) ssApplyAllCaseFold(flag, fun, arg);
return 0;
}
static final int[] SS = new int []{0x73, 0x73};
private void ssApplyAllCaseFold(int flag, ApplyAllCaseFoldFunction fun, Object arg) {
fun.apply(0xdf, SS, 2, arg);
}
protected final CaseFoldCodeItem[]getCaseFoldCodesByStringWithMap(int mapSize, int[][]map,
boolean essTsettFlag, int flag,
byte[]bytes, int p, int end) {
int b = bytes[p] & 0xff;
if (0x41 <= b && b <= 0x5a) {
CaseFoldCodeItem item0 = CaseFoldCodeItem.create(1, b + 0x20);
if (b == 0x53 && essTsettFlag && end > p + 1 &&
(bytes[p+1] == (byte)0x53 || bytes[p+1] == (byte)0x73)) {
CaseFoldCodeItem item1 = CaseFoldCodeItem.create(2, 0xdf);
return new CaseFoldCodeItem[]{item0, item1};
} else {
return new CaseFoldCodeItem[]{item0};
}
} else if (0x61 <= b && b <= 0x7a) {
CaseFoldCodeItem item0 = CaseFoldCodeItem.create(1, b - 0x20);
if (b == 0x73 && essTsettFlag && end >p + 1 &&
(bytes[p+1] == (byte)0x73 || bytes[p+1] == (byte)0x53)) {
CaseFoldCodeItem item1 = CaseFoldCodeItem.create(2, 0xdf);
return new CaseFoldCodeItem[]{item0, item1};
} else {
return new CaseFoldCodeItem[]{item0};
}
} else if (b == 0xdf && essTsettFlag) {
CaseFoldCodeItem item0 = CaseFoldCodeItem.create(1, 's', 's');
CaseFoldCodeItem item1 = CaseFoldCodeItem.create(1, 'S', 'S');
CaseFoldCodeItem item2 = CaseFoldCodeItem.create(1, 's', 'S');
CaseFoldCodeItem item3 = CaseFoldCodeItem.create(1, 'S', 's');
return new CaseFoldCodeItem[]{item0, item1, item2, item3};
} else {
for (int i=0; i<mapSize; i++) {
if (b == map[i][0]) {
return new CaseFoldCodeItem[]{CaseFoldCodeItem.create(1, map[i][1])};
} else if (b == map[i][1]) {
return new CaseFoldCodeItem[]{CaseFoldCodeItem.create(1, map[i][0])};
}
}
}
return CaseFoldCodeItem.EMPTY_FOLD_CODES;
}
@Override
public void applyAllCaseFold(int flag, ApplyAllCaseFoldFunction fun, Object arg) {
applyAllCaseFoldWithMap(CaseFoldMap.length, CaseFoldMap, foldFlag, flag, fun, arg);
}
@Override
public CaseFoldCodeItem[]caseFoldCodesByString(int flag, byte[]bytes, int p, int end) {
return getCaseFoldCodesByStringWithMap(CaseFoldMap.length, CaseFoldMap, foldFlag, flag, bytes, p, end);
}
@Override
public boolean isCodeCType(int code, int ctype) {
return code < 256 ? isCodeCTypeInternal(code, ctype) : false;
}
}