1+ using System ;
2+ using System . Text ;
3+
4+ using MsieJavaScriptEngine . Extensions ;
5+ using MsieJavaScriptEngine . Utilities ;
6+
7+ namespace MsieJavaScriptEngine . Helpers
8+ {
9+ /// <summary>
10+ /// Text helpers
11+ /// </summary>
12+ public static class TextHelpers
13+ {
14+ /// <summary>
15+ /// Array of characters used to find the next line break
16+ /// </summary>
17+ private static readonly char [ ] _nextLineBreakChars = new char [ ] { '\r ' , '\n ' } ;
18+
19+
20+ /// <summary>
21+ /// Gets a fragment from the text line
22+ /// </summary>
23+ /// <param name="textLine">Content of the text line</param>
24+ /// <param name="columnNumber">Column number</param>
25+ /// <param name="maxFragmentLength">Maximum length of the text fragment</param>
26+ public static string GetTextFragmentFromLine ( string textLine , int columnNumber ,
27+ int maxFragmentLength = 100 )
28+ {
29+ if ( string . IsNullOrEmpty ( textLine ) )
30+ {
31+ return string . Empty ;
32+ }
33+
34+ string fragment ;
35+ int lineLength = textLine . Length ;
36+
37+ if ( lineLength > maxFragmentLength )
38+ {
39+ const string ellipsisSymbol = "…" ;
40+ string startPart = string . Empty ;
41+ string endPart = string . Empty ;
42+
43+ var leftOffset = ( int ) Math . Floor ( ( double ) maxFragmentLength / 2 ) ;
44+ int fragmentStartPosition = columnNumber - leftOffset - 1 ;
45+ if ( fragmentStartPosition > 0 )
46+ {
47+ if ( lineLength - fragmentStartPosition < maxFragmentLength )
48+ {
49+ fragmentStartPosition = lineLength - maxFragmentLength ;
50+ }
51+ }
52+ else
53+ {
54+ fragmentStartPosition = 0 ;
55+ }
56+ int fragmentLength = maxFragmentLength ;
57+
58+ if ( fragmentStartPosition > 0 )
59+ {
60+ startPart = ellipsisSymbol ;
61+ }
62+ if ( fragmentStartPosition + fragmentLength < lineLength )
63+ {
64+ endPart = ellipsisSymbol ;
65+ }
66+
67+ StringBuilder fragmentBuilder = StringBuilderPool . GetBuilder ( ) ;
68+ if ( startPart . Length > 0 )
69+ {
70+ fragmentBuilder . Append ( startPart ) ;
71+ }
72+ fragmentBuilder . Append ( textLine . Substring ( fragmentStartPosition , fragmentLength ) ) ;
73+ if ( endPart . Length > 0 )
74+ {
75+ fragmentBuilder . Append ( endPart ) ;
76+ }
77+
78+ fragment = fragmentBuilder . ToString ( ) ;
79+ StringBuilderPool . ReleaseBuilder ( fragmentBuilder ) ;
80+ }
81+ else
82+ {
83+ fragment = textLine ;
84+ }
85+
86+ return fragment ;
87+ }
88+
89+ /// <summary>
90+ /// Finds a next line break
91+ /// </summary>
92+ /// <param name="sourceText">Source text</param>
93+ /// <param name="startPosition">Position in the input string that defines the leftmost
94+ /// position to be searched</param>
95+ /// <param name="length">Number of characters in the substring to include in the search</param>
96+ /// <param name="lineBreakPosition">Position of line break</param>
97+ /// <param name="lineBreakLength">Length of line break</param>
98+ internal static void FindNextLineBreak ( string sourceText , int startPosition , int length ,
99+ out int lineBreakPosition , out int lineBreakLength )
100+ {
101+ lineBreakPosition = sourceText . IndexOfAny ( _nextLineBreakChars , startPosition , length ) ;
102+ if ( lineBreakPosition != - 1 )
103+ {
104+ lineBreakLength = 1 ;
105+ char currentCharacter = sourceText [ lineBreakPosition ] ;
106+
107+ if ( currentCharacter == '\r ' )
108+ {
109+ int nextCharacterPosition = lineBreakPosition + 1 ;
110+ char nextCharacter ;
111+
112+ if ( sourceText . TryGetChar ( nextCharacterPosition , out nextCharacter )
113+ && nextCharacter == '\n ' )
114+ {
115+ lineBreakLength = 2 ;
116+ }
117+ }
118+ }
119+ else
120+ {
121+ lineBreakLength = 0 ;
122+ }
123+ }
124+ }
125+ }
0 commit comments