A game about forced loneliness, made by TACStudios
at master 125 lines 5.5 kB view raw
1<#/*THIS IS A T4 FILE - see t4_text_templating.md for what it is and how to run codegen*/#> 2<#@ template debug="True" #> 3<#@ output extension=".gen.cs" encoding="utf-8" #> 4<#@ assembly name="System.Core" #> 5<#@ import namespace="System.Linq" #> 6 7//------------------------------------------------------------------------------ 8// <auto-generated> 9// This code was generated by a tool. 10// 11// TextTransform Samples/Packages/com.unity.collections/Unity.Collections/FixedStringFormatMethods.tt 12// 13// Changes to this file may cause incorrect behavior and will be lost if 14// the code is regenerated. 15// </auto-generated> 16//------------------------------------------------------------------------------ 17 18using System; 19using Unity.Collections.LowLevel.Unsafe; 20 21namespace Unity.Collections 22{ 23 /// <summary> 24 /// Provides extension methods for FixedString*N*Bytes. 25 /// </summary> 26 public unsafe static partial class FixedStringMethods 27 { 28<# 29 for (var ARGS = 1; ARGS <= 10; ++ARGS) 30 { 31 var TYPES = String.Join(", ", Enumerable.Range(0, ARGS).Select(n => $"T{n}")); 32 var PARAMS = String.Join(", ", Enumerable.Range(0, ARGS).Select(n => $"in T{n} arg{n}")); 33 var ARGNAMES = String.Join(", ", Enumerable.Range(0, ARGS).Select(n => $"arg{n}")); 34 var TxDOCS = String.Join("\r\n /// ", Enumerable.Range(0, ARGS).Select(n => $"<typeparam name=\"T{n}\">The type of value to interpolate into the format string.</typeparam>")); 35 var ARGxDOCS = String.Join("\r\n /// ", Enumerable.Range(0, ARGS).Select(n => $"<param name=\"arg{n}\">A FixedString*N*Bytes to interpolate into the format string.</param>")); 36 var BCOMPAT = String.Join(", ", Enumerable.Range(0, ARGS).Select(n => $"typeof(FixedString128Bytes /*T{n}*/)")); 37#> 38 /// <summary> 39 /// Interpolates strings into a format string and appends the result to this string. 40 /// </summary> 41 /// <remarks> 42 /// Similar to `StringBuilder.AppendFormat` but with significant limitations: 43 /// - Only supports FixedString*N*Bytes arguments. To use other string types, convert them to FixedString*N*Bytes first. 44 /// - Only supports numeric format placeholders of the form `{0}` .. `{N}`. 45 /// - No format modifiers (*e.g.* `{0:x}`) are supported. 46 /// 47 /// The overloads of this method take up to ten strings to interpolate into the format string. 48 /// </remarks> 49 /// <typeparam name="T">A FixedString*N*Bytes type.</typeparam> 50 /// <typeparam name="U">A FixedString*N*Bytes type.</typeparam> 51 /// <#=TxDOCS#> 52 /// <param name="dest">The string to append to.</param>d 53 /// <param name="format">A string to be interpolated and appended.</param> 54 /// <#=ARGxDOCS#> 55 /// <returns><see cref="FormatError.None"/> if successful. Otherwise returns the appropriate <see cref="FormatError"/>.</returns> 56 [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(FixedString128Bytes /*T*/), typeof(FixedString128Bytes /*U*/), <#=BCOMPAT#> })] 57 public static unsafe FormatError AppendFormat<T, U, <#=TYPES#>>(ref this T dest, in U format, <#=PARAMS#>) 58 where T : unmanaged, INativeList<byte>, IUTF8Bytes 59 where U : unmanaged, INativeList<byte>, IUTF8Bytes 60<# 61 for (var a = 0; a < ARGS; ++a) 62 WriteLine(" where T{0} : unmanaged, INativeList<byte>, IUTF8Bytes", a); 63#> 64 { 65 ref var formatRef = ref UnsafeUtilityExtensions.AsRef(in format); 66 int formatLength = formatRef.Length; 67 byte* formatBytes = formatRef.GetUnsafePtr(); 68 int i = 0; 69 FormatError err = FormatError.None; 70 while (i < formatLength) 71 { 72 byte currByte = formatBytes[i++]; 73 if (currByte == (byte)'{') 74 { 75 if (i < formatLength) 76 currByte = formatBytes[i++]; 77 else 78 return FormatError.BadFormatSpecifier; 79 80 if (currByte >= (byte)'0' && currByte <= (byte)'9' && i < formatLength && formatBytes[i++] == (byte)'}') 81 { 82 switch (currByte - (byte)'0') 83 { 84<# 85 for(var a = 0; a < ARGS; ++a) 86 { 87 WriteLine($" case {a}: err = dest.Append(in arg{a}); break;"); 88 } 89#> 90 default: err = FormatError.BadFormatSpecifier; break; 91 } 92 } 93 else if (currByte == (byte)'{') 94 err = dest.AppendRawByte(currByte); 95 else 96 err = FormatError.BadFormatSpecifier; 97 } 98 else if (currByte == (byte)'}') 99 { 100 if (i < formatLength) 101 currByte = formatBytes[i++]; 102 else 103 err = FormatError.BadFormatSpecifier; 104 105 if (currByte == (byte)'}') 106 err = dest.AppendRawByte(currByte); 107 else 108 err = FormatError.BadFormatSpecifier; 109 } 110 else 111 err = dest.AppendRawByte(currByte); 112 113 if (err != FormatError.None) 114 return err; 115 } 116 117 return FormatError.None; 118 } 119 120<# 121 } 122#> 123 124 } 125}