StringUtility.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using System.Text;
  13. /// <summary> A helper to format string. </summary>
  14. public static class StringUtility
  15. {
  16. /// <summary> The cached string builder. </summary>
  17. [ThreadStatic]
  18. private static StringBuilder s_CachedStringBuilder = null;
  19. /// <summary> Get format string. </summary>
  20. /// <exception cref="Exception"> Thrown when an exception error condition occurs.</exception>
  21. /// <param name="format"> Describes the format to use.</param>
  22. /// <param name="arg0"> The argument 0.</param>
  23. /// <returns> The formatted value. </returns>
  24. public static string Format(string format, object arg0)
  25. {
  26. if (format == null)
  27. {
  28. throw new Exception("Format is invalid.");
  29. }
  30. CheckCachedStringBuilder();
  31. s_CachedStringBuilder.Length = 0;
  32. s_CachedStringBuilder.AppendFormat(format, arg0);
  33. return s_CachedStringBuilder.ToString();
  34. }
  35. /// <summary> Get format string. </summary>
  36. /// <exception cref="Exception"> Thrown when an exception error condition occurs.</exception>
  37. /// <param name="format"> Describes the format to use.</param>
  38. /// <param name="arg0"> The argument 0.</param>
  39. /// <param name="arg1"> The first argument.</param>
  40. /// <returns> The formatted value. </returns>
  41. public static string Format(string format, object arg0, object arg1)
  42. {
  43. if (format == null)
  44. {
  45. throw new Exception("Format is invalid.");
  46. }
  47. CheckCachedStringBuilder();
  48. s_CachedStringBuilder.Length = 0;
  49. s_CachedStringBuilder.AppendFormat(format, arg0, arg1);
  50. return s_CachedStringBuilder.ToString();
  51. }
  52. /// <summary> Get format string. </summary>
  53. /// <exception cref="Exception"> Thrown when an exception error condition occurs.</exception>
  54. /// <param name="format"> Describes the format to use.</param>
  55. /// <param name="arg0"> The argument 0.</param>
  56. /// <param name="arg1"> The first argument.</param>
  57. /// <param name="arg2"> The second argument.</param>
  58. /// <returns> The formatted value. </returns>
  59. public static string Format(string format, object arg0, object arg1, object arg2)
  60. {
  61. if (format == null)
  62. {
  63. throw new Exception("Format is invalid.");
  64. }
  65. CheckCachedStringBuilder();
  66. s_CachedStringBuilder.Length = 0;
  67. s_CachedStringBuilder.AppendFormat(format, arg0, arg1, arg2);
  68. return s_CachedStringBuilder.ToString();
  69. }
  70. /// <summary> Get format string. </summary>
  71. /// <exception cref="Exception"> Thrown when an exception error condition occurs.</exception>
  72. /// <param name="format"> Describes the format to use.</param>
  73. /// <param name="args"> A variable-length parameters list containing arguments.</param>
  74. /// <returns> The formatted value. </returns>
  75. public static string Format(string format, params object[] args)
  76. {
  77. if (format == null)
  78. {
  79. throw new Exception("Format is invalid.");
  80. }
  81. if (args == null)
  82. {
  83. throw new Exception("Args is invalid.");
  84. }
  85. CheckCachedStringBuilder();
  86. s_CachedStringBuilder.Length = 0;
  87. s_CachedStringBuilder.AppendFormat(format, args);
  88. return s_CachedStringBuilder.ToString();
  89. }
  90. /// <summary> Get full name by type. </summary>
  91. /// <typeparam name="T"> Generic type parameter.</typeparam>
  92. /// <param name="name"> The name.</param>
  93. /// <returns> The full name. </returns>
  94. public static string GetFullName<T>(string name)
  95. {
  96. return GetFullName(typeof(T), name);
  97. }
  98. /// <summary> Get full name by type. </summary>
  99. /// <exception cref="Exception"> Thrown when an exception error condition occurs.</exception>
  100. /// <param name="type"> The type.</param>
  101. /// <param name="name"> The name.</param>
  102. /// <returns> The full name. </returns>
  103. public static string GetFullName(Type type, string name)
  104. {
  105. if (type == null)
  106. {
  107. throw new Exception("Type is invalid.");
  108. }
  109. string typeName = type.FullName;
  110. return string.IsNullOrEmpty(name) ? typeName : Format("{0}.{1}", typeName, name);
  111. }
  112. /// <summary> Gets byte length string. </summary>
  113. /// <param name="byteLength"> Length of the byte.</param>
  114. /// <returns> The byte length string. </returns>
  115. public static string GetByteLengthString(long byteLength)
  116. {
  117. if (byteLength < 1024L) // 2 ^ 10
  118. {
  119. return Format("{0} B", byteLength.ToString());
  120. }
  121. if (byteLength < 1048576L) // 2 ^ 20
  122. {
  123. return Format("{0} KB", (byteLength / 1024f).ToString("F2"));
  124. }
  125. if (byteLength < 1073741824L) // 2 ^ 30
  126. {
  127. return Format("{0} MB", (byteLength / 1048576f).ToString("F2"));
  128. }
  129. if (byteLength < 1099511627776L) // 2 ^ 40
  130. {
  131. return Format("{0} GB", (byteLength / 1073741824f).ToString("F2"));
  132. }
  133. if (byteLength < 1125899906842624L) // 2 ^ 50
  134. {
  135. return Format("{0} TB", (byteLength / 1099511627776f).ToString("F2"));
  136. }
  137. if (byteLength < 1152921504606846976L) // 2 ^ 60
  138. {
  139. return Format("{0} PB", (byteLength / 1125899906842624f).ToString("F2"));
  140. }
  141. return Format("{0} EB", (byteLength / 1152921504606846976f).ToString("F2"));
  142. }
  143. /// <summary> Check cached string builder. </summary>
  144. private static void CheckCachedStringBuilder()
  145. {
  146. if (s_CachedStringBuilder == null)
  147. {
  148. s_CachedStringBuilder = new StringBuilder(1024);
  149. }
  150. }
  151. }
  152. }