-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormattedText.cs
55 lines (47 loc) · 1.87 KB
/
FormattedText.cs
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
46
47
48
49
50
51
52
53
54
55
using System.Collections.Generic;
using System.Linq;
namespace YetAnotherPacketParser
{
internal class FormattedText
{
public FormattedText(IEnumerable<FormattedTextSegment> formattedTexts)
{
this.Segments = formattedTexts;
}
public IEnumerable<FormattedTextSegment> Segments { get; }
public string UnformattedText => string.Join(string.Empty, this.Segments.Select(t => t.Text));
public FormattedText Substring(int startIndex)
{
List<FormattedTextSegment> segments = new List<FormattedTextSegment>();
int index = 0;
foreach (FormattedTextSegment segment in this.Segments)
{
// TODO: Consider using an enumerator and converting this to a while loop. When we find the first
// instance where we cross the startIndex, break the loop and just add the rest of the segments
// directly.
int nextIndex = index + segment.Text.Length;
if (index < startIndex && nextIndex > startIndex)
{
string substringText = segment.Text.Substring(startIndex - index);
segments.Add(new FormattedTextSegment(
substringText,
segment.Italic,
segment.Bolded,
segment.Underlined,
segment.IsSubscript,
segment.IsSuperscript));
}
else if (index >= startIndex)
{
segments.Add(segment);
}
index = nextIndex;
}
return new FormattedText(segments);
}
public override string ToString()
{
return string.Join("; ", this.Segments.Select(t => t.ToString()));
}
}
}