From 4ac004ff5d4a4114ce0bfcf21069d2c98849a3b0 Mon Sep 17 00:00:00 2001 From: Aptivi CEO Date: Mon, 2 Sep 2024 13:25:54 +0300 Subject: [PATCH] ref - Consolidated type-agnostic tools --- Type: ref Breaking: False Doc Required: False Backport Required: False Part: 1/1 --- .../Parsers/VCalendarParser.cs | 5 +- .../Parsers/VCalendarParserTools.cs | 129 ------------------ VisualCard.Calendar/Parts/Calendar.cs | 5 +- 3 files changed, 6 insertions(+), 133 deletions(-) diff --git a/VisualCard.Calendar/Parsers/VCalendarParser.cs b/VisualCard.Calendar/Parsers/VCalendarParser.cs index e8adfae..cf94885 100644 --- a/VisualCard.Calendar/Parsers/VCalendarParser.cs +++ b/VisualCard.Calendar/Parsers/VCalendarParser.cs @@ -30,6 +30,7 @@ using VisualCard.Calendar.Parts.Implementations; using VisualCard.Calendar.Exceptions; using VisualCard.Parts.Enums; +using VisualCard.Parsers; namespace VisualCard.Calendar.Parsers { @@ -159,7 +160,7 @@ public Parts.Calendar Parse() } // Check the type for allowed types - string[] elementTypes = VCalendarParserTools.GetTypes(splitArgs, defaultType, specifierRequired); + string[] elementTypes = VcardParserTools.GetTypes(splitArgs, defaultType, specifierRequired); foreach (string elementType in elementTypes) { string elementTypeUpper = elementType.ToUpper(); @@ -168,7 +169,7 @@ public Parts.Calendar Parse() } // Handle the part type - string values = VCalendarParserTools.GetValuesString(splitArgs, defaultValue, VCalendarConstants._valueArgumentSpecifier); + string values = VcardParserTools.GetValuesString(splitArgs, defaultValue, VCalendarConstants._valueArgumentSpecifier); switch (type) { case PartType.Strings: diff --git a/VisualCard.Calendar/Parsers/VCalendarParserTools.cs b/VisualCard.Calendar/Parsers/VCalendarParserTools.cs index b181acd..d505764 100644 --- a/VisualCard.Calendar/Parsers/VCalendarParserTools.cs +++ b/VisualCard.Calendar/Parsers/VCalendarParserTools.cs @@ -33,56 +33,6 @@ namespace VisualCard.Calendar.Parsers internal class VCalendarParserTools { #pragma warning disable IDE0060 - internal static string GetTypesString(string[] args, string @default, bool isSpecifierRequired = true) - { - // We're given an array of split arguments of an element delimited by the colon, such as: "...TYPE=home..." - // Filter list of arguments with the arguments that start with the type argument specifier, or, if specifier is not required, - // that doesn't have an equals sign - var ArgType = args.Where((arg) => arg.StartsWith(VCalendarConstants._typeArgumentSpecifier) || !arg.Contains("=")).ToArray(); - - // Trying to specify type without TYPE= is illegal according to RFC2426 in vCard 3.0 and 4.0 - if (ArgType.Count() > 0 && !ArgType[0].StartsWith(VCalendarConstants._typeArgumentSpecifier) && isSpecifierRequired) - throw new InvalidDataException("Type must be prepended with TYPE="); - - // Get the type from the split argument - string Type = ""; - if (isSpecifierRequired) - // Attempt to get the value from the key strictly - Type = - ArgType.Count() > 0 ? - string.Join(VCalendarConstants._valueDelimiter.ToString(), ArgType.Select((arg) => arg.Substring(VCalendarConstants._typeArgumentSpecifier.Length))) : - @default; - else - // Attempt to get the value from the key - Type = - ArgType.Count() > 0 ? - string.Join(VCalendarConstants._valueDelimiter.ToString(), ArgType.Select((arg) => arg.StartsWith(VCalendarConstants._typeArgumentSpecifier) ? arg.Substring(VCalendarConstants._typeArgumentSpecifier.Length) : arg)) : - @default; - - // Return the type - return Type; - } - - internal static string[] GetTypes(string[] args, string @default, bool isSpecifierRequired = true) => - GetTypesString(args, @default, isSpecifierRequired).Split([VCalendarConstants._valueDelimiter], StringSplitOptions.RemoveEmptyEntries); - - internal static string GetValuesString(string[] args, string @default, string argSpecifier) - { - // We're given an array of split arguments of an element delimited by the colon, such as: "...TYPE=home..." - // Filter list of arguments with the arguments that start with the specified specifier (key) - var argFromSpecifier = args.Where((arg) => arg.StartsWith(argSpecifier)); - - // Attempt to get the value from the key - string argString = - argFromSpecifier.Count() > 0 ? - string.Join(VCalendarConstants._valueDelimiter.ToString(), argFromSpecifier.Select((arg) => arg.Substring(argSpecifier.Length))) : - @default; - return argString; - } - - internal static string[] GetValues(string[] args, string @default, string argSpecifier) => - GetValuesString(args, @default, argSpecifier).Split([VCalendarConstants._valueDelimiter], StringSplitOptions.RemoveEmptyEntries); - internal static bool StringSupported(CalendarStringsEnum stringsEnum, Version calendarVersion) => stringsEnum switch { @@ -166,85 +116,6 @@ internal static (PartType type, object enumeration, Type enumType, Func throw new InvalidOperationException($"Unknown prefix {prefix}"), }; - - internal static string MakeStringBlock(string target, int firstLength) - { - const int maxChars = 74; - int maxCharsFirst = maxChars - firstLength + 1; - - // Construct the block - StringBuilder block = new(); - int selectedMax = maxCharsFirst; - int processed = 0; - for (int currCharNum = 0; currCharNum < target.Length; currCharNum++) - { - if (target[currCharNum] != '\n' && target[currCharNum] != '\r') - { - block.Append(target[currCharNum]); - processed++; - } - if (processed >= selectedMax || target[currCharNum] == '\n') - { - // Append a new line because we reached the maximum limit - selectedMax = maxChars; - processed = 0; - block.Append("\n "); - } - } - return block.ToString(); - } - - internal static IEnumerable GetDigits(int num) - { - int individualFactor = 0; - int tennerFactor = Convert.ToInt32(Math.Pow(10, num.ToString().Length)); - while (tennerFactor > 1) - { - num -= tennerFactor * individualFactor; - tennerFactor /= 10; - individualFactor = num / tennerFactor; - yield return individualFactor; - } - } - - internal static bool IsEncodingBlob(string[] args, string keyEncoded) - { - string encoding = GetValuesString(args, "b", VCalendarConstants._encodingArgumentSpecifier); - bool isValidUri = Uri.TryCreate(keyEncoded, UriKind.Absolute, out Uri uri); - if (isValidUri) - { - if (uri.Scheme == "data") - return true; - return false; - } - return - encoding.Equals("b", StringComparison.OrdinalIgnoreCase) || - encoding.Equals("BASE64", StringComparison.OrdinalIgnoreCase) || - encoding.Equals("BLOB", StringComparison.OrdinalIgnoreCase); - } - - internal static Stream GetBlobData(string[] args, string keyEncoded) - { - if (IsEncodingBlob(args, keyEncoded)) - { - bool isValidUri = Uri.TryCreate(keyEncoded, UriKind.Absolute, out Uri uri); - string dataStr; - if (isValidUri) - { - if (uri.Scheme == "data") - dataStr = uri.AbsolutePath.Substring(uri.AbsolutePath.IndexOf(",") + 1); - else - throw new InvalidDataException("Contains a valid URL; you should fetch that URL manually and convert the response to the stream."); - } - else - dataStr = keyEncoded; - byte[] dataBytes = Convert.FromBase64String(dataStr); - Stream blobStream = new MemoryStream(dataBytes); - return blobStream; - } - else - throw new InvalidOperationException("Not a blob. You should somehow handle it."); - } #pragma warning restore IDE0060 } } diff --git a/VisualCard.Calendar/Parts/Calendar.cs b/VisualCard.Calendar/Parts/Calendar.cs index 1b87941..8381546 100644 --- a/VisualCard.Calendar/Parts/Calendar.cs +++ b/VisualCard.Calendar/Parts/Calendar.cs @@ -27,6 +27,7 @@ using VisualCard.Calendar.Parsers; using VisualCard.Calendar.Parts.Comparers; using VisualCard.Calendar.Parts.Enums; +using VisualCard.Parsers; using VisualCard.Parts.Enums; namespace VisualCard.Calendar.Parts @@ -171,7 +172,7 @@ internal string SaveToString(Version version, Dictionary