|
| 1 | +/* |
| 2 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 3 | + * this software and associated documentation files (the "Software"), to deal in |
| 4 | + * the Software without restriction, including without limitation the rights to |
| 5 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 6 | + * of the Software, and to permit persons to whom the Software is furnished to do |
| 7 | + * so, subject to the following conditions: |
| 8 | + * |
| 9 | + * The above copyright notice and this permission notice shall be included in all |
| 10 | + * copies or substantial portions of the Software. |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 13 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 16 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 18 | + * SOFTWARE. |
| 19 | + */ |
| 20 | +package org.jcodings; |
| 21 | + |
| 22 | +import org.jcodings.ascii.AsciiTables; |
| 23 | +import org.jcodings.constants.PosixBracket; |
| 24 | +import org.jcodings.exception.CharacterPropertyException; |
| 25 | +import org.jcodings.exception.EncodingError; |
| 26 | + |
| 27 | +abstract class AbstractEncoding extends Encoding { |
| 28 | + |
| 29 | + private final short CTypeTable[]; |
| 30 | + |
| 31 | + protected AbstractEncoding(String name, int minLength, int maxLength, short[]CTypeTable) { |
| 32 | + super(name, minLength, maxLength); |
| 33 | + this.CTypeTable = CTypeTable; |
| 34 | + } |
| 35 | + |
| 36 | + /** CTYPE_TO_BIT |
| 37 | + */ |
| 38 | + private static int CTypeToBit(int ctype) { |
| 39 | + return 1 << ctype; |
| 40 | + } |
| 41 | + |
| 42 | + /** ONIGENC_IS_XXXXXX_CODE_CTYPE |
| 43 | + */ |
| 44 | + protected final boolean isCodeCTypeInternal(int code, int ctype) { |
| 45 | + return (CTypeTable[code] & CTypeToBit(ctype)) != 0; |
| 46 | + } |
| 47 | + |
| 48 | + /** onigenc_is_mbc_newline_0x0a / used also by multibyte encodings |
| 49 | + * |
| 50 | + */ |
| 51 | + @Override |
| 52 | + public boolean isNewLine(byte[]bytes, int p, int end) { |
| 53 | + return p < end ? bytes[p] == Encoding.NEW_LINE : false; |
| 54 | + } |
| 55 | + |
| 56 | + protected final int asciiMbcCaseFold(int flag, byte[]bytes, IntHolder pp, int end, byte[]lower) { |
| 57 | + lower[0] = AsciiTables.ToLowerCaseTable[bytes[pp.value] & 0xff]; |
| 58 | + pp.value++; |
| 59 | + return 1; |
| 60 | + } |
| 61 | + |
| 62 | + /** onigenc_ascii_mbc_case_fold |
| 63 | + */ |
| 64 | + @Override |
| 65 | + public int mbcCaseFold(int flag, byte[]bytes, IntHolder pp, int end, byte[]lower) { |
| 66 | + return asciiMbcCaseFold(flag, bytes, pp, end, lower); |
| 67 | + } |
| 68 | + |
| 69 | + protected final void asciiApplyAllCaseFold(int flag, ApplyAllCaseFoldFunction fun, Object arg) { |
| 70 | + int[]code = new int[]{0}; |
| 71 | + |
| 72 | + for (int i=0; i<AsciiTables.LowerMap.length; i++) { |
| 73 | + code[0] = AsciiTables.LowerMap[i][1]; |
| 74 | + fun.apply(AsciiTables.LowerMap[i][0], code, 1, arg); |
| 75 | + |
| 76 | + code[0] = AsciiTables.LowerMap[i][0]; |
| 77 | + fun.apply(AsciiTables.LowerMap[i][1], code, 1, arg); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** onigenc_ascii_apply_all_case_fold / used also by multibyte encodings |
| 82 | + */ |
| 83 | + @Override |
| 84 | + public void applyAllCaseFold(int flag, ApplyAllCaseFoldFunction fun, Object arg) { |
| 85 | + asciiApplyAllCaseFold(flag, fun, arg); |
| 86 | + } |
| 87 | + |
| 88 | + protected final CaseFoldCodeItem[]asciiCaseFoldCodesByString(int flag, byte[]bytes, int p, int end) { |
| 89 | + int b = bytes[p] & 0xff; |
| 90 | + |
| 91 | + if (0x41 <= b && b <= 0x5a) { |
| 92 | + return new CaseFoldCodeItem[]{CaseFoldCodeItem.create(1, b + 0x20)}; |
| 93 | + } else if (0x61 <= b && b <= 0x7a) { |
| 94 | + return new CaseFoldCodeItem[]{CaseFoldCodeItem.create(1, b - 0x20)}; |
| 95 | + } else { |
| 96 | + return CaseFoldCodeItem.EMPTY_FOLD_CODES; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** onigenc_ascii_get_case_fold_codes_by_str / used also by multibyte encodings |
| 101 | + */ |
| 102 | + @Override |
| 103 | + public CaseFoldCodeItem[]caseFoldCodesByString(int flag, byte[]bytes, int p, int end) { |
| 104 | + return asciiCaseFoldCodesByString(flag, bytes, p, end); |
| 105 | + } |
| 106 | + |
| 107 | + /** onigenc_ascii_only_case_map / onigenc_single_byte_ascii_only_case_map |
| 108 | + */ |
| 109 | + int asciiOnlyCaseMap(IntHolder flagP, byte[]bytes, IntHolder pp, int end, byte[]to, int toP, int toEnd) { |
| 110 | + int toStart = toP; |
| 111 | + int flags = flagP.value; |
| 112 | + |
| 113 | + while (pp.value < end && toP < toEnd) { |
| 114 | + // specialize for singlebyte ? |
| 115 | + int length = length(bytes, pp.value, end); |
| 116 | + if (length < 0) return length; |
| 117 | + int code = mbcToCode(bytes, pp.value, end); |
| 118 | + pp.value += length; |
| 119 | + |
| 120 | + if (code >= 'a' && code <= 'z' && ((flags & Config.CASE_UPCASE) != 0)) { |
| 121 | + flags |= Config.CASE_MODIFIED; |
| 122 | + code += 'A' - 'a'; |
| 123 | + } else if (code >= 'A' && code <= 'Z' && ((flags & (Config.CASE_DOWNCASE | Config.CASE_FOLD)) != 0)) { |
| 124 | + flags |= Config.CASE_MODIFIED; |
| 125 | + code += 'a' - 'A'; |
| 126 | + } |
| 127 | + toP += codeToMbc(code, to, toP); |
| 128 | + if ((flags & Config.CASE_TITLECASE) != 0) { |
| 129 | + flags ^= (Config.CASE_UPCASE | Config.CASE_DOWNCASE | Config.CASE_TITLECASE); |
| 130 | + } |
| 131 | + } |
| 132 | + flagP.value = flags; |
| 133 | + return toP - toStart; |
| 134 | + } |
| 135 | + |
| 136 | + int singleByteAsciiOnlyCaseMap(IntHolder flagP, byte[]bytes, IntHolder pp, int end, byte[]to, int toP, int toEnd) { |
| 137 | + int toStart = toP; |
| 138 | + int flags = flagP.value; |
| 139 | + |
| 140 | + while (pp.value < end && toP < toEnd) { |
| 141 | + int code = bytes[pp.value++] & 0xff; |
| 142 | + |
| 143 | + if (code >= 'a' && code <= 'z' && ((flags & Config.CASE_UPCASE) != 0)) { |
| 144 | + flags |= Config.CASE_MODIFIED; |
| 145 | + code += 'A' - 'a'; |
| 146 | + } else if (code >= 'A' && code <= 'Z' && ((flags & (Config.CASE_DOWNCASE | Config.CASE_FOLD)) != 0)) { |
| 147 | + flags |= Config.CASE_MODIFIED; |
| 148 | + code += 'a' - 'A'; |
| 149 | + } |
| 150 | + to[toP++] = (byte)code; |
| 151 | + if ((flags & Config.CASE_TITLECASE) != 0) { |
| 152 | + flags ^= (Config.CASE_UPCASE | Config.CASE_DOWNCASE | Config.CASE_TITLECASE); |
| 153 | + } |
| 154 | + } |
| 155 | + flagP.value = flags; |
| 156 | + return toP - toStart; |
| 157 | + } |
| 158 | + |
| 159 | + /** onigenc_minimum_property_name_to_ctype |
| 160 | + * notably overridden by unicode encodings |
| 161 | + */ |
| 162 | + @Override |
| 163 | + public int propertyNameToCType(byte[]bytes, int p, int end) { |
| 164 | + Integer ctype = PosixBracket.PBSTableUpper.get(bytes, p, end); |
| 165 | + if (ctype != null) return ctype; |
| 166 | + throw new CharacterPropertyException(EncodingError.ERR_INVALID_CHAR_PROPERTY_NAME, bytes, p, end - p); |
| 167 | + } |
| 168 | +} |
0 commit comments