19 using System.Collections.Generic;
20 using System.Collections.ObjectModel;
23 using System.Text.RegularExpressions;
33 #region Public properties 44 _text = String.Join(Environment.NewLine, _lines);
51 public int NumberOfLines
63 public bool IgnoreHashedComments {
get;
set; }
67 #region Public methods 75 public void Add(
string text)
77 Add(TransformNewLines(text), Environment.NewLine);
86 public void Add(
string text,
string newLineSeparator)
88 foreach (
string line
in text.Split(
89 new string[] { newLineSeparator }, StringSplitOptions.None))
101 if (IgnoreHashedComments)
103 line = Regex.Replace(line,
@"#.*$", String.Empty);
106 if (String.IsNullOrEmpty(line))
return;
109 line = line.TrimEnd(
' ',
'\t',
'\n',
'\r');
110 bool forceNewLine = !HasSameIndent(line);
111 forceNewLine |= String.IsNullOrEmpty(line);
113 forceNewLine |= IsListItem(line);
114 if (forceNewLine || _lines.Count == 0)
120 _lines[_lines.Count - 1] +=
" " + line;
131 _lines =
new Collection<string>();
134 public Multiline(
bool ignoreHashedComments)
137 IgnoreHashedComments = ignoreHashedComments;
150 public Multiline(
string text,
bool ignoreHashedComments)
151 :
this(ignoreHashedComments)
158 #region Private methods 165 private bool HasSameIndent(
string line)
167 Regex r =
new Regex(
@"^(\s+)");
168 Match m = r.Match(line);
169 int indent = (m.Success) ? m.Length : 0;
170 bool isSame = _lastIndent == indent;
171 if (String.IsNullOrEmpty(line))
179 _lastIndent = indent;
184 private bool IsListItem(
string line)
186 Regex r =
new Regex(
@"^\s*(([-*])|(#+|\d+)\.?\)?)\s");
187 Match m = r.Match(line);
190 _lastIndent = m.Length;
206 private string TransformNewLines(
string text)
209 string result = Regex.Replace(text,
@"\n\r",
"\r\n");
210 result = Regex.Replace(result,
@"(?<!\r)\n",
"\r\n");
211 result = Regex.Replace(result,
@"\r(?!\n)",
"\r\n");
215 if (Environment.NewLine !=
"\r\n")
217 result = Regex.Replace(result,
@"\r\n", Environment.NewLine);
225 #region Private fields 230 Collection<String> _lines;
Represents a text with multiple lines.
Multiline(string text)
Creates an instance using text as initial text.
void AddLine(string line)
Adds an individual line.
void Add(string text)
Adds text to the multiline text.
void Add(string text, string newLineSeparator)
Adds text to the multiline text by splitting it into multiple lines by the newLineSeparator ...