A game about forced loneliness, made by TACStudios
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.Collections" #>
6<#@ import namespace="System.Collections.Generic" #>
7<#@ import namespace="System.Linq" #>
8<#@ import namespace="System.Text" #>
9<#@ import namespace="System.Reflection" #>
10
11//------------------------------------------------------------------------------
12// <auto-generated>
13// This code was generated by a tool.
14//
15// TextTransform Samples/Packages/com.unity.collections/Unity.Collections/FixedStringFormat.tt
16//
17// Changes to this file may cause incorrect behavior and will be lost if
18// the code is regenerated.
19// </auto-generated>
20//------------------------------------------------------------------------------
21
22using System;
23using System.Runtime.InteropServices;
24using Unity.Collections.LowLevel.Unsafe;
25using UnityEngine.Internal;
26
27<#
28List<List<T>> AllCombinationsOf<T>(List<List<T>> sets)
29{
30 // need array bounds checking etc for production
31 var combinations = new List<List<T>>();
32
33 // prime the data
34 foreach (var value in sets[0])
35 combinations.Add(new List<T> { value });
36
37 foreach (var set in sets.Skip(1))
38 combinations = AddExtraSet(combinations, set);
39
40 return combinations;
41}
42
43List<List<T>> AddExtraSet<T>(List<List<T>> combinations, List<T> set)
44{
45 var newCombinations = from value in set
46 from combination in combinations
47 select new List<T>(combination) { value };
48
49 return newCombinations.ToList();
50}
51
52string WithCommas(IEnumerable<string> input)
53{
54 return string.Join(", ", input);
55}
56
57var ARGTYPES = new[] { "int", "float", "string", "FixedStringN", null };
58var ARGCOUNT = 4;
59
60var ARGSETS = new List<List<string>>();
61for (int i = 0; i < ARGCOUNT; ++i)
62 ARGSETS.Add(ARGTYPES.ToList());
63
64var ARGCOMBOS = AllCombinationsOf(ARGSETS);
65#>
66
67namespace Unity.Collections
68{
69 /// <summary>
70 /// Provides formatting methods for FixedString*N*.
71 /// </summary>
72 [GenerateTestsForBurstCompatibility]
73 public static class FixedString
74 {
75<#
76 foreach (var COMBO in ARGCOMBOS)
77 {
78 while (COMBO.Count != 0 && COMBO.Last() == null)
79 COMBO.RemoveAt(COMBO.Count - 1);
80 if (COMBO.Count == 0 || COMBO.IndexOf(null) != -1)
81 continue;
82
83 var numFixedStringN = COMBO.Count((s) => s == "FixedStringN");
84
85 // turn FixedStringN into T1..Tn
86 var GENERICSPARAM = "";
87 var BCOMPAT = "";
88
89 if (COMBO.Contains("string"))
90 {
91 BCOMPAT = "[ExcludeFromBurstCompatTesting(\"Takes managed string\")]";
92 }
93
94 var GENERICSCONSTRAINT = new StringBuilder();
95 var TxDOCS = new StringBuilder();
96 if (numFixedStringN > 0)
97 {
98 GENERICSPARAM = $"<{string.Join(",", Enumerable.Range(1, numFixedStringN).Select((n) => $"T{n}"))}>";
99 if (!COMBO.Contains("string"))
100 {
101 BCOMPAT = $"[GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] {{ {string.Join(", ", Enumerable.Range(1, numFixedStringN).Select((n) => $"typeof(FixedString32Bytes /*$T{n}*/)"))} }} )]";
102 }
103
104 for (int i = 0; i < numFixedStringN; ++i)
105 {
106 var index = COMBO.IndexOf("FixedStringN");
107 COMBO[index] = $"T{i + 1}";
108 GENERICSCONSTRAINT.Append($" where T{i + 1} : unmanaged, INativeList<byte>, IUTF8Bytes\n");
109 TxDOCS.Append($" /// <typeparam name=\"T{i + 1}\"><undoc /></typeparam>\r\n");
110 }
111 }
112
113 var ARGS = Enumerable.Range(0, COMBO.Count).Select((n) => $"{COMBO[n]} arg{n}");
114 var ARGNAMES = Enumerable.Range(0, COMBO.Count).Select((n) => $"arg{n}").ToList();
115 var CONVERSIONS = new StringBuilder();
116
117 for (int i = 0; i < COMBO.Count; ++i)
118 {
119 if (COMBO[i].StartsWith("T"))
120 continue;
121 CONVERSIONS.Append($" FixedString32Bytes carg{i} = default; carg{i}.Append(arg{i});\n");
122 ARGNAMES[i] = $"carg{i}";
123 }
124
125 var RETURNTYPE = "FixedString128Bytes";
126 if (COMBO.Count > 3)
127 RETURNTYPE = "FixedString512Bytes";
128
129 var ARGxDOCS = String.Join("\r\n /// ", Enumerable.Range(0, COMBO.Count).Select(n => $"<param name=\"arg{n}\">Value to interpolate into the format string.</param>"));
130#>
131
132 /// <summary>
133 /// Returns a new string produced by interpolating a format string.
134 /// </summary>
135 /// <remarks>
136 /// Similar to StringBuilder.AppendFormat but with significant limitations:
137 /// - Only supports numeric format placeholders of the form `{0}` .. `{N}`.
138 /// - No format modifiers (*e.g.* `{0:x}`) are supported.
139 ///
140 /// The various overloads of this method take up to four values for interpolation. The allowed argument types are:
141 /// - FixedString*N*Bytes
142 /// - string
143 /// - int
144 /// - float
145 /// - structs implementing INativeList<byte> and IUTF8Bytes
146 ///
147 /// <seealso cref="FixedStringMethods.AppendFormat"/>
148 /// </remarks>
149 /// <param name="formatString">The format string.</param>
150<#=TxDOCS#>
151 /// <#=ARGxDOCS#>
152 /// <returns>A new string produced by interpolating the format string.</returns>
153 <#=BCOMPAT#>
154 public static <#=RETURNTYPE#> Format<#=GENERICSPARAM#>(<#=RETURNTYPE#> formatString, <#=WithCommas(ARGS)#>)
155<#=GENERICSCONSTRAINT#>
156 {
157 <#=RETURNTYPE#> result = default;
158<#=CONVERSIONS#>
159
160 result.AppendFormat(formatString, <#=WithCommas(ARGNAMES)#>);
161 return result;
162 }
163<#
164 }
165#>
166
167 }
168}