Ejemplo n.º 1
0
        /// <summary>
        /// Parses the string argument as if it was an <see cref="int"/> value and returns the
        /// result. Throws <see cref="FormatException"/> if the string does not represent an
        /// <see cref="int"/> quantity. The second argument specifies the radix to use when parsing
        /// the value.
        /// <para/>
        /// NOTE: This was parseInt() in Lucene
        /// </summary>
        /// <param name="chars"> A string representation of an int quantity. </param>
        /// <param name="radix"> The base to use for conversion. </param>
        /// <returns> The value represented by the argument </returns>
        /// <exception cref="FormatException"> If the argument could not be parsed as an int quantity. </exception>
        public static int ParseInt32(char[] chars, int offset, int len, int radix)
        {
            int minRadix = 2, maxRadix = 36;

            if (chars is null || radix < minRadix || radix > maxRadix)
            {
                throw NumberFormatException.Create();
            }
            int i = 0;

            if (len == 0)
            {
                throw NumberFormatException.Create("chars length is 0");
            }
            bool negative = chars[offset + i] == '-';

            if (negative && ++i == len)
            {
                throw NumberFormatException.Create("can't convert to an int");
            }
            if (negative == true)
            {
                offset++;
                len--;
            }
            return(Parse(chars, offset, len, radix, negative));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Converts a hex string at the specified index and length of <paramref name="hex"/>
        /// into an <see cref="int"/>.
        /// <para/>
        /// NOTE: This was hexToInt() in Lucene
        /// </summary>
        /// <param name="hex">
        /// A string in capital or lower case hex, of no more then 16
        /// characters.
        /// </param>
        /// <param name="startIndex">The index of the first character to begin parsing.</param>
        /// <param name="length">The number of characters to parse.</param>
        /// <exception cref="FormatException">if the string is more than 16 characters long, or if any
        /// character is not in the set [0-9a-fA-f]</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <paramref name="length"/> is less than zero.
        /// <para/>
        /// -or-
        /// <para/>
        /// <paramref name="startIndex"/> and <paramref name="length"/> refer to a location outside of <paramref name="hex"/>.
        /// </exception>
        // LUCENENET specific overload
        public static int HexToInt32(string hex, int startIndex, int length)
        {
            if ((length > 16) || !Integer.TryParse(hex, startIndex, length, radix: 16, out int result))
            {
                throw NumberFormatException.Create();
            }

            return(result);
        }
Ejemplo n.º 3
0
        public static int GetPrefixCodedInt32Shift(BytesRef val)
        {
            int shift = val.Bytes[val.Offset] - SHIFT_START_INT32;

            if (shift > 31 || shift < 0)
            {
                throw NumberFormatException.Create("Invalid shift value in prefixCoded bytes (is encoded value really an INT?)");
            }
            return(shift);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns an <see cref="int"/> from prefixCoded bytes.
        /// Rightmost bits will be zero for lower precision codes.
        /// This method can be used to decode a term's value.
        /// <para/>
        /// NOTE: This was prefixCodedToInt() in Lucene
        /// </summary>
        /// <exception cref="FormatException"> if the supplied <see cref="BytesRef"/> is
        /// not correctly prefix encoded. </exception>
        /// <seealso cref="Int32ToPrefixCodedBytes(int, int, BytesRef)"/>
        public static int PrefixCodedToInt32(BytesRef val)
        {
            long sortableBits = 0;

            for (int i = val.Offset, limit = val.Offset + val.Length; i < limit; i++)
            {
                sortableBits <<= 7;
                var b = val.Bytes[i];
                if (b < 0)
                {
                    throw NumberFormatException.Create("Invalid prefixCoded numerical value representation (byte " + (b & 0xff).ToString("x") + " at position " + (i - val.Offset) + " is invalid)");
                }
                sortableBits |= b;
            }
            return((int)((sortableBits << GetPrefixCodedInt32Shift(val)) ^ 0x80000000));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns a <see cref="long"/> from prefixCoded bytes.
        /// Rightmost bits will be zero for lower precision codes.
        /// This method can be used to decode a term's value.
        /// <para/>
        /// NOTE: This was prefixCodedToLong() in Lucene
        /// </summary>
        /// <exception cref="FormatException"> if the supplied <see cref="BytesRef"/> is
        /// not correctly prefix encoded. </exception>
        /// <seealso cref="Int64ToPrefixCodedBytes(long, int, BytesRef)"/>
        public static long PrefixCodedToInt64(BytesRef val)
        {
            long sortableBits = 0L;

            for (int i = val.Offset + 1, limit = val.Offset + val.Length; i < limit; i++)
            {
                sortableBits <<= 7;
                var b = val.Bytes[i];
                if (b < 0)
                {
                    throw NumberFormatException.Create("Invalid prefixCoded numerical value representation (byte " + (b & 0xff).ToString("x") + " at position " + (i - val.Offset) + " is invalid)");
                }
                sortableBits |= (byte)b;
            }
            return((long)((ulong)(sortableBits << GetPrefixCodedInt64Shift(val)) ^ 0x8000000000000000L)); // LUCENENET TODO: Is the casting here necessary?
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Converts a hex string into an <see cref="int"/>.
        /// <para/>
        /// NOTE: This was hexToInt() in Lucene
        /// </summary>
        /// <param name="hex">
        /// A string in capital or lower case hex, of no more then 16
        /// characters.
        /// </param>
        /// <exception cref="FormatException">if the string is more than 16 characters long, or if any
        /// character is not in the set [0-9a-fA-f]</exception>
        public static int HexToInt32(string hex)
        {
            int len = hex.Length;

            if (len > 16)
            {
                throw NumberFormatException.Create();
            }

            try
            {
                // LUCENENET NOTE: Convert.ToInt32(string, int) throws a
                // FormatException if the character does not fall in
                // the correct range, which is the behavior we are expecting.
                // But throws an ArgumentException if passed a negative number.
                // So we need to convert to FormatException to provide the correct behavior.
                return(Convert.ToInt32(hex, 16));
            }
            catch (ArgumentException e)
            {
                throw NumberFormatException.Create(e);
            }
        }
Ejemplo n.º 7
0
        private static int Parse(char[] chars, int offset, int len, int radix, bool negative)
        {
            int max    = int.MinValue / radix;
            int result = 0;

            for (int i = 0; i < len; i++)
            {
                int digit = (int)char.GetNumericValue(chars[i + offset]);
                if (digit == -1)
                {
                    throw NumberFormatException.Create("Unable to parse");
                }
                if (max > result)
                {
                    throw NumberFormatException.Create("Unable to parse");
                }
                int next = result * radix - digit;
                if (next > result)
                {
                    throw NumberFormatException.Create("Unable to parse");
                }
                result = next;
            }

            /*while (offset < len) {
             * }*/
            if (!negative)
            {
                result = -result;
                if (result < 0)
                {
                    throw NumberFormatException.Create("Unable to parse");
                }
            }
            return(result);
        }