System.Xml.XmlUtf8RawTextWriter.WriteCommentOrPi C# (CSharp) Method

WriteCommentOrPi() protected method

protected WriteCommentOrPi ( string text, int stopChar ) : void
text string
stopChar int
return void
        protected unsafe void WriteCommentOrPi( string text, int stopChar ) {
            // write text
            fixed ( char * pSrcBegin = text )
            fixed ( byte * pDstBegin = bufBytes ) {
                char * pSrc = pSrcBegin;
                char * pSrcEnd = pSrcBegin + text.Length;
                byte * pDst = pDstBegin + bufPos;

                int ch = 0;
                for (;;) {
                    byte * pDstEnd = pDst + ( pSrcEnd - pSrc );
                    if ( pDstEnd > pDstBegin + bufLen ) {
                        pDstEnd = pDstBegin + bufLen;
                    }


                    while ( pDst < pDstEnd && ( ( ( xmlCharType.charProperties[( ch = *pSrc )] & XmlCharType.fText ) != 0 ) && ch != stopChar && ch <= 0x7F ) ) {



                        *pDst = (byte)ch;
                        pDst++;
                        pSrc++;
                    }

                    Debug.Assert( pSrc <= pSrcEnd );

                    // end of value
                    if ( pSrc >= pSrcEnd ) {
                        break;
                    }

                    // end of buffer
                    if ( pDst >= pDstEnd ) {
                        bufPos = (int)(pDst - pDstBegin);
                        FlushBuffer();
                        pDst = pDstBegin + 1;
                        continue;
                    }

                    // handle special characters
                    switch ( ch ) {
                        case '-':
                            *pDst = (byte) '-';
                            pDst++;
                            if ( ch == stopChar ) {
                                // Insert space between adjacent dashes or before comment's end dashes
                                if ( pSrc + 1 == pSrcEnd || *(pSrc + 1)== '-' ) {
                                    *pDst = (byte) ' ';
                                    pDst++;
                                }
                            }
                            break;
                        case '?':
                            *pDst = (byte) '?';
                            pDst++;
                            if ( ch == stopChar ) {
                                // Processing instruction: insert space between adjacent '?' and '>' 
                                if ( pSrc + 1 < pSrcEnd && *(pSrc + 1)== '>' ) {
                                    *pDst = (byte) ' ';
                                    pDst++;
                                }
                            }
                            break;
                        case ']':
                            *pDst = (byte) ']';
                            pDst++;
                            break;
                        case (char)0xD:
                            if ( newLineHandling == NewLineHandling.Replace ) {
                                // Normalize "\r\n", or "\r" to NewLineChars
                                if ( pSrc[1] == '\n' ) {
                                    pSrc++;
                                }
                                pDst = WriteNewLine( pDst );
                            }
                            else {
                                *pDst = (byte)ch;
                                pDst++;
                            }
                            break;
                        case (char)0xA:
                            if ( newLineHandling == NewLineHandling.Replace ) {
                                pDst = WriteNewLine( pDst );
                            }
                            else {
                                *pDst = (byte)ch;
                                pDst++;
                            }
                            break;
                        case '<':
                        case '&':
                        case (char)0x9:
                            *pDst = (byte)ch;
                            pDst++;
                            break;
                        default:
                            if ( InRange( ch, SurHighStart, SurLowEnd ) ) { pDst = EncodeSurrogate( pSrc, pSrcEnd, pDst ); pSrc += 2; } else if ( ch <= 0x7F || ch >= 0xFFFE ) { pDst = InvalidXmlChar( ch, pDst, false ); pSrc++; } else { pDst = EncodeMultibyteUTF8( ch, pDst ); pSrc++; };
                            continue;
                    }
                    pSrc++;
                }
                bufPos = (int)(pDst - pDstBegin);
            }
        }