-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBrainf_ckInlineFormatterHelper.StackTrace.cs
More file actions
193 lines (161 loc) · 6.35 KB
/
Copy pathBrainf_ckInlineFormatterHelper.StackTrace.cs
File metadata and controls
193 lines (161 loc) · 6.35 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Media;
using Brainf_ckSharp.Models;
using CommunityToolkit.WinUI;
#nullable enable
namespace Brainf_ckSharp.Uwp.AttachedProperties;
/// <summary>
/// A <see langword="class"/> with an attached XAML property to formatted Brainf*ck/PBrain code
/// </summary>
public static partial class Brainf_ckInlineFormatterHelper
{
/// <summary>
/// Gets the value of <see cref="StackTraceProperty"/> for a given <see cref="Paragraph"/>
/// </summary>
/// <param name="element">The input <see cref="Paragraph"/> for which to get the property value</param>
/// <returns>The value of the <see cref="StackTraceProperty"/> property for the input <see cref="Paragraph"/> instance</returns>
public static IReadOnlyList<string>? GetStackTrace(Paragraph element)
{
return element.GetValue(StackTraceProperty) as IReadOnlyList<string>;
}
/// <summary>
/// Sets the value of <see cref="StackTraceProperty"/> for a given <see cref="Paragraph"/>
/// </summary>
/// <param name="element">The input <see cref="Paragraph"/> for which to set the property value</param>
/// <param name="value">The value to set for the <see cref="StackTraceProperty"/> property</param>
public static void SetStackTrace(Paragraph element, IReadOnlyList<string>? value)
{
element.SetValue(StackTraceProperty, value);
}
/// <summary>
/// A property that shows a formatted Brainf_ck code to a <see cref="Paragraph"/> object
/// </summary>
public static readonly DependencyProperty StackTraceProperty = DependencyProperty.RegisterAttached(
"StackTrace",
typeof(IReadOnlyList<string>),
typeof(Brainf_ckInlineFormatterHelper),
new(Array.Empty<FunctionDefinition>(), OnStackTracePropertyChanged));
// Localized resources
private static readonly string At = "StackTrace/At".GetLocalized()!;
private static readonly string Frames = "StackTrace/Frames".GetLocalized()!;
/// <summary>
/// Updates the UI when <see cref="StackTraceProperty"/> changes
/// </summary>
/// <param name="d">The source <see cref="DependencyObject"/> instance</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> info for the current update</param>
private static void OnStackTracePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// Unpack the arguments
Paragraph @this = (Paragraph)d;
IReadOnlyList<string> value = (IReadOnlyList<string>)e.NewValue;
@this.Inlines.Clear();
if (value.Count == 0)
{
return;
}
int i = 0;
foreach ((string Item, int Occurrences, int Length) entry in CompressStackTrace(value))
{
if (i++ > 0)
{
@this.Inlines.Add(new LineBreak());
}
// Insert the "at" separator if needed
@this.Inlines.Add(new Run
{
Text = $"{At}{(entry.Occurrences > 1 ? $" [{entry.Occurrences * entry.Length} {Frames}]" : string.Empty)}",
Foreground = new SolidColorBrush(Colors.DimGray),
FontSize = @this.FontSize - 1
});
@this.Inlines.Add(new LineBreak());
// Add the formatted call line
Span line = new();
SetSource(line, entry.Item);
@this.Inlines.Add(line);
}
}
/// <summary>
/// Compresses a stack trace by aggregating recursive calls
/// </summary>
/// <param name="frames">The input list of stack frames</param>
public static IEnumerable<(string Item, int Occurrences, int Length)> CompressStackTrace(IReadOnlyList<string> frames)
{
int i = 0;
List<(int Length, int Occurrences)> info = [];
while (i < frames.Count)
{
for (int step = 1; step < 5 && i + (step * 2) - 1 < frames.Count; step++)
{
// Find a valid sub-pattern of a given length
bool valid = true;
for (int j = 0; j < step; j++)
{
if (!frames[i + j].Equals(frames[i + step + j]))
{
valid = false;
break;
}
}
if (!valid)
{
continue;
}
// Check of many times the pattern repeats
int occurrences = 2;
for (int j = i + (step * 2); j + step - 1 < frames.Count; j += step)
{
valid = true;
for (int k = 0; k < step; k++)
{
if (!frames[i + k].Equals(frames[j + k]))
{
valid = false;
break;
}
}
if (valid)
{
occurrences++;
}
}
// Store the current sub-sequence info
info.Add((step, occurrences));
}
// Return the current compressed chunk
if (info.Count == 0)
{
yield return (frames[i], 1, 1);
i += 1;
}
else
{
(int Length, int Occurrences) best = info.OrderByDescending(item => item.Length * item.Occurrences).ThenBy(item => item.Length).First();
StringBuilder builder = new();
for (int j = 0; j < best.Length; j++)
{
_ = builder.Append(frames[i + j]);
}
string call = builder.ToString();
if (call.Contains(':'))
{
// Only aggregate recursive calls
yield return (call, best.Occurrences, best.Length);
i += best.Length * best.Occurrences;
}
else
{
// The repeated loops are just user code
yield return (frames[i], 1, 1);
i += 1;
}
}
info.Clear();
}
}
}