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]))
161 buffer.Fallback (ch, chars [charIndex], charIndex - 1);
166 buffer.Fallback (ch, charIndex - 1);
167 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
168 fallback_chars = new char [buffer.Remaining];
169 for (int i = 0; i < fallback_chars.Length; i++)
170 fallback_chars [i] = buffer.GetNextChar ();
171 byteIndex += GetBytes (fallback_chars, 0,
172 fallback_chars.Length, bytes, byteIndex,
173 ref buffer, ref fallback_chars);
179 // Get the number of characters needed to decode a byte buffer.
180 public override int GetCharCount (byte[] bytes, int index, int count)
183 throw new ArgumentNullException ("bytes");
185 if (index < 0 || index > bytes.Length) {
186 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
188 if (count < 0 || count > (bytes.Length - index)) {
189 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
194 // Get the characters that result from decoding a byte buffer.
195 public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
196 char[] chars, int charIndex)
199 throw new ArgumentNullException ("bytes");
202 throw new ArgumentNullException ("chars");
204 if (byteIndex < 0 || byteIndex > bytes.Length) {
205 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
207 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
208 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
210 if (charIndex < 0 || charIndex > chars.Length) {
211 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
213 if ((chars.Length - charIndex) < byteCount) {
214 throw new ArgumentException (_("Arg_InsufficientSpace"));
216 int count = byteCount;
217 while (count-- > 0) {
218 chars [charIndex++] = (char)(bytes [byteIndex++]);
223 // Get the maximum number of bytes needed to encode a
224 // specified number of characters.
225 public override int GetMaxByteCount (int charCount)
228 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
233 // Get the maximum number of characters needed to decode a
234 // specified number of bytes.
235 public override int GetMaxCharCount (int byteCount)
238 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
243 // Decode a buffer of bytes into a string.
244 public override String GetString (byte[] bytes, int index, int count)
247 throw new ArgumentNullException ("bytes");
249 if (index < 0 || index > bytes.Length) {
250 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
252 if (count < 0 || count > (bytes.Length - index)) {
253 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
258 fixed (byte* bytePtr = bytes) {
259 string s = string.FastAllocateString (count);
261 fixed (char* charPtr = s) {
262 byte* currByte = bytePtr + index;
263 byte* lastByte = currByte + count;
264 char* currChar = charPtr;
266 while (currByte < lastByte)
267 currChar++ [0] = (char) currByte++ [0];
274 public override String GetString (byte[] bytes)
277 throw new ArgumentNullException ("bytes");
280 return GetString (bytes, 0, bytes.Length);
285 // Get the mail body name for this encoding.
286 public override String BodyName
293 // Get the human-readable name for this encoding.
294 public override String EncodingName
297 return "Western European (ISO)";
301 // Get the mail agent header name for this encoding.
302 public override String HeaderName
309 // Determine if this encoding can be displayed in a Web browser.
310 public override bool IsBrowserDisplay
317 // Determine if this encoding can be saved from a Web browser.
318 public override bool IsBrowserSave
325 // Determine if this encoding can be displayed in a mail/news agent.
326 public override bool IsMailNewsDisplay
333 // Determine if this encoding can be saved from a mail/news agent.
334 public override bool IsMailNewsSave
341 // Get the IANA-preferred Web name for this encoding.
342 public override String WebName
349 #endif // !ECMA_COMPAT
351 }; // class Latin1Encoding
353 }; // namespace System.Text