2 * Latin1Encoding.cs - Implementation of the
3 * "System.Text.Latin1Encoding" class.
5 * Copyright (c) 2002 Southern Storm Software, Pty Ltd
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
32 internal class Latin1Encoding : Encoding
34 // Magic number used by Windows for the ISO Latin1 code page.
35 internal const int ISOLATIN_CODE_PAGE = 28591;
38 public Latin1Encoding () : base (ISOLATIN_CODE_PAGE)
40 // Nothing to do here.
44 public override bool IsSingleByte {
48 public override bool IsAlwaysNormalized (NormalizationForm form)
50 return form == NormalizationForm.FormC;
54 // Get the number of bytes needed to encode a character buffer.
55 public override int GetByteCount (char[] chars, int index, int count)
58 throw new ArgumentNullException ("chars");
60 if (index < 0 || index > chars.Length) {
61 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
63 if (count < 0 || count > (chars.Length - index)) {
64 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
69 // Convenience wrappers for "GetByteCount".
70 public override int GetByteCount (String s)
73 throw new ArgumentNullException ("s");
78 // Get the bytes that result from encoding a character buffer.
79 public override int GetBytes (char[] chars, int charIndex, int charCount,
80 byte[] bytes, int byteIndex)
83 // well, yes, I know this #if is ugly, but I think it is the simplest switch.
84 EncoderFallbackBuffer buffer = null;
85 char [] fallback_chars = null;
86 return GetBytes (chars, charIndex, charCount, bytes,
87 byteIndex, ref buffer, ref fallback_chars);
90 int GetBytes (char[] chars, int charIndex, int charCount,
91 byte[] bytes, int byteIndex,
92 ref EncoderFallbackBuffer buffer,
93 ref char [] fallback_chars)
97 throw new ArgumentNullException ("chars");
100 throw new ArgumentNullException ("bytes");
102 if (charIndex < 0 || charIndex > chars.Length) {
103 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
105 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
106 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
108 if (byteIndex < 0 || byteIndex > bytes.Length) {
109 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
111 if ((bytes.Length - byteIndex) < charCount) {
112 throw new ArgumentException (_("Arg_InsufficientSpace"));
114 int count = charCount;
116 while (count-- > 0) {
117 ch = chars [charIndex++];
118 if (ch < (char)0x0100) {
119 bytes [byteIndex++] = (byte)ch;
120 } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
121 bytes [byteIndex++] = (byte)(ch - 0xFEE0);
125 buffer = EncoderFallback.CreateFallbackBuffer ();
126 if (Char.IsSurrogate (ch) && count > 1 &&
127 Char.IsSurrogate (chars [charIndex]))
128 buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
130 buffer.Fallback (ch, charIndex - 1);
131 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
132 fallback_chars = new char [buffer.Remaining];
133 for (int i = 0; i < fallback_chars.Length; i++)
134 fallback_chars [i] = buffer.GetNextChar ();
135 byteIndex += GetBytes (fallback_chars, 0,
136 fallback_chars.Length, bytes, byteIndex,
137 ref buffer, ref fallback_chars);
139 bytes [byteIndex++] = (byte)'?';
146 // Convenience wrappers for "GetBytes".
147 public override int GetBytes (String s, int charIndex, int charCount,
148 byte[] bytes, int byteIndex)
151 // I know this #if is ugly, but I think it is the simplest switch.
152 EncoderFallbackBuffer buffer = null;
153 char [] fallback_chars = null;
154 return GetBytes (s, charIndex, charCount, bytes, byteIndex,
155 ref buffer, ref fallback_chars);
158 int GetBytes (String s, int charIndex, int charCount,
159 byte[] bytes, int byteIndex,
160 ref EncoderFallbackBuffer buffer,
161 ref char [] fallback_chars)
165 throw new ArgumentNullException ("s");
168 throw new ArgumentNullException ("bytes");
170 if (charIndex < 0 || charIndex > s.Length) {
171 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
173 if (charCount < 0 || charCount > (s.Length - charIndex)) {
174 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
176 if (byteIndex < 0 || byteIndex > bytes.Length) {
177 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
179 if ((bytes.Length - byteIndex) < charCount) {
180 throw new ArgumentException (_("Arg_InsufficientSpace"));
182 int count = charCount;
184 while (count-- > 0) {
185 ch = s [charIndex++];
186 if (ch < (char)0x0100) {
187 bytes [byteIndex++] = (byte)ch;
188 } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
189 bytes [byteIndex++] = (byte)(ch - 0xFEE0);
194 buffer = EncoderFallback.CreateFallbackBuffer ();
195 if (Char.IsSurrogate (ch) && count > 1 &&
196 Char.IsSurrogate (s [charIndex]))
197 buffer.Fallback (ch, s [charIndex], charIndex++ - 1);
199 buffer.Fallback (ch, charIndex - 1);
200 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
201 fallback_chars = new char [buffer.Remaining];
202 for (int i = 0; i < fallback_chars.Length; i++)
203 fallback_chars [i] = buffer.GetNextChar ();
204 byteIndex += GetBytes (fallback_chars, 0,
205 fallback_chars.Length, bytes, byteIndex,
206 ref buffer, ref fallback_chars);
208 bytes [byteIndex++] = (byte)'?';
215 // Get the number of characters needed to decode a byte buffer.
216 public override int GetCharCount (byte[] bytes, int index, int count)
219 throw new ArgumentNullException ("bytes");
221 if (index < 0 || index > bytes.Length) {
222 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
224 if (count < 0 || count > (bytes.Length - index)) {
225 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
230 // Get the characters that result from decoding a byte buffer.
231 public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
232 char[] chars, int charIndex)
235 throw new ArgumentNullException ("bytes");
238 throw new ArgumentNullException ("chars");
240 if (byteIndex < 0 || byteIndex > bytes.Length) {
241 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
243 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
244 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
246 if (charIndex < 0 || charIndex > chars.Length) {
247 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
249 if ((chars.Length - charIndex) < byteCount) {
250 throw new ArgumentException (_("Arg_InsufficientSpace"));
252 int count = byteCount;
253 while (count-- > 0) {
254 chars [charIndex++] = (char)(bytes [byteIndex++]);
259 // Get the maximum number of bytes needed to encode a
260 // specified number of characters.
261 public override int GetMaxByteCount (int charCount)
264 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
269 // Get the maximum number of characters needed to decode a
270 // specified number of bytes.
271 public override int GetMaxCharCount (int byteCount)
274 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
279 // Decode a buffer of bytes into a string.
280 public override String GetString (byte[] bytes, int index, int count)
283 throw new ArgumentNullException ("bytes");
285 if (index < 0 || index > bytes.Length) {
286 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
288 if (count < 0 || count > (bytes.Length - index)) {
289 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
294 fixed (byte* bytePtr = bytes) {
295 string s = string.InternalAllocateStr (count);
297 fixed (char* charPtr = s) {
298 byte* currByte = bytePtr + index;
299 byte* lastByte = currByte + count;
300 char* currChar = charPtr;
302 while (currByte < lastByte)
303 currChar++ [0] = (char) currByte++ [0];
310 public override String GetString (byte[] bytes)
313 throw new ArgumentNullException ("bytes");
316 return GetString (bytes, 0, bytes.Length);
321 // Get the mail body name for this encoding.
322 public override String BodyName
329 // Get the human-readable name for this encoding.
330 public override String EncodingName
333 return "Western European (ISO)";
337 // Get the mail agent header name for this encoding.
338 public override String HeaderName
345 // Determine if this encoding can be displayed in a Web browser.
346 public override bool IsBrowserDisplay
353 // Determine if this encoding can be saved from a Web browser.
354 public override bool IsBrowserSave
361 // Determine if this encoding can be displayed in a mail/news agent.
362 public override bool IsMailNewsDisplay
369 // Determine if this encoding can be saved from a mail/news agent.
370 public override bool IsMailNewsSave
377 // Get the IANA-preferred Web name for this encoding.
378 public override String WebName
385 #endif // !ECMA_COMPAT
387 }; // class Latin1Encoding
389 }; // namespace System.Text