Skip to content

Commit

Permalink
imp - Made line numbers more accurate
Browse files Browse the repository at this point in the history
---

We've made line numbers more accurate when debugging an exception related to parsing due to invalid syntax, value, or other issue.

---

Type: imp
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Oct 2, 2024
1 parent 3f9d252 commit 9142f54
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 31 deletions.
14 changes: 6 additions & 8 deletions VisualCard.Calendar/CalendarTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ public static Parts.Calendar[] GetCalendars(StreamReader stream)

// Parse the lines of the calendar file
string CalendarLine;
StringBuilder CalendarContent = new();
List<(int, string)> lines = [];
Version CalendarVersion = new();
int lineNumber = 0;
while (!stream.EndOfStream)
{
bool append = false;
lineNumber++;

// Skip empty lines
CalendarLine = stream.ReadLine();
Expand All @@ -97,7 +99,7 @@ public static Parts.Calendar[] GetCalendars(StreamReader stream)
!CalendarLine.EqualsNoCase(VCalendarConstants._endText))
append = true;
if (append)
CalendarContent.Append(CalendarLine);
lines.Add((lineNumber, CalendarLine));

// All vCalendars must begin with BEGIN:VCALENDAR
if (!CalendarLine.EqualsNoCase(VCalendarConstants._beginText) && !BeginSpotted)
Expand Down Expand Up @@ -130,17 +132,13 @@ public static Parts.Calendar[] GetCalendars(StreamReader stream)
EndSpotted = true;

// Make a new parser instance
string content = CalendarContent.ToString();
string[] contentLines = content.SplitNewLines();
VCalendarParser CalendarParser = new(contentLines, CalendarVersion);
VCalendarParser CalendarParser = new([.. lines], CalendarVersion);
FinalParsers.Add(CalendarParser);

// Clear the content in case we want to make a second contact
CalendarContent.Clear();
lines.Clear();
BeginSpotted = false;
}
else if (append)
CalendarContent.AppendLine();
}

// Close the stream to avoid stuck file handle
Expand Down
13 changes: 7 additions & 6 deletions VisualCard.Calendar/Parsers/VCalendarParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ namespace VisualCard.Calendar.Parsers
internal class VCalendarParser
{
private readonly Version calendarVersion = new();
private readonly string[] calendarContent = [];
private readonly (int, string)[] calendarContent = [];

/// <summary>
/// vCalendar calendar content
/// </summary>
public string[] CalendarContent =>
public (int, string)[] CalendarContent =>
calendarContent;
/// <summary>
/// vCalendar calendar version
Expand Down Expand Up @@ -73,8 +73,9 @@ public Parts.Calendar Parse()
for (int i = 0; i < CalendarContent.Length; i++)
{
// Get line
string _value = CalendarContent[i];
int lineNumber = i + 1;
var content = CalendarContent[i];
string _value = content.Item2;
int lineNumber = content.Item1;
if (string.IsNullOrEmpty(_value))
continue;

Expand All @@ -84,7 +85,7 @@ public Parts.Calendar Parse()
subPart = begins[begins.Count - 1].Item2;

// First, check to see if we need to construct blocks
string secondLine = i + 1 < CalendarContent.Length ? CalendarContent[i + 1] : "";
string secondLine = i + 1 < CalendarContent.Length ? CalendarContent[i + 1].Item2 : "";
bool firstConstructedLine = !_value.StartsWith(VcardConstants._spaceBreak) && !_value.StartsWith(VcardConstants._tabBreak);
constructing = secondLine.StartsWithAnyOf([VcardConstants._spaceBreak, VcardConstants._tabBreak]);
secondLine = secondLine.Length > 1 ? secondLine.Substring(1) : "";
Expand Down Expand Up @@ -540,7 +541,7 @@ private void SaveLastSubPart(Parts.Calendar? subpart, ref Parts.Calendar part)
throw new ArgumentException($"Can't place {subpart.GetType().Name} inside {part.GetType().Name}");
}

internal VCalendarParser(string[] calendarContent, Version calendarVersion)
internal VCalendarParser((int, string)[] calendarContent, Version calendarVersion)
{
this.calendarContent = calendarContent;
this.calendarVersion = calendarVersion;
Expand Down
14 changes: 6 additions & 8 deletions VisualCard/CardTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ public static Card[] GetCards(StreamReader stream)

// Parse the lines of the card file
string CardLine;
StringBuilder CardContent = new();
Version CardVersion = new();
List<Card> nestedCards = [];
List<(int, string)> lines = [];
bool nested = false;
bool versionDirect = false;
int lineNumber = 0;
while (!stream.EndOfStream)
{
bool append = false;
lineNumber++;

// Skip empty lines
CardLine = stream.ReadLine();
Expand Down Expand Up @@ -125,7 +127,7 @@ public static Card[] GetCards(StreamReader stream)
continue;
}
if (append)
CardContent.Append(CardLine);
lines.Add((lineNumber, CardLine));

// All VCards must begin with BEGIN:VCARD
if (!CardLine.EqualsNoCase(VcardConstants._beginText) && !BeginSpotted)
Expand Down Expand Up @@ -170,22 +172,18 @@ public static Card[] GetCards(StreamReader stream)
nested = false;

// Make a new parser instance
string content = CardContent.ToString();
string[] contentLines = content.SplitNewLines();
VcardParser CardParser = new(contentLines, CardVersion)
VcardParser CardParser = new([.. lines], CardVersion)
{
nestedCards = [.. nestedCards]
};
FinalParsers.Add(CardParser);

// Clear the content in case we want to make a second contact
CardContent.Clear();
lines.Clear();
nestedCards.Clear();
BeginSpotted = false;
versionDirect = false;
}
else if (append)
CardContent.AppendLine();
}

// Close the stream to avoid stuck file handle
Expand Down
19 changes: 10 additions & 9 deletions VisualCard/Parsers/VcardParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ internal class VcardParser
{
internal Card[] nestedCards = [];
private readonly Version cardVersion = new();
private string[] cardContent = [];
private (int, string)[] cardContent = [];

/// <summary>
/// VCard card content
/// </summary>
public string[] CardContent =>
public (int, string)[] CardContent =>
cardContent;
/// <summary>
/// VCard card version
Expand All @@ -69,9 +69,9 @@ public Card Parse()
// Move kind to the top
if (CardVersion.Major >= 4)
{
string kindLine = CardContent.SingleOrDefault((line) => line.ToUpper().StartsWith(VcardConstants._kindSpecifier));
if (!string.IsNullOrEmpty(kindLine))
cardContent = [kindLine, .. cardContent.Where((line) => line != kindLine).ToArray()];
var kindLine = CardContent.SingleOrDefault((line) => line.Item2.ToUpper().StartsWith(VcardConstants._kindSpecifier));
if (!string.IsNullOrEmpty(kindLine.Item2))
cardContent = [kindLine, .. cardContent.Where((line) => line.Item2 != kindLine.Item2).ToArray()];
}

// Iterate through all the lines
Expand All @@ -82,13 +82,14 @@ public Card Parse()
for (int i = 0; i < CardContent.Length; i++)
{
// Get line
string _value = CardContent[i];
int lineNumber = i + 1;
var content = CardContent[i];
string _value = content.Item2;
int lineNumber = content.Item1;
if (string.IsNullOrEmpty(_value))
continue;

// First, check to see if we need to construct blocks
string secondLine = i + 1 < CardContent.Length ? CardContent[i + 1] : "";
string secondLine = i + 1 < CardContent.Length ? CardContent[i + 1].Item2 : "";
bool firstConstructedLine = !_value.StartsWith(VcardConstants._spaceBreak) && !_value.StartsWith(VcardConstants._tabBreak);
constructing = secondLine.StartsWithAnyOf([VcardConstants._spaceBreak, VcardConstants._tabBreak]);
secondLine = secondLine.Length > 1 ? secondLine.Substring(1) : "";
Expand Down Expand Up @@ -321,7 +322,7 @@ private bool ValidateComponent<TComponent>(ref string[] expectedFields, out stri
return actualFields.SequenceEqual(expectedFields);
}

internal VcardParser(string[] cardContent, Version cardVersion)
internal VcardParser((int, string)[] cardContent, Version cardVersion)
{
this.cardContent = cardContent;
this.cardVersion = cardVersion;
Expand Down

0 comments on commit 9142f54

Please sign in to comment.