📄 supportclass.cs
字号:
{
if (s == null)
{
throw new ArgumentException("null");
}
if (radix < MIN_RADIX)
{
throw new NotSupportedException("radix " + radix +
" less than Number.MIN_RADIX");
}
if (radix > MAX_RADIX)
{
throw new NotSupportedException("radix " + radix +
" greater than Number.MAX_RADIX");
}
long result = 0;
long mult = 1;
s = s.ToLower();
for (int i = s.Length - 1; i >= 0; i--)
{
int weight = digits.IndexOf(s[i]);
if (weight == -1)
throw new FormatException("Invalid number for the specified radix");
result += (weight * mult);
mult *= radix;
}
return result;
}
/// <summary>
/// Performs an unsigned bitwise right shift with the specified number
/// </summary>
/// <param name="number">Number to operate on</param>
/// <param name="bits">Ammount of bits to shift</param>
/// <returns>The resulting number from the shift operation</returns>
public static int URShift(int number, int bits)
{
if (number >= 0)
return number >> bits;
else
return (number >> bits) + (2 << ~bits);
}
/// <summary>
/// Performs an unsigned bitwise right shift with the specified number
/// </summary>
/// <param name="number">Number to operate on</param>
/// <param name="bits">Ammount of bits to shift</param>
/// <returns>The resulting number from the shift operation</returns>
public static long URShift(long number, int bits)
{
if (number >= 0)
return number >> bits;
else
return (number >> bits) + (2 << ~bits);
}
/// <summary>
/// Returns the index of the first bit that is set to true that occurs
/// on or after the specified starting index. If no such bit exists
/// then -1 is returned.
/// </summary>
/// <param name="bits">The BitArray object.</param>
/// <param name="fromIndex">The index to start checking from (inclusive).</param>
/// <returns>The index of the next set bit.</returns>
public static int NextSetBit(System.Collections.BitArray bits, int fromIndex)
{
for (int i = fromIndex; i < bits.Length; i++)
{
if (bits[i] == true)
{
return i;
}
}
return -1;
}
}
/// <summary>
/// Mimics Java's Character class.
/// </summary>
public class Character
{
private const char charNull= '\0';
private const char charZero = '0';
private const char charA = 'a';
/// <summary>
/// </summary>
public static int MAX_RADIX
{
get
{
return 36;
}
}
/// <summary>
/// </summary>
public static int MIN_RADIX
{
get
{
return 2;
}
}
/// <summary>
///
/// </summary>
/// <param name="digit"></param>
/// <param name="radix"></param>
/// <returns></returns>
public static char ForDigit(int digit, int radix)
{
// if radix or digit is out of range,
// return the null character.
if (radix < Character.MIN_RADIX)
return charNull;
if (radix > Character.MAX_RADIX)
return charNull;
if (digit < 0)
return charNull;
if (digit >= radix)
return charNull;
// if digit is less than 10,
// return '0' plus digit
if (digit < 10)
return (char) ( (int) charZero + digit);
// otherwise, return 'a' plus digit.
return (char) ((int) charA + digit - 10);
}
}
/// <summary>
///
/// </summary>
public class Date
{
/// <summary>
///
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
static public long GetTime(DateTime dateTime)
{
TimeSpan ts = dateTime.Subtract(new DateTime(1970, 1, 1));
ts = ts.Subtract(TimeZone.CurrentTimeZone.GetUtcOffset(dateTime));
return ts.Ticks / TimeSpan.TicksPerMillisecond;
}
}
/// <summary>
///
/// </summary>
public class Single
{
/// <summary>
///
/// </summary>
/// <param name="s"></param>
/// <param name="style"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static System.Single Parse(System.String s, System.Globalization.NumberStyles style, System.IFormatProvider provider)
{
try
{
if (s.EndsWith("f") || s.EndsWith("F"))
return System.Single.Parse(s.Substring(0, s.Length - 1), style, provider);
else
return System.Single.Parse(s, style, provider);
}
catch (System.FormatException fex)
{
throw fex;
}
}
/// <summary>
///
/// </summary>
/// <param name="s"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static System.Single Parse(System.String s, System.IFormatProvider provider)
{
try
{
if (s.EndsWith("f") || s.EndsWith("F"))
return System.Single.Parse(s.Substring(0, s.Length - 1), provider);
else
return System.Single.Parse(s, provider);
}
catch (System.FormatException fex)
{
throw fex;
}
}
/// <summary>
///
/// </summary>
/// <param name="s"></param>
/// <param name="style"></param>
/// <returns></returns>
public static System.Single Parse(System.String s, System.Globalization.NumberStyles style)
{
try
{
if (s.EndsWith("f") || s.EndsWith("F"))
return System.Single.Parse(s.Substring(0, s.Length - 1), style);
else
return System.Single.Parse(s, style);
}
catch(System.FormatException fex)
{
throw fex;
}
}
/// <summary>
///
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static System.Single Parse(System.String s)
{
try
{
if (s.EndsWith("f") || s.EndsWith("F"))
return System.Single.Parse(s.Substring(0, s.Length - 1));
else
return System.Single.Parse(s);
}
catch(System.FormatException fex)
{
throw fex;
}
}
}
/// <summary>
///
/// </summary>
public class AppSettings
{
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <param name="defValue"></param>
/// <returns></returns>
public static int Get(System.String key, int defValue)
{
System.String theValue = System.Configuration.ConfigurationSettings.AppSettings.Get(key);
if (theValue == null)
{
return defValue;
}
return System.Convert.ToInt16(theValue.Trim());
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <param name="defValue"></param>
/// <returns></returns>
public static long Get(System.String key, long defValue)
{
System.String theValue = System.Configuration.ConfigurationSettings.AppSettings.Get(key);
if (theValue == null)
{
return defValue;
}
return System.Convert.ToInt32(theValue.Trim());
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <param name="defValue"></param>
/// <returns></returns>
public static System.String Get(System.String key, System.String defValue)
{
System.String theValue = System.Configuration.ConfigurationSettings.AppSettings.Get(key);
if (theValue == null)
{
return defValue;
}
return theValue;
}
}
public static System.Collections.SortedList TailMap(System.Collections.SortedList list, System.Object limit)
{
System.Collections.Comparer comparer = System.Collections.Comparer.Default;
System.Collections.SortedList newList = new System.Collections.SortedList();
if (list != null)
{
if (list.Count > 0)
{
int index = 0;
while (comparer.Compare(list.GetKey(index), limit) < 0)
index++;
for (; index < list.Count; index++)
newList.Add(list.GetKey(index), list[list.GetKey(index)]);
}
}
return newList;
}
/// <summary>
/// Use for .NET 1.1 Framework only.
/// </summary>
public class CompressionSupport
{
public interface ICompressionAdapter
{
byte[] Compress(byte[] input);
byte[] Uncompress(byte[] input);
}
private static ICompressionAdapter compressionAdapter;
public static byte[] Uncompress(byte[] input)
{
CheckCompressionSupport();
return compressionAdapter.Uncompress(input);
}
public static byte[] Compress(byte[] input)
{
CheckCompressionSupport();
return compressionAdapter.Compress(input);
}
private static void CheckCompressionSupport()
{
if (compressionAdapter == null)
{
System.String compressionLibClassName = SupportClass.AppSettings.Get("Lucene.Net.CompressionLib.class", null);
if (compressionLibClassName == null)
throw new System.SystemException("Compression support not configured");
Type compressionLibClass = Type.GetType(compressionLibClassName, true);
System.Object compressionAdapterObj = Activator.CreateInstance(compressionLibClass);
compressionAdapter = compressionAdapterObj as ICompressionAdapter;
if (compressionAdapter == null)
throw new System.SystemException("Compression adapter does not support the ICompressionAdapter interface");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -