mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 20:48:24 +01:00
3117 lines
120 KiB
C#
3117 lines
120 KiB
C#
/*
|
|
This file is generated by the T4 generator.
|
|
See LString.Format.gen.tt
|
|
|
|
IBufferWriter notes, and why didn't we use that?
|
|
|
|
It forces a managed API via GetMemory.
|
|
It only has "give me memory of X size or greater" and "Give me at least 1". It cannot
|
|
do "Give me X or smaller" which is more useful for LString so we can be more confident
|
|
in a single TryWrite() call being enough.
|
|
GetSpan can't return an empty span to say "no memory", so all we can do is throw if out of buffer.
|
|
|
|
We want to ask for some size we think is enough (e.g. 128 elements) but not run risk of choosing too low. If there's not enough, a smaller might be OK
|
|
but GetSpan can only throw in this case. We could start small and go up and it's possible we only need a tiny amount, say 4 characters, but what smallest size do we choose to start
|
|
because we don't want to TryWrite and request multiple times.
|
|
|
|
*/
|
|
using System.Diagnostics;
|
|
using System.Runtime.CompilerServices;
|
|
using Brutal.Localization.FixedBuffers;
|
|
|
|
namespace Brutal.Localization;
|
|
|
|
public partial class LString
|
|
{
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 0 replacement variables.
|
|
/// </summary>
|
|
/// <returns>The localized string</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 0 replacement variables.</exception>
|
|
public string Format()
|
|
{
|
|
ValidateReplacementCount(0);
|
|
|
|
return _localizedString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 0 replacement variables, into an <see cref="IFixedBufferWriter{T}" />.
|
|
/// </summary>
|
|
/// <param name="spanWriter">The span to format the LString to.</param>
|
|
/// <typeparam name="TWriter">The IFixedBufferWriter to write the output to.</typeparam>
|
|
/// <returns>spanWriter with its contents changed to the formatted LString.</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 0 replacement variables or spanWriter does not have enough space to hold the formatted string.</exception>
|
|
/// <remarks>Formatting to an <see cref="IFixedBufferWriter{T}" /> initially requests a Span of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> length from the
|
|
/// passed in <see cref="IFixedBufferWriter{T}" /> and attempts to format into that. If there is not enough space it will increase its
|
|
/// requested Span length and repeat. This continues until it succeeds in formatting or runs out of buffer storage.<para>
|
|
/// You can change the value of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> but if it's too small, Format may try to format more times than you expect.
|
|
/// If you will be formatting very large LStrings, then increase this value and ensure that your <see cref="IFixedBufferWriter{T}" /> can provide
|
|
/// a Span of at least that length. </para>
|
|
/// </remarks>
|
|
public Span<char> Format<TWriter>(TWriter spanWriter)
|
|
where TWriter : IFixedBufferWriter<char>, allows ref struct
|
|
{
|
|
ValidateReplacementCount(0);
|
|
var spanLengthToRequest = LStringGlobalOptions.FormatInitialSpanLength;
|
|
|
|
while (true)
|
|
{
|
|
if (!spanWriter.TryGetSpan(spanLengthToRequest, out var span) && span.IsEmpty)
|
|
throw new ArgumentException($"Out of buffer.");
|
|
|
|
var gotWhatWeRequested = span.Length >= spanLengthToRequest;
|
|
|
|
var h = new MemoryExtensions.TryWriteInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
span,
|
|
out var mightBeAbleToFit
|
|
);
|
|
|
|
if (!mightBeAbleToFit)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
var success = true;
|
|
success &= h.AppendFormatted(_localizedString);
|
|
|
|
if (success)
|
|
{
|
|
var elementsWritten = h.ElementsWritten();
|
|
spanWriter.Advance(elementsWritten);
|
|
return span[..elementsWritten];
|
|
}
|
|
|
|
if (!gotWhatWeRequested)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
spanLengthToRequest *= 2;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 1 replacement variable.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <returns>The localized string</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 1 replacement variable.</exception>
|
|
public string Format<T0>(in T0 replacement1)
|
|
{
|
|
ValidateReplacementCount(1);
|
|
|
|
Span<char> span = stackalloc char[LStringGlobalOptions.FormatInitialSpanLength];
|
|
var h = new DefaultInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
null,
|
|
span
|
|
);
|
|
|
|
FormatReplacement1(0, in replacement1, ref h);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
return h.ToStringAndClear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 1 replacement variable, into an <see cref="IFixedBufferWriter{T}" />.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="spanWriter">The span to format the LString to.</param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="TWriter">The IFixedBufferWriter to write the output to.</typeparam>
|
|
/// <returns>spanWriter with its contents changed to the formatted LString.</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 1 replacement variable or spanWriter does not have enough space to hold the formatted string.</exception>
|
|
/// <remarks>Formatting to an <see cref="IFixedBufferWriter{T}" /> initially requests a Span of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> length from the
|
|
/// passed in <see cref="IFixedBufferWriter{T}" /> and attempts to format into that. If there is not enough space it will increase its
|
|
/// requested Span length and repeat. This continues until it succeeds in formatting or runs out of buffer storage.<para>
|
|
/// You can change the value of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> but if it's too small, Format may try to format more times than you expect.
|
|
/// If you will be formatting very large LStrings, then increase this value and ensure that your <see cref="IFixedBufferWriter{T}" /> can provide
|
|
/// a Span of at least that length. </para>
|
|
/// </remarks>
|
|
public Span<char> Format<TWriter, T0>(TWriter spanWriter, in T0 replacement1)
|
|
where TWriter : IFixedBufferWriter<char>, allows ref struct
|
|
{
|
|
ValidateReplacementCount(1);
|
|
var spanLengthToRequest = LStringGlobalOptions.FormatInitialSpanLength;
|
|
|
|
while (true)
|
|
{
|
|
if (!spanWriter.TryGetSpan(spanLengthToRequest, out var span) && span.IsEmpty)
|
|
throw new ArgumentException($"Out of buffer.");
|
|
|
|
var gotWhatWeRequested = span.Length >= spanLengthToRequest;
|
|
|
|
var h = new MemoryExtensions.TryWriteInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
span,
|
|
out var mightBeAbleToFit
|
|
);
|
|
|
|
if (!mightBeAbleToFit)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
var success = true;
|
|
success &= TryFormatReplacement1(0, in replacement1, ref h);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
success &= h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
if (success)
|
|
{
|
|
var elementsWritten = h.ElementsWritten();
|
|
spanWriter.Advance(elementsWritten);
|
|
return span[..elementsWritten];
|
|
}
|
|
|
|
if (!gotWhatWeRequested)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
spanLengthToRequest *= 2;
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private bool TryFormatReplacement1<T0>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref MemoryExtensions.TryWriteInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 1);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
return TryFormatReplacement(replacement1, replacementIndex, ref h);
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{1} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private void FormatReplacement1<T0>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref DefaultInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 1);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
FormatReplacement(replacement1, replacementIndex, ref h);
|
|
return;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{1} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 2 replacement variables.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <returns>The localized string</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 2 replacement variables.</exception>
|
|
public string Format<T0, T1>(in T0 replacement1, in T1 replacement2)
|
|
{
|
|
ValidateReplacementCount(2);
|
|
|
|
Span<char> span = stackalloc char[LStringGlobalOptions.FormatInitialSpanLength];
|
|
var h = new DefaultInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
null,
|
|
span
|
|
);
|
|
|
|
FormatReplacement2(0, in replacement1, in replacement2, ref h);
|
|
FormatReplacement2(1, in replacement1, in replacement2, ref h);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
return h.ToStringAndClear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 2 replacement variables, into an <see cref="IFixedBufferWriter{T}" />.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="spanWriter">The span to format the LString to.</param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="TWriter">The IFixedBufferWriter to write the output to.</typeparam>
|
|
/// <returns>spanWriter with its contents changed to the formatted LString.</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 2 replacement variables or spanWriter does not have enough space to hold the formatted string.</exception>
|
|
/// <remarks>Formatting to an <see cref="IFixedBufferWriter{T}" /> initially requests a Span of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> length from the
|
|
/// passed in <see cref="IFixedBufferWriter{T}" /> and attempts to format into that. If there is not enough space it will increase its
|
|
/// requested Span length and repeat. This continues until it succeeds in formatting or runs out of buffer storage.<para>
|
|
/// You can change the value of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> but if it's too small, Format may try to format more times than you expect.
|
|
/// If you will be formatting very large LStrings, then increase this value and ensure that your <see cref="IFixedBufferWriter{T}" /> can provide
|
|
/// a Span of at least that length. </para>
|
|
/// </remarks>
|
|
public Span<char> Format<TWriter, T0, T1>(
|
|
TWriter spanWriter,
|
|
in T0 replacement1,
|
|
in T1 replacement2
|
|
)
|
|
where TWriter : IFixedBufferWriter<char>, allows ref struct
|
|
{
|
|
ValidateReplacementCount(2);
|
|
var spanLengthToRequest = LStringGlobalOptions.FormatInitialSpanLength;
|
|
|
|
while (true)
|
|
{
|
|
if (!spanWriter.TryGetSpan(spanLengthToRequest, out var span) && span.IsEmpty)
|
|
throw new ArgumentException($"Out of buffer.");
|
|
|
|
var gotWhatWeRequested = span.Length >= spanLengthToRequest;
|
|
|
|
var h = new MemoryExtensions.TryWriteInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
span,
|
|
out var mightBeAbleToFit
|
|
);
|
|
|
|
if (!mightBeAbleToFit)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
var success = true;
|
|
success &= TryFormatReplacement2(0, in replacement1, in replacement2, ref h);
|
|
success &= TryFormatReplacement2(1, in replacement1, in replacement2, ref h);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
success &= h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
if (success)
|
|
{
|
|
var elementsWritten = h.ElementsWritten();
|
|
spanWriter.Advance(elementsWritten);
|
|
return span[..elementsWritten];
|
|
}
|
|
|
|
if (!gotWhatWeRequested)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
spanLengthToRequest *= 2;
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private bool TryFormatReplacement2<T0, T1>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref MemoryExtensions.TryWriteInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 2);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
return TryFormatReplacement(replacement1, replacementIndex, ref h);
|
|
case 1:
|
|
return TryFormatReplacement(replacement2, replacementIndex, ref h);
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{2} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private void FormatReplacement2<T0, T1>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref DefaultInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 2);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
FormatReplacement(replacement1, replacementIndex, ref h);
|
|
return;
|
|
case 1:
|
|
FormatReplacement(replacement2, replacementIndex, ref h);
|
|
return;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{2} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 3 replacement variables.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <returns>The localized string</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 3 replacement variables.</exception>
|
|
public string Format<T0, T1, T2>(in T0 replacement1, in T1 replacement2, in T2 replacement3)
|
|
{
|
|
ValidateReplacementCount(3);
|
|
|
|
Span<char> span = stackalloc char[LStringGlobalOptions.FormatInitialSpanLength];
|
|
var h = new DefaultInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
null,
|
|
span
|
|
);
|
|
|
|
FormatReplacement3(0, in replacement1, in replacement2, in replacement3, ref h);
|
|
FormatReplacement3(1, in replacement1, in replacement2, in replacement3, ref h);
|
|
FormatReplacement3(2, in replacement1, in replacement2, in replacement3, ref h);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
return h.ToStringAndClear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 3 replacement variables, into an <see cref="IFixedBufferWriter{T}" />.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="spanWriter">The span to format the LString to.</param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="TWriter">The IFixedBufferWriter to write the output to.</typeparam>
|
|
/// <returns>spanWriter with its contents changed to the formatted LString.</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 3 replacement variables or spanWriter does not have enough space to hold the formatted string.</exception>
|
|
/// <remarks>Formatting to an <see cref="IFixedBufferWriter{T}" /> initially requests a Span of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> length from the
|
|
/// passed in <see cref="IFixedBufferWriter{T}" /> and attempts to format into that. If there is not enough space it will increase its
|
|
/// requested Span length and repeat. This continues until it succeeds in formatting or runs out of buffer storage.<para>
|
|
/// You can change the value of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> but if it's too small, Format may try to format more times than you expect.
|
|
/// If you will be formatting very large LStrings, then increase this value and ensure that your <see cref="IFixedBufferWriter{T}" /> can provide
|
|
/// a Span of at least that length. </para>
|
|
/// </remarks>
|
|
public Span<char> Format<TWriter, T0, T1, T2>(
|
|
TWriter spanWriter,
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3
|
|
)
|
|
where TWriter : IFixedBufferWriter<char>, allows ref struct
|
|
{
|
|
ValidateReplacementCount(3);
|
|
var spanLengthToRequest = LStringGlobalOptions.FormatInitialSpanLength;
|
|
|
|
while (true)
|
|
{
|
|
if (!spanWriter.TryGetSpan(spanLengthToRequest, out var span) && span.IsEmpty)
|
|
throw new ArgumentException($"Out of buffer.");
|
|
|
|
var gotWhatWeRequested = span.Length >= spanLengthToRequest;
|
|
|
|
var h = new MemoryExtensions.TryWriteInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
span,
|
|
out var mightBeAbleToFit
|
|
);
|
|
|
|
if (!mightBeAbleToFit)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
var success = true;
|
|
success &= TryFormatReplacement3(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement3(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement3(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
success &= h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
if (success)
|
|
{
|
|
var elementsWritten = h.ElementsWritten();
|
|
spanWriter.Advance(elementsWritten);
|
|
return span[..elementsWritten];
|
|
}
|
|
|
|
if (!gotWhatWeRequested)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
spanLengthToRequest *= 2;
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private bool TryFormatReplacement3<T0, T1, T2>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref MemoryExtensions.TryWriteInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 3);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
return TryFormatReplacement(replacement1, replacementIndex, ref h);
|
|
case 1:
|
|
return TryFormatReplacement(replacement2, replacementIndex, ref h);
|
|
case 2:
|
|
return TryFormatReplacement(replacement3, replacementIndex, ref h);
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{3} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private void FormatReplacement3<T0, T1, T2>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref DefaultInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 3);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
FormatReplacement(replacement1, replacementIndex, ref h);
|
|
return;
|
|
case 1:
|
|
FormatReplacement(replacement2, replacementIndex, ref h);
|
|
return;
|
|
case 2:
|
|
FormatReplacement(replacement3, replacementIndex, ref h);
|
|
return;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{3} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 4 replacement variables.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <returns>The localized string</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 4 replacement variables.</exception>
|
|
public string Format<T0, T1, T2, T3>(
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4
|
|
)
|
|
{
|
|
ValidateReplacementCount(4);
|
|
|
|
Span<char> span = stackalloc char[LStringGlobalOptions.FormatInitialSpanLength];
|
|
var h = new DefaultInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
null,
|
|
span
|
|
);
|
|
|
|
FormatReplacement4(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
ref h
|
|
);
|
|
FormatReplacement4(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
ref h
|
|
);
|
|
FormatReplacement4(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
ref h
|
|
);
|
|
FormatReplacement4(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
return h.ToStringAndClear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 4 replacement variables, into an <see cref="IFixedBufferWriter{T}" />.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <param name="spanWriter">The span to format the LString to.</param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <typeparam name="TWriter">The IFixedBufferWriter to write the output to.</typeparam>
|
|
/// <returns>spanWriter with its contents changed to the formatted LString.</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 4 replacement variables or spanWriter does not have enough space to hold the formatted string.</exception>
|
|
/// <remarks>Formatting to an <see cref="IFixedBufferWriter{T}" /> initially requests a Span of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> length from the
|
|
/// passed in <see cref="IFixedBufferWriter{T}" /> and attempts to format into that. If there is not enough space it will increase its
|
|
/// requested Span length and repeat. This continues until it succeeds in formatting or runs out of buffer storage.<para>
|
|
/// You can change the value of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> but if it's too small, Format may try to format more times than you expect.
|
|
/// If you will be formatting very large LStrings, then increase this value and ensure that your <see cref="IFixedBufferWriter{T}" /> can provide
|
|
/// a Span of at least that length. </para>
|
|
/// </remarks>
|
|
public Span<char> Format<TWriter, T0, T1, T2, T3>(
|
|
TWriter spanWriter,
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4
|
|
)
|
|
where TWriter : IFixedBufferWriter<char>, allows ref struct
|
|
{
|
|
ValidateReplacementCount(4);
|
|
var spanLengthToRequest = LStringGlobalOptions.FormatInitialSpanLength;
|
|
|
|
while (true)
|
|
{
|
|
if (!spanWriter.TryGetSpan(spanLengthToRequest, out var span) && span.IsEmpty)
|
|
throw new ArgumentException($"Out of buffer.");
|
|
|
|
var gotWhatWeRequested = span.Length >= spanLengthToRequest;
|
|
|
|
var h = new MemoryExtensions.TryWriteInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
span,
|
|
out var mightBeAbleToFit
|
|
);
|
|
|
|
if (!mightBeAbleToFit)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
var success = true;
|
|
success &= TryFormatReplacement4(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement4(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement4(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement4(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
success &= h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
if (success)
|
|
{
|
|
var elementsWritten = h.ElementsWritten();
|
|
spanWriter.Advance(elementsWritten);
|
|
return span[..elementsWritten];
|
|
}
|
|
|
|
if (!gotWhatWeRequested)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
spanLengthToRequest *= 2;
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private bool TryFormatReplacement4<T0, T1, T2, T3>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref MemoryExtensions.TryWriteInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 4);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
return TryFormatReplacement(replacement1, replacementIndex, ref h);
|
|
case 1:
|
|
return TryFormatReplacement(replacement2, replacementIndex, ref h);
|
|
case 2:
|
|
return TryFormatReplacement(replacement3, replacementIndex, ref h);
|
|
case 3:
|
|
return TryFormatReplacement(replacement4, replacementIndex, ref h);
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{4} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private void FormatReplacement4<T0, T1, T2, T3>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref DefaultInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 4);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
FormatReplacement(replacement1, replacementIndex, ref h);
|
|
return;
|
|
case 1:
|
|
FormatReplacement(replacement2, replacementIndex, ref h);
|
|
return;
|
|
case 2:
|
|
FormatReplacement(replacement3, replacementIndex, ref h);
|
|
return;
|
|
case 3:
|
|
FormatReplacement(replacement4, replacementIndex, ref h);
|
|
return;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{4} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 5 replacement variables.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <param name="replacement5">The argument to format into replacement variable #5 </param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <typeparam name="T4">Type of replacement5</typeparam>
|
|
/// <returns>The localized string</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 5 replacement variables.</exception>
|
|
public string Format<T0, T1, T2, T3, T4>(
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4,
|
|
in T4 replacement5
|
|
)
|
|
{
|
|
ValidateReplacementCount(5);
|
|
|
|
Span<char> span = stackalloc char[LStringGlobalOptions.FormatInitialSpanLength];
|
|
var h = new DefaultInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
null,
|
|
span
|
|
);
|
|
|
|
FormatReplacement5(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
ref h
|
|
);
|
|
FormatReplacement5(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
ref h
|
|
);
|
|
FormatReplacement5(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
ref h
|
|
);
|
|
FormatReplacement5(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
ref h
|
|
);
|
|
FormatReplacement5(
|
|
4,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
return h.ToStringAndClear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 5 replacement variables, into an <see cref="IFixedBufferWriter{T}" />.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <param name="replacement5">The argument to format into replacement variable #5 </param>
|
|
/// <param name="spanWriter">The span to format the LString to.</param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <typeparam name="T4">Type of replacement5</typeparam>
|
|
/// <typeparam name="TWriter">The IFixedBufferWriter to write the output to.</typeparam>
|
|
/// <returns>spanWriter with its contents changed to the formatted LString.</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 5 replacement variables or spanWriter does not have enough space to hold the formatted string.</exception>
|
|
/// <remarks>Formatting to an <see cref="IFixedBufferWriter{T}" /> initially requests a Span of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> length from the
|
|
/// passed in <see cref="IFixedBufferWriter{T}" /> and attempts to format into that. If there is not enough space it will increase its
|
|
/// requested Span length and repeat. This continues until it succeeds in formatting or runs out of buffer storage.<para>
|
|
/// You can change the value of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> but if it's too small, Format may try to format more times than you expect.
|
|
/// If you will be formatting very large LStrings, then increase this value and ensure that your <see cref="IFixedBufferWriter{T}" /> can provide
|
|
/// a Span of at least that length. </para>
|
|
/// </remarks>
|
|
public Span<char> Format<TWriter, T0, T1, T2, T3, T4>(
|
|
TWriter spanWriter,
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4,
|
|
in T4 replacement5
|
|
)
|
|
where TWriter : IFixedBufferWriter<char>, allows ref struct
|
|
{
|
|
ValidateReplacementCount(5);
|
|
var spanLengthToRequest = LStringGlobalOptions.FormatInitialSpanLength;
|
|
|
|
while (true)
|
|
{
|
|
if (!spanWriter.TryGetSpan(spanLengthToRequest, out var span) && span.IsEmpty)
|
|
throw new ArgumentException($"Out of buffer.");
|
|
|
|
var gotWhatWeRequested = span.Length >= spanLengthToRequest;
|
|
|
|
var h = new MemoryExtensions.TryWriteInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
span,
|
|
out var mightBeAbleToFit
|
|
);
|
|
|
|
if (!mightBeAbleToFit)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
var success = true;
|
|
success &= TryFormatReplacement5(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement5(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement5(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement5(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement5(
|
|
4,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
success &= h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
if (success)
|
|
{
|
|
var elementsWritten = h.ElementsWritten();
|
|
spanWriter.Advance(elementsWritten);
|
|
return span[..elementsWritten];
|
|
}
|
|
|
|
if (!gotWhatWeRequested)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
spanLengthToRequest *= 2;
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private bool TryFormatReplacement5<T0, T1, T2, T3, T4>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref readonly T4 replacement5,
|
|
ref MemoryExtensions.TryWriteInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 5);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
return TryFormatReplacement(replacement1, replacementIndex, ref h);
|
|
case 1:
|
|
return TryFormatReplacement(replacement2, replacementIndex, ref h);
|
|
case 2:
|
|
return TryFormatReplacement(replacement3, replacementIndex, ref h);
|
|
case 3:
|
|
return TryFormatReplacement(replacement4, replacementIndex, ref h);
|
|
case 4:
|
|
return TryFormatReplacement(replacement5, replacementIndex, ref h);
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{5} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private void FormatReplacement5<T0, T1, T2, T3, T4>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref readonly T4 replacement5,
|
|
ref DefaultInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 5);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
FormatReplacement(replacement1, replacementIndex, ref h);
|
|
return;
|
|
case 1:
|
|
FormatReplacement(replacement2, replacementIndex, ref h);
|
|
return;
|
|
case 2:
|
|
FormatReplacement(replacement3, replacementIndex, ref h);
|
|
return;
|
|
case 3:
|
|
FormatReplacement(replacement4, replacementIndex, ref h);
|
|
return;
|
|
case 4:
|
|
FormatReplacement(replacement5, replacementIndex, ref h);
|
|
return;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{5} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 6 replacement variables.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <param name="replacement5">The argument to format into replacement variable #5 </param>
|
|
/// <param name="replacement6">The argument to format into replacement variable #6 </param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <typeparam name="T4">Type of replacement5</typeparam>
|
|
/// <typeparam name="T5">Type of replacement6</typeparam>
|
|
/// <returns>The localized string</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 6 replacement variables.</exception>
|
|
public string Format<T0, T1, T2, T3, T4, T5>(
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4,
|
|
in T4 replacement5,
|
|
in T5 replacement6
|
|
)
|
|
{
|
|
ValidateReplacementCount(6);
|
|
|
|
Span<char> span = stackalloc char[LStringGlobalOptions.FormatInitialSpanLength];
|
|
var h = new DefaultInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
null,
|
|
span
|
|
);
|
|
|
|
FormatReplacement6(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
ref h
|
|
);
|
|
FormatReplacement6(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
ref h
|
|
);
|
|
FormatReplacement6(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
ref h
|
|
);
|
|
FormatReplacement6(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
ref h
|
|
);
|
|
FormatReplacement6(
|
|
4,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
ref h
|
|
);
|
|
FormatReplacement6(
|
|
5,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
return h.ToStringAndClear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 6 replacement variables, into an <see cref="IFixedBufferWriter{T}" />.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <param name="replacement5">The argument to format into replacement variable #5 </param>
|
|
/// <param name="replacement6">The argument to format into replacement variable #6 </param>
|
|
/// <param name="spanWriter">The span to format the LString to.</param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <typeparam name="T4">Type of replacement5</typeparam>
|
|
/// <typeparam name="T5">Type of replacement6</typeparam>
|
|
/// <typeparam name="TWriter">The IFixedBufferWriter to write the output to.</typeparam>
|
|
/// <returns>spanWriter with its contents changed to the formatted LString.</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 6 replacement variables or spanWriter does not have enough space to hold the formatted string.</exception>
|
|
/// <remarks>Formatting to an <see cref="IFixedBufferWriter{T}" /> initially requests a Span of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> length from the
|
|
/// passed in <see cref="IFixedBufferWriter{T}" /> and attempts to format into that. If there is not enough space it will increase its
|
|
/// requested Span length and repeat. This continues until it succeeds in formatting or runs out of buffer storage.<para>
|
|
/// You can change the value of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> but if it's too small, Format may try to format more times than you expect.
|
|
/// If you will be formatting very large LStrings, then increase this value and ensure that your <see cref="IFixedBufferWriter{T}" /> can provide
|
|
/// a Span of at least that length. </para>
|
|
/// </remarks>
|
|
public Span<char> Format<TWriter, T0, T1, T2, T3, T4, T5>(
|
|
TWriter spanWriter,
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4,
|
|
in T4 replacement5,
|
|
in T5 replacement6
|
|
)
|
|
where TWriter : IFixedBufferWriter<char>, allows ref struct
|
|
{
|
|
ValidateReplacementCount(6);
|
|
var spanLengthToRequest = LStringGlobalOptions.FormatInitialSpanLength;
|
|
|
|
while (true)
|
|
{
|
|
if (!spanWriter.TryGetSpan(spanLengthToRequest, out var span) && span.IsEmpty)
|
|
throw new ArgumentException($"Out of buffer.");
|
|
|
|
var gotWhatWeRequested = span.Length >= spanLengthToRequest;
|
|
|
|
var h = new MemoryExtensions.TryWriteInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
span,
|
|
out var mightBeAbleToFit
|
|
);
|
|
|
|
if (!mightBeAbleToFit)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
var success = true;
|
|
success &= TryFormatReplacement6(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement6(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement6(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement6(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement6(
|
|
4,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement6(
|
|
5,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
success &= h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
if (success)
|
|
{
|
|
var elementsWritten = h.ElementsWritten();
|
|
spanWriter.Advance(elementsWritten);
|
|
return span[..elementsWritten];
|
|
}
|
|
|
|
if (!gotWhatWeRequested)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
spanLengthToRequest *= 2;
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private bool TryFormatReplacement6<T0, T1, T2, T3, T4, T5>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref readonly T4 replacement5,
|
|
ref readonly T5 replacement6,
|
|
ref MemoryExtensions.TryWriteInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 6);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
return TryFormatReplacement(replacement1, replacementIndex, ref h);
|
|
case 1:
|
|
return TryFormatReplacement(replacement2, replacementIndex, ref h);
|
|
case 2:
|
|
return TryFormatReplacement(replacement3, replacementIndex, ref h);
|
|
case 3:
|
|
return TryFormatReplacement(replacement4, replacementIndex, ref h);
|
|
case 4:
|
|
return TryFormatReplacement(replacement5, replacementIndex, ref h);
|
|
case 5:
|
|
return TryFormatReplacement(replacement6, replacementIndex, ref h);
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{6} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private void FormatReplacement6<T0, T1, T2, T3, T4, T5>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref readonly T4 replacement5,
|
|
ref readonly T5 replacement6,
|
|
ref DefaultInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 6);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
FormatReplacement(replacement1, replacementIndex, ref h);
|
|
return;
|
|
case 1:
|
|
FormatReplacement(replacement2, replacementIndex, ref h);
|
|
return;
|
|
case 2:
|
|
FormatReplacement(replacement3, replacementIndex, ref h);
|
|
return;
|
|
case 3:
|
|
FormatReplacement(replacement4, replacementIndex, ref h);
|
|
return;
|
|
case 4:
|
|
FormatReplacement(replacement5, replacementIndex, ref h);
|
|
return;
|
|
case 5:
|
|
FormatReplacement(replacement6, replacementIndex, ref h);
|
|
return;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{6} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 7 replacement variables.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <param name="replacement5">The argument to format into replacement variable #5 </param>
|
|
/// <param name="replacement6">The argument to format into replacement variable #6 </param>
|
|
/// <param name="replacement7">The argument to format into replacement variable #7 </param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <typeparam name="T4">Type of replacement5</typeparam>
|
|
/// <typeparam name="T5">Type of replacement6</typeparam>
|
|
/// <typeparam name="T6">Type of replacement7</typeparam>
|
|
/// <returns>The localized string</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 7 replacement variables.</exception>
|
|
public string Format<T0, T1, T2, T3, T4, T5, T6>(
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4,
|
|
in T4 replacement5,
|
|
in T5 replacement6,
|
|
in T6 replacement7
|
|
)
|
|
{
|
|
ValidateReplacementCount(7);
|
|
|
|
Span<char> span = stackalloc char[LStringGlobalOptions.FormatInitialSpanLength];
|
|
var h = new DefaultInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
null,
|
|
span
|
|
);
|
|
|
|
FormatReplacement7(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
FormatReplacement7(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
FormatReplacement7(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
FormatReplacement7(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
FormatReplacement7(
|
|
4,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
FormatReplacement7(
|
|
5,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
FormatReplacement7(
|
|
6,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
return h.ToStringAndClear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 7 replacement variables, into an <see cref="IFixedBufferWriter{T}" />.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <param name="replacement5">The argument to format into replacement variable #5 </param>
|
|
/// <param name="replacement6">The argument to format into replacement variable #6 </param>
|
|
/// <param name="replacement7">The argument to format into replacement variable #7 </param>
|
|
/// <param name="spanWriter">The span to format the LString to.</param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <typeparam name="T4">Type of replacement5</typeparam>
|
|
/// <typeparam name="T5">Type of replacement6</typeparam>
|
|
/// <typeparam name="T6">Type of replacement7</typeparam>
|
|
/// <typeparam name="TWriter">The IFixedBufferWriter to write the output to.</typeparam>
|
|
/// <returns>spanWriter with its contents changed to the formatted LString.</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 7 replacement variables or spanWriter does not have enough space to hold the formatted string.</exception>
|
|
/// <remarks>Formatting to an <see cref="IFixedBufferWriter{T}" /> initially requests a Span of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> length from the
|
|
/// passed in <see cref="IFixedBufferWriter{T}" /> and attempts to format into that. If there is not enough space it will increase its
|
|
/// requested Span length and repeat. This continues until it succeeds in formatting or runs out of buffer storage.<para>
|
|
/// You can change the value of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> but if it's too small, Format may try to format more times than you expect.
|
|
/// If you will be formatting very large LStrings, then increase this value and ensure that your <see cref="IFixedBufferWriter{T}" /> can provide
|
|
/// a Span of at least that length. </para>
|
|
/// </remarks>
|
|
public Span<char> Format<TWriter, T0, T1, T2, T3, T4, T5, T6>(
|
|
TWriter spanWriter,
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4,
|
|
in T4 replacement5,
|
|
in T5 replacement6,
|
|
in T6 replacement7
|
|
)
|
|
where TWriter : IFixedBufferWriter<char>, allows ref struct
|
|
{
|
|
ValidateReplacementCount(7);
|
|
var spanLengthToRequest = LStringGlobalOptions.FormatInitialSpanLength;
|
|
|
|
while (true)
|
|
{
|
|
if (!spanWriter.TryGetSpan(spanLengthToRequest, out var span) && span.IsEmpty)
|
|
throw new ArgumentException($"Out of buffer.");
|
|
|
|
var gotWhatWeRequested = span.Length >= spanLengthToRequest;
|
|
|
|
var h = new MemoryExtensions.TryWriteInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
span,
|
|
out var mightBeAbleToFit
|
|
);
|
|
|
|
if (!mightBeAbleToFit)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
var success = true;
|
|
success &= TryFormatReplacement7(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement7(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement7(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement7(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement7(
|
|
4,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement7(
|
|
5,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement7(
|
|
6,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
success &= h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
if (success)
|
|
{
|
|
var elementsWritten = h.ElementsWritten();
|
|
spanWriter.Advance(elementsWritten);
|
|
return span[..elementsWritten];
|
|
}
|
|
|
|
if (!gotWhatWeRequested)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
spanLengthToRequest *= 2;
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private bool TryFormatReplacement7<T0, T1, T2, T3, T4, T5, T6>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref readonly T4 replacement5,
|
|
ref readonly T5 replacement6,
|
|
ref readonly T6 replacement7,
|
|
ref MemoryExtensions.TryWriteInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 7);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
return TryFormatReplacement(replacement1, replacementIndex, ref h);
|
|
case 1:
|
|
return TryFormatReplacement(replacement2, replacementIndex, ref h);
|
|
case 2:
|
|
return TryFormatReplacement(replacement3, replacementIndex, ref h);
|
|
case 3:
|
|
return TryFormatReplacement(replacement4, replacementIndex, ref h);
|
|
case 4:
|
|
return TryFormatReplacement(replacement5, replacementIndex, ref h);
|
|
case 5:
|
|
return TryFormatReplacement(replacement6, replacementIndex, ref h);
|
|
case 6:
|
|
return TryFormatReplacement(replacement7, replacementIndex, ref h);
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{7} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private void FormatReplacement7<T0, T1, T2, T3, T4, T5, T6>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref readonly T4 replacement5,
|
|
ref readonly T5 replacement6,
|
|
ref readonly T6 replacement7,
|
|
ref DefaultInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 7);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
FormatReplacement(replacement1, replacementIndex, ref h);
|
|
return;
|
|
case 1:
|
|
FormatReplacement(replacement2, replacementIndex, ref h);
|
|
return;
|
|
case 2:
|
|
FormatReplacement(replacement3, replacementIndex, ref h);
|
|
return;
|
|
case 3:
|
|
FormatReplacement(replacement4, replacementIndex, ref h);
|
|
return;
|
|
case 4:
|
|
FormatReplacement(replacement5, replacementIndex, ref h);
|
|
return;
|
|
case 5:
|
|
FormatReplacement(replacement6, replacementIndex, ref h);
|
|
return;
|
|
case 6:
|
|
FormatReplacement(replacement7, replacementIndex, ref h);
|
|
return;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{7} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 8 replacement variables.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <param name="replacement5">The argument to format into replacement variable #5 </param>
|
|
/// <param name="replacement6">The argument to format into replacement variable #6 </param>
|
|
/// <param name="replacement7">The argument to format into replacement variable #7 </param>
|
|
/// <param name="replacement8">The argument to format into replacement variable #8 </param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <typeparam name="T4">Type of replacement5</typeparam>
|
|
/// <typeparam name="T5">Type of replacement6</typeparam>
|
|
/// <typeparam name="T6">Type of replacement7</typeparam>
|
|
/// <typeparam name="T7">Type of replacement8</typeparam>
|
|
/// <returns>The localized string</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 8 replacement variables.</exception>
|
|
public string Format<T0, T1, T2, T3, T4, T5, T6, T7>(
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4,
|
|
in T4 replacement5,
|
|
in T5 replacement6,
|
|
in T6 replacement7,
|
|
in T7 replacement8
|
|
)
|
|
{
|
|
ValidateReplacementCount(8);
|
|
|
|
Span<char> span = stackalloc char[LStringGlobalOptions.FormatInitialSpanLength];
|
|
var h = new DefaultInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
null,
|
|
span
|
|
);
|
|
|
|
FormatReplacement8(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
FormatReplacement8(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
FormatReplacement8(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
FormatReplacement8(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
FormatReplacement8(
|
|
4,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
FormatReplacement8(
|
|
5,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
FormatReplacement8(
|
|
6,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
FormatReplacement8(
|
|
7,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
return h.ToStringAndClear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 8 replacement variables, into an <see cref="IFixedBufferWriter{T}" />.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <param name="replacement5">The argument to format into replacement variable #5 </param>
|
|
/// <param name="replacement6">The argument to format into replacement variable #6 </param>
|
|
/// <param name="replacement7">The argument to format into replacement variable #7 </param>
|
|
/// <param name="replacement8">The argument to format into replacement variable #8 </param>
|
|
/// <param name="spanWriter">The span to format the LString to.</param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <typeparam name="T4">Type of replacement5</typeparam>
|
|
/// <typeparam name="T5">Type of replacement6</typeparam>
|
|
/// <typeparam name="T6">Type of replacement7</typeparam>
|
|
/// <typeparam name="T7">Type of replacement8</typeparam>
|
|
/// <typeparam name="TWriter">The IFixedBufferWriter to write the output to.</typeparam>
|
|
/// <returns>spanWriter with its contents changed to the formatted LString.</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 8 replacement variables or spanWriter does not have enough space to hold the formatted string.</exception>
|
|
/// <remarks>Formatting to an <see cref="IFixedBufferWriter{T}" /> initially requests a Span of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> length from the
|
|
/// passed in <see cref="IFixedBufferWriter{T}" /> and attempts to format into that. If there is not enough space it will increase its
|
|
/// requested Span length and repeat. This continues until it succeeds in formatting or runs out of buffer storage.<para>
|
|
/// You can change the value of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> but if it's too small, Format may try to format more times than you expect.
|
|
/// If you will be formatting very large LStrings, then increase this value and ensure that your <see cref="IFixedBufferWriter{T}" /> can provide
|
|
/// a Span of at least that length. </para>
|
|
/// </remarks>
|
|
public Span<char> Format<TWriter, T0, T1, T2, T3, T4, T5, T6, T7>(
|
|
TWriter spanWriter,
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4,
|
|
in T4 replacement5,
|
|
in T5 replacement6,
|
|
in T6 replacement7,
|
|
in T7 replacement8
|
|
)
|
|
where TWriter : IFixedBufferWriter<char>, allows ref struct
|
|
{
|
|
ValidateReplacementCount(8);
|
|
var spanLengthToRequest = LStringGlobalOptions.FormatInitialSpanLength;
|
|
|
|
while (true)
|
|
{
|
|
if (!spanWriter.TryGetSpan(spanLengthToRequest, out var span) && span.IsEmpty)
|
|
throw new ArgumentException($"Out of buffer.");
|
|
|
|
var gotWhatWeRequested = span.Length >= spanLengthToRequest;
|
|
|
|
var h = new MemoryExtensions.TryWriteInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
span,
|
|
out var mightBeAbleToFit
|
|
);
|
|
|
|
if (!mightBeAbleToFit)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
var success = true;
|
|
success &= TryFormatReplacement8(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement8(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement8(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement8(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement8(
|
|
4,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement8(
|
|
5,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement8(
|
|
6,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement8(
|
|
7,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
success &= h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
if (success)
|
|
{
|
|
var elementsWritten = h.ElementsWritten();
|
|
spanWriter.Advance(elementsWritten);
|
|
return span[..elementsWritten];
|
|
}
|
|
|
|
if (!gotWhatWeRequested)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
spanLengthToRequest *= 2;
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private bool TryFormatReplacement8<T0, T1, T2, T3, T4, T5, T6, T7>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref readonly T4 replacement5,
|
|
ref readonly T5 replacement6,
|
|
ref readonly T6 replacement7,
|
|
ref readonly T7 replacement8,
|
|
ref MemoryExtensions.TryWriteInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 8);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
return TryFormatReplacement(replacement1, replacementIndex, ref h);
|
|
case 1:
|
|
return TryFormatReplacement(replacement2, replacementIndex, ref h);
|
|
case 2:
|
|
return TryFormatReplacement(replacement3, replacementIndex, ref h);
|
|
case 3:
|
|
return TryFormatReplacement(replacement4, replacementIndex, ref h);
|
|
case 4:
|
|
return TryFormatReplacement(replacement5, replacementIndex, ref h);
|
|
case 5:
|
|
return TryFormatReplacement(replacement6, replacementIndex, ref h);
|
|
case 6:
|
|
return TryFormatReplacement(replacement7, replacementIndex, ref h);
|
|
case 7:
|
|
return TryFormatReplacement(replacement8, replacementIndex, ref h);
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{8} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private void FormatReplacement8<T0, T1, T2, T3, T4, T5, T6, T7>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref readonly T4 replacement5,
|
|
ref readonly T5 replacement6,
|
|
ref readonly T6 replacement7,
|
|
ref readonly T7 replacement8,
|
|
ref DefaultInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 8);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
FormatReplacement(replacement1, replacementIndex, ref h);
|
|
return;
|
|
case 1:
|
|
FormatReplacement(replacement2, replacementIndex, ref h);
|
|
return;
|
|
case 2:
|
|
FormatReplacement(replacement3, replacementIndex, ref h);
|
|
return;
|
|
case 3:
|
|
FormatReplacement(replacement4, replacementIndex, ref h);
|
|
return;
|
|
case 4:
|
|
FormatReplacement(replacement5, replacementIndex, ref h);
|
|
return;
|
|
case 5:
|
|
FormatReplacement(replacement6, replacementIndex, ref h);
|
|
return;
|
|
case 6:
|
|
FormatReplacement(replacement7, replacementIndex, ref h);
|
|
return;
|
|
case 7:
|
|
FormatReplacement(replacement8, replacementIndex, ref h);
|
|
return;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{8} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 9 replacement variables.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <param name="replacement5">The argument to format into replacement variable #5 </param>
|
|
/// <param name="replacement6">The argument to format into replacement variable #6 </param>
|
|
/// <param name="replacement7">The argument to format into replacement variable #7 </param>
|
|
/// <param name="replacement8">The argument to format into replacement variable #8 </param>
|
|
/// <param name="replacement9">The argument to format into replacement variable #9 </param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <typeparam name="T4">Type of replacement5</typeparam>
|
|
/// <typeparam name="T5">Type of replacement6</typeparam>
|
|
/// <typeparam name="T6">Type of replacement7</typeparam>
|
|
/// <typeparam name="T7">Type of replacement8</typeparam>
|
|
/// <typeparam name="T8">Type of replacement9</typeparam>
|
|
/// <returns>The localized string</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 9 replacement variables.</exception>
|
|
public string Format<T0, T1, T2, T3, T4, T5, T6, T7, T8>(
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4,
|
|
in T4 replacement5,
|
|
in T5 replacement6,
|
|
in T6 replacement7,
|
|
in T7 replacement8,
|
|
in T8 replacement9
|
|
)
|
|
{
|
|
ValidateReplacementCount(9);
|
|
|
|
Span<char> span = stackalloc char[LStringGlobalOptions.FormatInitialSpanLength];
|
|
var h = new DefaultInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
null,
|
|
span
|
|
);
|
|
|
|
FormatReplacement9(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
FormatReplacement9(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
FormatReplacement9(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
FormatReplacement9(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
FormatReplacement9(
|
|
4,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
FormatReplacement9(
|
|
5,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
FormatReplacement9(
|
|
6,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
FormatReplacement9(
|
|
7,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
FormatReplacement9(
|
|
8,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
return h.ToStringAndClear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 9 replacement variables, into an <see cref="IFixedBufferWriter{T}" />.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <param name="replacement5">The argument to format into replacement variable #5 </param>
|
|
/// <param name="replacement6">The argument to format into replacement variable #6 </param>
|
|
/// <param name="replacement7">The argument to format into replacement variable #7 </param>
|
|
/// <param name="replacement8">The argument to format into replacement variable #8 </param>
|
|
/// <param name="replacement9">The argument to format into replacement variable #9 </param>
|
|
/// <param name="spanWriter">The span to format the LString to.</param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <typeparam name="T4">Type of replacement5</typeparam>
|
|
/// <typeparam name="T5">Type of replacement6</typeparam>
|
|
/// <typeparam name="T6">Type of replacement7</typeparam>
|
|
/// <typeparam name="T7">Type of replacement8</typeparam>
|
|
/// <typeparam name="T8">Type of replacement9</typeparam>
|
|
/// <typeparam name="TWriter">The IFixedBufferWriter to write the output to.</typeparam>
|
|
/// <returns>spanWriter with its contents changed to the formatted LString.</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 9 replacement variables or spanWriter does not have enough space to hold the formatted string.</exception>
|
|
/// <remarks>Formatting to an <see cref="IFixedBufferWriter{T}" /> initially requests a Span of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> length from the
|
|
/// passed in <see cref="IFixedBufferWriter{T}" /> and attempts to format into that. If there is not enough space it will increase its
|
|
/// requested Span length and repeat. This continues until it succeeds in formatting or runs out of buffer storage.<para>
|
|
/// You can change the value of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> but if it's too small, Format may try to format more times than you expect.
|
|
/// If you will be formatting very large LStrings, then increase this value and ensure that your <see cref="IFixedBufferWriter{T}" /> can provide
|
|
/// a Span of at least that length. </para>
|
|
/// </remarks>
|
|
public Span<char> Format<TWriter, T0, T1, T2, T3, T4, T5, T6, T7, T8>(
|
|
TWriter spanWriter,
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4,
|
|
in T4 replacement5,
|
|
in T5 replacement6,
|
|
in T6 replacement7,
|
|
in T7 replacement8,
|
|
in T8 replacement9
|
|
)
|
|
where TWriter : IFixedBufferWriter<char>, allows ref struct
|
|
{
|
|
ValidateReplacementCount(9);
|
|
var spanLengthToRequest = LStringGlobalOptions.FormatInitialSpanLength;
|
|
|
|
while (true)
|
|
{
|
|
if (!spanWriter.TryGetSpan(spanLengthToRequest, out var span) && span.IsEmpty)
|
|
throw new ArgumentException($"Out of buffer.");
|
|
|
|
var gotWhatWeRequested = span.Length >= spanLengthToRequest;
|
|
|
|
var h = new MemoryExtensions.TryWriteInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
span,
|
|
out var mightBeAbleToFit
|
|
);
|
|
|
|
if (!mightBeAbleToFit)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
var success = true;
|
|
success &= TryFormatReplacement9(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement9(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement9(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement9(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement9(
|
|
4,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement9(
|
|
5,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement9(
|
|
6,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement9(
|
|
7,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement9(
|
|
8,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
success &= h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
if (success)
|
|
{
|
|
var elementsWritten = h.ElementsWritten();
|
|
spanWriter.Advance(elementsWritten);
|
|
return span[..elementsWritten];
|
|
}
|
|
|
|
if (!gotWhatWeRequested)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
spanLengthToRequest *= 2;
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private bool TryFormatReplacement9<T0, T1, T2, T3, T4, T5, T6, T7, T8>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref readonly T4 replacement5,
|
|
ref readonly T5 replacement6,
|
|
ref readonly T6 replacement7,
|
|
ref readonly T7 replacement8,
|
|
ref readonly T8 replacement9,
|
|
ref MemoryExtensions.TryWriteInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 9);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
return TryFormatReplacement(replacement1, replacementIndex, ref h);
|
|
case 1:
|
|
return TryFormatReplacement(replacement2, replacementIndex, ref h);
|
|
case 2:
|
|
return TryFormatReplacement(replacement3, replacementIndex, ref h);
|
|
case 3:
|
|
return TryFormatReplacement(replacement4, replacementIndex, ref h);
|
|
case 4:
|
|
return TryFormatReplacement(replacement5, replacementIndex, ref h);
|
|
case 5:
|
|
return TryFormatReplacement(replacement6, replacementIndex, ref h);
|
|
case 6:
|
|
return TryFormatReplacement(replacement7, replacementIndex, ref h);
|
|
case 7:
|
|
return TryFormatReplacement(replacement8, replacementIndex, ref h);
|
|
case 8:
|
|
return TryFormatReplacement(replacement9, replacementIndex, ref h);
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{9} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private void FormatReplacement9<T0, T1, T2, T3, T4, T5, T6, T7, T8>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref readonly T4 replacement5,
|
|
ref readonly T5 replacement6,
|
|
ref readonly T6 replacement7,
|
|
ref readonly T7 replacement8,
|
|
ref readonly T8 replacement9,
|
|
ref DefaultInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 9);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
FormatReplacement(replacement1, replacementIndex, ref h);
|
|
return;
|
|
case 1:
|
|
FormatReplacement(replacement2, replacementIndex, ref h);
|
|
return;
|
|
case 2:
|
|
FormatReplacement(replacement3, replacementIndex, ref h);
|
|
return;
|
|
case 3:
|
|
FormatReplacement(replacement4, replacementIndex, ref h);
|
|
return;
|
|
case 4:
|
|
FormatReplacement(replacement5, replacementIndex, ref h);
|
|
return;
|
|
case 5:
|
|
FormatReplacement(replacement6, replacementIndex, ref h);
|
|
return;
|
|
case 6:
|
|
FormatReplacement(replacement7, replacementIndex, ref h);
|
|
return;
|
|
case 7:
|
|
FormatReplacement(replacement8, replacementIndex, ref h);
|
|
return;
|
|
case 8:
|
|
FormatReplacement(replacement9, replacementIndex, ref h);
|
|
return;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{9} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 10 replacement variables.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <param name="replacement5">The argument to format into replacement variable #5 </param>
|
|
/// <param name="replacement6">The argument to format into replacement variable #6 </param>
|
|
/// <param name="replacement7">The argument to format into replacement variable #7 </param>
|
|
/// <param name="replacement8">The argument to format into replacement variable #8 </param>
|
|
/// <param name="replacement9">The argument to format into replacement variable #9 </param>
|
|
/// <param name="replacement10">The argument to format into replacement variable #10 </param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <typeparam name="T4">Type of replacement5</typeparam>
|
|
/// <typeparam name="T5">Type of replacement6</typeparam>
|
|
/// <typeparam name="T6">Type of replacement7</typeparam>
|
|
/// <typeparam name="T7">Type of replacement8</typeparam>
|
|
/// <typeparam name="T8">Type of replacement9</typeparam>
|
|
/// <typeparam name="T9">Type of replacement10</typeparam>
|
|
/// <returns>The localized string</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 10 replacement variables.</exception>
|
|
public string Format<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4,
|
|
in T4 replacement5,
|
|
in T5 replacement6,
|
|
in T6 replacement7,
|
|
in T7 replacement8,
|
|
in T8 replacement9,
|
|
in T9 replacement10
|
|
)
|
|
{
|
|
ValidateReplacementCount(10);
|
|
|
|
Span<char> span = stackalloc char[LStringGlobalOptions.FormatInitialSpanLength];
|
|
var h = new DefaultInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
null,
|
|
span
|
|
);
|
|
|
|
FormatReplacement10(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
FormatReplacement10(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
FormatReplacement10(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
FormatReplacement10(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
FormatReplacement10(
|
|
4,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
FormatReplacement10(
|
|
5,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
FormatReplacement10(
|
|
6,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
FormatReplacement10(
|
|
7,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
FormatReplacement10(
|
|
8,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
FormatReplacement10(
|
|
9,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
return h.ToStringAndClear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format the localized version of an LString that has 10 replacement variables, into an <see cref="IFixedBufferWriter{T}" />.
|
|
/// </summary>
|
|
/// <param name="replacement1">The argument to format into replacement variable #1 </param>
|
|
/// <param name="replacement2">The argument to format into replacement variable #2 </param>
|
|
/// <param name="replacement3">The argument to format into replacement variable #3 </param>
|
|
/// <param name="replacement4">The argument to format into replacement variable #4 </param>
|
|
/// <param name="replacement5">The argument to format into replacement variable #5 </param>
|
|
/// <param name="replacement6">The argument to format into replacement variable #6 </param>
|
|
/// <param name="replacement7">The argument to format into replacement variable #7 </param>
|
|
/// <param name="replacement8">The argument to format into replacement variable #8 </param>
|
|
/// <param name="replacement9">The argument to format into replacement variable #9 </param>
|
|
/// <param name="replacement10">The argument to format into replacement variable #10 </param>
|
|
/// <param name="spanWriter">The span to format the LString to.</param>
|
|
/// <typeparam name="T0">Type of replacement1</typeparam>
|
|
/// <typeparam name="T1">Type of replacement2</typeparam>
|
|
/// <typeparam name="T2">Type of replacement3</typeparam>
|
|
/// <typeparam name="T3">Type of replacement4</typeparam>
|
|
/// <typeparam name="T4">Type of replacement5</typeparam>
|
|
/// <typeparam name="T5">Type of replacement6</typeparam>
|
|
/// <typeparam name="T6">Type of replacement7</typeparam>
|
|
/// <typeparam name="T7">Type of replacement8</typeparam>
|
|
/// <typeparam name="T8">Type of replacement9</typeparam>
|
|
/// <typeparam name="T9">Type of replacement10</typeparam>
|
|
/// <typeparam name="TWriter">The IFixedBufferWriter to write the output to.</typeparam>
|
|
/// <returns>spanWriter with its contents changed to the formatted LString.</returns>
|
|
/// <exception cref="ArgumentException">Thrown if the template string does not have 10 replacement variables or spanWriter does not have enough space to hold the formatted string.</exception>
|
|
/// <remarks>Formatting to an <see cref="IFixedBufferWriter{T}" /> initially requests a Span of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> length from the
|
|
/// passed in <see cref="IFixedBufferWriter{T}" /> and attempts to format into that. If there is not enough space it will increase its
|
|
/// requested Span length and repeat. This continues until it succeeds in formatting or runs out of buffer storage.<para>
|
|
/// You can change the value of <see cref="LStringGlobalOptions.FormatInitialSpanLength" /> but if it's too small, Format may try to format more times than you expect.
|
|
/// If you will be formatting very large LStrings, then increase this value and ensure that your <see cref="IFixedBufferWriter{T}" /> can provide
|
|
/// a Span of at least that length. </para>
|
|
/// </remarks>
|
|
public Span<char> Format<TWriter, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(
|
|
TWriter spanWriter,
|
|
in T0 replacement1,
|
|
in T1 replacement2,
|
|
in T2 replacement3,
|
|
in T3 replacement4,
|
|
in T4 replacement5,
|
|
in T5 replacement6,
|
|
in T6 replacement7,
|
|
in T7 replacement8,
|
|
in T8 replacement9,
|
|
in T9 replacement10
|
|
)
|
|
where TWriter : IFixedBufferWriter<char>, allows ref struct
|
|
{
|
|
ValidateReplacementCount(10);
|
|
var spanLengthToRequest = LStringGlobalOptions.FormatInitialSpanLength;
|
|
|
|
while (true)
|
|
{
|
|
if (!spanWriter.TryGetSpan(spanLengthToRequest, out var span) && span.IsEmpty)
|
|
throw new ArgumentException($"Out of buffer.");
|
|
|
|
var gotWhatWeRequested = span.Length >= spanLengthToRequest;
|
|
|
|
var h = new MemoryExtensions.TryWriteInterpolatedStringHandler(
|
|
_literalCharCount,
|
|
_replacements.Count,
|
|
span,
|
|
out var mightBeAbleToFit
|
|
);
|
|
|
|
if (!mightBeAbleToFit)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
var success = true;
|
|
success &= TryFormatReplacement10(
|
|
0,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement10(
|
|
1,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement10(
|
|
2,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement10(
|
|
3,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement10(
|
|
4,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement10(
|
|
5,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement10(
|
|
6,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement10(
|
|
7,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement10(
|
|
8,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
success &= TryFormatReplacement10(
|
|
9,
|
|
in replacement1,
|
|
in replacement2,
|
|
in replacement3,
|
|
in replacement4,
|
|
in replacement5,
|
|
in replacement6,
|
|
in replacement7,
|
|
in replacement8,
|
|
in replacement9,
|
|
in replacement10,
|
|
ref h
|
|
);
|
|
|
|
if (_endLiteralStart < _localizedString.Length)
|
|
success &= h.AppendFormatted(_localizedString.AsSpan()[_endLiteralStart..]);
|
|
|
|
if (success)
|
|
{
|
|
var elementsWritten = h.ElementsWritten();
|
|
spanWriter.Advance(elementsWritten);
|
|
return span[..elementsWritten];
|
|
}
|
|
|
|
if (!gotWhatWeRequested)
|
|
ThrowOutOfBufferException(span);
|
|
|
|
spanLengthToRequest *= 2;
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private bool TryFormatReplacement10<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref readonly T4 replacement5,
|
|
ref readonly T5 replacement6,
|
|
ref readonly T6 replacement7,
|
|
ref readonly T7 replacement8,
|
|
ref readonly T8 replacement9,
|
|
ref readonly T9 replacement10,
|
|
ref MemoryExtensions.TryWriteInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 10);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
return TryFormatReplacement(replacement1, replacementIndex, ref h);
|
|
case 1:
|
|
return TryFormatReplacement(replacement2, replacementIndex, ref h);
|
|
case 2:
|
|
return TryFormatReplacement(replacement3, replacementIndex, ref h);
|
|
case 3:
|
|
return TryFormatReplacement(replacement4, replacementIndex, ref h);
|
|
case 4:
|
|
return TryFormatReplacement(replacement5, replacementIndex, ref h);
|
|
case 5:
|
|
return TryFormatReplacement(replacement6, replacementIndex, ref h);
|
|
case 6:
|
|
return TryFormatReplacement(replacement7, replacementIndex, ref h);
|
|
case 7:
|
|
return TryFormatReplacement(replacement8, replacementIndex, ref h);
|
|
case 8:
|
|
return TryFormatReplacement(replacement9, replacementIndex, ref h);
|
|
case 9:
|
|
return TryFormatReplacement(replacement10, replacementIndex, ref h);
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{10} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
private void FormatReplacement10<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(
|
|
int replacementIndex,
|
|
ref readonly T0 replacement1,
|
|
ref readonly T1 replacement2,
|
|
ref readonly T2 replacement3,
|
|
ref readonly T3 replacement4,
|
|
ref readonly T4 replacement5,
|
|
ref readonly T5 replacement6,
|
|
ref readonly T6 replacement7,
|
|
ref readonly T7 replacement8,
|
|
ref readonly T8 replacement9,
|
|
ref readonly T9 replacement10,
|
|
ref DefaultInterpolatedStringHandler h
|
|
)
|
|
{
|
|
Debug.Assert(_replacements.Count == 10);
|
|
switch (_replacements[replacementIndex].TemplateReplacementIndex)
|
|
{
|
|
case 0:
|
|
FormatReplacement(replacement1, replacementIndex, ref h);
|
|
return;
|
|
case 1:
|
|
FormatReplacement(replacement2, replacementIndex, ref h);
|
|
return;
|
|
case 2:
|
|
FormatReplacement(replacement3, replacementIndex, ref h);
|
|
return;
|
|
case 3:
|
|
FormatReplacement(replacement4, replacementIndex, ref h);
|
|
return;
|
|
case 4:
|
|
FormatReplacement(replacement5, replacementIndex, ref h);
|
|
return;
|
|
case 5:
|
|
FormatReplacement(replacement6, replacementIndex, ref h);
|
|
return;
|
|
case 6:
|
|
FormatReplacement(replacement7, replacementIndex, ref h);
|
|
return;
|
|
case 7:
|
|
FormatReplacement(replacement8, replacementIndex, ref h);
|
|
return;
|
|
case 8:
|
|
FormatReplacement(replacement9, replacementIndex, ref h);
|
|
return;
|
|
case 9:
|
|
FormatReplacement(replacement10, replacementIndex, ref h);
|
|
return;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(
|
|
$"ArgumentIndex out of range. maximum:{10} provided:{_replacements[replacementIndex].TemplateReplacementIndex}"
|
|
);
|
|
}
|
|
}
|
|
|
|
private static void ThrowOutOfBufferException(Span<char> span)
|
|
{
|
|
throw new ArgumentException(
|
|
$"Out of buffer. {span.Length} elements was too small for string interpolation."
|
|
);
|
|
}
|
|
|
|
private const int MaxReplacementCount = 10;
|
|
}
|