X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FSystem.Text%2FLatin1Encoding.cs;h=3cee3c0a3491085d5d404367bd785c811ed32d4b;hb=7e18ed47c9606f3981e7b18cbc238d6781843153;hp=dcf625dc2235624f6886840e85eacf8e9dfbb71c;hpb=a8dea00dfb9bf386c75c32be9c5279d0e4ed62c4;p=mono.git diff --git a/mcs/class/corlib/System.Text/Latin1Encoding.cs b/mcs/class/corlib/System.Text/Latin1Encoding.cs index dcf625dc223..3cee3c0a349 100644 --- a/mcs/class/corlib/System.Text/Latin1Encoding.cs +++ b/mcs/class/corlib/System.Text/Latin1Encoding.cs @@ -68,6 +68,20 @@ internal class Latin1Encoding : Encoding public override int GetBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) { +#if NET_2_0 +// well, yes, I know this #if is ugly, but I think it is the simplest switch. + EncoderFallbackBuffer buffer = null; + char [] fallback_chars = null; + return GetBytes (chars, charIndex, charCount, bytes, + byteIndex, ref buffer, ref fallback_chars); + } + + int GetBytes (char[] chars, int charIndex, int charCount, + byte[] bytes, int byteIndex, + ref EncoderFallbackBuffer buffer, + ref char [] fallback_chars) + { +#endif if (chars == null) { throw new ArgumentNullException ("chars"); } @@ -95,7 +109,24 @@ internal class Latin1Encoding : Encoding } else if (ch >= '\uFF01' && ch <= '\uFF5E') { bytes [byteIndex++] = (byte)(ch - 0xFEE0); } else { +#if NET_2_0 + if (buffer == null) + buffer = EncoderFallback.CreateFallbackBuffer (); + if (Char.IsSurrogate (ch) && count > 1 && + Char.IsSurrogate (chars [charIndex])) + buffer.Fallback (ch, chars [charIndex], charIndex++ - 1); + else + buffer.Fallback (ch, charIndex - 1); + if (fallback_chars == null || fallback_chars.Length < buffer.Remaining) + fallback_chars = new char [buffer.Remaining]; + for (int i = 0; i < fallback_chars.Length; i++) + fallback_chars [i] = buffer.GetNextChar (); + byteIndex += GetBytes (fallback_chars, 0, + fallback_chars.Length, bytes, byteIndex, + ref buffer, ref fallback_chars); +#else bytes [byteIndex++] = (byte)'?'; +#endif } } return charCount; @@ -105,6 +136,20 @@ internal class Latin1Encoding : Encoding public override int GetBytes (String s, int charIndex, int charCount, byte[] bytes, int byteIndex) { +#if NET_2_0 +// I know this #if is ugly, but I think it is the simplest switch. + EncoderFallbackBuffer buffer = null; + char [] fallback_chars = null; + return GetBytes (s, charIndex, charCount, bytes, byteIndex, + ref buffer, ref fallback_chars); + } + + int GetBytes (String s, int charIndex, int charCount, + byte[] bytes, int byteIndex, + ref EncoderFallbackBuffer buffer, + ref char [] fallback_chars) + { +#endif if (s == null) { throw new ArgumentNullException ("s"); } @@ -132,7 +177,25 @@ internal class Latin1Encoding : Encoding } else if (ch >= '\uFF01' && ch <= '\uFF5E') { bytes [byteIndex++] = (byte)(ch - 0xFEE0); } else { + +#if NET_2_0 + if (buffer == null) + buffer = EncoderFallback.CreateFallbackBuffer (); + if (Char.IsSurrogate (ch) && count > 1 && + Char.IsSurrogate (s [charIndex])) + buffer.Fallback (ch, s [charIndex], charIndex++ - 1); + else + buffer.Fallback (ch, charIndex - 1); + if (fallback_chars == null || fallback_chars.Length < buffer.Remaining) + fallback_chars = new char [buffer.Remaining]; + for (int i = 0; i < fallback_chars.Length; i++) + fallback_chars [i] = buffer.GetNextChar (); + byteIndex += GetBytes (fallback_chars, 0, + fallback_chars.Length, bytes, byteIndex, + ref buffer, ref fallback_chars); +#else bytes [byteIndex++] = (byte)'?'; +#endif } } return charCount;