-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTextCoordinateSystemComputer.cs
More file actions
45 lines (38 loc) · 1.29 KB
/
Copy pathTextCoordinateSystemComputer.cs
File metadata and controls
45 lines (38 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Buffers;
namespace HydraScript.Domain.FrontEnd.Lexer.Impl;
public class TextCoordinateSystemComputer : ITextCoordinateSystemComputer
{
private readonly SearchValues<char> _sv = SearchValues.Create('\n');
/// <inheritdoc/>
public IReadOnlyList<int> GetLines(string text)
{
var newText = text.EndsWith(Environment.NewLine)
? text
: text + Environment.NewLine;
var textLength = newText.Length;
var indices = new List<int>(capacity: 128) { -1 };
var textAsSpan = newText.AsSpan();
while (true)
{
var start = indices[^1] + 1;
if (start == textLength)
break;
var index = textAsSpan.Slice(
start,
length: textLength - start).IndexOfAny(_sv);
indices.Add(start + index);
}
return indices;
}
/// <inheritdoc/>
public Coordinates GetCoordinates(int absoluteIndex, IReadOnlyList<int> newLineList)
{
for (var i = 1; i < newLineList.Count; i++)
if (absoluteIndex <= newLineList[i])
{
var offset = newLineList[i - 1];
return new Coordinates(Line: i, Column: absoluteIndex - offset);
}
return new Coordinates();
}
}