Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
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 {
27  /// <summary>
28  /// A simple class for number conversions.
29  /// </summary>
30  public class Number
31  {
32  /// <summary>
33  /// Min radix value.
34  /// </summary>
35  public const int MIN_RADIX = 2;
36  /// <summary>
37  /// Max radix value.
38  /// </summary>
39  public const int MAX_RADIX = 36;
40 
41  private const System.String digits = "0123456789abcdefghijklmnopqrstuvwxyz";
42 
43 
44  /// <summary>
45  /// Converts a number to System.String.
46  /// </summary>
47  /// <param name="number"></param>
48  /// <returns></returns>
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 
77  /// <summary>
78  /// Converts a number to System.String.
79  /// </summary>
80  /// <param name="f"></param>
81  /// <returns></returns>
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 
94  /// <summary>
95  /// Converts a number to System.String in the specified radix.
96  /// </summary>
97  /// <param name="i">A number to be converted.</param>
98  /// <param name="radix">A radix.</param>
99  /// <returns>A System.String representation of the number in the specified redix.</returns>
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 
129  /// <summary>
130  /// Parses a number in the specified radix.
131  /// </summary>
132  /// <param name="s">An input System.String.</param>
133  /// <param name="radix">A radix.</param>
134  /// <returns>The parsed number in the specified radix.</returns>
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 
171  /// <summary>
172  /// Performs an unsigned bitwise right shift with the specified number
173  /// </summary>
174  /// <param name="number">Number to operate on</param>
175  /// <param name="bits">Ammount of bits to shift</param>
176  /// <returns>The resulting number from the shift operation</returns>
177  public static int URShift(int number, int bits)
178  {
179  return (int)(((uint)number) >> bits);
180  }
181 
182 
183  /// <summary>
184  /// Performs an unsigned bitwise right shift with the specified number
185  /// </summary>
186  /// <param name="number">Number to operate on</param>
187  /// <param name="bits">Ammount of bits to shift</param>
188  /// <returns>The resulting number from the shift operation</returns>
189  public static long URShift(long number, int bits)
190  {
191  return (long)(((ulong)number) >> bits);
192  }
193 
194 
195  /// <summary>
196  /// Returns the index of the first bit that is set to true that occurs
197  /// on or after the specified starting index. If no such bit exists
198  /// then -1 is returned.
199  /// </summary>
200  /// <param name="bits">The BitArray object.</param>
201  /// <param name="fromIndex">The index to start checking from (inclusive).</param>
202  /// <returns>The index of the next set bit.</returns>
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 
215  /// <summary>
216  /// Converts a System.String number to long.
217  /// </summary>
218  /// <param name="s"></param>
219  /// <returns></returns>
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 }