5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
10 using System.Runtime.InteropServices;
16 public abstract class MonoEncoding : Encoding
18 readonly int win_code_page;
20 public MonoEncoding (int codePage)
25 public MonoEncoding (int codePage, int windowsCodePage)
28 win_code_page = windowsCodePage;
31 public override int WindowsCodePage {
32 get { return win_code_page != 0 ? win_code_page : base.WindowsCodePage; }
36 [CLSCompliant (false)]
37 public unsafe void HandleFallback (ref EncoderFallbackBuffer buffer,
38 char* chars, ref int charIndex, ref int charCount,
39 byte* bytes, ref int byteIndex, ref int byteCount)
42 buffer = EncoderFallback.CreateFallbackBuffer ();
43 if (Char.IsSurrogate (chars [charIndex]) && charCount > 0 &&
44 Char.IsSurrogate (chars [charIndex + 1])) {
45 buffer.Fallback (chars [charIndex], chars [charIndex + 1], charIndex);
50 buffer.Fallback (chars [charIndex], charIndex);
51 char [] tmp = new char [buffer.Remaining];
53 while (buffer.Remaining > 0)
54 tmp [idx++] = buffer.GetNextChar ();
55 fixed (char* tmparr = tmp) {
56 byteIndex += GetBytes (tmparr, tmp.Length, bytes + byteIndex, byteCount);
61 // Get the bytes that result from encoding a character buffer.
62 public override int GetByteCount (
63 char [] chars, int index, int count)
66 throw new ArgumentNullException ("chars");
67 if (index < 0 || index > chars.Length)
68 throw new ArgumentOutOfRangeException
69 ("index", Strings.GetString ("ArgRange_Array"));
70 if (count < 0 || count > (chars.Length - index))
71 throw new ArgumentOutOfRangeException
72 ("count", Strings.GetString ("ArgRange_Array"));
78 fixed (char* cptr = chars) {
79 return GetByteCountImpl (
85 // Get the bytes that result from encoding a character buffer.
86 public override int GetBytes (
87 char [] chars, int charIndex, int charCount,
88 byte [] bytes, int byteIndex)
91 throw new ArgumentNullException ("chars");
93 throw new ArgumentNullException ("bytes");
94 if (charIndex < 0 || charIndex > chars.Length)
95 throw new ArgumentOutOfRangeException
96 ("charIndex", Strings.GetString ("ArgRange_Array"));
97 if (charCount < 0 || charCount > (chars.Length - charIndex))
98 throw new ArgumentOutOfRangeException
99 ("charCount", Strings.GetString ("ArgRange_Array"));
100 if (byteIndex < 0 || byteIndex > bytes.Length)
101 throw new ArgumentOutOfRangeException
102 ("byteIndex", Strings.GetString ("ArgRange_Array"));
103 if (bytes.Length - byteIndex < charCount)
104 throw new ArgumentException (Strings.GetString ("Arg_InsufficientSpace"), "bytes");
110 fixed (char* cptr = chars) {
111 fixed (byte* bptr = bytes) {
112 return GetBytesImpl (
116 bytes.Length - byteIndex);
122 // Convenience wrappers for "GetBytes".
123 public override int GetBytes (string s, int charIndex, int charCount,
124 byte [] bytes, int byteIndex)
126 // Validate the parameters.
128 throw new ArgumentNullException("s");
130 throw new ArgumentNullException("bytes");
131 if(charIndex < 0 || charIndex > s.Length)
132 throw new ArgumentOutOfRangeException
134 Strings.GetString("ArgRange_StringIndex"));
135 if(charCount < 0 || charCount > (s.Length - charIndex))
136 throw new ArgumentOutOfRangeException
138 Strings.GetString("ArgRange_StringRange"));
139 if(byteIndex < 0 || byteIndex > bytes.Length)
140 throw new ArgumentOutOfRangeException
142 Strings.GetString("ArgRange_Array"));
143 if((bytes.Length - byteIndex) < charCount)
144 throw new ArgumentException
145 (Strings.GetString("Arg_InsufficientSpace"), "bytes");
147 if (charCount == 0 || bytes.Length == byteIndex)
150 fixed (char* cptr = s) {
151 fixed (byte* bptr = bytes) {
152 return GetBytesImpl (
156 bytes.Length - byteIndex);
163 public unsafe override int GetByteCount (char* chars, int count)
166 return GetByteCountImpl (chars, count);
169 public unsafe override int GetBytes (char* chars, int charCount,
170 byte* bytes, int byteCount)
173 return GetBytesImpl (chars, charCount, bytes, byteCount);
177 [CLSCompliant (false)]
178 public unsafe abstract int GetByteCountImpl (char* chars, int charCount);
180 [CLSCompliant (false)]
181 public unsafe abstract int GetBytesImpl (char* chars, int charCount,
182 byte* bytes, int byteCount);
185 public abstract class MonoEncoder : Encoder
187 MonoEncoding encoding;
189 public MonoEncoder (MonoEncoding encoding)
191 this.encoding = encoding;
194 public override int GetByteCount (
195 char [] chars, int index, int count, bool refresh)
198 throw new ArgumentNullException ("chars");
199 if (index < 0 || index > chars.Length)
200 throw new ArgumentOutOfRangeException
201 ("index", Strings.GetString ("ArgRange_Array"));
202 if (count < 0 || count > (chars.Length - index))
203 throw new ArgumentOutOfRangeException
204 ("count", Strings.GetString ("ArgRange_Array"));
210 fixed (char* cptr = chars) {
211 return GetByteCountImpl (
212 cptr + index, count, refresh);
217 public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex, bool flush)
220 throw new ArgumentNullException ("chars");
222 throw new ArgumentNullException ("bytes");
223 if (charIndex < 0 || charIndex > chars.Length)
224 throw new ArgumentOutOfRangeException
225 ("charIndex", Strings.GetString ("ArgRange_Array"));
226 if (charCount < 0 || charCount > (chars.Length - charIndex))
227 throw new ArgumentOutOfRangeException
228 ("charCount", Strings.GetString ("ArgRange_Array"));
229 if (byteIndex < 0 || byteIndex > bytes.Length)
230 throw new ArgumentOutOfRangeException
231 ("byteIndex", Strings.GetString ("ArgRange_Array"));
232 if (bytes.Length - byteIndex < charCount)
233 throw new ArgumentException (Strings.GetString ("Arg_InsufficientSpace"), "bytes");
238 fixed (char* cptr = chars) {
239 fixed (byte* bptr = bytes) {
240 return GetBytesImpl (cptr + charIndex,
243 bytes.Length - byteIndex,
250 public unsafe abstract int GetByteCountImpl (char* chars, int charCount, bool refresh);
252 public unsafe abstract int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount, bool refresh);
255 public unsafe override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount, bool flush)
257 return GetBytesImpl (chars, charCount, bytes, byteCount, flush);
260 //[CLSCompliant (false)]
261 public unsafe void HandleFallback (
262 char* chars, ref int charIndex, ref int charCount,
263 byte* bytes, ref int byteIndex, ref int byteCount)
265 EncoderFallbackBuffer buffer = FallbackBuffer;
266 encoding.HandleFallback (ref buffer,
267 chars, ref charIndex, ref charCount,
268 bytes, ref byteIndex, ref byteCount);