Skip to content

Commit 6b7311b

Browse files
authored
Merge pull request #534 from Shane32/refactor_ext_methods
Refactor compatibility extension methods
2 parents 923dbe5 + d5e466d commit 6b7311b

File tree

5 files changed

+43
-67
lines changed

5 files changed

+43
-67
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
6-
namespace QRCoder.Framework4._0Methods
1+
#if NET35
2+
namespace QRCoder
73
{
8-
class Stream4Methods
4+
internal static class StreamExtensions
95
{
10-
public static void CopyTo(System.IO.Stream input, System.IO.Stream output)
6+
/// <summary>
7+
/// Copies a stream to another stream.
8+
/// </summary>
9+
public static void CopyTo(this System.IO.Stream input, System.IO.Stream output)
1110
{
1211
byte[] buffer = new byte[16 * 1024];
1312
int bytesRead;
@@ -18,3 +17,4 @@ public static void CopyTo(System.IO.Stream input, System.IO.Stream output)
1817
}
1918
}
2019
}
20+
#endif
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace QRCoder
2+
{
3+
internal static class StringExtensions
4+
{
5+
/// <summary>
6+
/// Indicates whether the specified string is null, empty, or consists only of white-space characters.
7+
/// </summary>
8+
/// <returns>
9+
/// <see langword="true"/> if the <paramref name="value"/> is null, empty, or white space; otherwise, <see langword="false"/>.
10+
/// </returns>
11+
public static bool IsNullOrWhiteSpace(this string value)
12+
{
13+
#if NET35
14+
if (value == null) return true;
15+
16+
for (int i = 0; i < value.Length; i++)
17+
{
18+
if (!char.IsWhiteSpace(value[i])) return false;
19+
}
20+
21+
return true;
22+
#else
23+
return string.IsNullOrWhiteSpace(value);
24+
#endif
25+
}
26+
}
27+
}

QRCoder/Framework4.0Methods/String4Methods.cs

-48
This file was deleted.

QRCoder/PayloadGenerator.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,7 @@ private string TimeToString()
20342034

20352035
private void ProcessCommonFields(StringBuilder sb)
20362036
{
2037-
if (String40Methods.IsNullOrWhiteSpace(Secret))
2037+
if (Secret.IsNullOrWhiteSpace())
20382038
{
20392039
throw new Exception("Secret must be a filled out base32 encoded string");
20402040
}
@@ -2043,7 +2043,7 @@ private void ProcessCommonFields(StringBuilder sb)
20432043
string escapedLabel = null;
20442044
string label = null;
20452045

2046-
if (!String40Methods.IsNullOrWhiteSpace(Issuer))
2046+
if (!Issuer.IsNullOrWhiteSpace())
20472047
{
20482048
if (Issuer.Contains(":"))
20492049
{
@@ -2052,7 +2052,7 @@ private void ProcessCommonFields(StringBuilder sb)
20522052
escapedIssuer = Uri.EscapeDataString(Issuer);
20532053
}
20542054

2055-
if (!String40Methods.IsNullOrWhiteSpace(Label))
2055+
if (!Label.IsNullOrWhiteSpace())
20562056
{
20572057
if (Label.Contains(":"))
20582058
{

QRCoder/QRCodeData.cs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1+
using System;
12
using System.Collections;
23
using System.Collections.Generic;
3-
using System.Linq;
4+
using System.IO;
5+
using System.IO.Compression;
46

57
namespace QRCoder
68
{
7-
using QRCoder.Framework4._0Methods;
8-
using System;
9-
using System.IO;
10-
using System.IO.Compression;
11-
129
public class QRCodeData : IDisposable
1310
{
1411
public List<BitArray> ModuleMatrix { get; set; }
@@ -48,7 +45,7 @@ public QRCodeData(byte[] rawData, Compression compressMode)
4845
{
4946
using (var dstream = new DeflateStream(input, CompressionMode.Decompress))
5047
{
51-
Stream4Methods.CopyTo(dstream, output);
48+
dstream.CopyTo(output);
5249
}
5350
bytes = new List<byte>(output.ToArray());
5451
}
@@ -62,7 +59,7 @@ public QRCodeData(byte[] rawData, Compression compressMode)
6259
{
6360
using (var dstream = new GZipStream(input, CompressionMode.Decompress))
6461
{
65-
Stream4Methods.CopyTo(dstream, output);
62+
dstream.CopyTo(output);
6663
}
6764
bytes = new List<byte>(output.ToArray());
6865
}

0 commit comments

Comments
 (0)