| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | |
| | | 8 | | namespace System.Text |
| | | 9 | | { |
| | | 10 | | internal ref partial struct ValueStringBuilder |
| | | 11 | | { |
| | | 12 | | // Copied from StringBuilder, can't be done via generic extension |
| | | 13 | | // as ValueStringBuilder is a ref struct and cannot be used in a generic. |
| | | 14 | | internal void AppendFormatHelper(IFormatProvider? provider, string format, ReadOnlySpan<object?> args) |
| | | 15 | | { |
| | 0 | 16 | | ArgumentNullException.ThrowIfNull(format); |
| | | 17 | | |
| | | 18 | | // Undocumented exclusive limits on the range for Argument Hole Index and Argument Hole Alignment. |
| | | 19 | | const int IndexLimit = 1_000_000; // Note: 0 <= ArgIndex < IndexLimit |
| | | 20 | | const int WidthLimit = 1_000_000; // Note: -WidthLimit < ArgAlign < WidthLimit |
| | | 21 | | |
| | | 22 | | // Query the provider (if one was supplied) for an ICustomFormatter. If there is one, |
| | | 23 | | // it needs to be used to transform all arguments. |
| | 0 | 24 | | ICustomFormatter? cf = (ICustomFormatter?)provider?.GetFormat(typeof(ICustomFormatter)); |
| | | 25 | | |
| | | 26 | | // Repeatedly find the next hole and process it. |
| | 0 | 27 | | int pos = 0; |
| | | 28 | | char ch; |
| | 0 | 29 | | while (true) |
| | | 30 | | { |
| | | 31 | | // Skip until either the end of the input or the first unescaped opening brace, whichever comes first. |
| | | 32 | | // Along the way we need to also unescape escaped closing braces. |
| | | 33 | | while (true) |
| | | 34 | | { |
| | | 35 | | // Find the next brace. If there isn't one, the remainder of the input is text to be appended, and |
| | 0 | 36 | | if ((uint)pos >= (uint)format.Length) |
| | | 37 | | { |
| | 0 | 38 | | return; |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | ReadOnlySpan<char> remainder = format.AsSpan(pos); |
| | 0 | 42 | | int countUntilNextBrace = remainder.IndexOfAny('{', '}'); |
| | 0 | 43 | | if (countUntilNextBrace < 0) |
| | | 44 | | { |
| | 0 | 45 | | Append(remainder); |
| | 0 | 46 | | return; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | // Append the text until the brace. |
| | 0 | 50 | | Append(remainder.Slice(0, countUntilNextBrace)); |
| | 0 | 51 | | pos += countUntilNextBrace; |
| | | 52 | | |
| | | 53 | | // Get the brace. It must be followed by another character, either a copy of itself in the case of |
| | | 54 | | // escaped, or an arbitrary character that's part of the hole in the case of an opening brace. |
| | 0 | 55 | | char brace = format[pos]; |
| | 0 | 56 | | ch = MoveNext(format, ref pos); |
| | 0 | 57 | | if (brace == ch) |
| | | 58 | | { |
| | 0 | 59 | | Append(ch); |
| | 0 | 60 | | pos++; |
| | 0 | 61 | | continue; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | // This wasn't an escape, so it must be an opening brace. |
| | 0 | 65 | | if (brace != '{') |
| | | 66 | | { |
| | 0 | 67 | | ThrowHelper.ThrowFormatInvalidString(pos, ExceptionResource.Format_UnexpectedClosingBrace); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | // Proceed to parse the hole. |
| | | 71 | | break; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | // We're now positioned just after the opening brace of an argument hole, which consists of |
| | | 75 | | // an opening brace, an index, an optional width preceded by a comma, and an optional format |
| | | 76 | | // preceded by a colon, with arbitrary amounts of spaces throughout. |
| | 0 | 77 | | int width = 0; |
| | 0 | 78 | | bool leftJustify = false; |
| | 0 | 79 | | ReadOnlySpan<char> itemFormatSpan = default; // used if itemFormat is null |
| | | 80 | | |
| | | 81 | | // First up is the index parameter, which is of the form: |
| | | 82 | | // at least on digit |
| | | 83 | | // optional any number of spaces |
| | | 84 | | // We've already read the first digit into ch. |
| | 0 | 85 | | Debug.Assert(format[pos - 1] == '{'); |
| | 0 | 86 | | Debug.Assert(ch != '{'); |
| | 0 | 87 | | int index = ch - '0'; |
| | 0 | 88 | | if ((uint)index >= 10u) |
| | | 89 | | { |
| | 0 | 90 | | ThrowHelper.ThrowFormatInvalidString(pos, ExceptionResource.Format_ExpectedAsciiDigit); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | // Common case is a single digit index followed by a closing brace. If it's not a closing brace, |
| | | 94 | | // proceed to finish parsing the full hole format. |
| | 0 | 95 | | ch = MoveNext(format, ref pos); |
| | 0 | 96 | | if (ch != '}') |
| | | 97 | | { |
| | | 98 | | // Continue consuming optional additional digits. |
| | 0 | 99 | | while (char.IsAsciiDigit(ch) && index < IndexLimit) |
| | | 100 | | { |
| | 0 | 101 | | index = index * 10 + ch - '0'; |
| | 0 | 102 | | ch = MoveNext(format, ref pos); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | // Consume optional whitespace. |
| | 0 | 106 | | while (ch == ' ') |
| | | 107 | | { |
| | 0 | 108 | | ch = MoveNext(format, ref pos); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | // Parse the optional alignment, which is of the form: |
| | | 112 | | // comma |
| | | 113 | | // optional any number of spaces |
| | | 114 | | // optional - |
| | | 115 | | // at least one digit |
| | | 116 | | // optional any number of spaces |
| | 0 | 117 | | if (ch == ',') |
| | | 118 | | { |
| | | 119 | | // Consume optional whitespace. |
| | | 120 | | do |
| | | 121 | | { |
| | 0 | 122 | | ch = MoveNext(format, ref pos); |
| | | 123 | | } |
| | 0 | 124 | | while (ch == ' '); |
| | | 125 | | |
| | | 126 | | // Consume an optional minus sign indicating left alignment. |
| | 0 | 127 | | if (ch == '-') |
| | | 128 | | { |
| | 0 | 129 | | leftJustify = true; |
| | 0 | 130 | | ch = MoveNext(format, ref pos); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | // Parse alignment digits. The read character must be a digit. |
| | 0 | 134 | | width = ch - '0'; |
| | 0 | 135 | | if ((uint)width >= 10u) |
| | | 136 | | { |
| | 0 | 137 | | ThrowHelper.ThrowFormatInvalidString(pos, ExceptionResource.Format_ExpectedAsciiDigit); |
| | | 138 | | } |
| | 0 | 139 | | ch = MoveNext(format, ref pos); |
| | 0 | 140 | | while (char.IsAsciiDigit(ch) && width < WidthLimit) |
| | | 141 | | { |
| | 0 | 142 | | width = width * 10 + ch - '0'; |
| | 0 | 143 | | ch = MoveNext(format, ref pos); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | // Consume optional whitespace |
| | 0 | 147 | | while (ch == ' ') |
| | | 148 | | { |
| | 0 | 149 | | ch = MoveNext(format, ref pos); |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | // The next character needs to either be a closing brace for the end of the hole, |
| | | 154 | | // or a colon indicating the start of the format. |
| | 0 | 155 | | if (ch != '}') |
| | | 156 | | { |
| | 0 | 157 | | if (ch != ':') |
| | | 158 | | { |
| | | 159 | | // Unexpected character |
| | 0 | 160 | | ThrowHelper.ThrowFormatInvalidString(pos, ExceptionResource.Format_UnclosedFormatItem); |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | // Search for the closing brace; everything in between is the format, |
| | | 164 | | // but opening braces aren't allowed. |
| | 0 | 165 | | int startingPos = pos; |
| | | 166 | | while (true) |
| | | 167 | | { |
| | 0 | 168 | | ch = MoveNext(format, ref pos); |
| | | 169 | | |
| | 0 | 170 | | if (ch == '}') |
| | | 171 | | { |
| | | 172 | | // Argument hole closed |
| | | 173 | | break; |
| | | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | if (ch == '{') |
| | | 177 | | { |
| | | 178 | | // Braces inside the argument hole are not supported |
| | 0 | 179 | | ThrowHelper.ThrowFormatInvalidString(pos, ExceptionResource.Format_UnclosedFormatItem); |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | startingPos++; |
| | 0 | 184 | | itemFormatSpan = format.AsSpan(startingPos, pos - startingPos); |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | // Construct the output for this arg hole. |
| | 0 | 189 | | Debug.Assert(format[pos] == '}'); |
| | 0 | 190 | | pos++; |
| | 0 | 191 | | string? s = null; |
| | 0 | 192 | | string? itemFormat = null; |
| | | 193 | | |
| | 0 | 194 | | if ((uint)index >= (uint)args.Length) |
| | | 195 | | { |
| | 0 | 196 | | ThrowHelper.ThrowFormatIndexOutOfRange(); |
| | | 197 | | } |
| | 0 | 198 | | object? arg = args[index]; |
| | | 199 | | |
| | 0 | 200 | | if (cf != null) |
| | | 201 | | { |
| | 0 | 202 | | if (!itemFormatSpan.IsEmpty) |
| | | 203 | | { |
| | 0 | 204 | | itemFormat = new string(itemFormatSpan); |
| | | 205 | | } |
| | | 206 | | |
| | 0 | 207 | | s = cf.Format(itemFormat, arg, provider); |
| | | 208 | | } |
| | | 209 | | |
| | 0 | 210 | | if (s == null) |
| | | 211 | | { |
| | | 212 | | // If arg is ISpanFormattable and the beginning doesn't need padding, |
| | | 213 | | // try formatting it into the remaining current chunk. |
| | 0 | 214 | | if ((leftJustify || width == 0) && |
| | 0 | 215 | | arg is ISpanFormattable spanFormattableArg && |
| | 0 | 216 | | spanFormattableArg.TryFormat(_chars.Slice(_pos), out int charsWritten, itemFormatSpan, provider) |
| | | 217 | | { |
| | 0 | 218 | | _pos += charsWritten; |
| | | 219 | | |
| | | 220 | | // Pad the end, if needed. |
| | 0 | 221 | | if (leftJustify && width > charsWritten) |
| | | 222 | | { |
| | 0 | 223 | | Append(' ', width - charsWritten); |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | // Continue to parse other characters. |
| | 0 | 227 | | continue; |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | // Otherwise, fallback to trying IFormattable or calling ToString. |
| | 0 | 231 | | if (arg is IFormattable formattableArg) |
| | | 232 | | { |
| | 0 | 233 | | if (itemFormatSpan.Length != 0) |
| | | 234 | | { |
| | 0 | 235 | | itemFormat ??= new string(itemFormatSpan); |
| | | 236 | | } |
| | 0 | 237 | | s = formattableArg.ToString(itemFormat, provider); |
| | | 238 | | } |
| | | 239 | | else |
| | | 240 | | { |
| | 0 | 241 | | s = arg?.ToString(); |
| | | 242 | | } |
| | | 243 | | |
| | 0 | 244 | | s ??= string.Empty; |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | // Append it to the final output of the Format String. |
| | 0 | 248 | | if (width <= s.Length) |
| | | 249 | | { |
| | 0 | 250 | | Append(s); |
| | | 251 | | } |
| | 0 | 252 | | else if (leftJustify) |
| | | 253 | | { |
| | 0 | 254 | | Append(s); |
| | 0 | 255 | | Append(' ', width - s.Length); |
| | | 256 | | } |
| | | 257 | | else |
| | | 258 | | { |
| | 0 | 259 | | Append(' ', width - s.Length); |
| | 0 | 260 | | Append(s); |
| | | 261 | | } |
| | | 262 | | |
| | | 263 | | // Continue parsing the rest of the format string. |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 267 | | static char MoveNext(string format, ref int pos) |
| | | 268 | | { |
| | 0 | 269 | | pos++; |
| | 0 | 270 | | if ((uint)pos >= (uint)format.Length) |
| | | 271 | | { |
| | 0 | 272 | | ThrowHelper.ThrowFormatInvalidString(pos, ExceptionResource.Format_UnclosedFormatItem); |
| | | 273 | | } |
| | 0 | 274 | | return format[pos]; |
| | | 275 | | } |
| | | 276 | | } |
| | | 277 | | } |
| | | 278 | | } |
| | | 279 | | |