TrimString() static private method

static private TrimString ( string value ) : string
value string
return string
Example #1
0
            // This method trims whitespace from the beginning 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--;
                }
            }