forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonAsciiEncoding.cs
More file actions
342 lines (297 loc) · 12.3 KB
/
PythonAsciiEncoding.cs
File metadata and controls
342 lines (297 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using System.Reflection;
using IronPython.Runtime.Operations;
namespace IronPython.Runtime {
/// <summary>
/// Simple implementation of ASCII encoding/decoding. The default instance (PythonAsciiEncoding.Instance) is
/// setup to always convert even values outside of the ASCII range. The EncoderFallback/DecoderFallbacks can
/// be replaced with versions that will throw exceptions instead though.
/// </summary>
[Serializable]
internal sealed class PythonAsciiEncoding : Encoding {
// Singleton (global) instances are readonly, so their fallbacks cannot be accidentally modified unless cloned
internal static readonly Encoding Instance = MakeNonThrowing();
internal PythonAsciiEncoding()
: base() {
}
internal PythonAsciiEncoding(EncoderFallback encoderFallback, DecoderFallback decoderFallback)
#if !NET45
// base(0, encoderFallback, decoderFallback) publicly accessible only in .NET Framework 4.6 and up
: base(0, encoderFallback, decoderFallback) {
#else
: base() {
// workaround for lack of proper base constructor access - implementation dependent
typeof(Encoding).GetField("encoderFallback", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(this, encoderFallback);
typeof(Encoding).GetField("decoderFallback", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(this, decoderFallback);
#endif
}
internal static Encoding MakeNonThrowing() {
Encoding enc;
#if FEATURE_ENCODING
enc = new PythonAsciiEncoding(new NonStrictEncoderFallback(), new NonStrictDecoderFallback());
#else
enc = new PythonAsciiEncoding();
#endif
return enc;
}
public override int GetByteCount(char[] chars, int index, int count)
=> GetByteCount(chars, index, count, null);
private int GetByteCount(char[] chars, int index, int count, EncoderFallbackBuffer efb) {
#if FEATURE_ENCODING
int byteCount = 0;
int charEnd = index + count;
while (index < charEnd) {
char c = chars[index];
if (c > 0x7f) {
if (efb == null) {
efb = EncoderFallback.CreateFallbackBuffer();
}
if (efb.Fallback(c, index)) {
byteCount += efb.Remaining;
while (efb.GetNextChar() != char.MinValue) { /* empty */ }
}
} else {
byteCount++;
}
index++;
}
return byteCount;
#else
return count;
#endif
}
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
=> GetBytes(chars, charIndex, charCount, bytes, byteIndex, null);
private int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, EncoderFallbackBuffer efb) {
int charEnd = charIndex + charCount;
int outputBytes = 0;
while (charIndex < charEnd) {
char c = chars[charIndex];
#if FEATURE_ENCODING
if (c > 0x7f) {
if (efb == null) {
efb = EncoderFallback.CreateFallbackBuffer();
}
if (efb.Fallback(c, charIndex)) {
while (efb.Remaining != 0) {
bytes[byteIndex++] = (byte)efb.GetNextChar();
outputBytes++;
}
}
} else {
bytes[byteIndex++] = (byte)c;
outputBytes++;
}
#else
bytes[byteIndex++] = (byte)c;
outputBytes++;
#endif
charIndex++;
}
return outputBytes;
}
public override int GetCharCount(byte[] bytes, int index, int count)
=> GetCharCount(bytes, index, count, null);
private int GetCharCount(byte[] bytes, int index, int count, DecoderFallbackBuffer dfb) {
int byteEnd = index + count;
int outputChars = 0;
while (index < byteEnd) {
byte b = bytes[index];
#if FEATURE_ENCODING
if (b > 0x7f) {
if (dfb == null) {
dfb = DecoderFallback.CreateFallbackBuffer();
}
try {
if (dfb.Fallback(new[] { b }, index)) {
outputChars += dfb.Remaining;
while (dfb.GetNextChar() != char.MinValue) { /* empty */ }
}
} catch (DecoderFallbackException ex) {
var dfe = new DecoderFallbackException("ordinal not in range(128)", ex.BytesUnknown, ex.Index);
dfe.Data.Add("encoding", EncodingName);
throw dfe;
}
} else {
outputChars++;
}
#else
outputChars++;
#endif
index++;
}
return outputChars;
}
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
=> GetChars(bytes, byteIndex, byteCount, chars, charIndex, null);
private int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, DecoderFallbackBuffer dfb) {
int byteEnd = byteIndex + byteCount;
int outputChars = 0;
while (byteIndex < byteEnd) {
byte b = bytes[byteIndex];
#if FEATURE_ENCODING
if (b > 0x7f) {
if (dfb == null) {
dfb = DecoderFallback.CreateFallbackBuffer();
}
try {
if (dfb.Fallback(new[] { b }, byteIndex)) {
while (dfb.Remaining != 0) {
chars[charIndex++] = dfb.GetNextChar();
outputChars++;
}
}
} catch (DecoderFallbackException ex) {
var dfe = new DecoderFallbackException("ordinal not in range(128)", ex.BytesUnknown, ex.Index);
dfe.Data.Add("encoding", EncodingName);
throw dfe;
}
} else {
chars[charIndex++] = (char)b;
outputChars++;
}
#else
chars[charIndex++] = (char)b;
outputChars++;
#endif
byteIndex++;
}
return outputChars;
}
public override int GetMaxByteCount(int charCount) {
return charCount * Math.Max(1, EncoderFallback.MaxCharCount);
}
public override int GetMaxCharCount(int byteCount) {
return byteCount * Math.Max(1, DecoderFallback.MaxCharCount);
}
public override string WebName {
get {
return "ascii";
}
}
#if FEATURE_ENCODING
public override string EncodingName {
get {
return "ascii";
}
}
public override Encoder GetEncoder() => new PythonAsciiEncoder(this);
private class PythonAsciiEncoder : Encoder {
private readonly PythonAsciiEncoding _encoding;
public PythonAsciiEncoder(PythonAsciiEncoding encoding) {
_encoding = encoding;
this.Fallback = encoding.EncoderFallback;
}
public override int GetByteCount(char[] chars, int index, int count, bool flush)
=> _encoding.GetByteCount(chars, index, count, this.FallbackBuffer);
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)
=> _encoding.GetBytes(chars, charIndex, charCount, bytes, byteIndex, this.FallbackBuffer);
}
public override Decoder GetDecoder() => new PythonAsciiDecoder(this);
private class PythonAsciiDecoder : Decoder {
private readonly PythonAsciiEncoding _encoding;
public PythonAsciiDecoder(PythonAsciiEncoding encoding) {
_encoding = encoding;
this.Fallback = encoding.DecoderFallback;
}
public override int GetCharCount(byte[] bytes, int index, int count)
=> _encoding.GetCharCount(bytes, index, count, this.FallbackBuffer);
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
=> _encoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex, this.FallbackBuffer);
}
#endif
}
#if FEATURE_ENCODING
internal class NonStrictEncoderFallback : EncoderFallback {
public override EncoderFallbackBuffer CreateFallbackBuffer() {
return new NonStrictEncoderFallbackBuffer();
}
public override int MaxCharCount {
get { return 1; }
}
}
internal class NonStrictEncoderFallbackBuffer : EncoderFallbackBuffer {
private List<char> _buffer = new List<char>();
private int _curIndex;
private int _prevIndex;
public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index) {
throw PythonOps.UnicodeEncodeError("ordinal not in range(128)", charUnknownHigh, charUnknownLow, index);
}
public override bool Fallback(char charUnknown, int index) {
if (charUnknown > 0xff) {
throw PythonOps.UnicodeEncodeError("ordinal not in range(128)", charUnknown, index);
}
if (_curIndex == _buffer.Count && _curIndex > 0) {
// save memory
_buffer.Clear();
_curIndex = 0;
}
_prevIndex = _curIndex;
_buffer.Add(charUnknown);
return true;
}
public override char GetNextChar() {
if (_curIndex == _buffer.Count) {
return char.MinValue;
}
return _buffer[_curIndex++];
}
public override bool MovePrevious() {
if (_curIndex > _prevIndex) {
_curIndex--;
return true;
}
return false;
}
public override int Remaining {
get { return _buffer.Count - _curIndex; }
}
}
internal class NonStrictDecoderFallback : DecoderFallback {
public override DecoderFallbackBuffer CreateFallbackBuffer() {
return new NonStrictDecoderFallbackBuffer();
}
public override int MaxCharCount {
get { return 1; }
}
}
// no ctors on DecoderFallbackBuffer in Silverlight
internal class NonStrictDecoderFallbackBuffer : DecoderFallbackBuffer {
private List<byte> _bytes = new List<byte>();
private int _curIndex;
private int _prevIndex;
public override bool Fallback(byte[] bytesUnknown, int index) {
if (_curIndex == _bytes.Count && _curIndex > 0) {
// save memory
_bytes.Clear();
_curIndex = 0;
}
_prevIndex = _curIndex;
_bytes.AddRange(bytesUnknown);
return true;
}
public override char GetNextChar() {
if (_curIndex == _bytes.Count) {
return char.MinValue;
}
return (char)_bytes[_curIndex++];
}
public override bool MovePrevious() {
if (_curIndex > _prevIndex) {
_curIndex--;
return true;
}
return false;
}
public override int Remaining {
get { return _bytes.Count - _curIndex; }
}
}
#endif
}