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.
43 public override bool IsSingleByte {
47 public override bool IsAlwaysNormalized (NormalizationForm form)
49 return form == NormalizationForm.FormC;
52 // Get the number of bytes needed to encode a character buffer.
53 public override int GetByteCount (char[] chars, int index, int count)
56 throw new ArgumentNullException ("chars");
58 if (index < 0 || index > chars.Length) {
59 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
61 if (count < 0 || count > (chars.Length - index)) {
62 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
67 // Convenience wrappers for "GetByteCount".
68 public override int GetByteCount (String s)
71 throw new ArgumentNullException ("s");
76 // Get the bytes that result from encoding a character buffer.
77 public override int GetBytes (char[] chars, int charIndex, int charCount,
78 byte[] bytes, int byteIndex)
80 EncoderFallbackBuffer buffer = null;
81 char [] fallback_chars = null;
82 return GetBytes (chars, charIndex, charCount, bytes,
83 byteIndex, ref buffer, ref fallback_chars);
86 int GetBytes (char[] chars, int charIndex, int charCount,
87 byte[] bytes, int byteIndex,
88 ref EncoderFallbackBuffer buffer,
89 ref char [] fallback_chars)
92 throw new ArgumentNullException ("chars");
95 fixed (char *cptr = chars) {
96 return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
101 // Convenience wrappers for "GetBytes".
102 public override int GetBytes (String s, int charIndex, int charCount,
103 byte[] bytes, int byteIndex)
105 EncoderFallbackBuffer buffer = null;
106 char [] fallback_chars = null;
107 return GetBytes (s, charIndex, charCount, bytes, byteIndex,
108 ref buffer, ref fallback_chars);
111 int GetBytes (String s, int charIndex, int charCount,
112 byte[] bytes, int byteIndex,
113 ref EncoderFallbackBuffer buffer,
114 ref char [] fallback_chars)
117 throw new ArgumentNullException ("s");
120 fixed (char *chars = s) {
121 return InternalGetBytes (chars, s.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
126 unsafe int InternalGetBytes (char *chars, int charLength, int charIndex, int charCount,
127 byte[] bytes, int byteIndex,
128 ref EncoderFallbackBuffer buffer,
129 ref char [] fallback_chars)
132 throw new ArgumentNullException ("bytes");
133 if (charIndex < 0 || charIndex > charLength)
134 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
135 if (charCount < 0 || charCount > (charLength - charIndex))
136 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
137 if (byteIndex < 0 || byteIndex > bytes.Length)
138 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
139 if ((bytes.Length - byteIndex) < charCount)
140 throw new ArgumentException (_("Arg_InsufficientSpace"));
142 int count = charCount;
144 while (count-- > 0) {
145 ch = chars [charIndex++];
146 if (ch < (char)0x0100) {
147 bytes [byteIndex++] = (byte)ch;
148 } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
149 bytes [byteIndex++] = (byte)(ch - 0xFEE0);
152 buffer = EncoderFallback.CreateFallbackBuffer ();
153 if (Char.IsSurrogate (ch) && count > 1 &&
154 Char.IsSurrogate (chars [charIndex]))
155 buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
157 buffer.Fallback (ch, charIndex - 1);
158 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
159 fallback_chars = new char [buffer.Remaining];
160 for (int i = 0; i < fallback_chars.Length; i++)
161 fallback_chars [i] = buffer.GetNextChar ();
162 byteIndex += GetBytes (fallback_chars, 0,
163 fallback_chars.Length, bytes, byteIndex,
164 ref buffer, ref fallback_chars);
170 // Get the number of characters needed to decode a byte buffer.
171 public override int GetCharCount (byte[] bytes, int index, int count)
174 throw new ArgumentNullException ("bytes");
176 if (index < 0 || index > bytes.Length) {
177 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
179 if (count < 0 || count > (bytes.Length - index)) {
180 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
185 // Get the characters that result from decoding a byte buffer.
186 public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
187 char[] chars, int charIndex)
190 throw new ArgumentNullException ("bytes");
193 throw new ArgumentNullException ("chars");
195 if (byteIndex < 0 || byteIndex > bytes.Length) {
196 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
198 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
199 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
201 if (charIndex < 0 || charIndex > chars.Length) {
202 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
204 if ((chars.Length - charIndex) < byteCount) {
205 throw new ArgumentException (_("Arg_InsufficientSpace"));
207 int count = byteCount;
208 while (count-- > 0) {
209 chars [charIndex++] = (char)(bytes [byteIndex++]);
214 // Get the maximum number of bytes needed to encode a
215 // specified number of characters.
216 public override int GetMaxByteCount (int charCount)
219 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
224 // Get the maximum number of characters needed to decode a
225 // specified number of bytes.
226 public override int GetMaxCharCount (int byteCount)
229 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
234 // Decode a buffer of bytes into a string.
235 public override String GetString (byte[] bytes, int index, int count)
238 throw new ArgumentNullException ("bytes");
240 if (index < 0 || index > bytes.Length) {
241 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
243 if (count < 0 || count > (bytes.Length - index)) {
244 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
249 fixed (byte* bytePtr = bytes) {
250 string s = string.InternalAllocateStr (count);
252 fixed (char* charPtr = s) {
253 byte* currByte = bytePtr + index;
254 byte* lastByte = currByte + count;
255 char* currChar = charPtr;
257 while (currByte < lastByte)
258 currChar++ [0] = (char) currByte++ [0];
265 public override String GetString (byte[] bytes)
268 throw new ArgumentNullException ("bytes");
271 return GetString (bytes, 0, bytes.Length);
276 // Get the mail body name for this encoding.
277 public override String BodyName
284 // Get the human-readable name for this encoding.
285 public override String EncodingName
288 return "Western European (ISO)";
292 // Get the mail agent header name for this encoding.
293 public override String HeaderName
300 // Determine if this encoding can be displayed in a Web browser.
301 public override bool IsBrowserDisplay
308 // Determine if this encoding can be saved from a Web browser.
309 public override bool IsBrowserSave
316 // Determine if this encoding can be displayed in a mail/news agent.
317 public override bool IsMailNewsDisplay
324 // Determine if this encoding can be saved from a mail/news agent.
325 public override bool IsMailNewsSave
332 // Get the IANA-preferred Web name for this encoding.
333 public override String WebName
340 #endif // !ECMA_COMPAT
342 }; // class Latin1Encoding
344 }; // namespace System.Text