19 using Lucene.Net.Search;
48 [Obsolete(
"For new indexes use NumericUtils instead, which provides a sortable binary representation (prefix encoded) of numeric values. To index and efficiently query numeric values use NumericField and NumericRangeQuery. This class is included for use with existing indices and will be removed in a future release (possibly Lucene 4.0).")]
52 private const int RADIX = 36;
54 private const char NEGATIVE_PREFIX =
'-';
57 private const char POSITIVE_PREFIX =
'0';
61 #if !PRE_LUCENE_NET_2_0_0_COMPATIBLE
62 public static readonly System.String MIN_STRING_VALUE = NEGATIVE_PREFIX +
"0000000000000";
64 public static readonly System.String MIN_STRING_VALUE = NEGATIVE_PREFIX +
"0000000000000000";
68 #if !PRE_LUCENE_NET_2_0_0_COMPATIBLE
69 public static readonly System.String MAX_STRING_VALUE = POSITIVE_PREFIX +
"1y2p0ij32e8e7";
71 public static readonly System.String MAX_STRING_VALUE = POSITIVE_PREFIX +
"7fffffffffffffff";
75 public static readonly
int STR_SIZE = MIN_STRING_VALUE.Length;
78 public static System.String LongToString(
long l)
81 if (l == System.Int64.MinValue)
84 return MIN_STRING_VALUE;
87 System.Text.StringBuilder buf =
new System.Text.StringBuilder(STR_SIZE);
91 buf.Append(NEGATIVE_PREFIX);
92 l = System.Int64.MaxValue + l + 1;
96 buf.Append(POSITIVE_PREFIX);
98 #if !PRE_LUCENE_NET_2_0_0_COMPATIBLE
99 System.String num = ToString(l);
101 System.String num = System.Convert.ToString(l, RADIX);
104 int padLen = STR_SIZE - num.Length - buf.Length;
111 return buf.ToString();
125 public static long StringToLong(System.String str)
129 throw new System.NullReferenceException(
"string cannot be null");
131 if (str.Length != STR_SIZE)
133 throw new System.FormatException(
"string is the wrong size");
136 if (str.Equals(MIN_STRING_VALUE))
138 return System.Int64.MinValue;
141 char prefix = str[0];
142 #if !PRE_LUCENE_NET_2_0_0_COMPATIBLE
143 long l = ToLong(str.Substring(1));
145 long l = System.Convert.ToInt64(str.Substring(1), RADIX);
148 if (prefix == POSITIVE_PREFIX)
152 else if (prefix == NEGATIVE_PREFIX)
154 l = l - System.Int64.MaxValue - 1;
158 throw new System.FormatException(
"string does not begin with the correct prefix");
164 #if !PRE_LUCENE_NET_2_0_0_COMPATIBLE
166 static System.String digits =
"0123456789abcdefghijklmnopqrstuvwxyz";
167 static long[] powersOf36 =
175 36L*36L*36L*36L*36L*36L,
176 36L*36L*36L*36L*36L*36L*36L,
177 36L*36L*36L*36L*36L*36L*36L*36L,
178 36L*36L*36L*36L*36L*36L*36L*36L*36L,
179 36L*36L*36L*36L*36L*36L*36L*36L*36L*36L,
180 36L*36L*36L*36L*36L*36L*36L*36L*36L*36L*36L,
181 36L*36L*36L*36L*36L*36L*36L*36L*36L*36L*36L*36L
184 public static System.String ToString(
long lval)
191 int maxStrLen = powersOf36.Length;
194 char[] tb =
new char[maxStrLen];
196 for (
int i = 0; i < maxStrLen; i++)
198 long pval = powersOf36[maxStrLen - i - 1];
199 int pos = (int)(curval / pval);
200 tb[outpos++] = digits.Substring(pos, 1).ToCharArray()[0];
201 curval = curval % pval;
205 return new System.String(tb, 0, outpos).TrimStart(
'0');
208 public static long ToLong(System.String t)
211 char[] tb = t.ToCharArray();
212 for (
int i = 0; i < tb.Length; i++)
214 ival += powersOf36[i] * digits.IndexOf(tb[tb.Length - i - 1]);