Skip to content

Commit

Permalink
imp - Handle missing argument delimiter cases
Browse files Browse the repository at this point in the history
---

Instead of a cryptic index out of bounds exception, we should print a helpful error message that says that the line must contain an argument delimiter when there is none.

---

Type: imp
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Sep 3, 2024
1 parent 9a0e2d7 commit 8dd463c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
23 changes: 12 additions & 11 deletions VisualCard.Calendar/Parsers/VCalendarParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,20 @@ public Parts.Calendar Parse()
valueBuilder.Clear();
}

// Variables
string value = _value.Substring(_value.IndexOf(VCalendarConstants._argumentDelimiter) + 1);
string prefixWithArgs = _value.Substring(0, _value.IndexOf(VCalendarConstants._argumentDelimiter));
string prefix = prefixWithArgs.Contains(';') ? prefixWithArgs.Substring(0, prefixWithArgs.IndexOf(';')) : prefixWithArgs;
string args = prefixWithArgs.Contains(';') ? prefixWithArgs.Substring(prefix.Length + 1) : "";
string[] splitArgs = args.Split([VCalendarConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
string[] splitValues = value.Split([VCalendarConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
bool isWithType = splitArgs.Length > 0;
List<string> finalArgs = [];

// Now, parse a line
try
{
// Now, parse a line
if (!_value.Contains(VCalendarConstants._argumentDelimiter))
throw new ArgumentException("The line must contain an argument delimiter.");
string value = _value.Substring(_value.IndexOf(VCalendarConstants._argumentDelimiter) + 1);
string prefixWithArgs = _value.Substring(0, _value.IndexOf(VCalendarConstants._argumentDelimiter));
string prefix = prefixWithArgs.Contains(';') ? prefixWithArgs.Substring(0, prefixWithArgs.IndexOf(';')) : prefixWithArgs;
string args = prefixWithArgs.Contains(';') ? prefixWithArgs.Substring(prefix.Length + 1) : "";
string[] splitArgs = args.Split([VCalendarConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
string[] splitValues = value.Split([VCalendarConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
bool isWithType = splitArgs.Length > 0;
List<string> finalArgs = [];

// Check to see if we have a BEGIN or an END prefix
if (prefix == VCalendarConstants._beginSpecifier)
{
Expand Down
25 changes: 13 additions & 12 deletions VisualCard/Parsers/VcardParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,21 @@ public Card Parse()
valueBuilder.Clear();
}

// Variables
string value = _value.Substring(_value.IndexOf(VcardConstants._argumentDelimiter) + 1);
string prefixWithArgs = _value.Substring(0, _value.IndexOf(VcardConstants._argumentDelimiter));
string prefix = prefixWithArgs.Contains(';') ? prefixWithArgs.Substring(0, prefixWithArgs.IndexOf(';')) : prefixWithArgs;
string args = prefixWithArgs.Contains(';') ? prefixWithArgs.Substring(prefix.Length + 1) : "";
string[] splitArgs = args.Split([VcardConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
string[] splitValues = value.Split([VcardConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
bool isWithType = splitArgs.Length > 0;
List<string> finalArgs = [];
int altId = -1;

// Now, parse a line
try
{
// Now, parse a line
if (!_value.Contains(VcardConstants._argumentDelimiter))
throw new ArgumentException("The line must contain an argument delimiter.");
string value = _value.Substring(_value.IndexOf(VcardConstants._argumentDelimiter) + 1);
string prefixWithArgs = _value.Substring(0, _value.IndexOf(VcardConstants._argumentDelimiter));
string prefix = prefixWithArgs.Contains(';') ? prefixWithArgs.Substring(0, prefixWithArgs.IndexOf(';')) : prefixWithArgs;
string args = prefixWithArgs.Contains(';') ? prefixWithArgs.Substring(prefix.Length + 1) : "";
string[] splitArgs = args.Split([VcardConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
string[] splitValues = value.Split([VcardConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
bool isWithType = splitArgs.Length > 0;
List<string> finalArgs = [];
int altId = -1;

// Get the part type
bool xNonstandard = prefix.StartsWith(VcardConstants._xSpecifier);
bool specifierRequired = CardVersion.Major >= 3;
Expand Down

0 comments on commit 8dd463c

Please sign in to comment.