private string Normalize(XPathNodeIterator nodeIterator) { string value; if (_argList.Count > 0) { value = _argList[0].Evaluate(nodeIterator).ToString(); } else { value = nodeIterator.Current.Value; } int modifyPos = -1; char[] chars = value.ToCharArray(); bool firstSpace = false; // Start false to trim the beginning XmlCharType xmlCharType = XmlCharType.Instance; for (int comparePos = 0; comparePos < chars.Length; comparePos++) { if (!xmlCharType.IsWhiteSpace(chars[comparePos])) { firstSpace = true; modifyPos++; chars[modifyPos] = chars[comparePos]; } else if (firstSpace) { firstSpace = false; modifyPos++; chars[modifyPos] = ' '; } } // Trim end if (modifyPos > -1 && chars[modifyPos] == ' ') { modifyPos--; } return(new string(chars, 0, modifyPos + 1)); }
internal static void VerifyCharData(string data, ExceptionType invCharExceptionType, ExceptionType invSurrogateExceptionType) { if (data == null || data.Length == 0) { return; } int i = 0; int len = data.Length; for (; ;) { while (i < len && s_xmlCharType.IsCharData(data[i])) { i++; } if (i == len) { return; } char ch = data[i]; if (XmlCharType.IsHighSurrogate(ch)) { if (i + 1 == len) { throw CreateException(SR.Xml_InvalidSurrogateMissingLowChar, invSurrogateExceptionType, 0, i + 1); } ch = data[i + 1]; if (XmlCharType.IsLowSurrogate(ch)) { i += 2; continue; } else { throw CreateInvalidSurrogatePairException(data[i + 1], data[i], invSurrogateExceptionType, 0, i + 1); } } throw CreateInvalidCharException(data, i, invCharExceptionType); } }
internal static void VerifyCharData(char[] data, int offset, int len, ExceptionType exceptionType) { if (data == null || len == 0) { return; } int i = offset; int endPos = offset + len; for (;;) { while (i < endPos && s_xmlCharType.IsCharData(data[i])) { i++; } if (i == endPos) { return; } char ch = data[i]; if (XmlCharType.IsHighSurrogate(ch)) { if (i + 1 == endPos) { throw CreateException(SR.Xml_InvalidSurrogateMissingLowChar, exceptionType, 0, offset - i + 1); } ch = data[i + 1]; if (XmlCharType.IsLowSurrogate(ch)) { i += 2; continue; } else { throw CreateInvalidSurrogatePairException(data[i + 1], data[i], exceptionType, 0, offset - i + 1); } } throw CreateInvalidCharException(data, len, i, exceptionType); } }
private double ScanNumber() { Debug.Assert(CurrentChar == '.' || XmlCharType.IsDigit(CurrentChar)); int start = _xpathExprIndex - 1; int len = 0; while (XmlCharType.IsDigit(CurrentChar)) { NextChar(); len++; } if (CurrentChar == '.') { NextChar(); len++; while (XmlCharType.IsDigit(CurrentChar)) { NextChar(); len++; } } return(XmlConvert.ToXPathDouble(_xpathExpr.Substring(start, len))); }
private string ScanName() { Debug.Assert(XmlCharType.IsStartNCNameSingleChar(CurrentChar)); int start = _xpathExprIndex - 1; int len = 0; while (true) { if (XmlCharType.IsNCNameSingleChar(CurrentChar)) { NextChar(); len++; } else { break; } } return(_xpathExpr.Substring(start, len)); }
// // Constructor // internal XmlCharCheckingReader(XmlReader reader, bool checkCharacters, bool ignoreWhitespace, bool ignoreComments, bool ignorePis, DtdProcessing dtdProcessing) : base(reader) { Debug.Assert(checkCharacters || ignoreWhitespace || ignoreComments || ignorePis || (int)dtdProcessing != -1); _state = State.Initial; _checkCharacters = checkCharacters; _ignoreWhitespace = ignoreWhitespace; _ignoreComments = ignoreComments; _ignorePis = ignorePis; _dtdProcessing = dtdProcessing; _lastNodeType = XmlNodeType.None; if (checkCharacters) { _xmlCharType = XmlCharType.Instance; } }
public static bool ValidatePrefix(string prefix) { if (prefix.Length == 0) { return(false); } XmlCharType xmlCharType = XmlCharType.Instance; if (!xmlCharType.IsStartNCNameChar(prefix[0])) { return(false); } for (int i = 1; i < prefix.Length; i++) { if (!xmlCharType.IsNCNameChar(prefix[i])) { return(false); } } return(true); }
/// <include file='doc\XmlReader.uex' path='docs/doc[@for="XmlReader.IsNameToken"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public static bool IsNameToken(string str) { if (str == String.Empty) { return(false); } int i = str.Length - 1; while (i >= 0) { if (XmlCharType.IsNameChar(str[i])) { i--; } else { return(false); } } return(true); }
internal int IsOnlyCharData(string str) { if (str != null) { for (int i = 0; i < str.Length; i++) { if ((charProperties[str[i]] & fCharData) == 0) { if (i + 1 >= str.Length || !(XmlCharType.IsHighSurrogate(str[i]) && XmlCharType.IsLowSurrogate(str[i + 1]))) { return(i); } else { i++; } } } } return(-1); }
private double ScanNumber() { int startIndex = this.xpathExprIndex - 1; int length = 0; while (XmlCharType.IsDigit(this.CurerntChar)) { this.NextChar(); length++; } if (this.CurerntChar == '.') { this.NextChar(); length++; while (XmlCharType.IsDigit(this.CurerntChar)) { this.NextChar(); length++; } } return(XmlConvert.ToXPathDouble(this.xpathExpr.Substring(startIndex, length))); }
// // Parsing qualified names // private static string ParseNCName(string qname, ref int position) { int qnameLength = qname.Length; int nameStart = position; if ( qnameLength == position || // Zero length ncname !XmlCharType.IsStartNCNameChar(qname[position]) // Start from invalid char ) { throw new XsltException(Res.Xslt_InvalidQName, qname); } position++; while (position < qnameLength && XmlCharType.IsNCNameChar(qname[position])) { position++; } return(qname.Substring(nameStart, position - nameStart)); }
internal override int GetFullChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { uint code; int i, j; byteCount += byteIndex; for (i = byteIndex, j = charIndex; i + 3 < byteCount;) { code = (uint)((bytes[i + 2] << 24) | (bytes[i + 3] << 16) | (bytes[i] << 8) | bytes[i + 1]); if (code > 0x10FFFF) { throw new ArgumentException(string.Format(ResXml.Enc_InvalidByteInEncoding, new object[1] { i }), (string)null); } else if (code > 0xFFFF) { Ucs4ToUTF16(code, chars, j); j++; } else { if (XmlCharType.IsSurrogate((int)code)) { throw new XmlException(ResXml.Xml_InvalidCharInThisEncoding, string.Empty); } else { chars[j] = (char)code; } } j++; i += 4; } return(j - charIndex); }
// Moving through the Stream public override bool Read() { Debug.Assert(this.processor != null || this.state == ReadState.Closed); if (this.state != ReadState.Interactive) { if (this.state == ReadState.Initial) { state = ReadState.Interactive; } else { return(false); } } while (true) // while -- to ignor empty whitespace nodes. { if (this.haveRecord) { this.processor.ResetOutput(); this.haveRecord = false; } this.processor.Execute(); if (this.haveRecord) { CheckCurrentInfo(); // check text nodes on whitespaces; switch (this.NodeType) { case XmlNodeType.Text: if (XmlCharType.IsOnlyWhitespace(this.Value)) { this.currentInfo.NodeType = XmlNodeType.Whitespace; goto case XmlNodeType.Whitespace; } Debug.Assert(this.Value.Length != 0, "It whould be Whitespace in this case"); break; case XmlNodeType.Whitespace: if (this.Value.Length == 0) { continue; // ignoring emty text nodes } if (this.XmlSpace == XmlSpace.Preserve) { this.currentInfo.NodeType = XmlNodeType.SignificantWhitespace; } break; } } else { Debug.Assert(this.processor.ExecutionDone); this.state = ReadState.EndOfFile; Reset(); } return(this.haveRecord); } }
private int ReadTextNodes() { bool textPreserveWS = _reader.XmlSpace == XmlSpace.Preserve; bool textIsWhite = true; int curTextNode = 0; do { switch (_reader.NodeType) { case XmlNodeType.Text: // XLinq reports WS nodes as Text so we need to analyze them here case XmlNodeType.CDATA: if (textIsWhite && !XmlCharType.IsOnlyWhitespace(_reader.Value)) { textIsWhite = false; } goto case XmlNodeType.SignificantWhitespace; case XmlNodeType.Whitespace: case XmlNodeType.SignificantWhitespace: ExtendRecordBuffer(curTextNode); FillupTextRecord(ref _records[curTextNode]); _reader.Read(); curTextNode++; break; case XmlNodeType.EntityReference: string local = _reader.LocalName; if (local.Length > 0 && ( local[0] == '#' || local == "lt" || local == "gt" || local == "quot" || local == "apos" )) { // Special treatment for character and built-in entities ExtendRecordBuffer(curTextNode); FillupCharacterEntityRecord(ref _records[curTextNode]); if (textIsWhite && !XmlCharType.IsOnlyWhitespace(_records[curTextNode].value)) { textIsWhite = false; } curTextNode++; } else { _reader.ResolveEntity(); _reader.Read(); } break; case XmlNodeType.EndEntity: _reader.Read(); break; default: _nodeType = ( !textIsWhite ? XmlNodeType.Text : textPreserveWS ? XmlNodeType.SignificantWhitespace : /*default: */ XmlNodeType.Whitespace ); return(curTextNode); } } while (true); }
internal static bool IsHighSurrogate(int ch) { return(XmlCharType.InRange(ch, 55296, 56319)); }
public bool NextLex() { SkipSpace(); switch (CurrentChar) { case '\0': _kind = LexKind.Eof; return(false); case ',': case '@': case '(': case ')': case '|': case '*': case '[': case ']': case '+': case '-': case '=': case '#': case '$': _kind = (LexKind)Convert.ToInt32(CurrentChar, CultureInfo.InvariantCulture); NextChar(); break; case '<': _kind = LexKind.Lt; NextChar(); if (CurrentChar == '=') { _kind = LexKind.Le; NextChar(); } break; case '>': _kind = LexKind.Gt; NextChar(); if (CurrentChar == '=') { _kind = LexKind.Ge; NextChar(); } break; case '!': _kind = LexKind.Bang; NextChar(); if (CurrentChar == '=') { _kind = LexKind.Ne; NextChar(); } break; case '.': _kind = LexKind.Dot; NextChar(); if (CurrentChar == '.') { _kind = LexKind.DotDot; NextChar(); } else if (XmlCharType.IsDigit(CurrentChar)) { _kind = LexKind.Number; _numberValue = ScanFraction(); } break; case '/': _kind = LexKind.Slash; NextChar(); if (CurrentChar == '/') { _kind = LexKind.SlashSlash; NextChar(); } break; case '"': case '\'': _kind = LexKind.String; _stringValue = ScanString(); break; default: if (XmlCharType.IsDigit(CurrentChar)) { _kind = LexKind.Number; _numberValue = ScanNumber(); } else if (XmlCharType.IsStartNCNameSingleChar(CurrentChar)) { _kind = LexKind.Name; _name = ScanName(); _prefix = string.Empty; // "foo:bar" is one lexeme not three because it doesn't allow spaces in between // We should distinct it from "foo::" and need process "foo ::" as well if (CurrentChar == ':') { NextChar(); // can be "foo:bar" or "foo::" if (CurrentChar == ':') { // "foo::" NextChar(); _kind = LexKind.Axe; } else { // "foo:*", "foo:bar" or "foo: " _prefix = _name; if (CurrentChar == '*') { NextChar(); _name = "*"; } else if (XmlCharType.IsStartNCNameSingleChar(CurrentChar)) { _name = ScanName(); } else { throw XPathException.Create(SR.Xp_InvalidName, SourceText); } } } else { SkipSpace(); if (CurrentChar == ':') { NextChar(); // it can be "foo ::" or just "foo :" if (CurrentChar == ':') { NextChar(); _kind = LexKind.Axe; } else { throw XPathException.Create(SR.Xp_InvalidName, SourceText); } } } SkipSpace(); _canBeFunction = (CurrentChar == '('); } else { throw XPathException.Create(SR.Xp_InvalidToken, SourceText); } break; } return(true); }
private unsafe void Decode(char *pChars, char *pCharsEndPos, byte *pBytes, byte *pBytesEndPos, out int charsDecoded, out int bytesDecoded) { #if DEBUG Debug.Assert(pCharsEndPos - pChars >= 0); Debug.Assert(pBytesEndPos - pBytes >= 0); #endif // walk hex digits pairing them up and shoving the value of each pair into a byte byte * pByte = pBytes; char * pChar = pChars; int b = _bits; int bFilled = _bitsFilled; XmlCharType xmlCharType = XmlCharType.Instance; while (pChar < pCharsEndPos && pByte < pBytesEndPos) { char ch = *pChar; // end? if (ch == '=') { break; } pChar++; // ignore white space if (xmlCharType.IsWhiteSpace(ch)) { continue; } int digit; if (ch > 122 || (digit = s_mapBase64[ch]) == Invalid) { throw new XmlException(SR.Xml_InvalidBase64Value, new string(pChars, 0, (int)(pCharsEndPos - pChars))); } b = (b << 6) | digit; bFilled += 6; if (bFilled >= 8) { // get top eight valid bits *pByte++ = (byte)((b >> (bFilled - 8)) & 0xFF); bFilled -= 8; if (pByte == pBytesEndPos) { goto Return; } } } if (pChar < pCharsEndPos && *pChar == '=') { bFilled = 0; // ignore padding chars do { pChar++; } while (pChar < pCharsEndPos && *pChar == '='); // ignore whitespace after the padding chars if (pChar < pCharsEndPos) { do { if (!xmlCharType.IsWhiteSpace(*pChar++)) { throw new XmlException(SR.Xml_InvalidBase64Value, new string(pChars, 0, (int)(pCharsEndPos - pChars))); } } while (pChar < pCharsEndPos); } } Return: _bits = b; _bitsFilled = bFilled; bytesDecoded = (int)(pByte - pBytes); charsDecoded = (int)(pChar - pChars); }
// // Constructor // public XmlTextEncoder(TextWriter textWriter) { _textWriter = textWriter; _quoteChar = '"'; _xmlCharType = XmlCharType.Instance; }
public bool NextLex() { SkipSpace(); switch (this.CurerntChar) { case '\0': kind = LexKind.Eof; return(false); case ',': case '@': case '(': case ')': case '|': case '*': case '[': case ']': case '+': case '-': case '=': case '#': case '$': kind = (LexKind)Convert.ToInt32(this.CurerntChar, CultureInfo.InvariantCulture); NextChar(); break; case '<': kind = LexKind.Lt; NextChar(); if (this.CurerntChar == '=') { kind = LexKind.Le; NextChar(); } break; case '>': kind = LexKind.Gt; NextChar(); if (this.CurerntChar == '=') { kind = LexKind.Ge; NextChar(); } break; case '!': kind = LexKind.Bang; NextChar(); if (this.CurerntChar == '=') { kind = LexKind.Ne; NextChar(); } break; case '.': kind = LexKind.Dot; NextChar(); if (this.CurerntChar == '.') { kind = LexKind.DotDot; NextChar(); } else if (XmlCharType.IsDigit(this.CurerntChar)) { kind = LexKind.Number; numberValue = ScanFraction(); } break; case '/': kind = LexKind.Slash; NextChar(); if (this.CurerntChar == '/') { kind = LexKind.SlashSlash; NextChar(); } break; case '"': case '\'': this.kind = LexKind.String; this.stringValue = ScanString(); break; default: if (XmlCharType.IsDigit(this.CurerntChar)) { kind = LexKind.Number; numberValue = ScanNumber(); } else if (xmlCharType.IsStartNCNameSingleChar(this.CurerntChar) #if XML10_FIFTH_EDITION || xmlCharType.IsNCNameHighSurrogateChar(this.CurerntChar) #endif ) { kind = LexKind.Name; this.name = ScanName(); this.prefix = string.Empty; // "foo:bar" is one lexem not three because it doesn't allow spaces in between // We should distinct it from "foo::" and need process "foo ::" as well if (this.CurerntChar == ':') { NextChar(); // can be "foo:bar" or "foo::" if (this.CurerntChar == ':') // "foo::" { NextChar(); kind = LexKind.Axe; } else // "foo:*", "foo:bar" or "foo: " { this.prefix = this.name; if (this.CurerntChar == '*') { NextChar(); this.name = "*"; } else if (xmlCharType.IsStartNCNameSingleChar(this.CurerntChar) #if XML10_FIFTH_EDITION || xmlCharType.IsNCNameHighSurrogateChar(this.CurerntChar) #endif ) { this.name = ScanName(); } else { throw XPathException.Create(Res.Xp_InvalidName, SourceText); } } } else { SkipSpace(); if (this.CurerntChar == ':') { NextChar(); // it can be "foo ::" or just "foo :" if (this.CurerntChar == ':') { NextChar(); kind = LexKind.Axe; } else { throw XPathException.Create(Res.Xp_InvalidName, SourceText); } } } SkipSpace(); this.canBeFunction = (this.CurerntChar == '('); } else { throw XPathException.Create(Res.Xp_InvalidToken, SourceText); } break; } return(true); }
public bool NextLex() { this.SkipSpace(); switch (this.CurerntChar) { case '[': case ']': case '|': case '#': case '$': case '(': case ')': case '*': case '+': case ',': case '-': case '=': case '@': this.kind = (LexKind)Convert.ToInt32(this.CurerntChar, CultureInfo.InvariantCulture); this.NextChar(); break; case '!': this.kind = LexKind.Bang; this.NextChar(); if (this.CurerntChar == '=') { this.kind = LexKind.Ne; this.NextChar(); } break; case '"': case '\'': this.kind = LexKind.String; this.stringValue = this.ScanString(); break; case '.': this.kind = LexKind.Dot; this.NextChar(); if (this.CurerntChar != '.') { if (XmlCharType.IsDigit(this.CurerntChar)) { this.kind = LexKind.Number; this.numberValue = this.ScanFraction(); } } else { this.kind = LexKind.DotDot; this.NextChar(); } break; case '/': this.kind = LexKind.Slash; this.NextChar(); if (this.CurerntChar == '/') { this.kind = LexKind.SlashSlash; this.NextChar(); } break; case '<': this.kind = LexKind.Lt; this.NextChar(); if (this.CurerntChar == '=') { this.kind = LexKind.Le; this.NextChar(); } break; case '>': this.kind = LexKind.Gt; this.NextChar(); if (this.CurerntChar == '=') { this.kind = LexKind.Ge; this.NextChar(); } break; case '\0': this.kind = LexKind.Eof; return(false); default: if (XmlCharType.IsDigit(this.CurerntChar)) { this.kind = LexKind.Number; this.numberValue = this.ScanNumber(); } else { if (!this.xmlCharType.IsStartNCNameSingleChar(this.CurerntChar)) { throw XPathException.Create("Xp_InvalidToken", this.SourceText); } this.kind = LexKind.Name; this.name = this.ScanName(); this.prefix = string.Empty; if (this.CurerntChar == ':') { this.NextChar(); if (this.CurerntChar != ':') { this.prefix = this.name; if (this.CurerntChar != '*') { if (!this.xmlCharType.IsStartNCNameSingleChar(this.CurerntChar)) { throw XPathException.Create("Xp_InvalidName", this.SourceText); } this.name = this.ScanName(); } else { this.NextChar(); this.name = "*"; } } else { this.NextChar(); this.kind = LexKind.Axe; } } else { this.SkipSpace(); if (this.CurerntChar == ':') { this.NextChar(); if (this.CurerntChar != ':') { throw XPathException.Create("Xp_InvalidName", this.SourceText); } this.NextChar(); this.kind = LexKind.Axe; } } this.SkipSpace(); this.canBeFunction = this.CurerntChar == '('; } break; } return(true); }
public void Write(char[] array, int offset, int count) { if (null == array) { throw new ArgumentNullException(nameof(array)); } if (0 > offset) { throw new ArgumentOutOfRangeException(nameof(offset)); } if (0 > count) { throw new ArgumentOutOfRangeException(nameof(count)); } if (count > array.Length - offset) { throw new ArgumentOutOfRangeException(nameof(count)); } if (_cacheAttrValue) { _attrValue.Append(array, offset, count); } int endPos = offset + count; int i = offset; char ch = (char)0; for (;;) { int startPos = i; //unsafe //{ while (i < endPos && _xmlCharType.IsAttributeValueChar(ch = array[i])) { i++; } //} if (startPos < i) { _textWriter.Write(array, startPos, i - startPos); } if (i == endPos) { break; } switch (ch) { case (char)0x9: _textWriter.Write(ch); break; case (char)0xA: case (char)0xD: if (_inAttribute) { WriteCharEntityImpl(ch); } else { _textWriter.Write(ch); } break; case '<': WriteEntityRefImpl("lt"); break; case '>': WriteEntityRefImpl("gt"); break; case '&': WriteEntityRefImpl("amp"); break; case '\'': if (_inAttribute && _quoteChar == ch) { WriteEntityRefImpl("apos"); } else { _textWriter.Write('\''); } break; case '"': if (_inAttribute && _quoteChar == ch) { WriteEntityRefImpl("quot"); } else { _textWriter.Write('"'); } break; default: if (XmlCharType.IsHighSurrogate(ch)) { if (i + 1 < endPos) { WriteSurrogateChar(array[++i], ch); } else { throw new Exception("throw new ArgumentException(SR.Xml_SurrogatePairSplit);"); } } else if (XmlCharType.IsLowSurrogate(ch)) { throw new Exception("throw XmlConvert.CreateInvalidHighSurrogateCharException(ch)"); } else { Debug.Assert((ch < 0x20 && !_xmlCharType.IsWhiteSpace(ch)) || (ch > 0xFFFD)); WriteCharEntityImpl(ch); } break; } i++; } }
internal int AddBooleanQuery(string xpathQuery) { string modifiedQuery = XmlCharType.IsOnlyWhitespace(xpathQuery) ? xpathQuery : $"boolean({xpathQuery})"; return(AddQuery(modifiedQuery)); }
public void Write(string text) { if (text == null) { return; } if (_cacheAttrValue) { _attrValue.Append(text); } // scan through the string to see if there are any characters to be escaped int len = text.Length; int i = 0; int startPos = 0; char ch = (char)0; for (;;) { while (i < len && _xmlCharType.IsAttributeValueChar(ch = text[i])) { i++; } if (i == len) { // reached the end of the string -> write it whole out _textWriter.Write(text); return; } if (_inAttribute) { if (ch == 0x9) { i++; continue; } } else { if (ch == 0x9 || ch == 0xA || ch == 0xD || ch == '"' || ch == '\'') { i++; continue; } } // some character that needs to be escaped is found: break; } char[] helperBuffer = new char[256]; for (;;) { if (startPos < i) { WriteStringFragment(text, startPos, i - startPos, helperBuffer); } if (i == len) { break; } switch (ch) { case (char)0x9: _textWriter.Write(ch); break; case (char)0xA: case (char)0xD: if (_inAttribute) { WriteCharEntityImpl(ch); } else { _textWriter.Write(ch); } break; case '<': WriteEntityRefImpl("lt"); break; case '>': WriteEntityRefImpl("gt"); break; case '&': WriteEntityRefImpl("amp"); break; case '\'': if (_inAttribute && _quoteChar == ch) { WriteEntityRefImpl("apos"); } else { _textWriter.Write('\''); } break; case '"': if (_inAttribute && _quoteChar == ch) { WriteEntityRefImpl("quot"); } else { _textWriter.Write('"'); } break; default: if (XmlCharType.IsHighSurrogate(ch)) { if (i + 1 < len) { WriteSurrogateChar(text[++i], ch); } else { throw new Exception("throw XmlConvert.CreateInvalidSurrogatePairException(text[i], ch);"); } } else if (XmlCharType.IsLowSurrogate(ch)) { throw new Exception("throw XmlConvert.CreateInvalidHighSurrogateCharException(ch);"); } else { Debug.Assert((ch < 0x20 && !_xmlCharType.IsWhiteSpace(ch)) || (ch > 0xFFFD)); WriteCharEntityImpl(ch); } break; } i++; startPos = i; while (i < len && _xmlCharType.IsAttributeValueChar(ch = text[i])) { i++; } } }
// This method trims whitespaces from the beginnig and the end of the string and cached writer events internal void Trim() { // if only one string value -> trim the write spaces directly if (_singleStringValue != null) { _singleStringValue = XmlConvert.TrimString(_singleStringValue); return; } // trim the string in StringBuilder string valBefore = _stringValue.ToString(); string valAfter = XmlConvert.TrimString(valBefore); if (valBefore != valAfter) { _stringValue = new StringBuilder(valAfter); } // trim the beginning of the recorded writer events XmlCharType xmlCharType = XmlCharType.Instance; int i = _firstItem; while (i == _firstItem && i <= _lastItem) { Item item = _items[i]; switch (item.type) { case ItemType.Whitespace: _firstItem++; break; case ItemType.String: case ItemType.Raw: case ItemType.ValueString: item.data = XmlConvert.TrimStringStart((string)item.data); if (((string)item.data).Length == 0) { // no characters left -> move the firstItem index to exclude it from the Replay _firstItem++; } break; case ItemType.StringChars: case ItemType.RawChars: BufferChunk bufChunk = (BufferChunk)item.data; int endIndex = bufChunk.index + bufChunk.count; while (bufChunk.index < endIndex && xmlCharType.IsWhiteSpace(bufChunk.buffer[bufChunk.index])) { bufChunk.index++; bufChunk.count--; } if (bufChunk.index == endIndex) { // no characters left -> move the firstItem index to exclude it from the Replay _firstItem++; } break; } i++; } // trim the end of the recorded writer events i = _lastItem; while (i == _lastItem && i >= _firstItem) { Item item = _items[i]; switch (item.type) { case ItemType.Whitespace: _lastItem--; break; case ItemType.String: case ItemType.Raw: case ItemType.ValueString: item.data = XmlConvert.TrimStringEnd((string)item.data); if (((string)item.data).Length == 0) { // no characters left -> move the lastItem index to exclude it from the Replay _lastItem--; } break; case ItemType.StringChars: case ItemType.RawChars: BufferChunk bufChunk = (BufferChunk)item.data; while (bufChunk.count > 0 && xmlCharType.IsWhiteSpace(bufChunk.buffer[bufChunk.index + bufChunk.count - 1])) { bufChunk.count--; } if (bufChunk.count == 0) { // no characters left -> move the lastItem index to exclude it from the Replay _lastItem--; } break; } i--; } }
public static string NormalizeSpace(string value) { XmlCharType xmlCharType = XmlCharType.Instance; StringBuilder sb = null; int idx, idxStart = 0, idxSpace = 0; for (idx = 0; idx < value.Length; idx++) { if (xmlCharType.IsWhiteSpace(value[idx])) { if (idx == idxStart) { // Previous character was a whitespace character, so discard this character idxStart++; } else if (value[idx] != ' ' || idxSpace == idx) { // Space was previous character or this is a non-space character if (sb == null) { sb = new StringBuilder(value.Length); } else { sb.Append(' '); } // Copy non-space characters into string builder if (idxSpace == idx) { sb.Append(value, idxStart, idx - idxStart - 1); } else { sb.Append(value, idxStart, idx - idxStart); } idxStart = idx + 1; } else { // Single whitespace character doesn't cause normalization, but mark its position idxSpace = idx + 1; } } } if (sb == null) { // Check for string that is entirely composed of whitespace if (idxStart == idx) { return(string.Empty); } // If string does not end with a space, then it must already be normalized if (idxStart == 0 && idxSpace != idx) { return(value); } sb = new StringBuilder(value.Length); } else if (idx != idxStart) { sb.Append(' '); } // Copy non-space characters into string builder if (idxSpace == idx) { sb.Append(value, idxStart, idx - idxStart - 1); } else { sb.Append(value, idxStart, idx - idxStart); } return(sb.ToString()); }
internal int AddStringQuery(string xpathQuery) { string modifiedQuery = XmlCharType.IsOnlyWhitespace(xpathQuery) ? xpathQuery : "string(" + xpathQuery + ")"; return(AddQuery(modifiedQuery)); }
/// <summary> /// Override Read in order to search for strippable whitespace, to concatenate adjacent text nodes, and to /// resolve entities. /// </summary> public override bool Read() { string?ws = null; // Clear text value _val = null; while (base.Read()) { switch (base.NodeType) { case XmlNodeType.Element: // Push boolean indicating whether whitespace children of this element should be stripped if (!base.IsEmptyElement) { _stkStrip.PushBit(_shouldStrip); // Strip if rules say we should and we're not within the scope of xml:space="preserve" _shouldStrip = _wsRules.ShouldStripSpace(base.LocalName, base.NamespaceURI) && (base.XmlSpace != XmlSpace.Preserve); } break; case XmlNodeType.EndElement: // Restore parent shouldStrip setting _shouldStrip = _stkStrip.PopBit(); break; case XmlNodeType.Text: case XmlNodeType.CDATA: // If preserving adjacent text, don't perform any further checks if (_preserveAdjacent) { return(true); } if (_shouldStrip) { // Reader may report whitespace as Text or CDATA if (XmlCharType.IsOnlyWhitespace(base.Value)) { goto case XmlNodeType.Whitespace; } // If whitespace was cached, then prepend it to text or CDATA value if (ws != null) { _val = string.Concat(ws, base.Value); } // Preserve adjacent whitespace _preserveAdjacent = true; return(true); } break; case XmlNodeType.Whitespace: case XmlNodeType.SignificantWhitespace: // If preserving adjacent text, don't perform any further checks if (_preserveAdjacent) { return(true); } if (_shouldStrip) { // Save whitespace until it can be determined whether it will be stripped ws = ws == null ? base.Value : string.Concat(ws, base.Value); // Read next event continue; } break; case XmlNodeType.EndEntity: // Read next event continue; } // No longer preserve adjacent space _preserveAdjacent = false; return(true); } return(false); }
private bool IsValidXmlVersion(string ver) { return(ver.Length >= 3 && ver[0] == '1' && ver[1] == '.' && XmlCharType.IsOnlyDigits(ver, 2, ver.Length - 2)); }
public void NextLex() { _prevLexEnd = _curIndex; _prevKind = _kind; SkipSpace(); _lexStart = _curIndex; switch (_curChar) { case '\0': _kind = LexKind.Eof; return; case '(': case ')': case '[': case ']': case '@': case ',': case '$': case '}': _kind = (LexKind)_curChar; NextChar(); break; case '.': NextChar(); if (_curChar == '.') { _kind = LexKind.DotDot; NextChar(); } else if (IsAsciiDigit(_curChar)) { SetSourceIndex(_lexStart); goto case '0'; } else { _kind = LexKind.Dot; } break; case ':': NextChar(); if (_curChar == ':') { _kind = LexKind.ColonColon; NextChar(); } else { _kind = LexKind.Unknown; } break; case '*': _kind = LexKind.Star; NextChar(); CheckOperator(true); break; case '/': NextChar(); if (_curChar == '/') { _kind = LexKind.SlashSlash; NextChar(); } else { _kind = LexKind.Slash; } break; case '|': _kind = LexKind.Union; NextChar(); break; case '+': _kind = LexKind.Plus; NextChar(); break; case '-': _kind = LexKind.Minus; NextChar(); break; case '=': _kind = LexKind.Eq; NextChar(); break; case '!': NextChar(); if (_curChar == '=') { _kind = LexKind.Ne; NextChar(); } else { _kind = LexKind.Unknown; } break; case '<': NextChar(); if (_curChar == '=') { _kind = LexKind.Le; NextChar(); } else { _kind = LexKind.Lt; } break; case '>': NextChar(); if (_curChar == '=') { _kind = LexKind.Ge; NextChar(); } else { _kind = LexKind.Gt; } break; case '"': case '\'': _kind = LexKind.String; ScanString(); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': _kind = LexKind.Number; ScanNumber(); break; default: if (XmlCharType.IsStartNCNameSingleChar(_curChar)) { _kind = LexKind.Name; _name = ScanNCName(); _prefix = string.Empty; _canBeFunction = false; _axis = XPathAxis.Unknown; bool colonColon = false; int saveSourceIndex = _curIndex; // "foo:bar" or "foo:*" -- one lexeme (no spaces allowed) // "foo::" or "foo ::" -- two lexemes, reported as one (AxisName) // "foo:?" or "foo :?" -- lexeme "foo" reported if (_curChar == ':') { NextChar(); if (_curChar == ':') { // "foo::" -> OperatorName, AxisName NextChar(); colonColon = true; SetSourceIndex(saveSourceIndex); } else { // "foo:bar", "foo:*" or "foo:?" if (_curChar == '*') { NextChar(); _prefix = _name; _name = "*"; } else if (XmlCharType.IsStartNCNameSingleChar(_curChar)) { _prefix = _name; _name = ScanNCName(); // Look ahead for '(' to determine whether QName can be a FunctionName saveSourceIndex = _curIndex; SkipSpace(); _canBeFunction = (_curChar == '('); SetSourceIndex(saveSourceIndex); } else { // "foo:?" -> OperatorName, NameTest // Return "foo" and leave ":" to be reported later as an unknown lexeme SetSourceIndex(saveSourceIndex); } } } else { SkipSpace(); if (_curChar == ':') { // "foo ::" or "foo :?" NextChar(); if (_curChar == ':') { NextChar(); colonColon = true; } SetSourceIndex(saveSourceIndex); } else { _canBeFunction = (_curChar == '('); } } if (!CheckOperator(false) && colonColon) { _axis = CheckAxis(); } } else { _kind = LexKind.Unknown; NextChar(); } break; } }
internal static bool IsSurrogate(int ch) { return(XmlCharType.InRange(ch, 55296, 57343)); }