namespace Brutal.Numerics;
/// Shared implementations backing the generated .format partials (TryFormat/TryParse).
internal static class StringHelpers
{
/// Formats a vector as <1, 2, 3>, the shape its TryParse accepts back.
/// false if is too small.
public static bool TryFormat(
TVector value,
Span destination,
out int charsWritten,
ReadOnlySpan format,
IFormatProvider? provider
)
where TVector : unmanaged, IVector
{
var separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator;
var formatString = format.IsEmpty ? null : format.ToString(); // the handler takes a string format (an IFormattable legacy) - null on the default path stays alloc-free
var handler = new MemoryExtensions.TryWriteInterpolatedStringHandler(
TVector.ComponentCount + 1,
2 * TVector.ComponentCount - 1,
destination,
provider,
out var b
);
b = b && handler.AppendLiteral("<");
for (var i = 0; i < TVector.ComponentCount; i++)
{
b = b && handler.AppendFormatted(value[i], formatString);
if (i != TVector.ComponentCount - 1)
b = b && handler.AppendFormatted(separator) && handler.AppendLiteral(" ");
}
_ = b && handler.AppendLiteral(">");
return destination.TryWrite(ref handler, out charsWritten);
}
///
public static bool TryFormat(
TVector value,
Span destination,
out int bytesWritten,
ReadOnlySpan format,
IFormatProvider? provider
)
where TVector : unmanaged, IVector
{
var separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator;
var formatString = format.IsEmpty ? null : format.ToString(); // the handler takes a string format (an IFormattable legacy) - null on the default path stays alloc-free
var handler = new Utf8.TryWriteInterpolatedStringHandler(
TVector.ComponentCount + 1,
2 * TVector.ComponentCount - 1,
destination,
provider,
out var b
);
b = b && handler.AppendLiteral("<");
for (var i = 0; i < TVector.ComponentCount; i++)
{
b = b && handler.AppendFormatted(value[i], formatString);
if (i != TVector.ComponentCount - 1)
b = b && handler.AppendFormatted(separator) && handler.AppendLiteral(" ");
}
_ = b && handler.AppendLiteral(">");
return Utf8.TryWrite(destination, ref handler, out bytesWritten);
}
/// Parses a vector from the shape, <1, 2, 3>.
/// true if the input parsed as a TVector.
public static bool TryParse(
ReadOnlySpan s,
NumberStyles style,
IFormatProvider? provider,
out TVector result
)
where TVector : unmanaged, IVector
where TComponent : INumberBase
{
result = default;
s = s.Trim();
if (s.Length < 2 || s[0] != '<' || s[^1] != '>')
return false;
s = s[1..^1];
var separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator;
var value = default(TVector); // built in a local so a mid-parse failure leaves result untouched
for (var i = 0; i < TVector.ComponentCount - 1; i++)
{
var separatorIndex = s.IndexOf(separator);
// trimmed because TryFormat writes "separator + space": the slice must parse under
// caller-supplied NumberStyles without demanding AllowLeadingWhite for OUR whitespace
if (
separatorIndex < 0
|| !TComponent.TryParse(s[..separatorIndex].Trim(), style, provider, out var component)
)
return false;
value[i] = component;
s = s[(separatorIndex + separator.Length)..];
}
if (!TComponent.TryParse(s.Trim(), style, provider, out var last))
return false;
value[TVector.ComponentCount - 1] = last;
result = value;
return true;
}
}