Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

FixedStringFormatMethods.tt 4.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // <auto-generated>
  8. // This code was generated by a tool.
  9. //
  10. // TextTransform Samples/Packages/com.unity.collections/Unity.Collections/FixedStringFormatMethods.tt
  11. //
  12. // Changes to this file may cause incorrect behavior and will be lost if
  13. // the code is regenerated.
  14. // </auto-generated>
  15. //------------------------------------------------------------------------------
  16. using System;
  17. using Unity.Collections.LowLevel.Unsafe;
  18. namespace Unity.Collections
  19. {
  20. /// <summary>
  21. /// Provides extension methods for FixedString*N*Bytes.
  22. /// </summary>
  23. public unsafe static partial class FixedStringMethods
  24. {
  25. <#
  26. for (var ARGS = 1; ARGS <= 10; ++ARGS)
  27. {
  28. var TYPES = String.Join(", ", Enumerable.Range(0, ARGS).Select(n => $"T{n}"));
  29. var PARAMS = String.Join(", ", Enumerable.Range(0, ARGS).Select(n => $"in T{n} arg{n}"));
  30. var ARGNAMES = String.Join(", ", Enumerable.Range(0, ARGS).Select(n => $"arg{n}"));
  31. 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>"));
  32. 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>"));
  33. var BCOMPAT = String.Join(", ", Enumerable.Range(0, ARGS).Select(n => $"typeof(FixedString128Bytes /*T{n}*/)"));
  34. #>
  35. /// <summary>
  36. /// Interpolates strings into a format string and appends the result to this string.
  37. /// </summary>
  38. /// <remarks>
  39. /// Similar to `StringBuilder.AppendFormat` but with significant limitations:
  40. /// - Only supports FixedString*N*Bytes arguments. To use other string types, convert them to FixedString*N*Bytes first.
  41. /// - Only supports numeric format placeholders of the form `{0}` .. `{N}`.
  42. /// - No format modifiers (*e.g.* `{0:x}`) are supported.
  43. ///
  44. /// The overloads of this method take up to ten strings to interpolate into the format string.
  45. /// </remarks>
  46. /// <typeparam name="T">A FixedString*N*Bytes type.</typeparam>
  47. /// <typeparam name="U">A FixedString*N*Bytes type.</typeparam>
  48. /// <#=TxDOCS#>
  49. /// <param name="dest">The string to append to.</param>d
  50. /// <param name="format">A string to be interpolated and appended.</param>
  51. /// <#=ARGxDOCS#>
  52. [BurstCompatible(GenericTypeArguments = new[] { typeof(FixedString128Bytes /*T*/), typeof(FixedString128Bytes /*U*/), <#=BCOMPAT#> })]
  53. public static unsafe void AppendFormat<T, U, <#=TYPES#>>(ref this T dest, in U format, <#=PARAMS#>)
  54. where T : struct, INativeList<byte>, IUTF8Bytes
  55. where U : struct, INativeList<byte>, IUTF8Bytes
  56. <#
  57. for (var a = 0; a < ARGS; ++a)
  58. WriteLine(" where T{0} : struct, INativeList<byte>, IUTF8Bytes", a);
  59. #>
  60. {
  61. ref var formatRef = ref UnsafeUtilityExtensions.AsRef(in format);
  62. int formatLength = formatRef.Length;
  63. byte* formatBytes = formatRef.GetUnsafePtr();
  64. for (var i = 0; i < formatLength; ++i)
  65. {
  66. if (formatBytes[i] == (byte)'{')
  67. {
  68. if (formatLength - i >= 3 && formatBytes[i + 1] != (byte)'{')
  69. {
  70. var index = formatBytes[i + 1] - (byte)'0';
  71. switch (index)
  72. {
  73. <#
  74. for(var a = 0; a < ARGS; ++a)
  75. {
  76. WriteLine($" case {a}: dest.Append(in arg{a}); i+=2; break;");
  77. }
  78. #>
  79. default:
  80. dest.AppendRawByte(formatBytes[i]);
  81. break;
  82. }
  83. }
  84. }
  85. else
  86. dest.AppendRawByte(formatBytes[i]);
  87. }
  88. }
  89. <#
  90. }
  91. #>
  92. }
  93. }