/*
* Copyright (C) 2016-2022 ActionTech.
* based on code by MyCATCopyrightHolder Copyright (c) 2013, OpenCloudDB/MyCAT.
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher.
*/
package com.actiontech.dble.util;

import com.actiontech.dble.config.util.ConfigException;

/**
 * @author mycat
 */
public final class LongUtil {
    private LongUtil() {
    }

    private static final byte[] MIN_VALUE = "-9223372036854775808".getBytes();

    public static byte[] toBytes(long i) {
        if (i == Long.MIN_VALUE) {
            return MIN_VALUE;
        }
        int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
        byte[] buf = new byte[size];
        getBytes(i, size, buf);
        return buf;
    }

    static int stringSize(long x) {
        long p = 10;
        for (int i = 1; i < 19; i++) {
            if (x < p) {
                return i;
            }
            p = 10 * p;
        }
        return 19;
    }

    static void getBytes(long i, int index, byte[] buf) {
        long q;
        int r;
        int charPos = index;
        byte sign = 0;

        if (i < 0) {
            sign = '-';
            i = -i;
        }

        // Get 2 digits/iteration using longs until quotient fits into an int
        while (i > Integer.MAX_VALUE) {
            q = i / 100;
            // really: r = i - (q * 100);
            r = (int) (i - ((q << 6) + (q << 5) + (q << 2)));
            i = q;
            buf[--charPos] = IntegerUtil.DIGIT_ONES[r];
            buf[--charPos] = IntegerUtil.DIGIT_TENS[r];
        }

        // Get 2 digits/iteration using ints
        int q2;
        int i2 = (int) i;
        while (i2 >= 65536) {
            q2 = i2 / 100;
            // really: r = i2 - (q * 100);
            r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
            i2 = q2;
            buf[--charPos] = IntegerUtil.DIGIT_ONES[r];
            buf[--charPos] = IntegerUtil.DIGIT_TENS[r];
        }

        // Fall thru to fast mode for smaller numbers
        // assert(i2 <= 65536, i2);
        for (; ; ) {
            q2 = (i2 * 52429) >>> (16 + 3);
            r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
            buf[--charPos] = IntegerUtil.DIGITS[r];
            i2 = q2;
            if (i2 == 0) {
                break;
            }
        }
        if (sign != 0) {
            buf[--charPos] = sign;
        }
    }


    public static boolean isLong(String intStr) {
        try {
            if (!StringUtil.isBlank(intStr)) {
                Long.parseLong(intStr);
                return true;
            }
        } catch (NumberFormatException e) {
            return false;
        }
        return false;
    }


    public static long parseLong(String intStr) {
        try {
            if (!StringUtil.isBlank(intStr)) {
                return Long.parseLong(intStr);
            }
        } catch (NumberFormatException e) {
            throw new ConfigException("incorrect long value: '" + intStr + "'");
        }
        throw new ConfigException("incorrect long value: 'null'");
    }

}
