Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
EncoderNLS.cs
Go to the documentation of this file.
3
4namespace System.Text;
5
6internal class EncoderNLS : Encoder
7{
8 internal char _charLeftOver;
9
10 private readonly Encoding _encoding;
11
12 private bool _mustFlush;
13
14 internal bool _throwOnOverflow;
15
16 internal int _charsUsed;
17
19
20 public bool MustFlush => _mustFlush;
21
22 internal bool HasLeftoverData
23 {
24 get
25 {
26 if (_charLeftOver == '\0')
27 {
28 if (_fallbackBuffer != null)
29 {
30 return _fallbackBuffer.Remaining > 0;
31 }
32 return false;
33 }
34 return true;
35 }
36 }
37
38 internal virtual bool HasState => _charLeftOver != '\0';
39
40 internal EncoderNLS(Encoding encoding)
41 {
42 _encoding = encoding;
44 Reset();
45 }
46
47 public override void Reset()
48 {
49 _charLeftOver = '\0';
50 if (_fallbackBuffer != null)
51 {
53 }
54 }
55
56 public unsafe override int GetByteCount(char[] chars, int index, int count, bool flush)
57 {
58 if (chars == null)
59 {
61 }
62 if (index < 0 || count < 0)
63 {
64 throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", SR.ArgumentOutOfRange_NeedNonNegNum);
65 }
66 if (chars.Length - index < count)
67 {
69 }
70 int num = -1;
71 fixed (char* ptr = &MemoryMarshal.GetReference<char>(chars))
72 {
73 num = GetByteCount(ptr + index, count, flush);
74 }
75 return num;
76 }
77
78 public unsafe override int GetByteCount(char* chars, int count, bool flush)
79 {
80 if (chars == null)
81 {
83 }
84 if (count < 0)
85 {
87 }
88 _mustFlush = flush;
89 _throwOnOverflow = true;
90 return _encoding.GetByteCount(chars, count, this);
91 }
92
93 public unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)
94 {
95 if (chars == null || bytes == null)
96 {
97 throw new ArgumentNullException((chars == null) ? "chars" : "bytes", SR.ArgumentNull_Array);
98 }
99 if (charIndex < 0 || charCount < 0)
100 {
101 throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", SR.ArgumentOutOfRange_NeedNonNegNum);
102 }
103 if (chars.Length - charIndex < charCount)
104 {
106 }
107 if (byteIndex < 0 || byteIndex > bytes.Length)
108 {
110 }
111 int byteCount = bytes.Length - byteIndex;
112 fixed (char* ptr = &MemoryMarshal.GetReference<char>(chars))
113 {
114 fixed (byte* ptr2 = &MemoryMarshal.GetReference<byte>(bytes))
115 {
116 return GetBytes(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount, flush);
117 }
118 }
119 }
120
121 public unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, bool flush)
122 {
123 if (chars == null || bytes == null)
124 {
125 throw new ArgumentNullException((chars == null) ? "chars" : "bytes", SR.ArgumentNull_Array);
126 }
127 if (byteCount < 0 || charCount < 0)
128 {
129 throw new ArgumentOutOfRangeException((byteCount < 0) ? "byteCount" : "charCount", SR.ArgumentOutOfRange_NeedNonNegNum);
130 }
131 _mustFlush = flush;
132 _throwOnOverflow = true;
134 }
135
136 public unsafe override 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)
137 {
138 if (chars == null || bytes == null)
139 {
140 throw new ArgumentNullException((chars == null) ? "chars" : "bytes", SR.ArgumentNull_Array);
141 }
142 if (charIndex < 0 || charCount < 0)
143 {
144 throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", SR.ArgumentOutOfRange_NeedNonNegNum);
145 }
146 if (byteIndex < 0 || byteCount < 0)
147 {
148 throw new ArgumentOutOfRangeException((byteIndex < 0) ? "byteIndex" : "byteCount", SR.ArgumentOutOfRange_NeedNonNegNum);
149 }
150 if (chars.Length - charIndex < charCount)
151 {
153 }
154 if (bytes.Length - byteIndex < byteCount)
155 {
157 }
158 fixed (char* ptr = &MemoryMarshal.GetReference<char>(chars))
159 {
160 fixed (byte* ptr2 = &MemoryMarshal.GetReference<byte>(bytes))
161 {
162 Convert(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount, flush, out charsUsed, out bytesUsed, out completed);
163 }
164 }
165 }
166
167 public unsafe override void Convert(char* chars, int charCount, byte* bytes, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
168 {
169 if (bytes == null || chars == null)
170 {
171 throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", SR.ArgumentNull_Array);
172 }
173 if (charCount < 0 || byteCount < 0)
174 {
175 throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", SR.ArgumentOutOfRange_NeedNonNegNum);
176 }
177 _mustFlush = flush;
178 _throwOnOverflow = false;
179 _charsUsed = 0;
180 bytesUsed = _encoding.GetBytes(chars, charCount, bytes, byteCount, this);
181 charsUsed = _charsUsed;
182 completed = charsUsed == charCount && (!flush || !HasState) && (_fallbackBuffer == null || _fallbackBuffer.Remaining == 0);
183 }
184
185 internal void ClearMustFlush()
186 {
187 _mustFlush = false;
188 }
189
190 internal int DrainLeftoverDataForGetByteCount(ReadOnlySpan<char> chars, out int charsConsumed)
191 {
192 if (_fallbackBuffer != null && _fallbackBuffer.Remaining > 0)
193 {
195 }
196 charsConsumed = 0;
197 if (_charLeftOver == '\0')
198 {
199 return 0;
200 }
201 char c = '\0';
202 if (chars.IsEmpty)
203 {
204 if (!MustFlush)
205 {
206 return 0;
207 }
208 }
209 else
210 {
211 c = chars[0];
212 }
213 if (Rune.TryCreate(_charLeftOver, c, out var result))
214 {
215 charsConsumed = 1;
216 if (_encoding.TryGetByteCount(result, out var byteCount))
217 {
218 return byteCount;
219 }
220 bool flag = base.FallbackBuffer.Fallback(_charLeftOver, c, -1);
221 }
222 else
223 {
224 bool flag = base.FallbackBuffer.Fallback(_charLeftOver, -1);
225 }
227 }
228
229 internal bool TryDrainLeftoverDataForGetBytes(ReadOnlySpan<char> chars, Span<byte> bytes, out int charsConsumed, out int bytesWritten)
230 {
231 charsConsumed = 0;
232 bytesWritten = 0;
233 if (_charLeftOver != 0)
234 {
235 char c = '\0';
236 if (chars.IsEmpty)
237 {
238 if (!MustFlush)
239 {
240 charsConsumed = 0;
241 bytesWritten = 0;
242 return true;
243 }
244 }
245 else
246 {
247 c = chars[0];
248 }
250 _charLeftOver = '\0';
251 if (Rune.TryCreate(charLeftOver, c, out var result))
252 {
253 charsConsumed = 1;
254 switch (_encoding.EncodeRune(result, bytes, out bytesWritten))
255 {
256 case OperationStatus.Done:
257 return true;
258 case OperationStatus.DestinationTooSmall:
259 _encoding.ThrowBytesOverflow(this, nothingEncoded: true);
260 break;
261 case OperationStatus.InvalidData:
262 base.FallbackBuffer.Fallback(charLeftOver, c, -1);
263 break;
264 }
265 }
266 else
267 {
268 base.FallbackBuffer.Fallback(charLeftOver, -1);
269 }
270 }
271 if (_fallbackBuffer != null && _fallbackBuffer.Remaining > 0)
272 {
274 }
275 return true;
276 }
277}
static string ArgumentOutOfRange_Index
Definition SR.cs:30
static string Argument_EncoderFallbackNotEmpty
Definition SR.cs:590
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ArgumentOutOfRange_IndexCountBuffer
Definition SR.cs:78
static string ArgumentNull_Array
Definition SR.cs:24
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
Definition SR.cs:7
bool TryDrainRemainingDataForGetBytes(Span< byte > bytes, out int bytesWritten)
override void Reset()
Definition EncoderNLS.cs:47
unsafe override int GetByteCount(char *chars, int count, bool flush)
Definition EncoderNLS.cs:78
unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)
Definition EncoderNLS.cs:93
int DrainLeftoverDataForGetByteCount(ReadOnlySpan< char > chars, out int charsConsumed)
unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, bool flush)
bool TryDrainLeftoverDataForGetBytes(ReadOnlySpan< char > chars, Span< byte > bytes, out int charsConsumed, out int bytesWritten)
virtual bool HasState
Definition EncoderNLS.cs:38
unsafe override void Convert(char *chars, int charCount, byte *bytes, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
unsafe override int GetByteCount(char[] chars, int index, int count, bool flush)
Definition EncoderNLS.cs:56
unsafe override 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)
readonly Encoding _encoding
Definition EncoderNLS.cs:10
EncoderNLS(Encoding encoding)
Definition EncoderNLS.cs:40
EncoderFallbackBuffer _fallbackBuffer
Definition Encoder.cs:9
EncoderFallback _fallback
Definition Encoder.cs:7
virtual OperationStatus EncodeRune(Rune value, Span< byte > bytes, out int bytesWritten)
Definition Encoding.cs:1114
virtual byte[] GetBytes(char[] chars)
Definition Encoding.cs:781
virtual string EncodingName
Definition Encoding.cs:362
EncoderFallback EncoderFallback
Definition Encoding.cs:460
virtual bool TryGetByteCount(Rune value, out int byteCount)
Definition Encoding.cs:1119
virtual int GetByteCount(char[] chars)
Definition Encoding.cs:713
static bool TryCreate(char ch, out Rune result)
Definition Rune.cs:511