Skip to content

Refactor compatibility extension methods #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
/// <summary>
/// Copies a stream to another stream.
/// </summary>
public static void CopyTo(this System.IO.Stream input, System.IO.Stream output)
{
byte[] buffer = new byte[16 * 1024];
int bytesRead;
Expand All @@ -18,3 +17,4 @@ public static void CopyTo(System.IO.Stream input, System.IO.Stream output)
}
}
}
#endif
27 changes: 27 additions & 0 deletions QRCoder/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace QRCoder
{
internal static class StringExtensions
{
/// <summary>
/// Indicates whether the specified string is null, empty, or consists only of white-space characters.
/// </summary>
/// <returns>
/// <see langword="true"/> if the <paramref name="value"/> is null, empty, or white space; otherwise, <see langword="false"/>.
/// </returns>
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;
}

return true;
#else
return string.IsNullOrWhiteSpace(value);
#endif
}
}
}
48 changes: 0 additions & 48 deletions QRCoder/Framework4.0Methods/String4Methods.cs

This file was deleted.

6 changes: 3 additions & 3 deletions QRCoder/PayloadGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2034,7 +2034,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");
}
Expand All @@ -2043,7 +2043,7 @@ private void ProcessCommonFields(StringBuilder sb)
string escapedLabel = null;
string label = null;

if (!String40Methods.IsNullOrWhiteSpace(Issuer))
if (!Issuer.IsNullOrWhiteSpace())
{
if (Issuer.Contains(":"))
{
Expand All @@ -2052,7 +2052,7 @@ private void ProcessCommonFields(StringBuilder sb)
escapedIssuer = Uri.EscapeDataString(Issuer);
}

if (!String40Methods.IsNullOrWhiteSpace(Label))
if (!Label.IsNullOrWhiteSpace())
{
if (Label.Contains(":"))
{
Expand Down
13 changes: 5 additions & 8 deletions QRCoder/QRCodeData.cs
Original file line number Diff line number Diff line change
@@ -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<BitArray> ModuleMatrix { get; set; }
Expand Down Expand Up @@ -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<byte>(output.ToArray());
}
Expand All @@ -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<byte>(output.ToArray());
}
Expand Down