Geen omschrijving
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.

FixedStringInternalMethods.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. namespace Unity.Collections
  3. {
  4. [GenerateTestsForBurstCompatibility]
  5. public unsafe static partial class FixedStringMethods
  6. {
  7. /// <summary>
  8. /// Append two characters to this IUTF8Bytes. This is used as a helper for internal formatting.
  9. /// </summary>
  10. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(FixedString128Bytes) })]
  11. internal static FormatError Append<T>(ref this T fs, char a, char b)
  12. where T : unmanaged, INativeList<byte>, IUTF8Bytes
  13. {
  14. FormatError err = FormatError.None;
  15. err |= fs.Append((Unicode.Rune) a);
  16. err |= fs.Append((Unicode.Rune) b);
  17. if (err != FormatError.None)
  18. return FormatError.Overflow;
  19. return FormatError.None;
  20. }
  21. /// <summary>
  22. /// Append three characters to this IUTF8Bytes. This is used as a helper for internal formatting.
  23. /// </summary>
  24. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(FixedString128Bytes) })]
  25. internal static FormatError Append<T>(ref this T fs, char a, char b, char c)
  26. where T : unmanaged, INativeList<byte>, IUTF8Bytes
  27. {
  28. FormatError err = FormatError.None;
  29. err |= fs.Append((Unicode.Rune) a);
  30. err |= fs.Append((Unicode.Rune) b);
  31. err |= fs.Append((Unicode.Rune) c);
  32. if (err != FormatError.None)
  33. return FormatError.Overflow;
  34. return FormatError.None;
  35. }
  36. /// <summary>
  37. /// Append 'I' 'n' 'f' 'i' 'n' 'i' 't' 'y' characters to this IUTF8Bytes. This is used as a helper for internal formatting.
  38. /// </summary>
  39. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(FixedString128Bytes) })]
  40. internal static FormatError Append<T>(ref this T fs, char a, char b, char c, char d, char e, char f, char g, char h)
  41. where T : unmanaged, INativeList<byte>, IUTF8Bytes
  42. {
  43. FormatError err = FormatError.None;
  44. err |= fs.Append((Unicode.Rune) a);
  45. err |= fs.Append((Unicode.Rune) b);
  46. err |= fs.Append((Unicode.Rune) c);
  47. err |= fs.Append((Unicode.Rune) d);
  48. err |= fs.Append((Unicode.Rune) e);
  49. err |= fs.Append((Unicode.Rune) f);
  50. err |= fs.Append((Unicode.Rune) g);
  51. err |= fs.Append((Unicode.Rune) h);
  52. if (err != FormatError.None)
  53. return FormatError.Overflow;
  54. return FormatError.None;
  55. }
  56. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(FixedString128Bytes) })]
  57. internal static FormatError AppendScientific<T>(ref this T fs, char *source, int sourceLength, int decimalExponent, char decimalSeparator = '.')
  58. where T : unmanaged, INativeList<byte>, IUTF8Bytes
  59. {
  60. FormatError error;
  61. if ((error = fs.Append(source[0])) != FormatError.None)
  62. return error;
  63. if (sourceLength > 1)
  64. {
  65. if ((error = fs.Append(decimalSeparator)) != FormatError.None)
  66. return error;
  67. for (var i = 1; i < sourceLength; ++i)
  68. {
  69. if ((error = fs.Append(source[i])) != FormatError.None)
  70. return error;
  71. }
  72. }
  73. if ((error = fs.Append('E')) != FormatError.None)
  74. return error;
  75. if (decimalExponent < 0)
  76. {
  77. if ((error = fs.Append('-')) != FormatError.None)
  78. return error;
  79. decimalExponent *= -1;
  80. decimalExponent -= sourceLength - 1;
  81. }
  82. else
  83. {
  84. if ((error = fs.Append('+')) != FormatError.None)
  85. return error;
  86. decimalExponent += sourceLength - 1;
  87. }
  88. var ascii = stackalloc char[2];
  89. const int decimalDigits = 2;
  90. for (var i = 0; i < decimalDigits; ++i)
  91. {
  92. var decimalDigit = decimalExponent % 10;
  93. ascii[1 - i] = (char)('0' + decimalDigit);
  94. decimalExponent /= 10;
  95. }
  96. for (var i = 0; i < decimalDigits; ++i)
  97. if ((error = fs.Append(ascii[i])) != FormatError.None)
  98. return error;
  99. return FormatError.None;
  100. }
  101. /// <summary>
  102. /// Check if runes a, b, c are found at offset offset
  103. /// </summary>
  104. /// <param name="offset">The target offset</param>
  105. /// <param name="a">rune a</param>
  106. /// <param name="b">rune b</param>
  107. /// <param name="c">rune c</param>
  108. /// <returns></returns>
  109. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(FixedString128Bytes) })]
  110. internal static bool Found<T>(ref this T fs, ref int offset, char a, char b, char c)
  111. where T : unmanaged, INativeList<byte>, IUTF8Bytes
  112. {
  113. int old = offset;
  114. if ((fs.Read(ref offset).value | 32) == a
  115. && (fs.Read(ref offset).value | 32) == b
  116. && (fs.Read(ref offset).value | 32) == c)
  117. return true;
  118. offset = old;
  119. return false;
  120. }
  121. /// <summary>
  122. ///
  123. /// </summary>
  124. /// <param name="offset"></param>
  125. /// <param name="a"></param>
  126. /// <param name="b"></param>
  127. /// <param name="c"></param>
  128. /// <param name="d"></param>
  129. /// <param name="e"></param>
  130. /// <param name="f"></param>
  131. /// <param name="g"></param>
  132. /// <param name="h"></param>
  133. /// <returns></returns>
  134. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(FixedString128Bytes) })]
  135. internal static bool Found<T>(ref this T fs, ref int offset, char a, char b, char c, char d, char e, char f, char g, char h)
  136. where T : unmanaged, INativeList<byte>, IUTF8Bytes
  137. {
  138. int old = offset;
  139. if ((fs.Read(ref offset).value | 32) == a
  140. && (fs.Read(ref offset).value | 32) == b
  141. && (fs.Read(ref offset).value | 32) == c
  142. && (fs.Read(ref offset).value | 32) == d
  143. && (fs.Read(ref offset).value | 32) == e
  144. && (fs.Read(ref offset).value | 32) == f
  145. && (fs.Read(ref offset).value | 32) == g
  146. && (fs.Read(ref offset).value | 32) == h)
  147. return true;
  148. offset = old;
  149. return false;
  150. }
  151. }
  152. }