Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
Number.cs
Go to the documentation of this file.
1 /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21 
22 using System;
23 using System.Globalization;
24 
25 namespace Lucene.Net.Support
26 {
30  public class Number
31  {
35  public const int MIN_RADIX = 2;
39  public const int MAX_RADIX = 36;
40 
41  private const System.String digits = "0123456789abcdefghijklmnopqrstuvwxyz";
42 
43 
49  public static System.String ToString(long number)
50  {
51  System.Text.StringBuilder s = new System.Text.StringBuilder();
52 
53  if (number == 0)
54  {
55  s.Append("0");
56  }
57  else
58  {
59  if (number < 0)
60  {
61  s.Append("-");
62  number = -number;
63  }
64 
65  while (number > 0)
66  {
67  char c = digits[(int)number % 36];
68  s.Insert(0, c);
69  number = number / 36;
70  }
71  }
72 
73  return s.ToString();
74  }
75 
76 
82  public static System.String ToString(float f)
83  {
84  if (((float)(int)f) == f)
85  {
86  return ((int)f).ToString() + ".0";
87  }
88  else
89  {
90  return f.ToString(NumberFormatInfo.InvariantInfo);
91  }
92  }
93 
100  public static System.String ToString(long i, int radix)
101  {
102  if (radix < MIN_RADIX || radix > MAX_RADIX)
103  radix = 10;
104 
105  char[] buf = new char[65];
106  int charPos = 64;
107  bool negative = (i < 0);
108 
109  if (!negative)
110  {
111  i = -i;
112  }
113 
114  while (i <= -radix)
115  {
116  buf[charPos--] = digits[(int)(-(i % radix))];
117  i = i / radix;
118  }
119  buf[charPos] = digits[(int)(-i)];
120 
121  if (negative)
122  {
123  buf[--charPos] = '-';
124  }
125 
126  return new System.String(buf, charPos, (65 - charPos));
127  }
128 
135  public static long Parse(System.String s, int radix)
136  {
137  if (s == null)
138  {
139  throw new ArgumentException("null");
140  }
141 
142  if (radix < MIN_RADIX)
143  {
144  throw new NotSupportedException("radix " + radix +
145  " less than Number.MIN_RADIX");
146  }
147  if (radix > MAX_RADIX)
148  {
149  throw new NotSupportedException("radix " + radix +
150  " greater than Number.MAX_RADIX");
151  }
152 
153  long result = 0;
154  long mult = 1;
155 
156  s = s.ToLower();
157 
158  for (int i = s.Length - 1; i >= 0; i--)
159  {
160  int weight = digits.IndexOf(s[i]);
161  if (weight == -1)
162  throw new FormatException("Invalid number for the specified radix");
163 
164  result += (weight * mult);
165  mult *= radix;
166  }
167 
168  return result;
169  }
170 
177  public static int URShift(int number, int bits)
178  {
179  return (int)(((uint)number) >> bits);
180  }
181 
182 
189  public static long URShift(long number, int bits)
190  {
191  return (long)(((ulong)number) >> bits);
192  }
193 
194 
203  public static int NextSetBit(System.Collections.BitArray bits, int fromIndex)
204  {
205  for (int i = fromIndex; i < bits.Length; i++)
206  {
207  if (bits[i] == true)
208  {
209  return i;
210  }
211  }
212  return -1;
213  }
214 
220  public static long ToInt64(System.String s)
221  {
222  long number = 0;
223  long factor;
224 
225  // handle negative number
226  if (s.StartsWith("-"))
227  {
228  s = s.Substring(1);
229  factor = -1;
230  }
231  else
232  {
233  factor = 1;
234  }
235 
236  // generate number
237  for (int i = s.Length - 1; i > -1; i--)
238  {
239  int n = digits.IndexOf(s[i]);
240 
241  // not supporting fractional or scientific notations
242  if (n < 0)
243  throw new System.ArgumentException("Invalid or unsupported character in number: " + s[i]);
244 
245  number += (n * factor);
246  factor *= 36;
247  }
248 
249  return number;
250  }
251  }
252 }