-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringView.cs
81 lines (72 loc) · 1.67 KB
/
StringView.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Text;
namespace LCS
{
readonly struct StringView
{
readonly public int Index;
readonly public int Length;
readonly DataSource DS;
readonly String String => DS.Strings[Index];
readonly public int Start => DS.Strings[Index].Start;
readonly public int End => Index + Length;
public StringView(int i, DataSource ds)
{
Index = i;
Length = ds.Strings[Index].Length;
DS = ds ?? throw new ArgumentNullException(nameof(ds));
}
public StringView(int i, int length, DataSource ds)
{
Index = i;
Length = length;
DS = ds ?? throw new ArgumentNullException(nameof(ds));
}
public readonly StringView Next => new StringView(Start, DS);
public readonly IEnumerable<StringView> Segments
{
get
{
if (Index == Start) yield break;
var p = Next;
do {
yield return p;
p = p.Next;
} while (p.Index != p.Start);
}
}
public readonly IEnumerable<StringView> Others
{
get
{
for (var i = 0; i < DS.Strings.Length; i++)
if (DS.Strings[i] == String)
yield return new StringView(i, DS);
}
}
public readonly IEnumerable<StringView> Prefixes
{
get
{
for (var i = 0; i < DS.Strings.Length; i++)
if (DS.Strings[i].Start == Start)
yield return new StringView(i, DS);
}
}
public override readonly string ToString()
{
var s = new StringBuilder();
s.Append(Utility.FormatRange(Index, Length));
s.Append($" > {Start:x}");
if (DS.Data.Length != 0)
{
s.Append(' ');
s.Append('"');
s.Append(Utility.FormatData(DS.Data, Start, Length));
s.Append('"');
}
return s.ToString();
}
}
}