Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
EncodingExtensions.cs
Go to the documentation of this file.
3
4namespace System.Text;
5
6public static class EncodingExtensions
7{
9 {
10 if (encoding == null)
11 {
12 throw new ArgumentNullException("encoding");
13 }
14 if (writer == null)
15 {
16 throw new ArgumentNullException("writer");
17 }
18 if (chars.Length <= 1048576)
19 {
20 int byteCount = encoding.GetByteCount(chars);
22 int bytes = encoding.GetBytes(chars, span);
23 writer.Advance(bytes);
24 return bytes;
25 }
26 encoding.GetEncoder().Convert(chars, writer, flush: true, out var bytesUsed, out var _);
27 return bytesUsed;
28 }
29
31 {
32 if (encoding == null)
33 {
34 throw new ArgumentNullException("encoding");
35 }
36 if (writer == null)
37 {
38 throw new ArgumentNullException("writer");
39 }
40 if (chars.IsSingleSegment)
41 {
42 return encoding.GetBytes(chars.FirstSpan, writer);
43 }
44 encoding.GetEncoder().Convert(in chars, writer, flush: true, out var bytesUsed, out var _);
45 return bytesUsed;
46 }
47
48 public static int GetBytes(this Encoding encoding, in ReadOnlySequence<char> chars, Span<byte> bytes)
49 {
50 if (encoding == null)
51 {
52 throw new ArgumentNullException("encoding");
53 }
54 if (chars.IsSingleSegment)
55 {
56 return encoding.GetBytes(chars.FirstSpan, bytes);
57 }
59 int length = bytes.Length;
60 Encoder encoder = encoding.GetEncoder();
61 bool isSingleSegment;
62 do
63 {
64 readOnlySequence.GetFirstSpan(out var first, out var next);
65 isSingleSegment = readOnlySequence.IsSingleSegment;
66 int bytes2 = encoder.GetBytes(first, bytes, isSingleSegment);
67 bytes = bytes.Slice(bytes2);
69 }
70 while (!isSingleSegment);
71 return length - bytes.Length;
72 }
73
74 public static byte[] GetBytes(this Encoding encoding, in ReadOnlySequence<char> chars)
75 {
76 if (encoding == null)
77 {
78 throw new ArgumentNullException("encoding");
79 }
80 if (chars.IsSingleSegment)
81 {
83 byte[] array = new byte[encoding.GetByteCount(firstSpan)];
84 encoding.GetBytes(firstSpan, array);
85 return array;
86 }
87 Encoder encoder = encoding.GetEncoder();
88 List<(byte[], int)> list = new List<(byte[], int)>();
89 int num = 0;
91 bool isSingleSegment;
92 do
93 {
94 readOnlySequence.GetFirstSpan(out var first, out var next);
95 isSingleSegment = readOnlySequence.IsSingleSegment;
96 int byteCount = encoder.GetByteCount(first, isSingleSegment);
97 byte[] array2 = ArrayPool<byte>.Shared.Rent(byteCount);
98 int bytes = encoder.GetBytes(first, array2, isSingleSegment);
99 list.Add((array2, bytes));
100 num += bytes;
101 if (num < 0)
102 {
103 num = int.MaxValue;
104 break;
105 }
107 }
108 while (!isSingleSegment);
109 byte[] array3 = new byte[num];
111 foreach (var (array4, num2) in list)
112 {
113 array4.AsSpan(0, num2).CopyTo(destination);
114 ArrayPool<byte>.Shared.Return(array4);
116 }
117 return array3;
118 }
119
121 {
122 if (encoding == null)
123 {
124 throw new ArgumentNullException("encoding");
125 }
126 if (writer == null)
127 {
128 throw new ArgumentNullException("writer");
129 }
130 if (bytes.Length <= 1048576)
131 {
132 int charCount = encoding.GetCharCount(bytes);
133 Span<char> span = writer.GetSpan(charCount);
134 int chars = encoding.GetChars(bytes, span);
135 writer.Advance(chars);
136 return chars;
137 }
138 encoding.GetDecoder().Convert(bytes, writer, flush: true, out var charsUsed, out var _);
139 return charsUsed;
140 }
141
143 {
144 if (encoding == null)
145 {
146 throw new ArgumentNullException("encoding");
147 }
148 if (writer == null)
149 {
150 throw new ArgumentNullException("writer");
151 }
152 if (bytes.IsSingleSegment)
153 {
154 return encoding.GetChars(bytes.FirstSpan, writer);
155 }
156 encoding.GetDecoder().Convert(in bytes, writer, flush: true, out var charsUsed, out var _);
157 return charsUsed;
158 }
159
161 {
162 if (encoding == null)
163 {
164 throw new ArgumentNullException("encoding");
165 }
166 if (bytes.IsSingleSegment)
167 {
168 return encoding.GetChars(bytes.FirstSpan, chars);
169 }
171 int length = chars.Length;
172 Decoder decoder = encoding.GetDecoder();
173 bool isSingleSegment;
174 do
175 {
176 readOnlySequence.GetFirstSpan(out var first, out var next);
177 isSingleSegment = readOnlySequence.IsSingleSegment;
178 int chars2 = decoder.GetChars(first, chars, isSingleSegment);
179 chars = chars.Slice(chars2);
181 }
182 while (!isSingleSegment);
183 return length - chars.Length;
184 }
185
186 public static string GetString(this Encoding encoding, in ReadOnlySequence<byte> bytes)
187 {
188 if (encoding == null)
189 {
190 throw new ArgumentNullException("encoding");
191 }
192 if (bytes.IsSingleSegment)
193 {
194 return encoding.GetString(bytes.FirstSpan);
195 }
196 Decoder decoder = encoding.GetDecoder();
197 List<(char[], int)> list = new List<(char[], int)>();
198 int num = 0;
200 bool isSingleSegment;
201 do
202 {
203 readOnlySequence.GetFirstSpan(out var first, out var next);
204 isSingleSegment = readOnlySequence.IsSingleSegment;
205 int charCount = decoder.GetCharCount(first, isSingleSegment);
206 char[] array = ArrayPool<char>.Shared.Rent(charCount);
207 int chars = decoder.GetChars(first, array, isSingleSegment);
208 list.Add((array, chars));
209 num += chars;
210 if (num < 0)
211 {
212 num = int.MaxValue;
213 break;
214 }
216 }
217 while (!isSingleSegment);
218 return string.Create(num, list, delegate(Span<char> span, List<(char[], int)> listOfSegments)
219 {
220 foreach (var (array2, num2) in listOfSegments)
221 {
222 array2.AsSpan(0, num2).CopyTo(span);
223 ArrayPool<char>.Shared.Return(array2);
224 span = span.Slice(num2);
225 }
226 });
227 }
228
229 public static void Convert(this Encoder encoder, ReadOnlySpan<char> chars, IBufferWriter<byte> writer, bool flush, out long bytesUsed, out bool completed)
230 {
231 if (encoder == null)
232 {
233 throw new ArgumentNullException("encoder");
234 }
235 if (writer == null)
236 {
237 throw new ArgumentNullException("writer");
238 }
239 long num = 0L;
240 do
241 {
242 int sizeHint = ((chars.Length <= 1048576) ? encoder.GetByteCount(chars, flush) : encoder.GetByteCount(chars.Slice(0, 1048576), flush: false));
243 Span<byte> span = writer.GetSpan(sizeHint);
244 encoder.Convert(chars, span, flush, out var charsUsed, out var bytesUsed2, out completed);
245 chars = chars.Slice(charsUsed);
246 writer.Advance(bytesUsed2);
247 num += bytesUsed2;
248 }
249 while (!chars.IsEmpty);
250 bytesUsed = num;
251 }
252
253 public static void Convert(this Encoder encoder, in ReadOnlySequence<char> chars, IBufferWriter<byte> writer, bool flush, out long bytesUsed, out bool completed)
254 {
256 long num = 0L;
257 bool isSingleSegment;
258 do
259 {
260 readOnlySequence.GetFirstSpan(out var first, out var next);
261 isSingleSegment = readOnlySequence.IsSingleSegment;
263 num += bytesUsed2;
265 }
266 while (!isSingleSegment);
267 bytesUsed = num;
268 }
269
270 public static void Convert(this Decoder decoder, ReadOnlySpan<byte> bytes, IBufferWriter<char> writer, bool flush, out long charsUsed, out bool completed)
271 {
272 if (decoder == null)
273 {
274 throw new ArgumentNullException("decoder");
275 }
276 if (writer == null)
277 {
278 throw new ArgumentNullException("writer");
279 }
280 long num = 0L;
281 do
282 {
283 int sizeHint = ((bytes.Length <= 1048576) ? decoder.GetCharCount(bytes, flush) : decoder.GetCharCount(bytes.Slice(0, 1048576), flush: false));
284 Span<char> span = writer.GetSpan(sizeHint);
285 decoder.Convert(bytes, span, flush, out var bytesUsed, out var charsUsed2, out completed);
286 bytes = bytes.Slice(bytesUsed);
287 writer.Advance(charsUsed2);
288 num += charsUsed2;
289 }
290 while (!bytes.IsEmpty);
291 charsUsed = num;
292 }
293
294 public static void Convert(this Decoder decoder, in ReadOnlySequence<byte> bytes, IBufferWriter<char> writer, bool flush, out long charsUsed, out bool completed)
295 {
297 long num = 0L;
298 bool isSingleSegment;
299 do
300 {
301 readOnlySequence.GetFirstSpan(out var first, out var next);
302 isSingleSegment = readOnlySequence.IsSingleSegment;
304 num += charsUsed2;
306 }
307 while (!isSingleSegment);
308 charsUsed = num;
309 }
310}
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
virtual void Convert(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed)
Definition Decoder.cs:142
int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
int GetCharCount(byte[] bytes, int index, int count)
int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)
virtual void Convert(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
Definition Encoder.cs:135
int GetByteCount(char[] chars, int index, int count, bool flush)
static string GetString(this Encoding encoding, in ReadOnlySequence< byte > bytes)
static long GetBytes(this Encoding encoding, in ReadOnlySequence< char > chars, IBufferWriter< byte > writer)
static long GetChars(this Encoding encoding, in ReadOnlySequence< byte > bytes, IBufferWriter< char > writer)
static void Convert(this Decoder decoder, in ReadOnlySequence< byte > bytes, IBufferWriter< char > writer, bool flush, out long charsUsed, out bool completed)
static void Convert(this Encoder encoder, ReadOnlySpan< char > chars, IBufferWriter< byte > writer, bool flush, out long bytesUsed, out bool completed)
static int GetChars(this Encoding encoding, in ReadOnlySequence< byte > bytes, Span< char > chars)
static void Convert(this Decoder decoder, ReadOnlySpan< byte > bytes, IBufferWriter< char > writer, bool flush, out long charsUsed, out bool completed)
static byte[] GetBytes(this Encoding encoding, in ReadOnlySequence< char > chars)
static int GetBytes(this Encoding encoding, in ReadOnlySequence< char > chars, Span< byte > bytes)
static long GetBytes(this Encoding encoding, ReadOnlySpan< char > chars, IBufferWriter< byte > writer)
static long GetChars(this Encoding encoding, ReadOnlySpan< byte > bytes, IBufferWriter< char > writer)
static void Convert(this Encoder encoder, in ReadOnlySequence< char > chars, IBufferWriter< byte > writer, bool flush, out long bytesUsed, out bool completed)
virtual int GetCharCount(byte[] bytes)
Definition Encoding.cs:887
virtual byte[] GetBytes(char[] chars)
Definition Encoding.cs:781
virtual Decoder GetDecoder()
Definition Encoding.cs:1004
virtual Encoder GetEncoder()
Definition Encoding.cs:1009
virtual char[] GetChars(byte[] bytes)
Definition Encoding.cs:921
virtual int GetByteCount(char[] chars)
Definition Encoding.cs:713
unsafe string GetString(byte *bytes, int byteCount)
Definition Encoding.cs:973