20 namespace Lucene.Net.Util
43 public static int ParseInt(
char[] chars)
45 return ParseInt(chars, 0, chars.Length, 10);
58 public static int ParseInt(
char[] chars,
int offset,
int len)
60 return ParseInt(chars, offset, len, 10);
78 public static int ParseInt(
char[] chars,
int offset,
int len,
int radix)
80 if (chars == null || radix < 2 || radix > 36)
82 throw new System.FormatException();
87 throw new System.FormatException(
"chars length is 0");
89 bool negative = chars[offset + i] ==
'-';
90 if (negative && ++i == len)
92 throw new System.FormatException(
"can't convert to an int");
99 return Parse(chars, offset, len, radix, negative);
103 private static int Parse(
char[] chars,
int offset,
int len,
int radix,
bool negative)
105 int max = System.Int32.MinValue / radix;
107 for (
int i = 0; i < len; i++)
109 int digit = (int) System.Char.GetNumericValue(chars[i + offset]);
112 throw new System.FormatException(
"Unable to parse");
116 throw new System.FormatException(
"Unable to parse");
118 int next = result * radix - digit;
121 throw new System.FormatException(
"Unable to parse");
133 throw new System.FormatException(
"Unable to parse");
146 public static int GetNextSize(
int targetSize)
155 return (targetSize >> 3) + (targetSize < 9?3:6) + targetSize;
158 public static int GetShrinkSize(
int currentSize,
int targetSize)
160 int newSize = GetNextSize(targetSize);
164 if (newSize < currentSize / 2)
170 public static int[] Grow(
int[] array,
int minSize)
172 if (array.Length < minSize)
174 int[] newArray =
new int[GetNextSize(minSize)];
175 Array.Copy(array, 0, newArray, 0, array.Length);
182 public static int[] Grow(
int[] array)
184 return Grow(array, 1 + array.Length);
187 public static int[] Shrink(
int[] array,
int targetSize)
189 int newSize = GetShrinkSize(array.Length, targetSize);
190 if (newSize != array.Length)
192 int[] newArray =
new int[newSize];
193 Array.Copy(array, 0, newArray, 0, newSize);
200 public static long[] Grow(
long[] array,
int minSize)
202 if (array.Length < minSize)
204 long[] newArray =
new long[GetNextSize(minSize)];
205 Array.Copy(array, 0, newArray, 0, array.Length);
212 public static long[] Grow(
long[] array)
214 return Grow(array, 1 + array.Length);
217 public static long[] Shrink(
long[] array,
int targetSize)
219 int newSize = GetShrinkSize(array.Length, targetSize);
220 if (newSize != array.Length)
222 long[] newArray =
new long[newSize];
223 Array.Copy(array, 0, newArray, 0, newSize);
230 public static byte[] Grow(byte[] array,
int minSize)
232 if (array.Length < minSize)
234 byte[] newArray =
new byte[GetNextSize(minSize)];
235 Array.Copy(array, 0, newArray, 0, array.Length);
242 public static byte[] Grow(byte[] array)
244 return Grow(array, 1 + array.Length);
247 public static byte[] Shrink(byte[] array,
int targetSize)
249 int newSize = GetShrinkSize(array.Length, targetSize);
250 if (newSize != array.Length)
252 byte[] newArray =
new byte[newSize];
253 Array.Copy(array, 0, newArray, 0, newSize);
263 public static int HashCode(
char[] array,
int start,
int end)
266 for (
int i = end - 1; i >= start; i--)
267 code = code * 31 + array[i];
274 public static int HashCode(byte[] array,
int start,
int end)
277 for (
int i = end - 1; i >= start; i--)
278 code = code * 31 + array[i];