Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FixedStringFormat.tt 5.8KB

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