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

io.airlift.joni.OptExactInfo Maven / Gradle / Ivy

Go to download

Java port of Oniguruma: http://www.geocities.jp/kosako3/oniguruma that uses byte arrays directly instead of java Strings and chars

There is a newer version: 2.1.5.2
Show newest version
/*
 * 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 io.airlift.joni;

import io.airlift.jcodings.Encoding;

final class OptExactInfo {
    static final int OPT_EXACT_MAXLEN = 24;

    final MinMaxLen mmd = new MinMaxLen();
    final OptAnchorInfo anchor = new OptAnchorInfo();

    boolean reachEnd;
    boolean ignoreCase;

    final byte bytes[] = new byte[OPT_EXACT_MAXLEN];
    int length;

    boolean isFull() {
        return length >= OPT_EXACT_MAXLEN;
    }

    void clear() {
        mmd.clear();
        anchor.clear();

        reachEnd = false;
        ignoreCase = false;
        length = 0;
    }

    void copy(OptExactInfo other) {
        mmd.copy(other.mmd);
        anchor.copy(other.anchor);
        reachEnd = other.reachEnd;
        ignoreCase = other.ignoreCase;
        length = other.length;

        System.arraycopy(other.bytes, 0, bytes, 0, OPT_EXACT_MAXLEN);
    }

    void concat(OptExactInfo other, Encoding enc) {
        if (!ignoreCase && other.ignoreCase) {
            if (length >= other.length) return; /* avoid */
            ignoreCase = true;
        }

        int p = 0; // add->s;
        int end = p + other.length;

        int i;
        for (i=length; p < end;) {
            int len = enc.length(other.bytes, p, end);
            if (i + len > OPT_EXACT_MAXLEN) break;
            for (int j=0; j OPT_EXACT_MAXLEN) break;
            for (int j=0; j low price */
            v2 = OptMapInfo.positionValue(enc, bytes[0] & 0xff);
            v1 = OptMapInfo.positionValue(enc, alt.bytes[0] & 0xff);

            if (length > 1) v1 += 5;
            if (alt.length > 1) v2 += 5;
        }

        if (!ignoreCase) v1 *= 2;
        if (!alt.ignoreCase) v2 *= 2;

        if (mmd.compareDistanceValue(alt.mmd, v1, v2) > 0) copy(alt);
    }

    // comp_opt_exact_or_map_info
    private static final int COMP_EM_BASE   = 20;
    int compare(OptMapInfo m) {
        if (m.value <= 0) return -1;

        int ve = COMP_EM_BASE * length * (ignoreCase ? 1 : 2);
        int vm = COMP_EM_BASE * 5 * 2 / m.value;

        return mmd.compareDistanceValue(m.mmd, ve, vm);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy