using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Brutal.Localization; public partial class LString { /// /// Takes a single argument and formats it into the buffer of a DefaultInterpolatedStringHandler /// /// The object to format. /// The index of the replaceable the argument is for. /// The interpolated string handler to use for formatting. /// The type of arg. /// Thrown if the replacement type of the argument is not handled. private bool TryFormatReplacement( T arg, int replaceableIndex, ref MemoryExtensions.TryWriteInterpolatedStringHandler h ) { var localizedSpan = _localizedString.AsSpan(); var replacementsSpan = CollectionsMarshal.AsSpan(_replacements); ref var replacement = ref replacementsSpan[replaceableIndex]; var success = true; if (replacement.HasPrecedingLiteral) success &= h.AppendFormatted( localizedSpan[ replacement.LiteralRange.Start.Value..replacement.LiteralRange.End.Value ] ); return success & h.AppendFormatted(arg); } /// /// Takes a single argument and formats it into the buffer of a DefaultInterpolatedStringHandler /// /// The object to format. /// The index of the replaceable the argument is for. /// The interpolated string handler to use for formatting. /// The type of arg. /// Thrown if the replacement type of the argument is not handled. private void FormatReplacement( in T arg, int replaceableIndex, ref DefaultInterpolatedStringHandler h ) { var localizedSpan = _localizedString.AsSpan(); var replacementsSpan = CollectionsMarshal.AsSpan(_replacements); ref var replacement = ref replacementsSpan[replaceableIndex]; if (replacement.HasPrecedingLiteral) h.AppendFormatted( localizedSpan[ replacement.LiteralRange.Start.Value..replacement.LiteralRange.End.Value ] ); h.AppendFormatted(arg); } private void ValidateReplacementCount(int numberOfPassedArguments) { if (_replacements.Count != numberOfPassedArguments) throw new ArgumentException( $"Incorrect number of replacement values passed to LString {Id}. expected:{_replacements.Count} passed:{numberOfPassedArguments}" ); } }