Skip to content

Commit

Permalink
imp - Refactored the QUOTED-PRINTABLE support
Browse files Browse the repository at this point in the history
---

We've refactored the QUOTED-PRINTABLE support.

---

Type: imp
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Jan 16, 2025
1 parent f10b195 commit 79d44ff
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 32 deletions.
6 changes: 3 additions & 3 deletions public/VisualCard/CardTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ public static Card[] GetCards(StreamReader stream)
var prop = new PropertyInfo(CardLine);
prefix = prop.Prefix;
value = prop.Value;
while (prop.Continue)
while (prop.CanContinueMultiline())
{
string nextLine = stream.ReadLine();
prop.rawValue.Append(nextLine);

prop.AddLine(nextLine);
//Add it to the current line for later processing
// Add it to the current line for later processing
CardLine += nextLine;
}
}
Expand Down
34 changes: 10 additions & 24 deletions public/VisualCard/Parsers/Arguments/PropertyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Textify.General;
using VisualCard.Parts.Comparers;

Expand All @@ -32,17 +33,17 @@ namespace VisualCard.Parsers.Arguments
[DebuggerDisplay("Property: {Arguments.Length} args, {Prefix} [G: {Group}] = {Value}")]
public class PropertyInfo : IEquatable<PropertyInfo?>
{
private readonly string rawValue = "";
private string tailValue = "";
internal readonly StringBuilder rawValue = new();
private readonly string prefix = "";
private readonly string group = "";
private readonly ArgumentInfo[] arguments = [];
private readonly bool printableMultiline;

/// <summary>
/// Raw value
/// </summary>
public string Value =>
rawValue + tailValue;
rawValue.ToString();

/// <summary>
/// Property prefix
Expand All @@ -68,11 +69,6 @@ public class PropertyInfo : IEquatable<PropertyInfo?>
public ArgumentInfo[] Arguments =>
arguments;

/// <summary>
/// Specifies if this property spans multiple lines
/// </summary>
public bool Multiline { get; set; }

/// <summary>
/// Checks to see if both the property info instances are equal
/// </summary>
Expand Down Expand Up @@ -123,18 +119,11 @@ public override int GetHashCode()
public static bool operator !=(PropertyInfo? left, PropertyInfo? right) =>
!(left == right);

internal static string Encoding(ArgumentInfo[] args)
=> VcardCommonTools.GetValuesString(args, "", VcardConstants._encodingArgumentSpecifier);

/// <inheritdoc/>
public bool Continue
=> Multiline ? (0 < Value?.Length ? '=' == Value[Value.Length - 1] : false) : false;
internal string Encoding()
=> VcardCommonTools.GetValuesString(arguments, "", VcardConstants._encodingArgumentSpecifier);

/// <inheritdoc/>
public void AddLine(string line)
{
tailValue += line;
}
internal bool CanContinueMultiline()
=> printableMultiline && 0 < Value?.Length && '=' == Value[Value.Length - 1];

internal PropertyInfo(string line)
{
Expand All @@ -158,13 +147,10 @@ internal PropertyInfo(string line)
prefix = xNonstandard ? VcardConstants._xSpecifier : prefix;

// Install values
this.rawValue = value;
this.rawValue.Append(value);
this.prefix = prefix;
this.arguments = finalArgs;
if (VcardConstants._quotedPrintable == Encoding(this.arguments))
Multiline = true;
else
Multiline = false;
printableMultiline = Encoding() == VcardConstants._quotedPrintable;
this.group = group.Trim();
}
}
Expand Down
4 changes: 2 additions & 2 deletions public/VisualCard/Parsers/VcardCommonTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ internal static string GetFirstValue(ArgumentInfo[] args, string @default, strin
return argString;
}

internal static string MakeStringBlock(string target, int firstLength = 0, bool writeSpace = true)
internal static string MakeStringBlock(string target, int firstLength = 0, bool writeSpace = true, string encoding = "")
{
const int maxChars = 74;
int maxCharsFirst = maxChars - firstLength;
Expand All @@ -522,7 +522,7 @@ internal static string MakeStringBlock(string target, int firstLength = 0, bool
selectedMax = writeSpace ? maxChars - 1 : maxChars;
processed = 0;
block.Append("\n");
if (writeSpace)
if (writeSpace && encoding != VcardConstants._quotedPrintable)
block.Append(" ");
}
if (target[currCharNum] != '\n' && target[currCharNum] != '\r')
Expand Down
2 changes: 1 addition & 1 deletion public/VisualCard/Parsers/VcardConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal static class VcardConstants
internal const string _spaceBreak = " ";
internal const string _tabBreak = "\x0009";

//Encodings
// Encodings
internal const string _quotedPrintable = "QUOTED-PRINTABLE";

// Available in vCard 2.1, 3.0, 4.0, and 5.0
Expand Down
4 changes: 2 additions & 2 deletions public/VisualCard/Parts/Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public string SaveToString()
cardBuilder.Append($"{group}.");
partBuilder.Append($"{prefix}");
partBuilder.Append($"{partArguments}");
partBuilder.Append($"{VcardCommonTools.MakeStringBlock(part.Value, partArgumentsLines[partArgumentsLines.Length - 1].Length + prefix.Length)}");
partBuilder.Append($"{VcardCommonTools.MakeStringBlock(part.Value, partArgumentsLines[partArgumentsLines.Length - 1].Length + prefix.Length, encoding: part.Property?.Encoding() ?? "")}");
cardBuilder.AppendLine($"{partBuilder}");
}
}
Expand Down Expand Up @@ -276,7 +276,7 @@ public string SaveToString()
cardBuilder.Append($"{group}.");
partBuilder.Append($"{prefix}");
partBuilder.Append($"{partArguments}");
partBuilder.Append($"{VcardCommonTools.MakeStringBlock(partRepresentation, partArgumentsLines[partArgumentsLines.Length - 1].Length + prefix.Length, !(partsArrayEnum == PartsArrayEnum.Agents && version.Major == 2))}");
partBuilder.Append($"{VcardCommonTools.MakeStringBlock(partRepresentation, partArgumentsLines[partArgumentsLines.Length - 1].Length + prefix.Length, !(partsArrayEnum == PartsArrayEnum.Agents && version.Major == 2), part.Property?.Encoding() ?? "")}");
cardBuilder.AppendLine($"{partBuilder}");
}
}
Expand Down

0 comments on commit 79d44ff

Please sign in to comment.