#/*THIS IS A T4 FILE - see t4_text_templating.md for what it is and how to run codegen*/#>
<#@ template debug="True" #>
<#@ output extension=".gen.cs" encoding="utf-8" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
//------------------------------------------------------------------------------
// 
//     This code was generated by a tool.
//
//     TextTransform Samples/Packages/com.unity.collections/Unity.Collections/FixedStringFormatMethods.tt
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// 
//------------------------------------------------------------------------------
using System;
using Unity.Collections.LowLevel.Unsafe;
namespace Unity.Collections
{
    /// 
    /// Provides extension methods for FixedString*N*Bytes.
    /// 
    public unsafe static partial class FixedStringMethods
    {
<#
    for (var ARGS = 1; ARGS <= 10; ++ARGS)
    {
        var TYPES    = String.Join(", ", Enumerable.Range(0, ARGS).Select(n => $"T{n}"));
        var PARAMS   = String.Join(", ", Enumerable.Range(0, ARGS).Select(n => $"in T{n} arg{n}"));
        var ARGNAMES = String.Join(", ", Enumerable.Range(0, ARGS).Select(n => $"arg{n}"));
        var TxDOCS   = String.Join("\r\n        /// ", Enumerable.Range(0, ARGS).Select(n => $"The type of value to interpolate into the format string."));
        var ARGxDOCS = String.Join("\r\n        /// ", Enumerable.Range(0, ARGS).Select(n => $"A FixedString*N*Bytes to interpolate into the format string."));
        var BCOMPAT  = String.Join(", ", Enumerable.Range(0, ARGS).Select(n => $"typeof(FixedString128Bytes /*T{n}*/)"));
#>
        /// 
        /// Interpolates strings into a format string and appends the result to this string.
        /// 
        /// 
        /// Similar to `StringBuilder.AppendFormat` but with significant limitations:
        /// - Only supports FixedString*N*Bytes arguments. To use other string types, convert them to FixedString*N*Bytes first.
        /// - Only supports numeric format placeholders of the form `{0}` .. `{N}`.
        /// - No format modifiers (*e.g.* `{0:x}`) are supported.
        ///
        /// The overloads of this method take up to ten strings to interpolate into the format string.
        /// 
        /// A FixedString*N*Bytes type.
        /// A FixedString*N*Bytes type.
        /// <#=TxDOCS#>
        /// The string to append to.d
        /// A string to be interpolated and appended.
        /// <#=ARGxDOCS#>
        ///  if successful.  Otherwise returns the appropriate .
        [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(FixedString128Bytes /*T*/), typeof(FixedString128Bytes /*U*/), <#=BCOMPAT#> })]
        public static unsafe FormatError AppendFormat>(ref this T dest, in U format, <#=PARAMS#>)
            where T : unmanaged, INativeList, IUTF8Bytes
            where U : unmanaged, INativeList, IUTF8Bytes
<#
        for (var a = 0; a < ARGS; ++a)
            WriteLine("            where T{0} : unmanaged, INativeList, IUTF8Bytes", a);
#>
        {
            ref var formatRef = ref UnsafeUtilityExtensions.AsRef(in format);
            int formatLength = formatRef.Length;
            byte* formatBytes = formatRef.GetUnsafePtr();
            int i = 0;
            FormatError err = FormatError.None;
            while (i < formatLength)
            {
                byte currByte = formatBytes[i++];
                if (currByte == (byte)'{')
                {
                    if (i < formatLength)
                        currByte = formatBytes[i++];
                    else
                        return FormatError.BadFormatSpecifier;
                    if (currByte >= (byte)'0' && currByte <= (byte)'9' && i < formatLength && formatBytes[i++] == (byte)'}')
                    {
                        switch (currByte - (byte)'0')
                        {
<#
        for(var a = 0; a < ARGS; ++a)
        {
            WriteLine($"                            case {a}: err = dest.Append(in arg{a}); break;");
        }
#>
                            default: err = FormatError.BadFormatSpecifier; break;
                        }
                    }
                    else if (currByte == (byte)'{')
                        err = dest.AppendRawByte(currByte);
                    else
                        err = FormatError.BadFormatSpecifier;
                }
                else if (currByte == (byte)'}')
                {
                    if (i < formatLength)
                        currByte = formatBytes[i++];
                    else
                        err = FormatError.BadFormatSpecifier;
                    if (currByte == (byte)'}')
                        err = dest.AppendRawByte(currByte);
                    else
                        err = FormatError.BadFormatSpecifier;
                }
                else
                    err = dest.AppendRawByte(currByte);
                if (err != FormatError.None)
                    return err;
            }
            return FormatError.None;
        }
<#
    }
#>
    }
}