暫無描述
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.

FixedStringFormatMethods.tt 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /// <returns><see cref="FormatError.None"/> if successful. Otherwise returns the appropriate <see cref="FormatError"/>.</returns>
  53. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(FixedString128Bytes /*T*/), typeof(FixedString128Bytes /*U*/), <#=BCOMPAT#> })]
  54. public static unsafe FormatError AppendFormat<T, U, <#=TYPES#>>(ref this T dest, in U format, <#=PARAMS#>)
  55. where T : unmanaged, INativeList<byte>, IUTF8Bytes
  56. where U : unmanaged, INativeList<byte>, IUTF8Bytes
  57. <#
  58. for (var a = 0; a < ARGS; ++a)
  59. WriteLine(" where T{0} : unmanaged, INativeList<byte>, IUTF8Bytes", a);
  60. #>
  61. {
  62. ref var formatRef = ref UnsafeUtilityExtensions.AsRef(in format);
  63. int formatLength = formatRef.Length;
  64. byte* formatBytes = formatRef.GetUnsafePtr();
  65. int i = 0;
  66. FormatError err = FormatError.None;
  67. while (i < formatLength)
  68. {
  69. byte currByte = formatBytes[i++];
  70. if (currByte == (byte)'{')
  71. {
  72. if (i < formatLength)
  73. currByte = formatBytes[i++];
  74. else
  75. return FormatError.BadFormatSpecifier;
  76. if (currByte >= (byte)'0' && currByte <= (byte)'9' && i < formatLength && formatBytes[i++] == (byte)'}')
  77. {
  78. switch (currByte - (byte)'0')
  79. {
  80. <#
  81. for(var a = 0; a < ARGS; ++a)
  82. {
  83. WriteLine($" case {a}: err = dest.Append(in arg{a}); break;");
  84. }
  85. #>
  86. default: err = FormatError.BadFormatSpecifier; break;
  87. }
  88. }
  89. else if (currByte == (byte)'{')
  90. err = dest.AppendRawByte(currByte);
  91. else
  92. err = FormatError.BadFormatSpecifier;
  93. }
  94. else if (currByte == (byte)'}')
  95. {
  96. if (i < formatLength)
  97. currByte = formatBytes[i++];
  98. else
  99. err = FormatError.BadFormatSpecifier;
  100. if (currByte == (byte)'}')
  101. err = dest.AppendRawByte(currByte);
  102. else
  103. err = FormatError.BadFormatSpecifier;
  104. }
  105. else
  106. err = dest.AppendRawByte(currByte);
  107. if (err != FormatError.None)
  108. return err;
  109. }
  110. return FormatError.None;
  111. }
  112. <#
  113. }
  114. #>
  115. }
  116. }