Skip to content

Commit

Permalink
Use expression body for method
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 committed Jun 5, 2024
1 parent 7698bf3 commit ee8d49a
Show file tree
Hide file tree
Showing 19 changed files with 32 additions and 93 deletions.
4 changes: 1 addition & 3 deletions QRCoder/AbstractQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ protected AbstractQRCode(QRCodeData data)
/// </summary>
/// <param name="data">The QRCodeData object generated by QRCodeGenerator.CreateQrCode().</param>
public virtual void SetQRCodeData(QRCodeData data)
{
QrCodeData = data;
}
=> QrCodeData = data;

/// <summary>
/// Disposes the QRCodeData object.
Expand Down
8 changes: 2 additions & 6 deletions QRCoder/ArtQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,15 @@ public ArtQRCode(QRCodeData data) : base(data) { }
/// <param name="pixelsPerModule">Amount of px each dark/light module of the QR code shall take place in the final QR code image</param>
/// <returns>QRCode graphic as bitmap</returns>
public Bitmap GetGraphic(int pixelsPerModule)
{
return GetGraphic(pixelsPerModule, Color.Black, Color.White, Color.Transparent);
}
=> GetGraphic(pixelsPerModule, Color.Black, Color.White, Color.Transparent);

/// <summary>
/// Renders an art-style QR code with dots as modules and a background image (With default settings: DarkColor=Black, LightColor=White, Background=Transparent, QuietZone=true)
/// </summary>
/// <param name="backgroundImage">A bitmap object that will be used as background picture</param>
/// <returns>QRCode graphic as bitmap</returns>
public Bitmap GetGraphic(Bitmap? backgroundImage = null)
{
return GetGraphic(10, Color.Black, Color.White, Color.Transparent, backgroundImage: backgroundImage);
}
=> GetGraphic(10, Color.Black, Color.White, Color.Transparent, backgroundImage: backgroundImage);

/// <summary>
/// Renders an art-style QR code with dots as modules and various user settings
Expand Down
8 changes: 2 additions & 6 deletions QRCoder/Base64QRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public Base64QRCode(QRCodeData data) : base(data)
/// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param>
/// <returns>Returns the QR code graphic as a base64-encoded string.</returns>
public string GetGraphic(int pixelsPerModule)
{
return GetGraphic(pixelsPerModule, Color.Black, Color.White, true);
}
=> GetGraphic(pixelsPerModule, Color.Black, Color.White, true);

/// <summary>
/// Returns a base64-encoded string that contains the resulting QR code as an image.
Expand All @@ -50,9 +48,7 @@ public string GetGraphic(int pixelsPerModule)
/// <param name="imgType">The type of image to generate (PNG, JPEG, GIF).</param>
/// <returns>Returns the QR code graphic as a base64-encoded string.</returns>
public string GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
{
return GetGraphic(pixelsPerModule, ColorTranslator.FromHtml(darkColorHtmlHex), ColorTranslator.FromHtml(lightColorHtmlHex), drawQuietZones, imgType);
}
=> GetGraphic(pixelsPerModule, ColorTranslator.FromHtml(darkColorHtmlHex), ColorTranslator.FromHtml(lightColorHtmlHex), drawQuietZones, imgType);

/// <summary>
/// Returns a base64-encoded string that contains the resulting QR code as an image.
Expand Down
8 changes: 2 additions & 6 deletions QRCoder/BitmapByteQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ public BitmapByteQRCode(QRCodeData data) : base(data) { }
/// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param>
/// <returns>Returns the QR code graphic as a bitmap byte array.</returns>
public byte[] GetGraphic(int pixelsPerModule)
{
return GetGraphic(pixelsPerModule, new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0xFF, 0xFF, 0xFF });
}
=> GetGraphic(pixelsPerModule, new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0xFF, 0xFF, 0xFF });

/// <summary>
/// Returns the QR code graphic as a bitmap byte array.
Expand All @@ -42,9 +40,7 @@ public byte[] GetGraphic(int pixelsPerModule)
/// <param name="lightColorHtmlHex">The color of the light modules in HTML hex format.</param>
/// <returns>Returns the QR code graphic as a bitmap byte array.</returns>
public byte[] GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex)
{
return GetGraphic(pixelsPerModule, HexColorToByteArray(darkColorHtmlHex), HexColorToByteArray(lightColorHtmlHex));
}
=> GetGraphic(pixelsPerModule, HexColorToByteArray(darkColorHtmlHex), HexColorToByteArray(lightColorHtmlHex));

/// <summary>
/// Returns the QR code graphic as a bitmap byte array.
Expand Down
8 changes: 2 additions & 6 deletions QRCoder/PayloadGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ private static bool IsValidQRIban(string iban)
/// <param name="bic">The BIC to validate.</param>
/// <returns>True if the BIC is valid; otherwise, false.</returns>
private static bool IsValidBic(string bic)
{
return Regex.IsMatch(bic.Replace(" ", ""), @"^([a-zA-Z]{4}[a-zA-Z]{2}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?)$");
}
=> Regex.IsMatch(bic.Replace(" ", ""), @"^([a-zA-Z]{4}[a-zA-Z]{2}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?)$");


/// <summary>
Expand Down Expand Up @@ -137,7 +135,5 @@ public static bool ChecksumMod10(string digits)
/// <param name="inp">The input string.</param>
/// <returns>True if the string is in hexadecimal format; otherwise, false.</returns>
private static bool isHexStyle(string inp)
{
return (System.Text.RegularExpressions.Regex.IsMatch(inp, @"\A\b[0-9a-fA-F]+\b\Z") || System.Text.RegularExpressions.Regex.IsMatch(inp, @"\A\b(0[xX])?[0-9a-fA-F]+\b\Z"));
}
=> System.Text.RegularExpressions.Regex.IsMatch(inp, @"\A\b[0-9a-fA-F]+\b\Z") || System.Text.RegularExpressions.Regex.IsMatch(inp, @"\A\b(0[xX])?[0-9a-fA-F]+\b\Z");
}
4 changes: 1 addition & 3 deletions QRCoder/PayloadGenerator/Bookmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public Bookmark(string url, string title)
/// </summary>
/// <returns>A string representation of the bookmark payload in the MEBKM format.</returns>
public override string ToString()
{
return $"MEBKM:TITLE:{_title};URL:{_url};;";
}
=> $"MEBKM:TITLE:{_title};URL:{_url};;";
}
}
4 changes: 1 addition & 3 deletions QRCoder/PayloadGenerator/PhoneNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public PhoneNumber(string number)
/// </summary>
/// <returns>The phone call payload as a string.</returns>
public override string ToString()
{
return $"tel:{_number}";
}
=> $"tel:{_number}";
}
}
4 changes: 1 addition & 3 deletions QRCoder/PayloadGenerator/RussiaPaymentOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,7 @@ private List<string> GetMandatoryFieldsAsList()
/// <param name="errorText">An optional error text. If null, a standard error text is generated</param>
/// <returns>Input value (in case it is valid)</returns>
private static string ValidateInput(string input, string fieldname, string pattern, string? errorText = null)
{
return ValidateInput(input, fieldname, new string[] { pattern }, errorText);
}
=> ValidateInput(input, fieldname, new string[] { pattern }, errorText);

/// <summary>
/// Validates a string against one or more given Regex patterns. Returns input if it matches all regex expressions (=valid) or throws Exception in case there's a mismatch
Expand Down
4 changes: 1 addition & 3 deletions QRCoder/PayloadGenerator/SlovenianUpnQr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ public class SlovenianUpnQr : Payload
/// <param name="maxLength">The maximum length of the string.</param>
/// <returns>The limited string.</returns>
private string LimitLength(string value, int maxLength)
{
return (value.Length <= maxLength) ? value : value.Substring(0, maxLength);
}
=> (value.Length <= maxLength) ? value : value.Substring(0, maxLength);

/// <summary>
/// Initializes a new instance of the <see cref="SlovenianUpnQr"/> class.
Expand Down
4 changes: 1 addition & 3 deletions QRCoder/PayloadGenerator/SwissQrCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,7 @@ public Iban(string iban, IbanType ibanType)
/// </summary>
/// <returns>A string representation of the IBAN.</returns>
public override string ToString()
{
return _iban.Replace("-", "").Replace("\n", "").Replace(" ", "");
}
=> _iban.Replace("-", "").Replace("\n", "").Replace(" ", "");

/// <summary>
/// Represents the type of IBAN.
Expand Down
5 changes: 1 addition & 4 deletions QRCoder/PayloadGenerator/WiFi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ public WiFi(string ssid, string password, Authentication authenticationMode, boo
/// Returns the WiFi payload as a string.
/// </summary>
public override string ToString()
{
return
$"WIFI:T:{_authenticationMode};S:{_ssid};P:{_password};{(_isHiddenSsid ? "H:true" : string.Empty)};";
}
=> $"WIFI:T:{_authenticationMode};S:{_ssid};P:{_password};{(_isHiddenSsid ? "H:true" : string.Empty)};";

/// <summary>
/// Specifies the authentication mode for the WiFi network.
Expand Down
4 changes: 1 addition & 3 deletions QRCoder/PdfByteQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public PdfByteQRCode(QRCodeData data) : base(data) { }
/// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param>
/// <returns>Returns the QR code graphic as a PDF byte array.</returns>
public byte[] GetGraphic(int pixelsPerModule)
{
return GetGraphic(pixelsPerModule, "#000000", "#ffffff");
}
=> GetGraphic(pixelsPerModule, "#000000", "#ffffff");

/// <summary>
/// Converts a hexadecimal color string to a byte array.
Expand Down
13 changes: 3 additions & 10 deletions QRCoder/PostscriptQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ public string GetGraphic(int pointsPerModule, string darkColorHex, string lightC
/// <param name="epsFormat">Indicates if the output should be in EPS format.</param>
/// <returns>Returns the QR code graphic as a PostScript string.</returns>
public string GetGraphic(Size viewBox, bool drawQuietZones = true, bool epsFormat = false)
{
return GetGraphic(viewBox, Color.Black, Color.White, drawQuietZones, epsFormat);
}
=> GetGraphic(viewBox, Color.Black, Color.White, drawQuietZones, epsFormat);

/// <summary>
/// Creates a colored PostScript code representation of the QR code.
Expand All @@ -87,9 +85,7 @@ public string GetGraphic(Size viewBox, bool drawQuietZones = true, bool epsForma
/// <param name="epsFormat">Indicates if the output should be in EPS format.</param>
/// <returns>Returns the QR code graphic as a PostScript string.</returns>
public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex, bool drawQuietZones = true, bool epsFormat = false)
{
return GetGraphic(viewBox, ColorTranslator.FromHtml(darkColorHex), ColorTranslator.FromHtml(lightColorHex), drawQuietZones, epsFormat);
}
=> GetGraphic(viewBox, ColorTranslator.FromHtml(darkColorHex), ColorTranslator.FromHtml(lightColorHex), drawQuietZones, epsFormat);

/// <summary>
/// Creates a colored PostScript code representation of the QR code.
Expand Down Expand Up @@ -134,10 +130,7 @@ public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool d
/// </summary>
/// <param name="input">The input double value.</param>
/// <returns>Returns the cleaned string representation of the double value.</returns>
private string CleanSvgVal(double input)
{
return input.ToString(System.Globalization.CultureInfo.InvariantCulture);
}
private string CleanSvgVal(double input) => input.ToString(System.Globalization.CultureInfo.InvariantCulture);

private const string PS_HEADER = @"%!PS-Adobe-3.0 {3}
%%Creator: QRCoder.NET
Expand Down
8 changes: 2 additions & 6 deletions QRCoder/QRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public QRCode(QRCodeData data) : base(data) { }
/// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param>
/// <returns>Returns the QR code graphic as a bitmap.</returns>
public Bitmap GetGraphic(int pixelsPerModule)
{
return GetGraphic(pixelsPerModule, Color.Black, Color.White, true);
}
=> GetGraphic(pixelsPerModule, Color.Black, Color.White, true);

/// <summary>
/// Creates a colored bitmap image of the QR code.
Expand All @@ -45,9 +43,7 @@ public Bitmap GetGraphic(int pixelsPerModule)
/// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
/// <returns>Returns the QR code graphic as a bitmap.</returns>
public Bitmap GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex, bool drawQuietZones = true)
{
return GetGraphic(pixelsPerModule, ColorTranslator.FromHtml(darkColorHtmlHex), ColorTranslator.FromHtml(lightColorHtmlHex), drawQuietZones);
}
=> GetGraphic(pixelsPerModule, ColorTranslator.FromHtml(darkColorHtmlHex), ColorTranslator.FromHtml(lightColorHtmlHex), drawQuietZones);

/// <summary>
/// Creates a colored bitmap image of the QR code.
Expand Down
8 changes: 2 additions & 6 deletions QRCoder/QRCodeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,7 @@ public byte[] GetRawData(Compression compressMode)
/// <param name="filePath">The path to the file where the raw data will be saved.</param>
/// <param name="compressMode">The compression mode used for the raw data.</param>
public void SaveRawData(string filePath, Compression compressMode)
{
File.WriteAllBytes(filePath, GetRawData(compressMode));
}
=> File.WriteAllBytes(filePath, GetRawData(compressMode));

/// <summary>
/// Gets the version of the QR code.
Expand All @@ -199,9 +197,7 @@ public void SaveRawData(string filePath, Compression compressMode)
/// <param name="version">The version of the QR code.</param>
/// <returns>Returns the number of modules per side.</returns>
private static int ModulesPerSideFromVersion(int version)
{
return 21 + (version - 1) * 4;
}
=> 21 + (version - 1) * 4;

/// <summary>
/// Releases all resources used by the <see cref="QRCodeData"/>.
Expand Down
12 changes: 3 additions & 9 deletions QRCoder/QRCodeGenerator/ModulePlacer.BlockedModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public BlockedModules(int size)
/// <param name="x">The x-coordinate of the module.</param>
/// <param name="y">The y-coordinate of the module.</param>
public void Add(int x, int y)
{
_blockedModules[y][x] = true;
}
=> _blockedModules[y][x] = true;

/// <summary>
/// Adds a blocked module defined by the specified rectangle.
Expand All @@ -69,9 +67,7 @@ public void Add(Rectangle rect)
/// <param name="y">The y-coordinate to check.</param>
/// <returns><c>true</c> if the coordinates are blocked; otherwise, <c>false</c>.</returns>
public bool IsBlocked(int x, int y)
{
return _blockedModules[y][x];
}
=> _blockedModules[y][x];

/// <summary>
/// Checks if the specified rectangle is blocked.
Expand All @@ -92,9 +88,7 @@ public bool IsBlocked(Rectangle r1)
}

public void Dispose()
{
Interlocked.CompareExchange(ref _staticBlockedModules, _blockedModules, null);
}
=> Interlocked.CompareExchange(ref _staticBlockedModules, _blockedModules, null);
}
}
}
4 changes: 1 addition & 3 deletions QRCoder/QRCodeGenerator/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public Point(int x, int y)
/// fall back to reflection, which causes heap allocations internally during the calls to <see cref="FieldInfo.GetValue(object)"/>.
/// </remarks>
public bool Equals(Point other)
{
return X == other.X && Y == other.Y;
}
=> X == other.X && Y == other.Y;
}
}
4 changes: 1 addition & 3 deletions QRCoderDemo/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ private void Form1_Load(object sender, EventArgs e)
}

private void buttonGenerate_Click(object sender, EventArgs e)
{
RenderQrCode();
}
=> RenderQrCode();

private void RenderQrCode()
{
Expand Down
11 changes: 4 additions & 7 deletions QRCoderTests/Helpers/HelperFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,15 @@ public static Bitmap BitmapSourceToBitmap(DrawingImage xamlImg)
#endif

public static string GetAssemblyPath()
{
return
#if NET5_0_OR_GREATER
AppDomain.CurrentDomain.BaseDirectory;
=> AppDomain.CurrentDomain.BaseDirectory;
#elif NETFRAMEWORK
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", "");
=> Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", "");
#elif NETCOREAPP1_1
Path.GetDirectoryName(typeof(HelperFunctions).GetTypeInfo().Assembly.Location).Replace("file:\\", "");
=> Path.GetDirectoryName(typeof(HelperFunctions).GetTypeInfo().Assembly.Location).Replace("file:\\", "");
#else
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).Replace("file:\\", "");
=> Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).Replace("file:\\", "");
#endif
}


#if !NETCOREAPP1_1
Expand Down

0 comments on commit ee8d49a

Please sign in to comment.