Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
EncoderFallbackBuffer.cs
Go to the documentation of this file.
3
4namespace System.Text;
5
6public abstract class EncoderFallbackBuffer
7{
8 internal unsafe char* charStart;
9
10 internal unsafe char* charEnd;
11
13
14 internal bool setEncoder;
15
16 internal bool bUsedEncoder;
17
18 internal bool bFallingBack;
19
20 internal int iRecursionCount;
21
23
24 private int originalCharCount;
25
26 public abstract int Remaining { get; }
27
28 public abstract bool Fallback(char charUnknown, int index);
29
30 public abstract bool Fallback(char charUnknownHigh, char charUnknownLow, int index);
31
32 public abstract char GetNextChar();
33
34 public abstract bool MovePrevious();
35
36 public virtual void Reset()
37 {
38 while (GetNextChar() != 0)
39 {
40 }
41 }
42
43 internal unsafe void InternalReset()
44 {
45 charStart = null;
46 bFallingBack = false;
48 Reset();
49 }
50
51 internal unsafe void InternalInitialize(char* charStart, char* charEnd, EncoderNLS encoder, bool setEncoder)
52 {
53 this.charStart = charStart;
54 this.charEnd = charEnd;
55 this.encoder = encoder;
56 this.setEncoder = setEncoder;
57 bUsedEncoder = false;
58 bFallingBack = false;
60 }
61
63 {
65 encoderFallbackBuffer.encoding = encoding;
66 encoderFallbackBuffer.encoder = encoder;
67 encoderFallbackBuffer.originalCharCount = originalCharCount;
68 return encoderFallbackBuffer;
69 }
70
71 internal char InternalGetNextChar()
72 {
73 char nextChar = GetNextChar();
74 bFallingBack = nextChar != '\0';
75 if (nextChar == '\0')
76 {
78 }
79 return nextChar;
80 }
81
82 private bool InternalFallback(ReadOnlySpan<char> chars, out int charsConsumed)
83 {
84 char c = chars[0];
85 char c2 = '\0';
86 if (!chars.IsEmpty)
87 {
88 c = chars[0];
89 if (chars.Length > 1)
90 {
91 c2 = chars[1];
92 }
93 }
94 int index = originalCharCount - chars.Length;
95 if (!char.IsSurrogatePair(c, c2))
96 {
97 charsConsumed = 1;
98 return Fallback(c, index);
99 }
100 charsConsumed = 2;
101 return Fallback(c, c2, index);
102 }
103
104 internal int InternalFallbackGetByteCount(ReadOnlySpan<char> chars, out int charsConsumed)
105 {
106 int result = 0;
107 if (InternalFallback(chars, out charsConsumed))
108 {
110 }
111 return result;
112 }
113
114 internal bool TryInternalFallbackGetBytes(ReadOnlySpan<char> chars, Span<byte> bytes, out int charsConsumed, out int bytesWritten)
115 {
116 if (InternalFallback(chars, out charsConsumed))
117 {
118 return TryDrainRemainingDataForGetBytes(bytes, out bytesWritten);
119 }
120 bytesWritten = 0;
121 return true;
122 }
123
124 internal bool TryDrainRemainingDataForGetBytes(Span<byte> bytes, out int bytesWritten)
125 {
126 int length = bytes.Length;
127 while (true)
128 {
129 Rune nextRune;
130 Rune rune = (nextRune = GetNextRune());
131 if (rune.Value == 0)
132 {
133 break;
134 }
135 int bytesWritten2;
136 switch (encoding.EncodeRune(nextRune, bytes, out bytesWritten2))
137 {
138 case OperationStatus.Done:
139 bytes = bytes.Slice(bytesWritten2);
140 break;
141 case OperationStatus.DestinationTooSmall:
142 {
143 for (int i = 0; i < nextRune.Utf16SequenceLength; i++)
144 {
145 MovePrevious();
146 }
147 bytesWritten = length - bytes.Length;
148 return false;
149 }
150 case OperationStatus.InvalidData:
152 break;
153 }
154 }
155 bytesWritten = length - bytes.Length;
156 return true;
157 }
158
160 {
161 int num = 0;
162 while (true)
163 {
164 Rune nextRune;
165 Rune rune = (nextRune = GetNextRune());
166 if (rune.Value == 0)
167 {
168 break;
169 }
170 if (!encoding.TryGetByteCount(nextRune, out var byteCount))
171 {
173 }
174 num += byteCount;
175 if (num < 0)
176 {
179 }
180 }
181 return num;
182 }
183
185 {
186 char nextChar = GetNextChar();
187 if (Rune.TryCreate(nextChar, out var result) || Rune.TryCreate(nextChar, GetNextChar(), out result))
188 {
189 return result;
190 }
192 }
193
194 internal unsafe virtual bool InternalFallback(char ch, ref char* chars)
195 {
196 int index = (int)(chars - charStart) - 1;
197 if (char.IsHighSurrogate(ch))
198 {
199 if (chars >= charEnd)
200 {
201 if (encoder != null && !encoder.MustFlush)
202 {
203 if (setEncoder)
204 {
205 bUsedEncoder = true;
206 encoder._charLeftOver = ch;
207 }
208 bFallingBack = false;
209 return false;
210 }
211 }
212 else
213 {
214 char c = *chars;
215 if (char.IsLowSurrogate(c))
216 {
217 if (bFallingBack && iRecursionCount++ > 250)
218 {
219 ThrowLastCharRecursive(char.ConvertToUtf32(ch, c));
220 }
221 chars++;
223 return bFallingBack;
224 }
225 }
226 }
227 if (bFallingBack && iRecursionCount++ > 250)
228 {
230 }
232 return bFallingBack;
233 }
234
235 [DoesNotReturn]
236 internal static void ThrowLastCharRecursive(int charRecursive)
237 {
238 throw new ArgumentException(SR.Format(SR.Argument_RecursiveFallback, charRecursive), "chars");
239 }
240}
static string Argument_InvalidCharSequenceNoIndex
Definition SR.cs:92
static string Argument_RecursiveFallback
Definition SR.cs:844
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
Definition SR.cs:7
bool Fallback(char charUnknownHigh, char charUnknownLow, int index)
static void ThrowLastCharRecursive(int charRecursive)
bool InternalFallback(ReadOnlySpan< char > chars, out int charsConsumed)
bool TryInternalFallbackGetBytes(ReadOnlySpan< char > chars, Span< byte > bytes, out int charsConsumed, out int bytesWritten)
static EncoderFallbackBuffer CreateAndInitialize(Encoding encoding, EncoderNLS encoder, int originalCharCount)
virtual unsafe bool InternalFallback(char ch, ref char *chars)
bool TryDrainRemainingDataForGetBytes(Span< byte > bytes, out int bytesWritten)
int InternalFallbackGetByteCount(ReadOnlySpan< char > chars, out int charsConsumed)
bool Fallback(char charUnknown, int index)
unsafe void InternalInitialize(char *charStart, char *charEnd, EncoderNLS encoder, bool setEncoder)
EncoderFallbackBuffer CreateFallbackBuffer()
new EncoderFallbackBuffer FallbackBuffer
Definition EncoderNLS.cs:26
static void ThrowConversionOverflow()
Definition Encoding.cs:1084
virtual OperationStatus EncodeRune(Rune value, Span< byte > bytes, out int bytesWritten)
Definition Encoding.cs:1114
EncoderFallback EncoderFallback
Definition Encoding.cs:460
virtual bool TryGetByteCount(Rune value, out int byteCount)
Definition Encoding.cs:1119
int Utf16SequenceLength
Definition Rune.cs:55
static bool TryCreate(char ch, out Rune result)
Definition Rune.cs:511