package com.ethlo.util;

/**
 * 
 * @author mha
 *
 */
public final class CharArrayIntegerUtil extends CharArrayNumberUtil
{
	private CharArrayIntegerUtil()
	{
		
	}
    
	/**
	 * Converts the integer value into a char representation, like int2char(10, 16) returns 'a' (hex)
	 * @param value The value to convert to char
	 * @return The char that represents the specified value
	 */
    public static char int2Char(int value, int radix) 
    {
    	Assert.isTrue(radix >= Character.MIN_RADIX || radix <= Character.MAX_RADIX, "radix must be between 1 and 36");
        Assert.isTrue(value < radix, "value must be < radix");
        return RADIX36_DIGITS[value];
    }
    
	public static int parsePositiveInt(char[] strNum)
	{
		return parsePositiveInt(strNum, 10, 0, strNum.length);
	}
	
	public static int parsePositiveInt(char[] strNum, int radix)
	{
		return parsePositiveInt(strNum, radix, 0, strNum.length);
	}
	
	public static int parsePositiveInt(char[] strNum, int radix, int offset, int length)
	{
		int result = 0;
		for (int i = offset; i < length; i++)
		{
			int digit = digit(strNum[i], radix);
			result *= radix;
			result -= digit;
		}
		return -result;
	}

    public static int toString(int i, char[] buf, int offset, int padTo)
    {
        return toString(i, 10, buf, offset, padTo);
    }

    /**
     * Handles only non-negative values
     * @param value
     * @param radix
     * @param buf
     * @param offset
     * @return
     */
    public static int toString(int value, int radix, char[] buf, int offset, int padTo) 
    {
        int charPos = offset + 32;
        value = -value;
        while (value <= -radix) 
        {
            buf[charPos--] = RADIX36_DIGITS[-(value % radix)];
            value = value / radix;
        }
        buf[charPos] = RADIX36_DIGITS[-value];
        
        final int length = ((32 + offset) - charPos) + 1;
        int l = length;
        while (l < padTo)
        {
            buf[--charPos] = '0';
            l++;
        }
        final int srcPos = charPos;
        System.arraycopy(buf, srcPos, buf, offset, padTo);
        return offset + length;
    }    
}

@@ -2040,16 +2040,182 @@
         
+copy(buf, srcPos, offset, padTo);%0A        return offset + length;%0A    %7D%0A%0A    private static void copy(char%5B%5D buf, int srcPos, int offset, int length)%0A    %7B%0A        //
 System.a
@@ -2241,37 +2241,38 @@
 s, buf, offset, 
-padTo
+length
 );%0A        retur
@@ -2270,40 +2270,112 @@
     
-return offset + length;%0A    %7D
+for (int i = 0; i %3C length; i++)%0A        %7B%0A            buf%5Boffset + i%5D = buf%5BsrcPos + i%5D;%0A        %7D%0A
     
+%7D
 %0A%7D%0A

Copy via loop