// // ITextSource.cs // // Author: // Mike Krüger // // Copyright (c) 2014 Xamarin Inc. (http://xamarin.com) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.IO; using System.Text; namespace MonoDevelop.Core.Text { /// /// A read-only view on a (potentially mutable) text source. /// The IDocument interface derives from this interface. /// public interface ITextSource { /// /// Gets a version identifier for this text source. /// Returns null for unversioned text sources. /// ITextSourceVersion Version { get; } /// /// Determines if a byte order mark was read or is going to be written. /// bool UseBOM { get; } /// /// Encoding of the text that was read from or is going to be saved to. /// Encoding Encoding { get; } /// /// Gets the total text length. /// /// The length of the text, in characters. /// This is the same as Text.Length, but is more efficient because /// it doesn't require creating a String object. int Length { get; } /// /// Gets the whole text as string. /// [System.Diagnostics.CodeAnalysis.SuppressMessage ("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")] string Text { get; } /// /// Gets a character at the specified position in the document. /// /// The index of the character to get. /// Offset is outside the valid range (0 to TextLength-1). /// The character at the specified position. /// This is the same as Text[offset], but is more efficient because /// it doesn't require creating a String object. char this [int offset] { get; } /// /// Gets a character at the specified position in the document. /// /// The index of the character to get. /// Offset is outside the valid range (0 to TextLength-1). /// The character at the specified position. /// This is the same as Text[offset], but is more efficient because /// it doesn't require creating a String object. char GetCharAt (int offset); /// /// Retrieves the text for a portion of the document. /// /// offset or length is outside the valid range. /// This is the same as Text.Substring, but is more efficient because /// it doesn't require creating a String object for the whole document. string GetTextAt (int offset, int length); /// /// Creates a new TextReader to read from this text source. /// TextReader CreateReader (); /// /// Creates a new TextReader to read from this text source. /// TextReader CreateReader (int offset, int length); /// /// Writes the text from this document into the TextWriter. /// void WriteTextTo (TextWriter writer); /// /// Writes the text from this document into the TextWriter. /// void WriteTextTo (TextWriter writer, int offset, int length); /// /// Creates an immutable snapshot of this text source. /// Unlike all other methods in this interface, this method is thread-safe. /// ITextSource CreateSnapshot (); /// /// Creates an immutable snapshot of a part of this text source. /// Unlike all other methods in this interface, this method is thread-safe. /// ITextSource CreateSnapshot (int offset, int length); } public static class TextSourceExtension { /// /// Retrieves the text for a portion of the document. /// /// offset or length is outside the valid range. public static string GetTextAt (this ITextSource source, ISegment segment) { if (source == null) throw new ArgumentNullException ("source"); return source.GetTextAt (segment.Offset, segment.Length); } public static string GetTextBetween (this ITextSource source, int startOffset, int endOffset) { if (source == null) throw new ArgumentNullException ("source"); if (startOffset < 0 || startOffset > source.Length) throw new ArgumentNullException ("startOffset"); if (endOffset < 0 || endOffset > source.Length) throw new ArgumentNullException ("endOffset"); if (startOffset > endOffset) throw new InvalidOperationException (); return source.GetTextAt (startOffset, endOffset - startOffset); } /// /// Writes the text from this document into a file. /// public static void WriteTextTo (this ITextSource source, string fileName) { if (source == null) throw new ArgumentNullException ("source"); TextFileUtility.WriteText (fileName, source.Text, source.Encoding, source.UseBOM); } /// /// Writes the text from this document into the TextWriter. /// public static void WriteTextTo (this ITextSource source, TextWriter writer, ISegment segment) { if (source == null) throw new ArgumentNullException ("source"); if (writer == null) throw new ArgumentNullException ("writer"); if (segment == null) throw new ArgumentNullException ("segment"); source.WriteTextTo (writer, segment.Offset, segment.Length); } /// /// Creates a new TextReader to read from this text source. /// public static TextReader CreateReader (this ITextSource source, ISegment segment) { if (source == null) throw new ArgumentNullException ("source"); if (segment == null) throw new ArgumentNullException ("segment"); return source.CreateReader (segment.Offset, segment.Length); } /// /// Creates an immutable snapshot of a part of this text source. /// Unlike all other methods in this interface, this method is thread-safe. /// public static ITextSource CreateSnapshot (this ITextSource source, ISegment segment) { if (source == null) throw new ArgumentNullException ("source"); if (segment == null) throw new ArgumentNullException ("segment"); return source.CreateSnapshot (segment.Offset, segment.Length); } } }