forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_multibytecodec.cs
More file actions
175 lines (141 loc) · 5.36 KB
/
_multibytecodec.cs
File metadata and controls
175 lines (141 loc) · 5.36 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
// 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.Text;
using System.Runtime.InteropServices;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using Microsoft.Scripting;
#if FALSE
[assembly: PythonModule("_multibytecodec", typeof(IronPython.Modules._multibytecodec))]
namespace IronPython.Modules {
public class _multibytecodec {
[PythonType]
public class MultibyteIncrementalDecoder {
private readonly MultibyteCodec _codec;
public MultibyteIncrementalDecoder(CodeContext context) {
_codec = GetCodec(this, context);
}
public object decode(CodeContext/*!*/ context, string @string, string errors="strict") {
return _codec.decode(context, @string, errors)[0];
}
public object errors {
get {
throw new NotImplementedException();
}
}
public void reset() {
}
}
[PythonType]
public class MultibyteIncrementalEncoder {
private readonly MultibyteCodec _codec;
public MultibyteIncrementalEncoder(CodeContext context) {
_codec = GetCodec(this, context);
}
public PythonTuple encode(CodeContext/*!*/ context, string unicode, string errors="strict") {
return _codec.encode(context, unicode, errors);
}
public object errors {
get {
throw new NotImplementedException();
}
}
public void reset() {
}
}
[PythonType]
public class MultibyteStreamReader {
private readonly MultibyteCodec _codec;
private readonly object _stream;
private readonly string _errors;
public MultibyteStreamReader(CodeContext context, object stream, string errors="strict") {
_codec = GetCodec(this, context);
_stream = stream;
_errors = errors;
}
public string read(CodeContext context) {
return (string)_codec.encode(context, (string)PythonOps.Invoke(context, _stream, "read"), _errors)[0];
}
public string read(CodeContext context, int size) {
return (string)_codec.encode(context, (string)PythonOps.Invoke(context, _stream, "read", size), _errors)[0];
}
public string readline(CodeContext context) {
return (string)_codec.encode(context, (string)PythonOps.Invoke(context, _stream, "readline"), _errors)[0];
}
public void readlines() {
}
public void reset() {
}
public object errors {
get {
return _errors;
}
}
public object stream {
get {
return _stream;
}
}
}
[PythonType]
public class MultibyteStreamWriter {
private readonly MultibyteCodec _codec;
private readonly object _stream;
private readonly string _errors;
public MultibyteStreamWriter(CodeContext context, object stream, string errors="strict") {
_codec = GetCodec(this, context);
_stream = stream;
_errors = errors;
}
public void write(CodeContext context, string @string) {
PythonOps.Invoke(context, _stream, "read", _codec.decode(context, @string, _errors)[0]);
}
public void writelines() {
}
public void reset() {
}
public object errors {
get {
return _errors;
}
}
public object stream {
get {
return _stream;
}
}
}
private static MultibyteCodec GetCodec(object self, CodeContext context) {
MultibyteCodec codec = PythonOps.GetBoundAttr(context, self, "codec") as MultibyteCodec;
if (codec == null) {
throw PythonOps.TypeError("codec is unexpected type");
}
return codec;
}
}
[PythonType]
public class MultibyteCodec {
private readonly Encoding _encoding;
private readonly string _encName;
public MultibyteCodec(Encoding encoding, string encName) {
_encoding = encoding;
_encName = encName;
}
public PythonTuple encode(CodeContext/*!*/ context, string unicode, string errors="strict") {
return PythonTuple.MakeTuple(
StringOps.DoEncode(context, unicode, errors, _encName, _encoding),
unicode.Length
);
}
public PythonTuple decode(CodeContext/*!*/ context, string @string, string errors="strict") {
return PythonTuple.MakeTuple(
StringOps.DoDecode(context, @string, errors, _encName, _encoding),
@string.Length
);
}
}
}
#endif