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 // until we change the callers:
35 internal static string _ (string arg) {
39 // Magic number used by Windows for the ISO Latin1 code page.
40 internal const int ISOLATIN_CODE_PAGE = 28591;
43 public Latin1Encoding () : base (ISOLATIN_CODE_PAGE)
45 // Nothing to do here.
48 public override bool IsSingleByte {
52 public override bool IsAlwaysNormalized (NormalizationForm form)
54 return form == NormalizationForm.FormC;
57 // Get the number of bytes needed to encode a character buffer.
58 public override int GetByteCount (char[] chars, int index, int count)
61 throw new ArgumentNullException ("chars");
63 if (index < 0 || index > chars.Length) {
64 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
66 if (count < 0 || count > (chars.Length - index)) {
67 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
72 // Convenience wrappers for "GetByteCount".
73 public override int GetByteCount (String s)
76 throw new ArgumentNullException ("s");
81 // Get the bytes that result from encoding a character buffer.
82 public override int GetBytes (char[] chars, int charIndex, int charCount,
83 byte[] bytes, int byteIndex)
85 EncoderFallbackBuffer buffer = null;
86 char [] fallback_chars = null;
87 return GetBytes (chars, charIndex, charCount, bytes,
88 byteIndex, ref buffer, ref fallback_chars);
91 int GetBytes (char[] chars, int charIndex, int charCount,
92 byte[] bytes, int byteIndex,
93 ref EncoderFallbackBuffer buffer,
94 ref char [] fallback_chars)
97 throw new ArgumentNullException ("chars");
100 fixed (char *cptr = chars) {
101 return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
106 // Convenience wrappers for "GetBytes".
107 public override int GetBytes (String s, int charIndex, int charCount,
108 byte[] bytes, int byteIndex)
110 EncoderFallbackBuffer buffer = null;
111 char [] fallback_chars = null;
112 return GetBytes (s, charIndex, charCount, bytes, byteIndex,
113 ref buffer, ref fallback_chars);
116 int GetBytes (String s, int charIndex, int charCount,
117 byte[] bytes, int byteIndex,
118 ref EncoderFallbackBuffer buffer,
119 ref char [] fallback_chars)
122 throw new ArgumentNullException ("s");
125 fixed (char *chars = s) {
126 return InternalGetBytes (chars, s.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
131 unsafe int InternalGetBytes (char *chars, int charLength, int charIndex, int charCount,
132 byte[] bytes, int byteIndex,
133 ref EncoderFallbackBuffer buffer,
134 ref char [] fallback_chars)
137 throw new ArgumentNullException ("bytes");
138 if (charIndex < 0 || charIndex > charLength)
139 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
140 if (charCount < 0 || charCount > (charLength - charIndex))
141 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
142 if (byteIndex < 0 || byteIndex > bytes.Length)
143 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
144 if ((bytes.Length - byteIndex) < charCount)
145 throw new ArgumentException (_("Arg_InsufficientSpace"));
147 int count = charCount;
149 while (count-- > 0) {
150 ch = chars [charIndex++];
151 if (ch < (char)0x0100) {
152 bytes [byteIndex++] = (byte)ch;
153 } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
154 bytes [byteIndex++] = (byte)(ch - 0xFEE0);
157 buffer = EncoderFallback.CreateFallbackBuffer ();
158 if (Char.IsSurrogate (ch) && count > 1 &&
159 Char.IsSurrogate (chars [charIndex]))
160 buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
162 buffer.Fallback (ch, charIndex - 1);
163 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
164 fallback_chars = new char [buffer.Remaining];
165 for (int i = 0; i < fallback_chars.Length; i++)
166 fallback_chars [i] = buffer.GetNextChar ();
167 byteIndex += GetBytes (fallback_chars, 0,
168 fallback_chars.Length, bytes, byteIndex,
169 ref buffer, ref fallback_chars);
175 // Get the number of characters needed to decode a byte buffer.
176 public override int GetCharCount (byte[] bytes, int index, int count)
179 throw new ArgumentNullException ("bytes");
181 if (index < 0 || index > bytes.Length) {
182 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
184 if (count < 0 || count > (bytes.Length - index)) {
185 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
190 // Get the characters that result from decoding a byte buffer.
191 public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
192 char[] chars, int charIndex)
195 throw new ArgumentNullException ("bytes");
198 throw new ArgumentNullException ("chars");
200 if (byteIndex < 0 || byteIndex > bytes.Length) {
201 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
203 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
204 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
206 if (charIndex < 0 || charIndex > chars.Length) {
207 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
209 if ((chars.Length - charIndex) < byteCount) {
210 throw new ArgumentException (_("Arg_InsufficientSpace"));
212 int count = byteCount;
213 while (count-- > 0) {
214 chars [charIndex++] = (char)(bytes [byteIndex++]);
219 // Get the maximum number of bytes needed to encode a
220 // specified number of characters.
221 public override int GetMaxByteCount (int charCount)
224 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
229 // Get the maximum number of characters needed to decode a
230 // specified number of bytes.
231 public override int GetMaxCharCount (int byteCount)
234 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
239 // Decode a buffer of bytes into a string.
240 public override String GetString (byte[] bytes, int index, int count)
243 throw new ArgumentNullException ("bytes");
245 if (index < 0 || index > bytes.Length) {
246 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
248 if (count < 0 || count > (bytes.Length - index)) {
249 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
254 fixed (byte* bytePtr = bytes) {
255 string s = string.InternalAllocateStr (count);
257 fixed (char* charPtr = s) {
258 byte* currByte = bytePtr + index;
259 byte* lastByte = currByte + count;
260 char* currChar = charPtr;
262 while (currByte < lastByte)
263 currChar++ [0] = (char) currByte++ [0];
270 public override String GetString (byte[] bytes)
273 throw new ArgumentNullException ("bytes");
276 return GetString (bytes, 0, bytes.Length);
281 // Get the mail body name for this encoding.
282 public override String BodyName
289 // Get the human-readable name for this encoding.
290 public override String EncodingName
293 return "Western European (ISO)";
297 // Get the mail agent header name for this encoding.
298 public override String HeaderName
305 // Determine if this encoding can be displayed in a Web browser.
306 public override bool IsBrowserDisplay
313 // Determine if this encoding can be saved from a Web browser.
314 public override bool IsBrowserSave
321 // Determine if this encoding can be displayed in a mail/news agent.
322 public override bool IsMailNewsDisplay
329 // Determine if this encoding can be saved from a mail/news agent.
330 public override bool IsMailNewsSave
337 // Get the IANA-preferred Web name for this encoding.
338 public override String WebName
345 #endif // !ECMA_COMPAT
347 }; // class Latin1Encoding
349 }; // namespace System.Text