-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathColumn.cs
More file actions
234 lines (183 loc) · 6.46 KB
/
Column.cs
File metadata and controls
234 lines (183 loc) · 6.46 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
namespace ColumnizerLib;
public class Column : IColumnMemory
{
//TODO Memory Functions need implementation
#region Fields
private const string REPLACEMENT = "...";
// Display-level maximum line length (separate from reader-level limit)
// Can be configured via SetMaxDisplayLength()
private static int _maxDisplayLength = 20_000;
private static readonly List<Func<ReadOnlyMemory<char>, ReadOnlyMemory<char>>> _replacementsMemory = [
//replace tab with 3 spaces, from old coding. Needed???
ReplaceTab,
//shorten string if it exceeds maxLength
input => input.Length > _maxDisplayLength
? ShortenMemory(input, _maxDisplayLength)
: input
];
#endregion
#region cTor
static Column ()
{
//.net 10 only supports Windows10+ which has full UTF8-font support
// Replace null char with UTF-8 Symbol U+2400 (␀)
//https://github.com/dotnet/core/blob/main/release-notes/10.0/supported-os.md
_replacementsMemory.Add(input => ReplaceNullChar(input, ' '));
EmptyColumn = new Column { FullValue = ReadOnlyMemory<char>.Empty };
}
#endregion
#region Properties
public static IColumnMemory EmptyColumn { get; }
[Obsolete]
IColumnizedLogLine IColumn.Parent { get; }
[Obsolete]
string IColumn.FullValue
{
get;
//set
//{
// field = value;
// var temp = FullValue.ToString();
// foreach (var replacement in _replacements)
// {
// temp = replacement(temp);
// }
// DisplayValue = temp.AsMemory();
//}
}
[Obsolete("Use the DisplayValue property of IColumnMemory")]
string IColumn.DisplayValue { get; }
[Obsolete("Use Text property of ITextValueMemory")]
string ITextValue.Text => DisplayValue.ToString();
public IColumnizedLogLineMemory Parent
{
get; set => field = value;
}
public ReadOnlyMemory<char> FullValue
{
get;
set
{
field = value;
var temp = value;
foreach (var replacement in _replacementsMemory)
{
temp = replacement(temp);
}
DisplayValue = temp;
}
}
public ReadOnlyMemory<char> DisplayValue { get; private set; }
public ReadOnlyMemory<char> Text => DisplayValue;
#endregion
#region Public methods
/// <summary>
/// Configures the maximum display length for all Column instances.
/// This is separate from the reader-level MaxLineLength.
/// </summary>
/// <param name="maxLength">Maximum length for displayed content. Must be at least 1000.</param>
public static void SetMaxDisplayLength (int maxLength)
{
if (maxLength < 1000)
{
throw new ArgumentOutOfRangeException(nameof(maxLength), Resources.Column_Error_Messages_MaximumDisplayLengthMustBeAtLeast1000Characters);
}
_maxDisplayLength = maxLength;
}
/// <summary>
/// Gets the current maximum display length setting.
/// </summary>
public static int GetMaxDisplayLength () => _maxDisplayLength;
public static Column[] CreateColumns (int count, IColumnizedLogLineMemory parent)
{
return CreateColumns(count, parent, ReadOnlyMemory<char>.Empty);
}
public static Column[] CreateColumns (int count, IColumnizedLogLineMemory parent, ReadOnlyMemory<char> defaultValue)
{
var output = new Column[count];
for (var i = 0; i < count; i++)
{
output[i] = new Column { FullValue = defaultValue, Parent = parent };
}
return output;
}
public override string ToString ()
{
return DisplayValue.ToString() ?? ReadOnlyMemory<char>.Empty.ToString();
}
#endregion
#region Private Methods
/// <summary>
/// Replaces tab characters with two spaces in the memory buffer.
/// </summary>
private static ReadOnlyMemory<char> ReplaceTab (ReadOnlyMemory<char> input)
{
var span = input.Span;
var tabIndex = span.IndexOf('\t');
if (tabIndex == -1)
{
return input;
}
// Count total tabs to calculate new length
var tabCount = 0;
foreach (var c in span)
{
if (c == '\t')
{
tabCount++;
}
}
// Allocate new buffer: original length + (tabCount * 1) since we replace 1 char with 2
var newLength = input.Length + tabCount;
var buffer = new char[newLength];
var bufferPos = 0;
for (var i = 0; i < span.Length; i++)
{
if (span[i] == '\t')
{
buffer[bufferPos++] = ' ';
buffer[bufferPos++] = ' ';
}
else
{
buffer[bufferPos++] = span[i];
}
}
return buffer;
}
/// <summary>
/// Shortens the memory buffer to the specified maximum length and appends "...".
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "Non Localiced Parameter")]
private static ReadOnlyMemory<char> ShortenMemory (ReadOnlyMemory<char> input, int maxLength)
{
var buffer = new char[maxLength + REPLACEMENT.Length];
input.Span[..maxLength].CopyTo(buffer);
REPLACEMENT.AsSpan().CopyTo(buffer.AsSpan(maxLength));
return buffer;
}
/// <summary>
/// Replaces null characters with the specified replacement character.
/// </summary>
private static ReadOnlyMemory<char> ReplaceNullChar (ReadOnlyMemory<char> input, char replacement)
{
var span = input.Span;
var nullIndex = span.IndexOf('\0');
if (nullIndex == -1)
{
return input;
}
// Need to create a new buffer since we're modifying content
var buffer = new char[input.Length];
span.CopyTo(buffer);
for (var i = 0; i < buffer.Length; i++)
{
if (buffer[i] == '\0')
{
buffer[i] = replacement;
}
}
return buffer;
}
#endregion
}