Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
OSEncoder.cs
Go to the documentation of this file.
2
3namespace System.Text;
4
5internal sealed class OSEncoder : Encoder
6{
7 private char _charLeftOver;
8
9 private readonly Encoding _encoding;
10
11 internal OSEncoder(Encoding encoding)
12 {
13 _encoding = encoding;
14 Reset();
15 }
16
17 public override void Reset()
18 {
19 _charLeftOver = '\0';
20 }
21
22 public unsafe override int GetByteCount(char[] chars, int index, int count, bool flush)
23 {
24 if (chars == null)
25 {
27 }
28 if (index < 0 || count < 0)
29 {
31 }
32 if (chars.Length - index < count)
33 {
35 }
36 if (chars.Length == 0 && (_charLeftOver == '\0' || !flush))
37 {
38 return 0;
39 }
40 fixed (char* ptr = chars)
41 {
42 Unsafe.SkipInit(out char c);
43 char* chars2 = ((ptr == null) ? (&c) : (ptr + index));
44 return GetByteCount(chars2, count, flush);
45 }
46 }
47
48 private unsafe int ConvertWithLeftOverChar(char* chars, int count, byte* bytes, int byteCount)
49 {
50 char* ptr = stackalloc char[2];
51 *ptr = _charLeftOver;
52 int num = 0;
53 if (count > 0 && char.IsLowSurrogate(*chars))
54 {
55 ptr[1] = *chars;
56 num++;
57 }
59 if (count - num > 0)
60 {
61 num2 += OSEncoding.WideCharToMultiByte(_encoding.CodePage, chars + num, count - num, (bytes == null) ? null : (bytes + num2), (bytes != null) ? (byteCount - num2) : 0);
62 }
63 return num2;
64 }
65
66 public unsafe override int GetByteCount(char* chars, int count, bool flush)
67 {
68 if (chars == null)
69 {
71 }
72 if (count < 0)
73 {
75 }
76 bool flag = count > 0 && !flush && char.IsHighSurrogate(chars[count - 1]);
77 if (flag)
78 {
79 count--;
80 }
81 if (_charLeftOver == '\0')
82 {
83 if (count <= 0)
84 {
85 return 0;
86 }
88 }
89 if (count == 0 && !flag && !flush)
90 {
91 return 0;
92 }
93 return ConvertWithLeftOverChar(chars, count, null, 0);
94 }
95
96 public unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)
97 {
98 if (chars == null || bytes == null)
99 {
100 throw new ArgumentNullException((chars == null) ? "chars" : "bytes", System.SR.ArgumentNull_Array);
101 }
102 if (charIndex < 0 || charCount < 0)
103 {
104 throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", System.SR.ArgumentOutOfRange_NeedNonNegNum);
105 }
106 if (chars.Length - charIndex < charCount)
107 {
109 }
110 if (byteIndex < 0 || byteIndex > bytes.Length)
111 {
113 }
114 if (bytes.Length == 0)
115 {
116 return 0;
117 }
118 if (charCount == 0 && (_charLeftOver == '\0' || !flush))
119 {
120 return 0;
121 }
122 fixed (char* ptr = chars)
123 {
124 fixed (byte* ptr2 = &bytes[0])
125 {
126 Unsafe.SkipInit(out char c);
127 char* chars2 = ((ptr == null) ? (&c) : (ptr + charIndex));
128 return GetBytes(chars2, charCount, ptr2 + byteIndex, bytes.Length - byteIndex, flush);
129 }
130 }
131 }
132
133 public unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, bool flush)
134 {
135 if (chars == null || bytes == null)
136 {
137 throw new ArgumentNullException((chars == null) ? "chars" : "bytes", System.SR.ArgumentNull_Array);
138 }
139 if (byteCount < 0 || charCount < 0)
140 {
141 throw new ArgumentOutOfRangeException((byteCount < 0) ? "byteCount" : "charCount", System.SR.ArgumentOutOfRange_NeedNonNegNum);
142 }
143 if (byteCount == 0)
144 {
145 return 0;
146 }
147 char c = ((charCount > 0 && !flush && char.IsHighSurrogate(chars[charCount - 1])) ? chars[charCount - 1] : '\0');
148 if (c != 0)
149 {
150 charCount--;
151 }
152 if (_charLeftOver == '\0')
153 {
154 if (charCount <= 0)
155 {
156 _charLeftOver = c;
157 return 0;
158 }
160 _charLeftOver = c;
161 return result;
162 }
163 if (charCount == 0 && c == '\0' && !flush)
164 {
165 return 0;
166 }
168 _charLeftOver = c;
169 return result2;
170 }
171
172 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)
173 {
174 if (chars == null || bytes == null)
175 {
176 throw new ArgumentNullException((chars == null) ? "chars" : "bytes", System.SR.ArgumentNull_Array);
177 }
178 if (charIndex < 0 || charCount < 0)
179 {
180 throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", System.SR.ArgumentOutOfRange_NeedNonNegNum);
181 }
182 if (byteIndex < 0 || byteCount < 0)
183 {
184 throw new ArgumentOutOfRangeException((byteIndex < 0) ? "byteIndex" : "byteCount", System.SR.ArgumentOutOfRange_NeedNonNegNum);
185 }
186 if (chars.Length - charIndex < charCount)
187 {
189 }
190 if (bytes.Length - byteIndex < byteCount)
191 {
193 }
194 if (bytes.Length == 0 || (chars.Length == 0 && (_charLeftOver == '\0' || !flush)))
195 {
196 bytesUsed = 0;
197 charsUsed = 0;
198 completed = false;
199 return;
200 }
201 fixed (char* ptr = chars)
202 {
203 fixed (byte* ptr2 = &bytes[0])
204 {
205 Unsafe.SkipInit(out char c);
206 char* chars2 = ((ptr == null) ? (&c) : (ptr + charIndex));
207 Convert(chars2, charCount, ptr2 + byteIndex, byteCount, flush, out charsUsed, out bytesUsed, out completed);
208 }
209 }
210 }
211
212 public unsafe override void Convert(char* chars, int charCount, byte* bytes, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
213 {
214 if (bytes == null || chars == null)
215 {
216 throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", System.SR.ArgumentNull_Array);
217 }
218 if (charCount < 0 || byteCount < 0)
219 {
220 throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", System.SR.ArgumentOutOfRange_NeedNonNegNum);
221 }
222 int num;
223 for (num = charCount; num > 0; num /= 2)
224 {
225 int byteCount2 = GetByteCount(chars, num, flush);
226 if (byteCount2 <= byteCount)
227 {
228 break;
229 }
230 }
231 if (num > 0)
232 {
233 bytesUsed = GetBytes(chars, num, bytes, byteCount, flush);
234 charsUsed = num;
235 completed = _charLeftOver == '\0' && charCount == num;
236 }
237 else
238 {
239 bytesUsed = 0;
240 charsUsed = 0;
241 completed = false;
242 }
243 }
244}
static string ArgumentOutOfRange_Index
Definition SR.cs:30
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
virtual int CodePage
Definition Encoding.cs:515
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)
Definition OSEncoder.cs:172
unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)
Definition OSEncoder.cs:96
unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, bool flush)
Definition OSEncoder.cs:133
unsafe int ConvertWithLeftOverChar(char *chars, int count, byte *bytes, int byteCount)
Definition OSEncoder.cs:48
readonly Encoding _encoding
Definition OSEncoder.cs:9
OSEncoder(Encoding encoding)
Definition OSEncoder.cs:11
override void Reset()
Definition OSEncoder.cs:17
unsafe override int GetByteCount(char[] chars, int index, int count, bool flush)
Definition OSEncoder.cs:22
unsafe override int GetByteCount(char *chars, int count, bool flush)
Definition OSEncoder.cs:66
unsafe override void Convert(char *chars, int charCount, byte *bytes, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
Definition OSEncoder.cs:212
static unsafe int WideCharToMultiByte(int codePage, char *pChars, int count, byte *pBytes, int byteCount)