PageRenderTime 19ms CodeModel.GetById 14ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/UiException/DefaultTextManager.cs

#
C# | 141 lines | 75 code | 23 blank | 43 comment | 4 complexity | a19593295d6f8ba5da24e5492938f6d8 MD5 | raw file
  1// ****************************************************************
  2// This is free software licensed under the NUnit license. You may
  3// obtain a copy of the license at http://nunit.org
  4// ****************************************************************
  5
  6using System;
  7using System.Collections.Generic;
  8using System.Text;
  9using System.Collections;
 10
 11namespace NUnit.UiException
 12{
 13    /// <summary>
 14    /// This is a default implementation of ITextManager interface.
 15    /// </summary>
 16    public class DefaultTextManager :
 17        IEnumerable,
 18        ITextManager
 19    {
 20        /// <summary>
 21        /// Hold the text to be managed by this instance.
 22        /// </summary>
 23        private string _text;
 24
 25        /// <summary>
 26        /// Array of strings where each string is a line in this text.
 27        /// </summary>
 28        private List<string> _lines;
 29
 30        /// <summary>
 31        /// Stores the character count of the longest line in this text.
 32        /// </summary>
 33        private int _maxLength;
 34
 35        /// <summary>
 36        /// Builds a new instance of TextManager.
 37        /// </summary>
 38        public DefaultTextManager()
 39        {
 40            _lines = new List<string>();
 41            Text = "";
 42
 43            return;
 44        }
 45
 46        /// <summary>
 47        /// Gets the number of lines in the text.
 48        /// </summary>
 49        public int LineCount
 50        {
 51            get { return (_lines.Count); }
 52        }
 53
 54        /// <summary>
 55        /// Gets or sets the text to be managed by this object.
 56        /// </summary>
 57        public string Text
 58        {
 59            get { return (_text); }
 60            set
 61            {
 62                if (value == null)
 63                    value = "";
 64
 65                _text = value;
 66                _populateLineCollection(value);
 67            }
 68        }
 69
 70        /// <summary>
 71        /// Gets the line of text at the specified startingPosition.
 72        /// (zero based startingPosition).
 73        /// </summary>
 74        /// <param name="lineIndex">The startingPosition of the line to get.</param>
 75        /// <returns>A string that represents the content of the specified line without
 76        /// the trailing characters.</returns>
 77        public string GetTextAt(int lineIndex)
 78        {
 79            return (_lines[lineIndex]);
 80        }
 81
 82        /// <summary>
 83        /// Gets the character count of the longest line in this text.
 84        /// </summary>
 85        public int MaxLength
 86        {
 87            get { return (_maxLength); }
 88        }
 89
 90        #region private definitions
 91
 92        /// <summary>
 93        /// setup member data with the input text.
 94        /// </summary>
 95        private void _populateLineCollection(string text)
 96        {
 97            string line;
 98            int textIndex;
 99            int newIndex;
100
101            textIndex = 0;
102
103            _lines.Clear();
104            _maxLength = 0;
105
106            while ((newIndex = text.IndexOf("\n", textIndex, StringComparison.Ordinal)) > textIndex)
107            {
108                line = text.Substring(textIndex, newIndex - textIndex).TrimEnd();
109                _maxLength = Math.Max(_maxLength, line.Length);
110
111                _lines.Add(line);
112                textIndex = newIndex + 1;
113            }
114
115            if (textIndex < text.Length)
116            {
117                line = text.Substring(textIndex).TrimEnd();
118                _maxLength = Math.Max(_maxLength, line.Length);
119                _lines.Add(line);
120            }
121
122            return;
123        }
124
125        #endregion
126
127        #region IEnumerable Membres
128
129        /// <summary>
130        /// Gets an IEnumerator that iterate through each line of the
131        /// current text. 
132        /// </summary>
133        /// <returns>An IEnumerator that iterate through each line of this text.</returns>
134        public IEnumerator GetEnumerator()
135        {
136            return (_lines.GetEnumerator());
137        }
138
139        #endregion      
140    }
141}