|
22 | 22 | using Microsoft.PythonTools.Analysis.Infrastructure; |
23 | 23 |
|
24 | 24 | namespace Microsoft.PythonTools.Parsing { |
25 | | - /// <summary> |
26 | | - /// Simple implementation of ASCII encoding/decoding. The default instance (PythonAsciiEncoding.Instance) is |
27 | | - /// setup to always convert even values outside of the ASCII range. The EncoderFallback/DecoderFallbacks can |
28 | | - /// be replaced with versions that will throw exceptions instead though. |
29 | | - /// </summary> |
30 | | - [Serializable] |
31 | | - sealed class PythonAsciiEncoding : Encoding { |
32 | | - internal static readonly Encoding Instance = MakeNonThrowing(); |
33 | | - internal static readonly Encoding SourceEncoding = MakeSourceEncoding(); |
34 | | - internal static readonly Encoding SourceEncodingNoFallback = MakeSourceEncodingNoFallback(); |
35 | | - |
36 | | - internal PythonAsciiEncoding() |
37 | | - : base() { |
38 | | - } |
39 | | - |
40 | | - internal static Encoding MakeNonThrowing() { |
41 | | - // we need to Clone the new instance here so that the base class marks us as non-readonly |
42 | | - Encoding enc = (Encoding)new PythonAsciiEncoding().Clone(); |
43 | | - enc.DecoderFallback = new NonStrictDecoderFallback(); |
44 | | - enc.EncoderFallback = new NonStrictEncoderFallback(); |
45 | | - return enc; |
46 | | - } |
47 | | - |
48 | | - private static Encoding MakeSourceEncoding() { |
49 | | - // we need to Clone the new instance here so that the base class marks us as non-readonly |
50 | | - Encoding enc = (Encoding)new PythonAsciiEncoding().Clone(); |
51 | | - enc.DecoderFallback = new SourceNonStrictDecoderFallback(); |
52 | | - return enc; |
53 | | - } |
54 | | - |
55 | | - private static Encoding MakeSourceEncodingNoFallback() { |
56 | | - // we need to Clone the new instance here so that the base class marks us as non-readonly |
57 | | - Encoding enc = (Encoding)new PythonAsciiEncoding().Clone(); |
58 | | - enc.DecoderFallback = new SourceNonStrictDecoderFallbackNoFallback(); |
59 | | - return enc; |
60 | | - } |
61 | | - |
62 | | - public override int GetByteCount(char[] chars, int index, int count) { |
63 | | - int byteCount = 0; |
64 | | - int charEnd = index + count; |
65 | | - while (index < charEnd) { |
66 | | - char c = chars[index]; |
67 | | - if (c > 0x7f) { |
68 | | - EncoderFallbackBuffer efb = EncoderFallback.CreateFallbackBuffer(); |
69 | | - if (efb.Fallback(c, index)) { |
70 | | - byteCount += efb.Remaining; |
71 | | - } |
72 | | - } else { |
73 | | - byteCount++; |
74 | | - } |
75 | | - index++; |
76 | | - } |
77 | | - return byteCount; |
78 | | - } |
79 | | - |
80 | | - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) { |
81 | | - int charEnd = charIndex + charCount; |
82 | | - int outputBytes = 0; |
83 | | - while (charIndex < charEnd) { |
84 | | - char c = chars[charIndex]; |
85 | | - if (c > 0x7f) { |
86 | | - EncoderFallbackBuffer efb = EncoderFallback.CreateFallbackBuffer(); |
87 | | - if (efb.Fallback(c, charIndex)) { |
88 | | - while (efb.Remaining != 0) { |
89 | | - bytes[byteIndex++] = (byte)efb.GetNextChar(); |
90 | | - outputBytes++; |
91 | | - } |
92 | | - } |
93 | | - } else { |
94 | | - bytes[byteIndex++] = (byte)c; |
95 | | - outputBytes++; |
96 | | - } |
97 | | - charIndex++; |
98 | | - } |
99 | | - return outputBytes; |
100 | | - } |
101 | | - |
102 | | - public override int GetCharCount(byte[] bytes, int index, int count) { |
103 | | - int byteEnd = index + count; |
104 | | - int outputChars = 0; |
105 | | - while (index < byteEnd) { |
106 | | - byte b = bytes[index]; |
107 | | - if (b > 0x7f) { |
108 | | - DecoderFallbackBuffer dfb = DecoderFallback.CreateFallbackBuffer(); |
109 | | - if (dfb.Fallback(new byte[] { b }, index)) { |
110 | | - outputChars += dfb.Remaining; |
111 | | - } |
112 | | - } else { |
113 | | - outputChars++; |
114 | | - } |
115 | | - index++; |
116 | | - } |
117 | | - return outputChars; |
118 | | - } |
119 | | - |
120 | | - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { |
121 | | - int byteEnd = byteIndex + byteCount; |
122 | | - int outputChars = 0; |
123 | | - while (byteIndex < byteEnd) { |
124 | | - byte b = bytes[byteIndex]; |
125 | | - if (b > 0x7f) { |
126 | | - DecoderFallbackBuffer dfb = DecoderFallback.CreateFallbackBuffer(); |
127 | | - if (dfb.Fallback(new byte[] { b }, byteIndex)) { |
128 | | - while (dfb.Remaining != 0) { |
129 | | - chars[charIndex++] = dfb.GetNextChar(); |
130 | | - outputChars++; |
131 | | - } |
132 | | - } |
133 | | - } else { |
134 | | - chars[charIndex++] = (char)b; |
135 | | - outputChars++; |
136 | | - } |
137 | | - byteIndex++; |
138 | | - } |
139 | | - return outputChars; |
140 | | - } |
141 | | - |
142 | | - public override int GetMaxByteCount(int charCount) { |
143 | | - return charCount * 4; |
144 | | - } |
145 | | - |
146 | | - public override int GetMaxCharCount(int byteCount) { |
147 | | - return byteCount; |
148 | | - } |
149 | | - |
150 | | - public override string WebName { |
151 | | - get { |
152 | | - return "ascii"; |
153 | | - } |
154 | | - } |
155 | | - |
156 | | - public override string EncodingName { |
157 | | - get { |
158 | | - return "ascii"; |
159 | | - } |
160 | | - } |
161 | | - } |
162 | | - |
163 | 25 | class NonStrictEncoderFallback : EncoderFallback { |
164 | 26 | public override EncoderFallbackBuffer CreateFallbackBuffer() { |
165 | 27 | return new NonStrictEncoderFallbackBuffer(); |
|
0 commit comments