Skip to content

Commit d66816d

Browse files
committed
Misc cleanup
1 parent 0e25239 commit d66816d

File tree

15 files changed

+176
-273
lines changed

15 files changed

+176
-273
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ csharp_style_var_elsewhere = true:suggestion
9090

9191
# C# code style settings - Expression-bodied members
9292
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#expression-bodied-members
93-
csharp_style_expression_bodied_methods = when_on_single_line:suggestion
93+
csharp_style_expression_bodied_methods = when_on_single_line:warning
9494
csharp_style_expression_bodied_constructors = false:warning
9595
csharp_style_expression_bodied_operators = when_on_single_line:warning
9696
csharp_style_expression_bodied_properties = when_on_single_line:warning

.github/workflows/wf-verify-formatting.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ jobs:
2424
- name: Restore NuGet Packages
2525
run: dotnet restore
2626

27-
- name: Format QRCoder
27+
- name: Format solution
2828
run: dotnet format --verify-no-changes --severity error

QRCoder/Base64QRCode.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,16 @@ public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
149149
#endif
150150
private string BitmapToBase64(Bitmap bmp, ImageType imgType)
151151
{
152-
var base64 = string.Empty;
153152
var iFormat = imgType switch
154153
{
155154
ImageType.Png => ImageFormat.Png,
156155
ImageType.Jpeg => ImageFormat.Jpeg,
157156
ImageType.Gif => ImageFormat.Gif,
158157
_ => ImageFormat.Png,
159158
};
160-
using (var memoryStream = new MemoryStream())
161-
{
162-
bmp.Save(memoryStream, iFormat);
163-
base64 = Convert.ToBase64String(memoryStream.ToArray(), Base64FormattingOptions.None);
164-
}
165-
return base64;
159+
using var memoryStream = new MemoryStream();
160+
bmp.Save(memoryStream, iFormat);
161+
return Convert.ToBase64String(memoryStream.ToArray(), Base64FormattingOptions.None);
166162
}
167163
#endif
168164

QRCoder/PayloadGenerator/Geolocation.cs

+5-8
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@ public Geolocation(string latitude, string longitude, GeolocationEncoding encodi
2828
/// Returns a string representation of the geolocation payload.
2929
/// </summary>
3030
/// <returns>A string representation of the geolocation payload in the specified encoding format.</returns>
31-
public override string ToString()
31+
public override string ToString() => _encoding switch
3232
{
33-
return _encoding switch
34-
{
35-
GeolocationEncoding.GEO => $"geo:{_latitude},{_longitude}",
36-
GeolocationEncoding.GoogleMaps => $"http://maps.google.com/maps?q={_latitude},{_longitude}",
37-
_ => "geo:",
38-
};
39-
}
33+
GeolocationEncoding.GEO => $"geo:{_latitude},{_longitude}",
34+
GeolocationEncoding.GoogleMaps => $"http://maps.google.com/maps?q={_latitude},{_longitude}",
35+
_ => "geo:",
36+
};
4037

4138
/// <summary>
4239
/// Defines the encoding types for geolocation payloads.

QRCoder/PayloadGenerator/MMS.cs

+5-19
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,12 @@ public MMS(string number, string subject, MMSEncoding encoding = MMSEncoding.MMS
4141
/// Returns the MMS payload as a string.
4242
/// </summary>
4343
/// <returns>The MMS payload as a string.</returns>
44-
public override string ToString()
44+
public override string ToString() => _encoding switch
4545
{
46-
var returnVal = string.Empty;
47-
switch (_encoding)
48-
{
49-
case MMSEncoding.MMSTO:
50-
var queryStringMmsTo = string.Empty;
51-
if (!string.IsNullOrEmpty(_subject))
52-
queryStringMmsTo = $"?subject={Uri.EscapeDataString(_subject)}";
53-
returnVal = $"mmsto:{_number}{queryStringMmsTo}";
54-
break;
55-
case MMSEncoding.MMS:
56-
var queryStringMms = string.Empty;
57-
if (!string.IsNullOrEmpty(_subject))
58-
queryStringMms = $"?body={Uri.EscapeDataString(_subject)}";
59-
returnVal = $"mms:{_number}{queryStringMms}";
60-
break;
61-
}
62-
return returnVal;
63-
}
46+
MMSEncoding.MMSTO => $"mmsto:{_number}{(string.IsNullOrEmpty(_subject) ? string.Empty : $"?subject={Uri.EscapeDataString(_subject)}")}",
47+
MMSEncoding.MMS => $"mms:{_number}{(string.IsNullOrEmpty(_subject) ? string.Empty : $"?body={Uri.EscapeDataString(_subject)}")}",
48+
_ => string.Empty,
49+
};
6450

6551
/// <summary>
6652
/// Defines the encoding types for the MMS payload.

QRCoder/PayloadGenerator/Mail.cs

+5-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public Mail(string? mailReceiver = null, string? subject = null, string? message
3636
/// <returns>The email payload as a string.</returns>
3737
public override string ToString()
3838
{
39-
var returnVal = string.Empty;
4039
switch (_encoding)
4140
{
4241
case MailEncoding.MAILTO:
@@ -46,16 +45,14 @@ public override string ToString()
4645
if (!string.IsNullOrEmpty(_message))
4746
parts.Add("body=" + Uri.EscapeDataString(_message));
4847
var queryString = parts.Any() ? $"?{string.Join("&", parts.ToArray())}" : "";
49-
returnVal = $"mailto:{_mailReceiver}{queryString}";
50-
break;
48+
return $"mailto:{_mailReceiver}{queryString}";
5149
case MailEncoding.MATMSG:
52-
returnVal = $"MATMSG:TO:{_mailReceiver};SUB:{EscapeInput(_subject ?? "")};BODY:{EscapeInput(_message ?? "")};;";
53-
break;
50+
return $"MATMSG:TO:{_mailReceiver};SUB:{EscapeInput(_subject ?? "")};BODY:{EscapeInput(_message ?? "")};;";
5451
case MailEncoding.SMTP:
55-
returnVal = $"SMTP:{_mailReceiver}:{EscapeInput(_subject ?? "", true)}:{EscapeInput(_message ?? "", true)}";
56-
break;
52+
return $"SMTP:{_mailReceiver}:{EscapeInput(_subject ?? "", true)}:{EscapeInput(_message ?? "", true)}";
53+
default:
54+
return string.Empty;
5755
}
58-
return returnVal;
5956
}
6057

6158
/// <summary>

QRCoder/PayloadGenerator/OneTimePassword.cs

+5-8
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,12 @@ public enum OoneTimePasswordAuthAlgorithm
9696
/// Returns the OTP payload as a string.
9797
/// </summary>
9898
/// <returns>The OTP payload as a string.</returns>
99-
public override string ToString()
99+
public override string ToString() => Type switch
100100
{
101-
return Type switch
102-
{
103-
OneTimePasswordAuthType.TOTP => TimeToString(),
104-
OneTimePasswordAuthType.HOTP => HMACToString(),
105-
_ => throw new ArgumentOutOfRangeException(),
106-
};
107-
}
101+
OneTimePasswordAuthType.TOTP => TimeToString(),
102+
OneTimePasswordAuthType.HOTP => HMACToString(),
103+
_ => throw new ArgumentOutOfRangeException(),
104+
};
108105

109106
// Note: Issuer:Label must only contain 1 : if either of the Issuer or the Label has a : then it is invalid.
110107
// Defaults are 6 digits and 30 for Period

QRCoder/PayloadGenerator/SMS.cs

+6-22
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,13 @@ public SMS(string number, string subject, SMSEncoding encoding = SMSEncoding.SMS
4141
/// Returns the SMS payload as a string.
4242
/// </summary>
4343
/// <returns>The SMS payload as a string.</returns>
44-
public override string ToString()
44+
public override string ToString() => _encoding switch
4545
{
46-
var returnVal = string.Empty;
47-
switch (_encoding)
48-
{
49-
case SMSEncoding.SMS:
50-
var queryString = string.Empty;
51-
if (!string.IsNullOrEmpty(_subject))
52-
queryString = $"?body={Uri.EscapeDataString(_subject)}";
53-
returnVal = $"sms:{_number}{queryString}";
54-
break;
55-
case SMSEncoding.SMS_iOS:
56-
var queryStringiOS = string.Empty;
57-
if (!string.IsNullOrEmpty(_subject))
58-
queryStringiOS = $";body={Uri.EscapeDataString(_subject)}";
59-
returnVal = $"sms:{_number}{queryStringiOS}";
60-
break;
61-
case SMSEncoding.SMSTO:
62-
returnVal = $"SMSTO:{_number}:{_subject}";
63-
break;
64-
}
65-
return returnVal;
66-
}
46+
SMSEncoding.SMS => $"sms:{_number}{(string.IsNullOrEmpty(_subject) ? string.Empty : $"?body={Uri.EscapeDataString(_subject)}")}",
47+
SMSEncoding.SMS_iOS => $"sms:{_number}{(string.IsNullOrEmpty(_subject) ? string.Empty : $";body={Uri.EscapeDataString(_subject)}")}",
48+
SMSEncoding.SMSTO => $"SMSTO:{_number}:{_subject}",
49+
_ => string.Empty,
50+
};
6751

6852
/// <summary>
6953
/// Specifies the encoding type for the SMS payload.

QRCoder/PayloadGenerator/SkypeCall.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ public SkypeCall(string skypeUsername)
2222
/// Converts the Skype call payload to a string.
2323
/// </summary>
2424
/// <returns>A string representation of the Skype call payload.</returns>
25-
public override string ToString()
26-
{
27-
return $"skype:{_skypeUsername}?call";
28-
}
25+
public override string ToString() => $"skype:{_skypeUsername}?call";
2926
}
3027
}

QRCoder/PayloadGenerator/Url.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ public Url(string url)
2424
/// Returns the URL payload as a string.
2525
/// </summary>
2626
/// <returns>The URL payload as a string, ensuring it starts with "http://" if no protocol is specified.</returns>
27-
public override string ToString()
28-
{
29-
return (!_url.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? "http://" + _url : _url);
30-
}
27+
public override string ToString() => !_url.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? "http://" + _url : _url;
3128
}
3229
}

QRCoder/SvgQRCode.cs

+18-49
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#if !NETSTANDARD1_3
22
using System;
3-
using System.Collections;
4-
using System.Collections.Generic;
53
using System.Drawing;
64
using System.Text;
7-
using System.Text.RegularExpressions;
85
using QRCoder.Extensions;
96
using static QRCoder.QRCodeGenerator;
107
using static QRCoder.SvgQRCode;
@@ -84,9 +81,7 @@ public string GetGraphic(int pixelsPerModule, string darkColorHex, string lightC
8481
/// <param name="logo">An optional logo to be rendered on the code (either Bitmap or SVG).</param>
8582
/// <returns>Returns the QR code graphic as an SVG string.</returns>
8683
public string GetGraphic(Size viewBox, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo? logo = null)
87-
{
88-
return GetGraphic(viewBox, Color.Black, Color.White, drawQuietZones, sizingMode, logo);
89-
}
84+
=> GetGraphic(viewBox, Color.Black, Color.White, drawQuietZones, sizingMode, logo);
9085

9186
/// <summary>
9287
/// Returns a QR code as an SVG string with custom colors and optional quiet zones and an optional logo.
@@ -99,9 +94,7 @@ public string GetGraphic(Size viewBox, bool drawQuietZones = true, SizingMode si
9994
/// <param name="logo">An optional logo to be rendered on the code (either Bitmap or SVG).</param>
10095
/// <returns>Returns the QR code graphic as an SVG string.</returns>
10196
public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo? logo = null)
102-
{
103-
return GetGraphic(viewBox, ColorTranslator.ToHtml(Color.FromArgb(darkColor.ToArgb())), ColorTranslator.ToHtml(Color.FromArgb(lightColor.ToArgb())), drawQuietZones, sizingMode, logo);
104-
}
97+
=> GetGraphic(viewBox, ColorTranslator.ToHtml(Color.FromArgb(darkColor.ToArgb())), ColorTranslator.ToHtml(Color.FromArgb(lightColor.ToArgb())), drawQuietZones, sizingMode, logo);
10598

10699
/// <summary>
107100
/// Returns a QR code as an SVG string with custom colors (in HEX syntax), optional quiet zones, and an optional logo.
@@ -220,9 +213,7 @@ public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex
220213
}
221214

222215
private bool IsBlockedByLogo(double x, double y, ImageAttributes attr, double pixelPerModule)
223-
{
224-
return x + pixelPerModule >= attr.X && x <= attr.X + attr.Width && y + pixelPerModule >= attr.Y && y <= attr.Y + attr.Height;
225-
}
216+
=> x + pixelPerModule >= attr.X && x <= attr.X + attr.Width && y + pixelPerModule >= attr.Y && y <= attr.Y + attr.Height;
226217

227218
private ImageAttributes GetLogoAttributes(SvgLogo logo, Size viewBox)
228219
{
@@ -247,13 +238,11 @@ private struct ImageAttributes
247238
public double Y;
248239
}
249240

241+
//Clean double values for international use/formats
242+
//We use explicitly "G15" to avoid differences between .NET full and Core platforms
243+
//https://stackoverflow.com/questions/64898117/tostring-has-a-different-behavior-between-net-462-and-net-core-3-1
250244
private string CleanSvgVal(double input)
251-
{
252-
//Clean double values for international use/formats
253-
//We use explicitly "G15" to avoid differences between .NET full and Core platforms
254-
//https://stackoverflow.com/questions/64898117/tostring-has-a-different-behavior-between-net-462-and-net-core-3-1
255-
return input.ToString("G15", System.Globalization.CultureInfo.InvariantCulture);
256-
}
245+
=> input.ToString("G15", System.Globalization.CultureInfo.InvariantCulture);
257246

258247
/// <summary>
259248
/// Mode of sizing attribution on svg root node
@@ -338,52 +327,35 @@ public SvgLogo(byte[] iconRasterized, int iconSizePercent = 15, bool fillLogoBac
338327
/// <summary>
339328
/// Returns the raw logo's data
340329
/// </summary>
341-
public object GetRawLogo()
342-
{
343-
return _logoRaw;
344-
}
330+
public object GetRawLogo() => _logoRaw;
345331

346332
/// <summary>
347333
/// Defines, if the logo shall be natively embedded.
348334
/// true=native svg embedding, false=embedding via image-tag
349335
/// </summary>
350-
public bool IsEmbedded()
351-
{
352-
return _isEmbedded;
353-
}
336+
public bool IsEmbedded() => _isEmbedded;
354337

355338
/// <summary>
356339
/// Returns the media type of the logo
357340
/// </summary>
358341
/// <returns></returns>
359-
public MediaType GetMediaType()
360-
{
361-
return _mediaType;
362-
}
342+
public MediaType GetMediaType() => _mediaType;
363343

364344
/// <summary>
365345
/// Returns the logo as data-uri
366346
/// </summary>
367347
public string GetDataUri()
368-
{
369-
return $"data:{GetMimeType(_mediaType)};base64,{_logoData}";
370-
}
348+
=> $"data:{GetMimeType(_mediaType)};base64,{_logoData}";
371349

372350
/// <summary>
373351
/// Returns how much of the QR code should be covered by the logo (in percent)
374352
/// </summary>
375-
public int GetIconSizePercent()
376-
{
377-
return _iconSizePercent;
378-
}
353+
public int GetIconSizePercent() => _iconSizePercent;
379354

380355
/// <summary>
381356
/// Returns if the background of the logo should be cleaned (no QR modules will be rendered behind the logo)
382357
/// </summary>
383-
public bool FillLogoBackground()
384-
{
385-
return _fillLogoBackground;
386-
}
358+
public bool FillLogoBackground() => _fillLogoBackground;
387359

388360
/// <summary>
389361
/// Media types for SvgLogos
@@ -400,15 +372,12 @@ public enum MediaType : int
400372
SVG = 1
401373
}
402374

403-
private string GetMimeType(MediaType type)
375+
private string GetMimeType(MediaType type) => type switch
404376
{
405-
return type switch
406-
{
407-
MediaType.PNG => "image/png",
408-
MediaType.SVG => "image/svg+xml",
409-
_ => throw new ArgumentOutOfRangeException(nameof(type)),
410-
};
411-
}
377+
MediaType.PNG => "image/png",
378+
MediaType.SVG => "image/svg+xml",
379+
_ => throw new ArgumentOutOfRangeException(nameof(type)),
380+
};
412381

413382
}
414383
}

QRCoderConsole/Program.cs

+8-11
Original file line numberDiff line numberDiff line change
@@ -238,17 +238,14 @@ public QRCodeGenerator.ECCLevel GetECCLevel(string value)
238238
#if NET6_0_WINDOWS
239239
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
240240
#endif
241-
public ImageFormat GetImageFormat(string value)
241+
public ImageFormat GetImageFormat(string value) => value.ToLower() switch
242242
{
243-
return value.ToLower() switch
244-
{
245-
"jpg" => ImageFormat.Jpeg,
246-
"jpeg" => ImageFormat.Jpeg,
247-
"gif" => ImageFormat.Gif,
248-
"bmp" => ImageFormat.Bmp,
249-
"tiff" => ImageFormat.Tiff,
250-
_ => ImageFormat.Png,
251-
};
252-
}
243+
"jpg" => ImageFormat.Jpeg,
244+
"jpeg" => ImageFormat.Jpeg,
245+
"gif" => ImageFormat.Gif,
246+
"bmp" => ImageFormat.Bmp,
247+
"tiff" => ImageFormat.Tiff,
248+
_ => ImageFormat.Png,
249+
};
253250
}
254251

0 commit comments

Comments
 (0)