All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.pwall.util.AbstractCharMapper Maven / Gradle / Ivy

There is a newer version: 2.4
Show newest version
/*
 * @(#) AbstractCharMapper.java
 *
 * javautil Java Utility Library
 * Copyright (c) 2013, 2014 Peter Wall
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package net.pwall.util;

/**
 * Abstract base class for {@link CharMapper} interface - provides useful mapping functions.
 *
 * @author Peter Wall
 */
public abstract class AbstractCharMapper implements CharMapper {

    private static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
            'B', 'C', 'D', 'E', 'F' };

    /**
     * Map a range of contiguous code points into strings using an array.  The first entry in
     * the array represents the code point specified by the base, the second is base + 1 etc.
     *
     * @param array     the array of strings
     * @param codePoint the Unicode code point
     * @param base      the code point representing the first entry in the array
     * @return          the string representing the code point, or {@code null} if the code
     *                  point is outside the range
     */
    public static String arrayMapping(String[] array, int codePoint, int base) {
        if (codePoint >= base && codePoint < base + array.length)
            return array[codePoint - base];
        return null;
    }

    /**
     * Map a code point using a lookup table.  The lookup uses a binary search so the table
     * entries must be in ascending order by code point.
     *
     * @param table     an array of mapping entries, each of which is an array of two strings:
     *                  a single character string containing the character to be mapped, and the
     *                  result string
     * @param codePoint the Unicode code point
     * @return          the string representing the code point, or {@code null} if no entry
     *                  exists in the table for the code point
     */
    public static String lookupMapping(String[][] table, int codePoint) {
        int hi = table.length;
        if (hi > 0 && codePoint >= table[0][0].charAt(0) &&
                codePoint <= table[hi - 1][0].charAt(0)) {
            int lo = 0;
            while (lo < hi) {
                int mid = (lo + hi) >>> 1;
                String[] entry = table[mid];
                char ch = entry[0].charAt(0);
                if (codePoint == ch)
                    return entry[1];
                if (codePoint < ch)
                    hi = mid;
                else
                    lo = mid + 1;
            }
        }
        return null;
    }

    /**
     * Map a code point using a lookup table.  The lookup uses a binary search so the table
     * entries must be in ascending order by code point.
     *
     * @param table     an array of mapping entries, each of which is an array of two strings:
     *                  a single character string containing the character to be mapped, and the
     *                  result string
     * @param codePoint the Unicode code point
     * @return          the string representing the code point, or {@code null} if no entry
     *                  exists in the table for the code point
     */
    public static String lookupMapping(CharMapperEntry[] table, int codePoint) {
        int hi = table.length;
        if (hi > 0 && codePoint >= table[0].getCodePoint() &&
                codePoint <= table[hi - 1].getCodePoint()) {
            int lo = 0;
            while (lo < hi) {
                int mid = (lo + hi) >>> 1;
                CharMapperEntry entry = table[mid];
                int entryCodePoint = entry.getCodePoint();
                if (codePoint == entryCodePoint)
                    return entry.getString();
                if (codePoint < entryCodePoint)
                    hi = mid;
                else
                    lo = mid + 1;
            }
        }
        return null;
    }

    /**
     * Map a code point into a decimal string with prefix and optional suffix.
     *
     * @param codePoint the Unicode code point
     * @param prefix    the prefix string
     * @param suffix    the suffix string (or {@code null} if no suffix needed)
     * @return          the decimal representation of the code point, with the specified prefix
     *                  and optional suffix
     */
    public static String decimalMapping(int codePoint, String prefix, String suffix) {
        StringBuilder sb = new StringBuilder(prefix.length() + 7 +
                (suffix != null ? suffix.length() : 0));
        sb.append(prefix);
        sb.append(codePoint);
        if (suffix != null)
            sb.append(suffix);
        return sb.toString();
    }

    /**
     * Map a code point into a fixed-length hexadecimal string with prefix and optional suffix.
     *
     * @param codePoint the Unicode code point
     * @param length    the number of hexadecimal digits
     * @param prefix    the prefix string
     * @param suffix    the suffix string (or {@code null} if no suffix needed)
     * @return          the hexadecimal representation of the code point, zero-padded to the
     *                  required number of digits, with the specified prefix and optional suffix
     */
    public static String hexMapping(int codePoint, int length, String prefix, String suffix) {
        StringBuilder sb = new StringBuilder(prefix.length() + length +
                (suffix != null ? suffix.length() : 0));
        sb.append(prefix);
        int i = sb.length();
        for (; length > 0; length--) {
            sb.insert(i, hexDigits[codePoint & 0xF]);
            codePoint >>>= 4;
        }
        if (suffix != null)
            sb.append(suffix);
        return sb.toString();
    }

    /**
     * Map a code point into a fixed-length hexadecimal string with prefix and no suffix.  This
     * is an optimization of the common case of no suffix being required.
     *
     * @param codePoint the Unicode code point
     * @param length    the number of hexadecimal digits
     * @param prefix    the prefix string
     * @return          the hexadecimal representation of the code point, zero-padded to the
     *                  required number of digits, with the specified prefix
     */
    public static String hexMapping(int codePoint, int length, String prefix) {
        StringBuilder sb = new StringBuilder(prefix.length() + length);
        sb.append(prefix);
        int i = sb.length();
        for (; length > 0; length--) {
            sb.insert(i, hexDigits[codePoint & 0xF]);
            codePoint >>>= 4;
        }
        return sb.toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy