internal static string[] BuildCharExceptionArgs(char invChar, char nextChar)
        {
            string[] aStringList = new string[2];

            // for surrogate characters include both high and low char in the message so that a full character is displayed
            if (XmlCharType.IsHighSurrogate(invChar) && nextChar != 0)
            {
                int combinedChar = XmlCharType.CombineSurrogateChar(nextChar, invChar);
                aStringList[0] = new string(new char[] { invChar, nextChar });
                aStringList[1] = string.Format(CultureInfo.InvariantCulture, "0x{0:X2}", combinedChar);
            }
            else
            {
                // don't include 0 character in the string - in means eof-of-string in native code, where this may bubble up to
                if ((int)invChar == 0)
                {
                    aStringList[0] = ".";
                }
                else
                {
                    aStringList[0] = invChar.ToString(CultureInfo.InvariantCulture);
                }
                aStringList[1] = string.Format(CultureInfo.InvariantCulture, "0x{0:X2}", (int)invChar);
            }
            return(aStringList);
        }
Beispiel #2
0
        public void WriteSurrogateCharEntity(char lowChar, char highChar)
        {
            if (!XmlCharType.IsLowSurrogate(lowChar) ||
                !XmlCharType.IsHighSurrogate(highChar))
            {
                throw new Exception("throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, highChar);");
            }
            int surrogateChar = XmlCharType.CombineSurrogateChar(lowChar, highChar);

            if (_cacheAttrValue)
            {
                _attrValue.Append(highChar);
                _attrValue.Append(lowChar);
            }

            _textWriter.Write("&#x");
            _textWriter.Write(surrogateChar.ToString("X", NumberFormatInfo.InvariantInfo));
            _textWriter.Write(';');
        }
        private static string EncodeName(string name, /*Name_not_NmToken*/ bool first, bool local)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(name);
            }

            StringBuilder bufBld       = null;
            int           length       = name.Length;
            int           copyPosition = 0;
            int           position     = 0;

            int underscorePos = name.IndexOf('_');

            MatchCollection mc = null;
            IEnumerator     en = null;

            if (underscorePos >= 0)
            {
                if (s_encodeCharPattern == null)
                {
                    s_encodeCharPattern = new Regex("(?<=_)[Xx]([0-9a-fA-F]{4}|[0-9a-fA-F]{8})_");
                }
                mc = s_encodeCharPattern.Matches(name, underscorePos);
                en = mc.GetEnumerator();
            }

            int matchPos = -1;

            if (en != null && en.MoveNext())
            {
                global::System.Text.RegularExpressions.Match m = (global::System.Text.RegularExpressions.Match)en.Current;
                matchPos = m.Index - 1;
            }
            if (first)
            {
                if ((!s_xmlCharType.IsStartNCNameCharXml4e(name[0]) && (local || (!local && name[0] != ':'))) ||
                    matchPos == 0)
                {
                    if (bufBld == null)
                    {
                        bufBld = new StringBuilder(length + 20);
                    }
                    bufBld.Append("_x");
                    if (length > 1 && XmlCharType.IsHighSurrogate(name[0]) && XmlCharType.IsLowSurrogate(name[1]))
                    {
                        int   x = name[0];
                        int   y = name[1];
                        Int32 u = XmlCharType.CombineSurrogateChar(y, x);
                        bufBld.Append(u.ToString("X8", CultureInfo.InvariantCulture));
                        position++;
                        copyPosition = 2;
                    }
                    else
                    {
                        bufBld.Append(((Int32)name[0]).ToString("X4", CultureInfo.InvariantCulture));
                        copyPosition = 1;
                    }

                    bufBld.Append('_');
                    position++;

                    if (matchPos == 0)
                    {
                        if (en.MoveNext())
                        {
                            global::System.Text.RegularExpressions.Match m = (global::System.Text.RegularExpressions.Match)en.Current;
                            matchPos = m.Index - 1;
                        }
                    }
                }
            }
            for (; position < length; position++)
            {
                if ((local && !s_xmlCharType.IsNCNameCharXml4e(name[position])) ||
                    (!local && !s_xmlCharType.IsNameCharXml4e(name[position])) ||
                    (matchPos == position))
                {
                    if (bufBld == null)
                    {
                        bufBld = new StringBuilder(length + 20);
                    }
                    if (matchPos == position)
                    {
                        if (en.MoveNext())
                        {
                            global::System.Text.RegularExpressions.Match m = (global::System.Text.RegularExpressions.Match)en.Current;
                            matchPos = m.Index - 1;
                        }
                    }

                    bufBld.Append(name, copyPosition, position - copyPosition);
                    bufBld.Append("_x");
                    if ((length > position + 1) && XmlCharType.IsHighSurrogate(name[position]) && XmlCharType.IsLowSurrogate(name[position + 1]))
                    {
                        int   x = name[position];
                        int   y = name[position + 1];
                        Int32 u = XmlCharType.CombineSurrogateChar(y, x);
                        bufBld.Append(u.ToString("X8", CultureInfo.InvariantCulture));
                        copyPosition = position + 2;
                        position++;
                    }
                    else
                    {
                        bufBld.Append(((Int32)name[position]).ToString("X4", CultureInfo.InvariantCulture));
                        copyPosition = position + 1;
                    }
                    bufBld.Append('_');
                }
            }
            if (copyPosition == 0)
            {
                return(name);
            }
            else
            {
                if (copyPosition < length)
                {
                    bufBld.Append(name, copyPosition, length - copyPosition);
                }
                return(bufBld.ToString());
            }
        }
Beispiel #4
0
 private int SurrogateCharToUtf32(char highSurrogate, char lowSurrogate)
 {
     return(XmlCharType.CombineSurrogateChar(lowSurrogate, highSurrogate));
 }