-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemverPreRelease.Formatting.cs
60 lines (55 loc) · 1.99 KB
/
SemverPreRelease.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
using System;
using Chasm.Formatting;
using JetBrains.Annotations;
namespace Chasm.SemanticVersioning
{
public readonly partial struct SemverPreRelease
#if NET6_0_OR_GREATER
: ISpanFormattable
#else
: IFormattable
#endif
{
[Pure] internal int CalculateLength()
=> text?.Length ?? SpanBuilder.CalculateLength(number);
internal void BuildString(ref SpanBuilder sb)
{
if (text is not null) sb.Append(text.AsSpan());
else sb.Append(number);
}
/// <summary>
/// <para>Returns the string representation of this pre-release identifier.</para>
/// </summary>
/// <returns>The string representation of this pre-release identifier.</returns>
[Pure] public override string ToString()
=> text ?? number.ToString();
/// <inheritdoc cref="ISpanFormattable.TryFormat"/>
[Pure] public bool TryFormat(Span<char> destination, out int charsWritten)
{
string? str = text;
if (str is not null)
{
#if NET6_0_OR_GREATER
bool res = str.TryCopyTo(destination);
#elif NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
bool res = ((ReadOnlySpan<char>)str).TryCopyTo(destination);
#else
bool res = str.AsSpan().TryCopyTo(destination);
#endif
charsWritten = res ? str.Length : 0;
return res;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
return number.TryFormat(destination, out charsWritten);
#else
return number.ToString().TryCopyTo(destination, out charsWritten);
#endif
}
[Pure] string IFormattable.ToString(string? _, IFormatProvider? __)
=> ToString();
#if NET6_0_OR_GREATER
[Pure] bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> _, IFormatProvider? __)
=> TryFormat(destination, out charsWritten);
#endif
}
}