Skip to content

Commit

Permalink
ref - Refactored block construction code
Browse files Browse the repository at this point in the history
---

Type: ref
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Oct 3, 2024
1 parent 7cf7b12 commit 94312cb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 44 deletions.
22 changes: 1 addition & 21 deletions VisualCard.Calendar/Parsers/VCalendarParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,12 @@ public Parts.Calendar Parse()
var calendar = new Parts.Calendar(CalendarVersion);

// Iterate through all the lines
bool constructing = false;
StringBuilder valueBuilder = new();
List<(string, Parts.Calendar)> begins = [];
for (int i = 0; i < CalendarContent.Length; i++)
{
// Get line
var content = CalendarContent[i];
string _value = content.Item2;
string _value = VcardCommonTools.ConstructBlocks(CalendarContent, ref i);
int lineNumber = content.Item1;
if (string.IsNullOrEmpty(_value))
continue;
Expand All @@ -86,24 +84,6 @@ public Parts.Calendar Parse()
if (begins.Count > 0)
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].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) : "";
if (constructing)
{
if (firstConstructedLine)
valueBuilder.Append(_value);
valueBuilder.Append(secondLine);
continue;
}
else if (!firstConstructedLine)
{
_value = valueBuilder.ToString();
valueBuilder.Clear();
}

try
{
// Now, parse a property
Expand Down
6 changes: 3 additions & 3 deletions VisualCard.Tests/ContactData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ public static class ContactData
VERSION:3.0
TEL:495-522-3560
EMAIL:john.s@acme.co
FN:John Sanders
NOTE:Note test for VisualCard
FN:John Sanders
N:Sanders;John;;;
ADR:;;Los Angeles;;;;USA
END:VCARD
Expand All @@ -159,11 +159,11 @@ public static class ContactData
TEL:495-522-3560
TEL;TYPE=VIDEO:495-522-3550
EMAIL:john.s@acme.co
FN:John Sanders
NOTE:Note test for VisualCard
FN:John Sanders
N:Sanders;John;;;
ADR:;;Los Angeles;;;;USA
X-VISUALCARD-KANA:Saunders;John
ADR:;;Los Angeles;;;;USA
END:VCARD
"""
Expand Down
34 changes: 34 additions & 0 deletions VisualCard/Parsers/VcardCommonTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Textify.General;
using VisualCard.Parsers.Arguments;
using VisualCard.Parsers.Recurrence;
using VisualCard.Parts.Enums;
Expand Down Expand Up @@ -696,5 +697,38 @@ internal static CardKind GetKindEnum(string kind) =>
"location" => CardKind.Location,
_ => CardKind.Others,
};

internal static string ConstructBlocks((int, string)[] cardContent, ref int i)
{
StringBuilder valueBuilder = new();
bool constructing = false;
int idx;
for (idx = i; idx < cardContent.Length; idx++)
{
// Get line
var content = cardContent[idx];
string _value = content.Item2;
if (string.IsNullOrEmpty(_value))
continue;

// First, check to see if we need to construct blocks
string secondLine = idx + 1 < cardContent.Length ? cardContent[idx + 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) : "";
if (constructing)
{
if (firstConstructedLine)
valueBuilder.Append(_value);
valueBuilder.Append(secondLine);
continue;
}
else if (firstConstructedLine && !constructing)
valueBuilder.Append(_value);
break;
}
i = idx;
return valueBuilder.ToString();
}
}
}
21 changes: 1 addition & 20 deletions VisualCard/Parsers/VcardParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,37 +75,18 @@ public Card Parse()
}

// Iterate through all the lines
bool constructing = false;
StringBuilder valueBuilder = new();
string[] allowedTypes = ["HOME", "WORK", "PREF"];
string kind = "individual";
for (int i = 0; i < CardContent.Length; i++)
{
// Get line
var content = CardContent[i];
string _value = content.Item2;
string _value = VcardCommonTools.ConstructBlocks(CardContent, ref i);
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].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) : "";
if (constructing)
{
if (firstConstructedLine)
valueBuilder.Append(_value);
valueBuilder.Append(secondLine);
continue;
}
else if (!firstConstructedLine)
{
_value = valueBuilder.ToString();
valueBuilder.Clear();
}

try
{
// Now, parse a property
Expand Down

0 comments on commit 94312cb

Please sign in to comment.