1 | // Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.IO; |
---|
21 | |
---|
22 | namespace ICSharpCode.NRefactory.Editor |
---|
23 | { |
---|
24 | /// <summary> |
---|
25 | /// Implements the ITextSource interface using a string. |
---|
26 | /// </summary> |
---|
27 | [Serializable] |
---|
28 | public class StringTextSource : ITextSource |
---|
29 | { |
---|
30 | /// <summary> |
---|
31 | /// Gets a text source containing the empty string. |
---|
32 | /// </summary> |
---|
33 | public static readonly StringTextSource Empty = new StringTextSource(string.Empty); |
---|
34 | |
---|
35 | readonly string text; |
---|
36 | readonly ITextSourceVersion version; |
---|
37 | |
---|
38 | /// <summary> |
---|
39 | /// Creates a new StringTextSource with the given text. |
---|
40 | /// </summary> |
---|
41 | public StringTextSource(string text) |
---|
42 | { |
---|
43 | if (text == null) |
---|
44 | throw new ArgumentNullException("text"); |
---|
45 | this.text = text; |
---|
46 | } |
---|
47 | |
---|
48 | /// <summary> |
---|
49 | /// Creates a new StringTextSource with the given text. |
---|
50 | /// </summary> |
---|
51 | public StringTextSource(string text, ITextSourceVersion version) |
---|
52 | { |
---|
53 | if (text == null) |
---|
54 | throw new ArgumentNullException("text"); |
---|
55 | this.text = text; |
---|
56 | this.version = version; |
---|
57 | } |
---|
58 | |
---|
59 | /// <inheritdoc/> |
---|
60 | public ITextSourceVersion Version { |
---|
61 | get { return version; } |
---|
62 | } |
---|
63 | |
---|
64 | /// <inheritdoc/> |
---|
65 | public int TextLength { |
---|
66 | get { return text.Length; } |
---|
67 | } |
---|
68 | |
---|
69 | /// <inheritdoc/> |
---|
70 | public string Text { |
---|
71 | get { return text; } |
---|
72 | } |
---|
73 | |
---|
74 | /// <inheritdoc/> |
---|
75 | public ITextSource CreateSnapshot() |
---|
76 | { |
---|
77 | return this; // StringTextSource is immutable |
---|
78 | } |
---|
79 | |
---|
80 | /// <inheritdoc/> |
---|
81 | public ITextSource CreateSnapshot(int offset, int length) |
---|
82 | { |
---|
83 | return new StringTextSource(text.Substring(offset, length)); |
---|
84 | } |
---|
85 | |
---|
86 | /// <inheritdoc/> |
---|
87 | public TextReader CreateReader() |
---|
88 | { |
---|
89 | return new StringReader(text); |
---|
90 | } |
---|
91 | |
---|
92 | /// <inheritdoc/> |
---|
93 | public TextReader CreateReader(int offset, int length) |
---|
94 | { |
---|
95 | return new StringReader(text.Substring(offset, length)); |
---|
96 | } |
---|
97 | |
---|
98 | /// <inheritdoc/> |
---|
99 | public void WriteTextTo(TextWriter writer) |
---|
100 | { |
---|
101 | writer.Write(text); |
---|
102 | } |
---|
103 | |
---|
104 | /// <inheritdoc/> |
---|
105 | public void WriteTextTo(TextWriter writer, int offset, int length) |
---|
106 | { |
---|
107 | writer.Write(text.Substring(offset, length)); |
---|
108 | } |
---|
109 | |
---|
110 | /// <inheritdoc/> |
---|
111 | public char GetCharAt(int offset) |
---|
112 | { |
---|
113 | return text[offset]; |
---|
114 | } |
---|
115 | |
---|
116 | /// <inheritdoc/> |
---|
117 | public string GetText(int offset, int length) |
---|
118 | { |
---|
119 | return text.Substring(offset, length); |
---|
120 | } |
---|
121 | |
---|
122 | /// <inheritdoc/> |
---|
123 | public string GetText(ISegment segment) |
---|
124 | { |
---|
125 | if (segment == null) |
---|
126 | throw new ArgumentNullException("segment"); |
---|
127 | return text.Substring(segment.Offset, segment.Length); |
---|
128 | } |
---|
129 | |
---|
130 | /// <inheritdoc/> |
---|
131 | public int IndexOf(char c, int startIndex, int count) |
---|
132 | { |
---|
133 | return text.IndexOf(c, startIndex, count); |
---|
134 | } |
---|
135 | |
---|
136 | /// <inheritdoc/> |
---|
137 | public int IndexOfAny(char[] anyOf, int startIndex, int count) |
---|
138 | { |
---|
139 | return text.IndexOfAny(anyOf, startIndex, count); |
---|
140 | } |
---|
141 | |
---|
142 | /// <inheritdoc/> |
---|
143 | public int IndexOf(string searchText, int startIndex, int count, StringComparison comparisonType) |
---|
144 | { |
---|
145 | return text.IndexOf(searchText, startIndex, count, comparisonType); |
---|
146 | } |
---|
147 | |
---|
148 | /// <inheritdoc/> |
---|
149 | public int LastIndexOf(char c, int startIndex, int count) |
---|
150 | { |
---|
151 | return text.LastIndexOf(c, startIndex + count - 1, count); |
---|
152 | } |
---|
153 | |
---|
154 | /// <inheritdoc/> |
---|
155 | public int LastIndexOf(string searchText, int startIndex, int count, StringComparison comparisonType) |
---|
156 | { |
---|
157 | return text.LastIndexOf(searchText, startIndex + count - 1, count, comparisonType); |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|