-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemanticVersion.Formatting.cs
396 lines (373 loc) · 18.9 KB
/
SemanticVersion.Formatting.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using System;
using Chasm.Formatting;
using JetBrains.Annotations;
namespace Chasm.SemanticVersioning
{
public sealed partial class SemanticVersion
: ISpanBuildable, ISpanBuildableFormat
#if NET6_0_OR_GREATER
, ISpanFormattable
#else
, IFormattable
#endif
{
[Pure] internal int CalculateLength()
{
int length = 2 + SpanBuilder.CalculateLength((uint)Major)
+ SpanBuilder.CalculateLength((uint)Minor)
+ SpanBuilder.CalculateLength((uint)Patch);
SemverPreRelease[] preReleases = _preReleases;
if (preReleases.Length != 0)
{
length += preReleases.Length;
for (int i = 0; i < preReleases.Length; i++)
length += preReleases[i].CalculateLength();
}
string[] buildMetadata = _buildMetadata;
if (buildMetadata.Length != 0)
{
length += buildMetadata.Length;
for (int i = 0; i < buildMetadata.Length; i++)
length += buildMetadata[i].Length;
}
return length;
}
internal void BuildString(ref SpanBuilder sb)
{
sb.Append((uint)Major);
sb.Append('.');
sb.Append((uint)Minor);
sb.Append('.');
sb.Append((uint)Patch);
SemverPreRelease[] preReleases = _preReleases;
if (preReleases.Length != 0)
{
sb.Append('-');
preReleases[0].BuildString(ref sb);
for (int i = 1; i < preReleases.Length; i++)
{
sb.Append('.');
preReleases[i].BuildString(ref sb);
}
}
string[] buildMetadata = _buildMetadata;
if (buildMetadata.Length != 0)
{
sb.Append('+');
sb.Append(buildMetadata[0].AsSpan());
for (int i = 1; i < buildMetadata.Length; i++)
{
sb.Append('.');
sb.Append(buildMetadata[i].AsSpan());
}
}
}
[Pure] int ISpanBuildable.CalculateLength() => CalculateLength();
void ISpanBuildable.BuildString(ref SpanBuilder sb) => BuildString(ref sb);
[Pure] internal int CalculateLength(ReadOnlySpan<char> format)
{
if (format.IsEmpty) return CalculateLength();
// NOTE: See the BuildString method for comments.
SpanParser parser = new SpanParser(format);
SemverPreRelease[] preReleases = _preReleases;
string[] buildMetadata = _buildMetadata;
int preReleaseIndex = 0;
int buildMetadataIndex = 0;
char separator = '\0';
static void FlushSeparator(ref int length, char separator)
{
if (separator == '\0') return;
length++;
}
int length = 0;
while (parser.CanRead())
{
char read = parser.Read();
switch (read)
{
case 'M':
if (parser.Skip('M')) throw new FormatException();
FlushSeparator(ref length, separator);
length += SpanBuilder.CalculateLength((uint)Major);
break;
case 'm':
if (parser.Skip('m') && Minor == 0 && Patch == 0) break;
FlushSeparator(ref length, separator);
length += SpanBuilder.CalculateLength((uint)Minor);
break;
case 'p':
if (parser.Skip('p') && Patch == 0) break;
FlushSeparator(ref length, separator);
length += SpanBuilder.CalculateLength((uint)Patch);
break;
case 'r':
if (parser.Skip('r'))
{
if (preReleaseIndex < preReleases.Length)
{
FlushSeparator(ref length, separator);
length += preReleases[preReleaseIndex++].CalculateLength();
length += preReleases.Length - preReleaseIndex; // '.' separators
while (preReleaseIndex < preReleases.Length)
length += preReleases[preReleaseIndex++].CalculateLength();
}
break;
}
if (parser.OnAsciiDigit)
{
ReadOnlySpan<char> digits = parser.ReadAsciiDigits();
preReleaseIndex = Utility.ParseNonNegativeInt32(digits);
}
if (preReleaseIndex < preReleases.Length)
{
FlushSeparator(ref length, separator);
length += preReleases[preReleaseIndex++].CalculateLength();
}
break;
case 'd':
if (parser.Skip('d'))
{
if (buildMetadataIndex < buildMetadata.Length)
{
FlushSeparator(ref length, separator);
length += buildMetadata[buildMetadataIndex++].Length;
length += buildMetadata.Length - buildMetadataIndex; // '.' separators
while (buildMetadataIndex < buildMetadata.Length)
length += buildMetadata[buildMetadataIndex++].Length;
}
break;
}
if (parser.OnAsciiDigit)
{
ReadOnlySpan<char> digits = parser.ReadAsciiDigits();
buildMetadataIndex = Utility.ParseNonNegativeInt32(digits);
}
if (buildMetadataIndex < buildMetadata.Length)
{
FlushSeparator(ref length, separator);
length += buildMetadata[buildMetadataIndex++].Length;
}
break;
default:
FlushSeparator(ref length, separator);
switch (read)
{
case '\'' or '"':
ReadOnlySpan<char> escaped = parser.ReadUntil(read);
if (!parser.Skip(read)) throw new FormatException();
length += escaped.Length;
break;
case '.' or '-' or '+' or '_' or ' ':
separator = read;
continue;
case '\\':
if (!parser.CanRead()) throw new FormatException();
parser.Skip();
goto default;
default:
length++;
break;
}
break;
}
separator = '\0';
}
FlushSeparator(ref length, separator);
return length;
}
internal void BuildString(ref SpanBuilder sb, ReadOnlySpan<char> format)
{
if (format.IsEmpty)
{
BuildString(ref sb);
return;
}
SpanParser parser = new SpanParser(format);
SemverPreRelease[] preReleases = _preReleases;
string[] buildMetadata = _buildMetadata;
int preReleaseIndex = 0;
int buildMetadataIndex = 0;
char separator = '\0';
static void FlushSeparator(ref SpanBuilder sb, char separator)
{
if (separator == '\0') return;
sb.Append(separator);
}
while (parser.CanRead())
{
char read = parser.Read();
switch (read)
{
case 'M':
// write the major version component
if (parser.Skip('M')) throw new FormatException(); // dotcover disable this line
FlushSeparator(ref sb, separator);
sb.Append((uint)Major);
break;
case 'm':
// write the minor version component (maybe optional)
if (parser.Skip('m') && Minor == 0 && Patch == 0) break;
FlushSeparator(ref sb, separator);
sb.Append((uint)Minor);
break;
case 'p':
// write the patch version component (maybe optional)
if (parser.Skip('p') && Patch == 0) break;
FlushSeparator(ref sb, separator);
sb.Append((uint)Patch);
break;
case 'r':
if (parser.Skip('r'))
{
// write all remaining pre-releases after the last one written
if (preReleaseIndex < preReleases.Length)
{
FlushSeparator(ref sb, separator);
preReleases[preReleaseIndex++].BuildString(ref sb);
while (preReleaseIndex < preReleases.Length)
{
sb.Append('.');
preReleases[preReleaseIndex++].BuildString(ref sb);
}
}
break;
}
// optionally set the next pre-release index
if (parser.OnAsciiDigit)
{
ReadOnlySpan<char> digits = parser.ReadAsciiDigits();
preReleaseIndex = Utility.ParseNonNegativeInt32(digits);
}
// write the next pre-release
if (preReleaseIndex < preReleases.Length)
{
FlushSeparator(ref sb, separator);
preReleases[preReleaseIndex++].BuildString(ref sb);
}
break;
case 'd':
if (parser.Skip('d'))
{
// write all remaining build metadata after the last one written
if (buildMetadataIndex < buildMetadata.Length)
{
FlushSeparator(ref sb, separator);
sb.Append(buildMetadata[buildMetadataIndex++].AsSpan());
while (buildMetadataIndex < buildMetadata.Length)
{
sb.Append('.');
sb.Append(buildMetadata[buildMetadataIndex++].AsSpan());
}
}
break;
}
// optionally set the next build metadata index
if (parser.OnAsciiDigit)
{
ReadOnlySpan<char> digits = parser.ReadAsciiDigits();
buildMetadataIndex = Utility.ParseNonNegativeInt32(digits);
}
// write the next build metadata
if (buildMetadataIndex < buildMetadata.Length)
{
FlushSeparator(ref sb, separator);
sb.Append(buildMetadata[buildMetadataIndex++].AsSpan());
}
break;
default:
// flush separator (the following format characters don't remove preceding separators)
FlushSeparator(ref sb, separator);
switch (read)
{
case '\'' or '"':
// append the text enclosed in quotes (quote characters can't be escaped inside)
ReadOnlySpan<char> escaped = parser.ReadUntil(read);
if (!parser.Skip(read)) throw new FormatException(); // dotcover disable this line
sb.Append(escaped);
break;
case '\\':
// append the following character as is
if (!parser.CanRead()) throw new FormatException(); // dotcover disable this line
sb.Append(parser.Read());
break;
case '.' or '-' or '+' or '_' or ' ':
// set the separator character and continue (skipping separator reset)
separator = read;
continue;
default:
// not a format character, append as is
sb.Append(read);
break;
}
break;
}
// reset separator
separator = '\0';
}
FlushSeparator(ref sb, separator);
}
[Pure] int ISpanBuildableFormat.CalculateLength(ReadOnlySpan<char> format) => CalculateLength(format);
void ISpanBuildableFormat.BuildString(ref SpanBuilder sb, ReadOnlySpan<char> format) => BuildString(ref sb, format);
/// <summary>
/// <para>Returns the SemVer 2.0.0 compliant string representation of this semantic version.</para>
/// </summary>
/// <returns>The SemVer 2.0.0 compliant string representation of this semantic version.</returns>
[Pure] public override string ToString() => SpanBuilder.Format(this);
/// <summary>
/// <para>Converts this semantic version to its equivalent string representation, using the specified <paramref name="format"/>.</para>
/// </summary>
/// <remarks>
/// <para>
/// <c>M</c>, <c>m</c>, <c>p</c> - major/minor/patch version components.<br/>
/// <c>rr</c> - all pre-release identifiers, <c>dd</c> - all build metadata identifiers.<br/>
/// The standard, SemVer 2.0.0, format is <c>M.m.p-rr+dd</c>.<br/>
/// </para>
/// <para>
/// <c>mm</c> - optional minor component, which is omitted if both minor and patch components are zero.<br/>
/// <c>pp</c> - optional patch component, which is omitted if it's zero.<br/>
/// <c>r</c>, <c>d</c> - the next pre-release/build metadata identifier, that comes after the last one specified.<br/>
/// <c>r0</c>, <c>r1</c>, …, <c>d0</c>, <c>d1</c>, … - the pre-release/build metadata identifier at the specified index.<br/>
/// <c>rr</c>, <c>dd</c> - all of the pre-release/build metadata identifiers that come after the last specified identifier.<br/>
/// </para>
/// <para>
/// <c>\Ma\jo\r</c> - backslash-escaped character. Backslash itself can be escaped as well.<br/>
/// <c>'map'</c>, <c>"Arr!"</c> - quote-escaped sequence of characters. Backslash can't escape closing quote characters.<br/>
/// <c>.</c>, <c>-</c>, <c>+</c>, <c>_</c>, <c> </c> - separator characters. When preceding an omitted identifier, the separator is omitted as well.<br/>
/// </para>
/// <para>
/// <a href="https://github.com/Chasmical/Chasm/tree/main/Chasm.SemanticVersioning#advanced-semanticversion-formatting">See more details and examples in Chasm.SemanticVersioning README.</a><br/>
/// </para>
/// </remarks>
/// <param name="format">The format to use.</param>
/// <returns>The string representation of this semantic version, as specified by <paramref name="format"/>.</returns>
/// <exception cref="FormatException">TODO</exception>
[Pure] public string ToString(string? format) => ToString(format.AsSpan());
/// <inheritdoc cref="ToString(string?)"/>
[Pure] public string ToString(ReadOnlySpan<char> format) => SpanBuilder.Format(this, format);
/// <summary>
/// <para>Tries to format this semantic version into the provided span of characters.</para>
/// </summary>
/// <param name="destination">When this method returns, this semantic version formatted as a span of characters.</param>
/// <param name="charsWritten">When this method returns, the number of characters that were written in <paramref name="destination"/>.</param>
/// <returns><see langword="true"/>, if the formatting was successful; otherwise, <see langword="false"/>.</returns>
[Pure] public bool TryFormat(Span<char> destination, out int charsWritten)
=> SpanBuilder.TryFormat(this, destination, out charsWritten);
/// <summary>
/// <para>Tries to format this semantic version into the provided span of characters.</para>
/// </summary>
/// <remarks><inheritdoc cref="ToString(string?)" path='/remarks' /></remarks>
/// <param name="destination">When this method returns, this semantic version formatted as a span of characters.</param>
/// <param name="charsWritten">When this method returns, the number of characters that were written in <paramref name="destination"/>.</param>
/// <param name="format">The format to use.</param>
/// <returns><see langword="true"/>, if the formatting was successful; otherwise, <see langword="false"/>.</returns>
/// <exception cref="FormatException">TODO</exception>
[Pure] public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format)
=> SpanBuilder.TryFormat(this, destination, out charsWritten, format);
[Pure] string IFormattable.ToString(string? format, IFormatProvider? _)
=> ToString(format);
#if NET6_0_OR_GREATER
[Pure] bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? _)
=> TryFormat(destination, out charsWritten, format);
#endif
}
}