PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/corlib/System/StringHelper.cs

https://bitbucket.org/cosi2/dotnetanywhere-wb
C# | 163 lines | 135 code | 14 blank | 14 comment | 53 complexity | 79f5ea5b12eea154c82c876173dada71 MD5 | raw file
  1. #if LOCALTEST
  2. using System;
  3. using System_.Text;
  4. namespace System_ {
  5. #else
  6. using System.Text;
  7. namespace System {
  8. #endif
  9. internal static class StringHelper {
  10. private static int ParseDecimal(string str, ref int ptr) {
  11. int p = ptr;
  12. int n = 0;
  13. while (true) {
  14. char c = str[p];
  15. if (c < '0' || c > '9') {
  16. break;
  17. }
  18. n = n * 10 + c - '0';
  19. ++p;
  20. }
  21. if (p == ptr) {
  22. return -1;
  23. }
  24. ptr = p;
  25. return n;
  26. }
  27. private static void ParseFormatSpecifier(string str, ref int ptr, out int n, out int width, out bool leftAlign, out string format) {
  28. // parses format specifier of form:
  29. // N,[\ +[-]M][:F]}
  30. //
  31. // where:
  32. try {
  33. // N = argument number (non-negative integer)
  34. n = ParseDecimal(str, ref ptr);
  35. if (n < 0) {
  36. throw new FormatException("Input string was not in a correct format.");
  37. }
  38. // M = width (non-negative integer)
  39. if (str[ptr] == ',') {
  40. // White space between ',' and number or sign.
  41. ++ptr;
  42. while (char.IsWhiteSpace(str[ptr])) {
  43. ++ptr;
  44. }
  45. int start = ptr;
  46. format = str.Substring(start, ptr - start);
  47. leftAlign = (str[ptr] == '-');
  48. if (leftAlign) {
  49. ++ptr;
  50. }
  51. width = ParseDecimal(str, ref ptr);
  52. if (width < 0) {
  53. throw new FormatException("Input string was not in a correct format.");
  54. }
  55. } else {
  56. width = 0;
  57. leftAlign = false;
  58. format = string.Empty;
  59. }
  60. // F = argument format (string)
  61. if (str[ptr] == ':') {
  62. int start = ++ptr;
  63. while (str[ptr] != '}') {
  64. ++ptr;
  65. }
  66. format += str.Substring(start, ptr - start);
  67. } else
  68. format = null;
  69. if (str[ptr++] != '}') {
  70. throw new FormatException("Input string was not in a correct format.");
  71. }
  72. } catch (IndexOutOfRangeException) {
  73. throw new FormatException("Input string was not in a correct format.");
  74. }
  75. }
  76. internal static void FormatHelper(StringBuilder result, IFormatProvider provider, string format, params object[] args) {
  77. if (format == null || args == null) {
  78. throw new ArgumentNullException();
  79. }
  80. int ptr = 0, start = 0, formatLen = format.Length;
  81. while (ptr < formatLen) {
  82. char c = format[ptr++];
  83. if (c == '{') {
  84. result.Append(format, start, ptr - start - 1);
  85. if (format[ptr] == '{') {
  86. // If a "{{" is found then it's not a format specifier, so just continue
  87. start = ptr++;
  88. continue;
  89. }
  90. // Start of format specifier, so parse it
  91. int n, width;
  92. bool leftAlign;
  93. string argFormat;
  94. ParseFormatSpecifier (format, ref ptr, out n, out width, out leftAlign, out argFormat);
  95. if (n >= args.Length) {
  96. throw new FormatException("Index (zero based) must be greater than or equal to zero and less than the size of the argument list.");
  97. }
  98. // Format the argument
  99. object arg = args[n];
  100. string str;
  101. ICustomFormatter formatter = null;
  102. if (provider != null) {
  103. formatter = provider.GetFormat(typeof(ICustomFormatter)) as ICustomFormatter;
  104. }
  105. if (arg == null) {
  106. str = string.Empty;
  107. } else if (formatter != null) {
  108. str = formatter.Format(argFormat, arg, provider);
  109. } else if (arg is IFormattable) {
  110. str = ((IFormattable)arg).ToString(argFormat, provider);
  111. } else {
  112. try
  113. {
  114. str = arg.ToString();
  115. }
  116. catch { str = "<Exception>"; }
  117. if (str == null) str = String.Empty;
  118. }
  119. // Apply any padding needed and append to result
  120. if (width > str.Length) {
  121. int padLen = width - str.Length;
  122. if (leftAlign) {
  123. result.Append(str);
  124. result.Append(' ', padLen);
  125. } else {
  126. result.Append(' ', padLen);
  127. result.Append(str);
  128. }
  129. } else {
  130. result.Append(str);
  131. }
  132. start = ptr;
  133. } else if (c == '}') {
  134. if (ptr < formatLen && format[ptr] == '}') {
  135. // Handle case of "}}" appearing in the string
  136. result.Append(format, start, ptr - start - 1);
  137. start = ptr++;
  138. } else {
  139. // single "}" not allowed without "{" beforehand
  140. throw new FormatException("Input string was not of the correct format.");
  141. }
  142. }
  143. }
  144. if (start < formatLen) {
  145. result.Append(format, start, formatLen - start);
  146. }
  147. }
  148. }
  149. }