/**************************************************************************** * Copyright 2019 Nreal Techonology Limited. All rights reserved. * * This file is part of NRSDK. * * https://www.nreal.ai/ * *****************************************************************************/ namespace NRKernal { using System; using System.Text; /// A helper to format string. public static class StringUtility { /// The cached string builder. [ThreadStatic] private static StringBuilder s_CachedStringBuilder = null; /// Get format string. /// Thrown when an exception error condition occurs. /// Describes the format to use. /// The argument 0. /// The formatted value. 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(); } /// Get format string. /// Thrown when an exception error condition occurs. /// Describes the format to use. /// The argument 0. /// The first argument. /// The formatted value. 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(); } /// Get format string. /// Thrown when an exception error condition occurs. /// Describes the format to use. /// The argument 0. /// The first argument. /// The second argument. /// The formatted value. 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(); } /// Get format string. /// Thrown when an exception error condition occurs. /// Describes the format to use. /// A variable-length parameters list containing arguments. /// The formatted value. 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(); } /// Get full name by type. /// Generic type parameter. /// The name. /// The full name. public static string GetFullName(string name) { return GetFullName(typeof(T), name); } /// Get full name by type. /// Thrown when an exception error condition occurs. /// The type. /// The name. /// The full 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); } /// Gets byte length string. /// Length of the byte. /// The byte length string. public static string GetByteLengthString(long byteLength) { if (byteLength < 1024L) // 2 ^ 10 { return Format("{0} B", byteLength.ToString()); } if (byteLength < 1048576L) // 2 ^ 20 { return Format("{0} KB", (byteLength / 1024f).ToString("F2")); } if (byteLength < 1073741824L) // 2 ^ 30 { return Format("{0} MB", (byteLength / 1048576f).ToString("F2")); } if (byteLength < 1099511627776L) // 2 ^ 40 { return Format("{0} GB", (byteLength / 1073741824f).ToString("F2")); } if (byteLength < 1125899906842624L) // 2 ^ 50 { return Format("{0} TB", (byteLength / 1099511627776f).ToString("F2")); } if (byteLength < 1152921504606846976L) // 2 ^ 60 { return Format("{0} PB", (byteLength / 1125899906842624f).ToString("F2")); } return Format("{0} EB", (byteLength / 1152921504606846976f).ToString("F2")); } /// Check cached string builder. private static void CheckCachedStringBuilder() { if (s_CachedStringBuilder == null) { s_CachedStringBuilder = new StringBuilder(1024); } } } }