using System; using System.Text; namespace Upsilon.Text { public class SourceText { private readonly string[] _text; public SourceText(string text) { _text = text.Split('\n'); } public string GetSpan(int startLine, int startPosition, int endLine, int endPosition) { if (startPosition < 0) startPosition = 0; if (startLine >= _text.Length) return string.Empty; if (startLine == endLine) { if (endPosition > _text[startLine].Length) endPosition = _text[startLine].Length; return _text[startLine].Substring(startPosition, endPosition - startPosition); } var sb = new StringBuilder(); sb.Append(_text[startLine].Substring(startPosition)); for (var i = startLine + 1; i < endLine; i++) { sb.Append(_text[i]); } if (endPosition > _text[endLine].Length) endPosition = _text[endLine].Length; sb.Append(_text[endLine].Substring(0, endPosition)); return sb.ToString(); } public string GetLine(int linePosition) { return _text[linePosition]; } public string GetSpan(TextSpan span) { return GetSpan(span.StartLine, span.StartPosition, span.EndLine, span.EndPosition); } } }