Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Document/NewLineFinder.cs @ 14419

Last change on this file since 14419 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 5.3 KB
Line 
1// Copyright (c) 2014 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
19using System;
20using System.Text;
21#if NREFACTORY
22using ICSharpCode.NRefactory.Editor;
23#endif
24
25namespace ICSharpCode.AvalonEdit.Document
26{
27  static class NewLineFinder
28  {
29    static readonly char[] newline = { '\r', '\n' };
30   
31    internal static readonly string[] NewlineStrings = { "\r\n", "\r", "\n" };
32   
33    /// <summary>
34    /// Gets the location of the next new line character, or SimpleSegment.Invalid
35    /// if none is found.
36    /// </summary>
37    internal static SimpleSegment NextNewLine(string text, int offset)
38    {
39      int pos = text.IndexOfAny(newline, offset);
40      if (pos >= 0) {
41        if (text[pos] == '\r') {
42          if (pos + 1 < text.Length && text[pos + 1] == '\n')
43            return new SimpleSegment(pos, 2);
44        }
45        return new SimpleSegment(pos, 1);
46      }
47      return SimpleSegment.Invalid;
48    }
49   
50    /// <summary>
51    /// Gets the location of the next new line character, or SimpleSegment.Invalid
52    /// if none is found.
53    /// </summary>
54    internal static SimpleSegment NextNewLine(ITextSource text, int offset)
55    {
56      int textLength = text.TextLength;
57      int pos = text.IndexOfAny(newline, offset, textLength - offset);
58      if (pos >= 0) {
59        if (text.GetCharAt(pos) == '\r') {
60          if (pos + 1 < textLength && text.GetCharAt(pos + 1) == '\n')
61            return new SimpleSegment(pos, 2);
62        }
63        return new SimpleSegment(pos, 1);
64      }
65      return SimpleSegment.Invalid;
66    }
67  }
68 
69  partial class TextUtilities
70  {
71    /// <summary>
72    /// Finds the next new line character starting at offset.
73    /// </summary>
74    /// <param name="text">The text source to search in.</param>
75    /// <param name="offset">The starting offset for the search.</param>
76    /// <param name="newLineType">The string representing the new line that was found, or null if no new line was found.</param>
77    /// <returns>The position of the first new line starting at or after <paramref name="offset"/>,
78    /// or -1 if no new line was found.</returns>
79    public static int FindNextNewLine(ITextSource text, int offset, out string newLineType)
80    {
81      if (text == null)
82        throw new ArgumentNullException("text");
83      if (offset < 0 || offset > text.TextLength)
84        throw new ArgumentOutOfRangeException("offset", offset, "offset is outside of text source");
85      SimpleSegment s = NewLineFinder.NextNewLine(text, offset);
86      if (s == SimpleSegment.Invalid) {
87        newLineType = null;
88        return -1;
89      } else {
90        if (s.Length == 2) {
91          newLineType = "\r\n";
92        } else if (text.GetCharAt(s.Offset) == '\n') {
93          newLineType = "\n";
94        } else {
95          newLineType = "\r";
96        }
97        return s.Offset;
98      }
99    }
100   
101    /// <summary>
102    /// Gets whether the specified string is a newline sequence.
103    /// </summary>
104    public static bool IsNewLine(string newLine)
105    {
106      return newLine == "\r\n" || newLine == "\n" || newLine == "\r";
107    }
108   
109    /// <summary>
110    /// Normalizes all new lines in <paramref name="input"/> to be <paramref name="newLine"/>.
111    /// </summary>
112    public static string NormalizeNewLines(string input, string newLine)
113    {
114      if (input == null)
115        return null;
116      if (!IsNewLine(newLine))
117        throw new ArgumentException("newLine must be one of the known newline sequences");
118      SimpleSegment ds = NewLineFinder.NextNewLine(input, 0);
119      if (ds == SimpleSegment.Invalid) // text does not contain any new lines
120        return input;
121      StringBuilder b = new StringBuilder(input.Length);
122      int lastEndOffset = 0;
123      do {
124        b.Append(input, lastEndOffset, ds.Offset - lastEndOffset);
125        b.Append(newLine);
126        lastEndOffset = ds.EndOffset;
127        ds = NewLineFinder.NextNewLine(input, lastEndOffset);
128      } while (ds != SimpleSegment.Invalid);
129      // remaining string (after last newline)
130      b.Append(input, lastEndOffset, input.Length - lastEndOffset);
131      return b.ToString();
132    }
133   
134    /// <summary>
135    /// Gets the newline sequence used in the document at the specified line.
136    /// </summary>
137    public static string GetNewLineFromDocument(IDocument document, int lineNumber)
138    {
139      IDocumentLine line = document.GetLineByNumber(lineNumber);
140      if (line.DelimiterLength == 0) {
141        // at the end of the document, there's no line delimiter, so use the delimiter
142        // from the previous line
143        line = line.PreviousLine;
144        if (line == null)
145          return Environment.NewLine;
146      }
147      return document.GetText(line.Offset + line.Length, line.DelimiterLength);
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.