PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/UiException/DefaultTextManager.cs

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