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