From e7c8df7e4edd864f967d2309d06aa611e19bbe86 Mon Sep 17 00:00:00 2001 From: Shane32 Date: Wed, 22 May 2024 18:51:15 -0400 Subject: [PATCH 1/2] Refactor compatibility extension methods --- .../StreamExtensions.cs} | 16 +++++----- QRCoder/Framework4.0Methods/String4Methods.cs | 31 ++++--------------- QRCoder/QRCodeData.cs | 13 +++----- 3 files changed, 19 insertions(+), 41 deletions(-) rename QRCoder/{Framework4.0Methods/Stream4Methods.cs => Extensions/StreamExtensions.cs} (50%) diff --git a/QRCoder/Framework4.0Methods/Stream4Methods.cs b/QRCoder/Extensions/StreamExtensions.cs similarity index 50% rename from QRCoder/Framework4.0Methods/Stream4Methods.cs rename to QRCoder/Extensions/StreamExtensions.cs index 6c58eb7a..14796167 100644 --- a/QRCoder/Framework4.0Methods/Stream4Methods.cs +++ b/QRCoder/Extensions/StreamExtensions.cs @@ -1,13 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace QRCoder.Framework4._0Methods +#if NET35 +namespace QRCoder { - class Stream4Methods + internal static class StreamExtensions { - public static void CopyTo(System.IO.Stream input, System.IO.Stream output) + /// + /// Copies a stream to another stream. + /// + public static void CopyTo(this System.IO.Stream input, System.IO.Stream output) { byte[] buffer = new byte[16 * 1024]; int bytesRead; @@ -18,3 +17,4 @@ public static void CopyTo(System.IO.Stream input, System.IO.Stream output) } } } +#endif diff --git a/QRCoder/Framework4.0Methods/String4Methods.cs b/QRCoder/Framework4.0Methods/String4Methods.cs index 748c0d69..abd343fd 100644 --- a/QRCoder/Framework4.0Methods/String4Methods.cs +++ b/QRCoder/Framework4.0Methods/String4Methods.cs @@ -5,13 +5,14 @@ namespace QRCoder internal static class String40Methods { /// - /// The IsNullOrWhiteSpace method from Framework4.0 + /// Indicates whether the specified string is null, empty, or consists only of white-space characters. /// /// - /// true if the is null or white space; otherwise, false. + /// if the is null, empty, or white space; otherwise, . /// public static bool IsNullOrWhiteSpace(String value) { +#if NET35 if (value == null) return true; for (int i = 0; i < value.Length; i++) @@ -20,29 +21,9 @@ public static bool IsNullOrWhiteSpace(String value) } return true; - } - - public static string ReverseString(string str) - { - char[] chars = str.ToCharArray(); - char[] result = new char[chars.Length]; - for (int i = 0, j = str.Length - 1; i < str.Length; i++, j--) - { - result[i] = chars[j]; - } - return new string(result); - } - - public static bool IsAllDigit(string str) - { - foreach (var c in str) - { - if (!char.IsDigit(c)) - { - return false; - } - } - return true; +#else + return string.IsNullOrWhiteSpace(value); +#endif } } } \ No newline at end of file diff --git a/QRCoder/QRCodeData.cs b/QRCoder/QRCodeData.cs index e9b8a571..dbb0f06c 100644 --- a/QRCoder/QRCodeData.cs +++ b/QRCoder/QRCodeData.cs @@ -1,14 +1,11 @@ +using System; using System.Collections; using System.Collections.Generic; -using System.Linq; +using System.IO; +using System.IO.Compression; namespace QRCoder { - using QRCoder.Framework4._0Methods; - using System; - using System.IO; - using System.IO.Compression; - public class QRCodeData : IDisposable { public List ModuleMatrix { get; set; } @@ -48,7 +45,7 @@ public QRCodeData(byte[] rawData, Compression compressMode) { using (var dstream = new DeflateStream(input, CompressionMode.Decompress)) { - Stream4Methods.CopyTo(dstream, output); + dstream.CopyTo(output); } bytes = new List(output.ToArray()); } @@ -62,7 +59,7 @@ public QRCodeData(byte[] rawData, Compression compressMode) { using (var dstream = new GZipStream(input, CompressionMode.Decompress)) { - Stream4Methods.CopyTo(dstream, output); + dstream.CopyTo(output); } bytes = new List(output.ToArray()); } From 3edab65adf707cef87eb95a328f8a0e93e0266df Mon Sep 17 00:00:00 2001 From: Shane32 Date: Thu, 23 May 2024 23:06:52 -0400 Subject: [PATCH 2/2] Refactor StringExtensions --- .../String4Methods.cs => Extensions/StringExtensions.cs} | 8 +++----- QRCoder/PayloadGenerator.cs | 6 +++--- 2 files changed, 6 insertions(+), 8 deletions(-) rename QRCoder/{Framework4.0Methods/String4Methods.cs => Extensions/StringExtensions.cs} (77%) diff --git a/QRCoder/Framework4.0Methods/String4Methods.cs b/QRCoder/Extensions/StringExtensions.cs similarity index 77% rename from QRCoder/Framework4.0Methods/String4Methods.cs rename to QRCoder/Extensions/StringExtensions.cs index abd343fd..34a44272 100644 --- a/QRCoder/Framework4.0Methods/String4Methods.cs +++ b/QRCoder/Extensions/StringExtensions.cs @@ -1,8 +1,6 @@ -using System; - namespace QRCoder { - internal static class String40Methods + internal static class StringExtensions { /// /// Indicates whether the specified string is null, empty, or consists only of white-space characters. @@ -10,14 +8,14 @@ internal static class String40Methods /// /// if the is null, empty, or white space; otherwise, . /// - public static bool IsNullOrWhiteSpace(String value) + public static bool IsNullOrWhiteSpace(this string value) { #if NET35 if (value == null) return true; for (int i = 0; i < value.Length; i++) { - if (!Char.IsWhiteSpace(value[i])) return false; + if (!char.IsWhiteSpace(value[i])) return false; } return true; diff --git a/QRCoder/PayloadGenerator.cs b/QRCoder/PayloadGenerator.cs index b4b0fd76..0736fd73 100644 --- a/QRCoder/PayloadGenerator.cs +++ b/QRCoder/PayloadGenerator.cs @@ -2030,7 +2030,7 @@ private string TimeToString() private void ProcessCommonFields(StringBuilder sb) { - if (String40Methods.IsNullOrWhiteSpace(Secret)) + if (Secret.IsNullOrWhiteSpace()) { throw new Exception("Secret must be a filled out base32 encoded string"); } @@ -2039,7 +2039,7 @@ private void ProcessCommonFields(StringBuilder sb) string escapedLabel = null; string label = null; - if (!String40Methods.IsNullOrWhiteSpace(Issuer)) + if (!Issuer.IsNullOrWhiteSpace()) { if (Issuer.Contains(":")) { @@ -2048,7 +2048,7 @@ private void ProcessCommonFields(StringBuilder sb) escapedIssuer = Uri.EscapeDataString(Issuer); } - if (!String40Methods.IsNullOrWhiteSpace(Label)) + if (!Label.IsNullOrWhiteSpace()) { if (Label.Contains(":")) {