Author: atsushi
Date: 2005-03-09 13:21:10 -0500 (Wed, 09 Mar 2005)
New Revision: 41610

Modified:
   trunk/mcs/class/System.XML/System.Xml/DTDReader.cs
   trunk/mcs/class/System.XML/System.Xml/XmlParserInput.cs
   trunk/mcs/class/System.XML/System.Xml/XmlTextReader.cs
   trunk/mcs/class/System.XML/Test/System.Xml/ChangeLog
   trunk/mcs/class/System.XML/Test/System.Xml/XmlTextReaderTests.cs
Log:
2005-03-09  Atsushi Enomoto <[EMAIL PROTECTED]>

        * XmlTextReader.cs, XmlParserInput.cs, DTDReader.cs: fix UCM-4
          surrogate calculations. This fixes bug #73513. Patch by
          Andrew Skiba.

        * XmlTextReaderTests.cs : added SurrogatePair() based on bug #73513.




Modified: trunk/mcs/class/System.XML/System.Xml/DTDReader.cs
===================================================================
--- trunk/mcs/class/System.XML/System.Xml/DTDReader.cs  2005-03-09 18:15:50 UTC 
(rev 41609)
+++ trunk/mcs/class/System.XML/System.Xml/DTDReader.cs  2005-03-09 18:21:10 UTC 
(rev 41610)
@@ -1317,16 +1317,6 @@
                        return currentInput.ReadChar ();
                }
 
-               private string ExpandSurrogateChar (int ch)
-               {
-                       if (ch < Char.MaxValue)
-                               return ((char) ch).ToString ();
-                       else {
-                               char [] tmp = new char [] {(char) (ch / 0x10000 
+ 0xD800 - 1), (char) (ch % 0x10000 + 0xDC00)};
-                               return new string (tmp);
-                       }
-               }
-
                // The reader is positioned on the first character after
                // the leading '<!--'.
                private void ReadComment ()
@@ -1524,10 +1514,19 @@
 
                private void AppendValueChar (int ch)
                {
-                       if (ch < Char.MaxValue)
+                       //See http://www.faqs.org/rfcs/rfc2781.html for used 
algorithm
+                       if (ch < 0x10000) {
                                valueBuffer.Append ((char) ch);
+                               return;
+                       }
+                       if (ch > 0x10FFFF)
+                               throw new XmlException ("The numeric entity 
value is too large", null, LineNumber, LinePosition);
                        else
-                               valueBuffer.Append (ExpandSurrogateChar (ch));
+                       {
+                               int utag = ch - 0x10000;
+                               valueBuffer.Append((char) ((utag >> 10) + 
0xD800));
+                               valueBuffer.Append((char) ((utag & 0x3FF) + 
0xDC00));
+                       }
                }
 
                private string CreateValueString ()

Modified: trunk/mcs/class/System.XML/System.Xml/XmlParserInput.cs
===================================================================
--- trunk/mcs/class/System.XML/System.Xml/XmlParserInput.cs     2005-03-09 
18:15:50 UTC (rev 41609)
+++ trunk/mcs/class/System.XML/System.Xml/XmlParserInput.cs     2005-03-09 
18:21:10 UTC (rev 41610)
@@ -100,9 +100,10 @@
 
                        peek_char = reader.Read ();
                        if (peek_char >= 0xD800 && peek_char <= 0xDBFF) {
+                               peek_char = 0x10000+((peek_char-0xD800)<<10);
                                int i = reader.Read ();
                                if (i >= 0xDC00 && i <= 0xDFFF)
-                                       peek_char += i;
+                                       peek_char += (i-0xDC00);
                        }
                        has_peek = true;
                        return peek_char;
@@ -124,17 +125,8 @@
                                return ch;
                        }
 
-                       if (has_peek) {
-                               ch = peek_char;
-                               has_peek = false;
-                       } else {
-                               ch = reader.Read ();
-                               if (ch >= 0xD800 && ch <= 0xDBFF) {
-                                       int i = reader.Read ();
-                                       if (i > 0xDC00 && i <= 0xDFFF)
-                                               ch += i;
-                               }
-                       }
+                       ch = PeekChar ();
+                       has_peek = false;
 
                        if (ch == '\n') {
                                line++;

Modified: trunk/mcs/class/System.XML/System.Xml/XmlTextReader.cs
===================================================================
--- trunk/mcs/class/System.XML/System.Xml/XmlTextReader.cs      2005-03-09 
18:15:50 UTC (rev 41609)
+++ trunk/mcs/class/System.XML/System.Xml/XmlTextReader.cs      2005-03-09 
18:21:10 UTC (rev 41610)
@@ -1082,27 +1082,43 @@
                        if (peekCharsLength == peekCharsIndex) {
                                if (!ReadTextReader ())
                                        return -1;
-                               else
                                        return PeekChar ();
                        }
 
+                       char c = peekChars [peekCharsIndex];
+                       if (c == 0)
+                               return -1;
+                       if (!char.IsSurrogate (c))
+                               return c;
+                       if (peekCharsLength == peekCharsIndex+1) {
+                               if (!ReadTextReader ())
+                                       //FIXME: copy MS.NET behaviour when 
unpaired surrogate found
                        return peekChars [peekCharsIndex];
+                               return PeekChar ();
+                       }
+            
+                       char highhalfChar = peekChars [peekCharsIndex];
+                       char lowhalfChar = peekChars [peekCharsIndex+1];
+
+                       if (((highhalfChar & 0xFC00) != 0xD800) || 
((lowhalfChar & 0xFC00) != 0xDC00))
+                               //FIXME: copy MS.NET behaviour when unpaired 
surrogate found
+                               return highhalfChar;
+                       return 0x10000 + (highhalfChar-0xD800)*0x400 + 
(lowhalfChar-0xDC00);
                }
 
                private int ReadChar ()
                {
-                       int ch;
+                       int ch = PeekChar ();
 
-                       if (peekCharsLength == peekCharsIndex) {
-                               if (!ReadTextReader ())
-                                       return -1;
-                       }
-                       ch = peekChars [peekCharsIndex++];
+                       peekCharsIndex++;
 
+                       if (ch >= 0x10000)
+                               peekCharsIndex++; //Increment by 2 when a 
compound UCS-4 character was found
+
                        if (ch == '\n') {
                                line++;
                                column = 1;
-                       } else if (ch == 0) {
+                       } else if (ch == -1) {
                                return -1;
                        } else {
                                column++;
@@ -1119,16 +1135,6 @@
                        return (peekCharsLength != 0);
                }
 
-               private string ExpandSurrogateChar (int ch)
-               {
-                       if (ch < Char.MaxValue)
-                               return ((char) ch).ToString ();
-                       else {
-                               char [] tmp = new char [] {(char) (ch / 0x10000 
+ 0xD800 - 1), (char) (ch % 0x10000 + 0xDC00)};
-                               return new string (tmp);
-                       }
-               }
-
                private bool ReadContent ()
                {
                        currentTagLength = 0;

Modified: trunk/mcs/class/System.XML/Test/System.Xml/ChangeLog
===================================================================
--- trunk/mcs/class/System.XML/Test/System.Xml/ChangeLog        2005-03-09 
18:15:50 UTC (rev 41609)
+++ trunk/mcs/class/System.XML/Test/System.Xml/ChangeLog        2005-03-09 
18:21:10 UTC (rev 41610)
@@ -1,3 +1,7 @@
+2005-03-09  Atsushi Enomoto <[EMAIL PROTECTED]>
+
+       * XmlTextReaderTests.cs : added SurrogatePair() based on bug #73513.
+
 2005-03-07  Atsushi Enomoto <[EMAIL PROTECTED]>
 
        * XmlDocumentTests.cs : Added ReadNodeEntityReferenceFillsChildren.

Modified: trunk/mcs/class/System.XML/Test/System.Xml/XmlTextReaderTests.cs
===================================================================
--- trunk/mcs/class/System.XML/Test/System.Xml/XmlTextReaderTests.cs    
2005-03-09 18:15:50 UTC (rev 41609)
+++ trunk/mcs/class/System.XML/Test/System.Xml/XmlTextReaderTests.cs    
2005-03-09 18:21:10 UTC (rev 41610)
@@ -1083,5 +1083,22 @@
                        }
                }
 #endif
+
+               [Test]
+               public void SurrogatePair ()
+               {
+                       string xml = @"<!DOCTYPE test [<!ELEMENT test ANY>
+               <!ENTITY % a '<!ENTITY ref " + "\"\uF090\u8080\"" + @">'>
+               %a;
+       ]>
+       <test>&ref;</test>";
+                       XmlValidatingReader r = new XmlValidatingReader (xml, 
XmlNodeType.Document, null);
+                       r.Read ();
+                       r.Read ();
+                       r.Read ();
+                       r.Read ();
+                       AssertEquals ("#1", 0xf090, (int) r.Value [0]);
+                       AssertEquals ("#1", 0x8080, (int) r.Value [1]);
+               }
        }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to