123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
-
- namespace NRKernal
- {
- using System;
- using System.Text;
-
- public static class StringUtility
- {
-
- [ThreadStatic]
- private static StringBuilder s_CachedStringBuilder = null;
-
-
-
-
-
- public static string Format(string format, object arg0)
- {
- if (format == null)
- {
- throw new Exception("Format is invalid.");
- }
- CheckCachedStringBuilder();
- s_CachedStringBuilder.Length = 0;
- s_CachedStringBuilder.AppendFormat(format, arg0);
- return s_CachedStringBuilder.ToString();
- }
-
-
-
-
-
-
- public static string Format(string format, object arg0, object arg1)
- {
- if (format == null)
- {
- throw new Exception("Format is invalid.");
- }
- CheckCachedStringBuilder();
- s_CachedStringBuilder.Length = 0;
- s_CachedStringBuilder.AppendFormat(format, arg0, arg1);
- return s_CachedStringBuilder.ToString();
- }
-
-
-
-
-
-
-
- public static string Format(string format, object arg0, object arg1, object arg2)
- {
- if (format == null)
- {
- throw new Exception("Format is invalid.");
- }
- CheckCachedStringBuilder();
- s_CachedStringBuilder.Length = 0;
- s_CachedStringBuilder.AppendFormat(format, arg0, arg1, arg2);
- return s_CachedStringBuilder.ToString();
- }
-
-
-
-
-
- public static string Format(string format, params object[] args)
- {
- if (format == null)
- {
- throw new Exception("Format is invalid.");
- }
- if (args == null)
- {
- throw new Exception("Args is invalid.");
- }
- CheckCachedStringBuilder();
- s_CachedStringBuilder.Length = 0;
- s_CachedStringBuilder.AppendFormat(format, args);
- return s_CachedStringBuilder.ToString();
- }
-
-
-
-
- public static string GetFullName<T>(string name)
- {
- return GetFullName(typeof(T), name);
- }
-
-
-
-
-
- public static string GetFullName(Type type, string name)
- {
- if (type == null)
- {
- throw new Exception("Type is invalid.");
- }
- string typeName = type.FullName;
- return string.IsNullOrEmpty(name) ? typeName : Format("{0}.{1}", typeName, name);
- }
-
-
-
- public static string GetByteLengthString(long byteLength)
- {
- if (byteLength < 1024L)
- {
- return Format("{0} B", byteLength.ToString());
- }
- if (byteLength < 1048576L)
- {
- return Format("{0} KB", (byteLength / 1024f).ToString("F2"));
- }
- if (byteLength < 1073741824L)
- {
- return Format("{0} MB", (byteLength / 1048576f).ToString("F2"));
- }
- if (byteLength < 1099511627776L)
- {
- return Format("{0} GB", (byteLength / 1073741824f).ToString("F2"));
- }
- if (byteLength < 1125899906842624L)
- {
- return Format("{0} TB", (byteLength / 1099511627776f).ToString("F2"));
- }
- if (byteLength < 1152921504606846976L)
- {
- return Format("{0} PB", (byteLength / 1125899906842624f).ToString("F2"));
- }
- return Format("{0} EB", (byteLength / 1152921504606846976f).ToString("F2"));
- }
-
- private static void CheckCachedStringBuilder()
- {
- if (s_CachedStringBuilder == null)
- {
- s_CachedStringBuilder = new StringBuilder(1024);
- }
- }
- }
- }
|