Skip to content

Commit e749651

Browse files
jschick04NikTilton
authored andcommitted
Updated template parsing logic into it's own method
1 parent f570c63 commit e749651

File tree

3 files changed

+72
-55
lines changed

3 files changed

+72
-55
lines changed

src/EventLogExpert.Eventing.Tests/Helpers/EventMethodsTests.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,18 @@ public void ConvertVariant_WhenHexInt64_ShouldReturnUInt64()
202202
}
203203

204204
[Fact]
205-
public void ConvertVariant_WhenInt16_ShouldReturnByte()
205+
public void ConvertVariant_WhenInt16_ShouldReturnShort()
206206
{
207207
// Arrange
208-
byte expectedValue = 100;
208+
short expectedValue = -1234;
209209
var variant = CreateVariant(EvtVariantType.Int16, expectedValue);
210210

211211
// Act
212212
var result = EventMethods.ConvertVariant(variant);
213213

214214
// Assert
215215
Assert.NotNull(result);
216+
Assert.IsType<short>(result);
216217
Assert.Equal(expectedValue, result);
217218
}
218219

@@ -258,8 +259,8 @@ public void ConvertVariant_WhenInvalidType_ShouldThrowInvalidDataException()
258259
var exception = Assert.Throws<InvalidDataException>(
259260
() => EventMethods.ConvertVariant(variant));
260261

261-
Assert.Contains("Invalid", exception.Message);
262262
Assert.Contains(nameof(EvtVariantType), exception.Message);
263+
Assert.Contains("9999", exception.Message);
263264
}
264265

265266
[Fact]
@@ -674,9 +675,11 @@ private static EvtVariant CreateVariantWithCount(EvtVariantType type, object? va
674675
break;
675676
case EvtVariantType.SByte:
676677
case EvtVariantType.Byte:
677-
case EvtVariantType.Int16:
678678
*(byte*)buffer = (byte)value;
679679
break;
680+
case EvtVariantType.Int16:
681+
*(short*)buffer = (short)value;
682+
break;
680683
case EvtVariantType.UInt16:
681684
*(ushort*)buffer = (ushort)value;
682685
break;

src/EventLogExpert.Eventing/EventResolvers/EventResolverBase.cs

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -199,33 +199,7 @@ private static bool DoesTemplateMatchPropertyCount(ReadOnlySpan<char> template,
199199
{
200200
if (template.IsEmpty) { return false; }
201201

202-
var cache = s_formattedPropertiesCache.GetAlternateLookup<ReadOnlySpan<char>>();
203-
204-
if (cache.TryGetValue(template, out string[]? dataNodes))
205-
{
206-
return dataNodes.Length == eventPropertyCount;
207-
}
208-
209-
List<string> temp = [];
210-
ReadOnlySpan<char> outTypeAttribute = "outType=\"";
211-
212-
foreach (var line in template.EnumerateLines())
213-
{
214-
int templateIndex = line.IndexOf(outTypeAttribute, StringComparison.Ordinal);
215-
216-
if (templateIndex == -1) { continue; }
217-
218-
templateIndex += outTypeAttribute.Length;
219-
int endIndex = line[templateIndex..].IndexOf('"');
220-
221-
if (endIndex != -1)
222-
{
223-
temp.Add(new string(line.Slice(templateIndex, endIndex)));
224-
}
225-
}
226-
227-
dataNodes = [.. temp];
228-
cache.TryAdd(template, dataNodes);
202+
string[] dataNodes = GetOrParseTemplateDataNodes(template);
229203

230204
return dataNodes.Length == 0 || dataNodes.Length == eventPropertyCount;
231205
}
@@ -235,30 +209,9 @@ private static List<string> GetFormattedProperties(ReadOnlySpan<char> template,
235209
string[]? dataNodes = null;
236210
List<string> providers = [];
237211

238-
var cache = s_formattedPropertiesCache.GetAlternateLookup<ReadOnlySpan<char>>();
239-
240-
if (!template.IsEmpty && !cache.TryGetValue(template, out dataNodes))
212+
if (!template.IsEmpty)
241213
{
242-
List<string> temp = [];
243-
ReadOnlySpan<char> outTypeAttribute = "outType=\"";
244-
245-
foreach (var line in template.EnumerateLines())
246-
{
247-
int templateIndex = line.IndexOf(outTypeAttribute, StringComparison.Ordinal);
248-
249-
if (templateIndex == -1) { continue; }
250-
251-
templateIndex += outTypeAttribute.Length;
252-
int endIndex = line[templateIndex..].IndexOf('"');
253-
254-
if (endIndex != -1)
255-
{
256-
temp.Add(new string(line.Slice(templateIndex, endIndex)));
257-
}
258-
}
259-
260-
dataNodes = [.. temp];
261-
cache.TryAdd(template, dataNodes);
214+
dataNodes = GetOrParseTemplateDataNodes(template);
262215
}
263216

264217
int index = 0;
@@ -308,6 +261,67 @@ private static List<string> GetFormattedProperties(ReadOnlySpan<char> template,
308261
return providers;
309262
}
310263

264+
private static string[] GetOrParseTemplateDataNodes(ReadOnlySpan<char> template)
265+
{
266+
var cache = s_formattedPropertiesCache.GetAlternateLookup<ReadOnlySpan<char>>();
267+
268+
if (cache.TryGetValue(template, out string[]? dataNodes))
269+
{
270+
return dataNodes;
271+
}
272+
273+
List<string> temp = [];
274+
ReadOnlySpan<char> dataElement = "<data ";
275+
ReadOnlySpan<char> outTypeAttribute = "outType=\"";
276+
277+
int searchStart = 0;
278+
279+
while (searchStart < template.Length)
280+
{
281+
int dataIndex = template[searchStart..].IndexOf(dataElement, StringComparison.OrdinalIgnoreCase);
282+
283+
if (dataIndex == -1) { break; }
284+
285+
dataIndex += searchStart;
286+
287+
// Find the end of this element
288+
int elementEnd = template[dataIndex..].IndexOf("/>");
289+
290+
if (elementEnd == -1)
291+
{
292+
elementEnd = template[dataIndex..].IndexOf('>');
293+
}
294+
295+
if (elementEnd == -1) { break; }
296+
297+
elementEnd += dataIndex;
298+
299+
ReadOnlySpan<char> element = template[dataIndex..elementEnd];
300+
int outTypeIndex = element.IndexOf(outTypeAttribute, StringComparison.Ordinal);
301+
302+
if (outTypeIndex != -1)
303+
{
304+
outTypeIndex += outTypeAttribute.Length;
305+
int endIndex = element[outTypeIndex..].IndexOf('"');
306+
307+
temp.Add(endIndex != -1 ?
308+
new string(element.Slice(outTypeIndex, endIndex)) :
309+
string.Empty);
310+
}
311+
else
312+
{
313+
temp.Add(string.Empty);
314+
}
315+
316+
searchStart = elementEnd + 1;
317+
}
318+
319+
dataNodes = [.. temp];
320+
cache.TryAdd(template, dataNodes);
321+
322+
return dataNodes;
323+
}
324+
311325
private static void ResizeBuffer(ref char[] buffer, ref Span<char> source, int sizeToAdd)
312326
{
313327
char[] newBuffer = ArrayPool<char>.Shared.Rent(source.Length + sizeToAdd);

src/EventLogExpert.Eventing/Helpers/EventMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ internal static partial class EventMethods
266266

267267
return stringArray;
268268
default:
269-
return $"[Unknown EvtVariantType: {variant.Type}]";
269+
throw new InvalidDataException($"Invalid {nameof(EvtVariantType)}: {variant.Type}");
270270
}
271271
}
272272

0 commit comments

Comments
 (0)