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 d66816d commit 7698bf3
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 81 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,4 @@ dotnet_diagnostic.IDE0005.severity = warning
# Enforce formatting https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/formatting-rules#rule-id-ide0055-fix-formatting
dotnet_diagnostic.IDE0055.severity = error
dotnet_diagnostic.IDE1006.severity = error
dotnet_diagnostic.IDE0022.severity = error
12 changes: 3 additions & 9 deletions QRCoder.Xaml/XamlQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ public XamlQRCode() { }
public XamlQRCode(QRCodeData data) : base(data) { }

public DrawingImage GetGraphic(int pixelsPerModule)
{
return GetGraphic(pixelsPerModule, true);
}
=> GetGraphic(pixelsPerModule, true);

public DrawingImage GetGraphic(int pixelsPerModule, bool drawQuietZones)
{
Expand All @@ -27,9 +25,7 @@ public DrawingImage GetGraphic(int pixelsPerModule, bool drawQuietZones)
}

public DrawingImage GetGraphic(Size viewBox, bool drawQuietZones = true)
{
return GetGraphic(viewBox, new SolidColorBrush(Colors.Black), new SolidColorBrush(Colors.White), drawQuietZones);
}
=> GetGraphic(viewBox, new SolidColorBrush(Colors.Black), new SolidColorBrush(Colors.White), drawQuietZones);

public DrawingImage GetGraphic(int pixelsPerModule, string darkColorHex, string lightColorHex, bool drawQuietZones = true)
{
Expand Down Expand Up @@ -69,9 +65,7 @@ public DrawingImage GetGraphic(Size viewBox, Brush darkBrush, Brush lightBrush,
}

private int GetDrawableModulesCount(bool drawQuietZones = true)
{
return QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
}
=> QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);

public double GetUnitsPerModule(Size viewBox, bool drawQuietZones = true)
{
Expand Down
8 changes: 2 additions & 6 deletions QRCoder/PayloadGenerator/SwissQrCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,17 +389,13 @@ public Contact(string name, string country, string addressLine1, string addressL
/// Creates a contact with structured address.
/// </summary>
public static Contact WithStructuredAddress(string name, string zipCode, string city, string country, string? street = null, string? houseNumber = null)
{
return new Contact(name, zipCode, city, country, street, houseNumber, AddressType.StructuredAddress);
}
=> new Contact(name, zipCode, city, country, street, houseNumber, AddressType.StructuredAddress);

/// <summary>
/// Creates a contact with combined address.
/// </summary>
public static Contact WithCombinedAddress(string name, string country, string addressLine1, string addressLine2)
{
return new Contact(name, null, null, country, addressLine1, addressLine2, AddressType.CombinedAddress);
}
=> new Contact(name, null, null, country, addressLine1, addressLine2, AddressType.CombinedAddress);


private Contact(string name, string? zipCode, string? city, string country, string? streetOrAddressline1, string? houseNumberOrAddressline2, AddressType addressType)
Expand Down
32 changes: 8 additions & 24 deletions QRCoder/QRCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public QRCodeGenerator()
/// <exception cref="QRCoder.Exceptions.DataTooLongException">Thrown when the payload is too big to be encoded in a QR code.</exception>
/// <returns>Returns the raw QR code data which can be used for rendering.</returns>
public QRCodeData CreateQrCode(PayloadGenerator.Payload payload)
{
return GenerateQrCode(payload);
}
=> GenerateQrCode(payload);

/// <summary>
/// Calculates the QR code data which than can be used in one of the rendering classes to generate a graphical representation.
Expand All @@ -55,9 +53,7 @@ public QRCodeData CreateQrCode(PayloadGenerator.Payload payload)
/// <exception cref="QRCoder.Exceptions.DataTooLongException">Thrown when the payload is too big to be encoded in a QR code.</exception>
/// <returns>Returns the raw QR code data which can be used for rendering.</returns>
public QRCodeData CreateQrCode(PayloadGenerator.Payload payload, ECCLevel eccLevel)
{
return GenerateQrCode(payload, eccLevel);
}
=> GenerateQrCode(payload, eccLevel);

/// <summary>
/// Calculates the QR code data which than can be used in one of the rendering classes to generate a graphical representation.
Expand All @@ -71,9 +67,7 @@ public QRCodeData CreateQrCode(PayloadGenerator.Payload payload, ECCLevel eccLev
/// <exception cref="QRCoder.Exceptions.DataTooLongException">Thrown when the payload is too big to be encoded in a QR code.</exception>
/// <returns>Returns the raw QR code data which can be used for rendering.</returns>
public QRCodeData CreateQrCode(string plainText, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1)
{
return GenerateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion);
}
=> GenerateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion);

/// <summary>
/// Calculates the QR code data which than can be used in one of the rendering classes to generate a graphical representation.
Expand All @@ -83,9 +77,7 @@ public QRCodeData CreateQrCode(string plainText, ECCLevel eccLevel, bool forceUt
/// <exception cref="QRCoder.Exceptions.DataTooLongException">Thrown when the payload is too big to be encoded in a QR code.</exception>
/// <returns>Returns the raw QR code data which can be used for rendering.</returns>
public QRCodeData CreateQrCode(byte[] binaryData, ECCLevel eccLevel)
{
return GenerateQrCode(binaryData, eccLevel);
}
=> GenerateQrCode(binaryData, eccLevel);


/// <summary>
Expand All @@ -95,9 +87,7 @@ public QRCodeData CreateQrCode(byte[] binaryData, ECCLevel eccLevel)
/// <exception cref="QRCoder.Exceptions.DataTooLongException">Thrown when the payload is too big to be encoded in a QR code.</exception>
/// <returns>Returns the raw QR code data which can be used for rendering.</returns>
public static QRCodeData GenerateQrCode(PayloadGenerator.Payload payload)
{
return GenerateQrCode(payload.ToString(), payload.EccLevel, false, false, payload.EciMode, payload.Version);
}
=> GenerateQrCode(payload.ToString(), payload.EccLevel, false, false, payload.EciMode, payload.Version);

/// <summary>
/// Calculates the QR code data which than can be used in one of the rendering classes to generate a graphical representation.
Expand Down Expand Up @@ -635,9 +625,7 @@ private static EncodingMode GetEncodingFromPlaintext(string plainText, bool forc
/// Checks if a character falls within a specified range.
/// </summary>
private static bool IsInRange(char c, char min, char max)
{
return (uint)(c - min) <= (uint)(max - min);
}
=> (uint)(c - min) <= (uint)(max - min);

/// <summary>
/// Calculates the message polynomial from a bit array which represents the encoded data.
Expand Down Expand Up @@ -1178,9 +1166,7 @@ int[] GetNotUniqueExponents(Polynom list)
/// This is used in Reed-Solomon and other error correction calculations involving Galois fields.
/// </summary>
private static int GetIntValFromAlphaExp(int exp)
{
return _galoisFieldByExponentAlpha[exp];
}
=> _galoisFieldByExponentAlpha[exp];

/// <summary>
/// Retrieves the exponent from the Galois field that corresponds to a given integer value.
Expand All @@ -1200,9 +1186,7 @@ private static int GetAlphaExpFromIntVal(int intVal)
/// This is particularly necessary when performing multiplications in the field which can result in exponents exceeding the field's maximum.
/// </summary>
private static int ShrinkAlphaExp(int alphaExp)
{
return (int)((alphaExp % 256) + Math.Floor((double)(alphaExp / 256)));
}
=> (int)((alphaExp % 256) + Math.Floor((double)(alphaExp / 256)));

/// <summary>
/// Creates a dictionary mapping alphanumeric characters to their respective positions used in QR code encoding.
Expand Down
32 changes: 8 additions & 24 deletions QRCoder/QRCodeGenerator/ModulePlacer.MaskPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,72 +27,56 @@ private static class MaskPattern
/// Applies a checkerboard mask on the QR code.
/// </summary>
public static bool Pattern1(int x, int y)
{
return (x + y) % 2 == 0;
}
=> (x + y) % 2 == 0;

/// <summary>
/// Mask pattern 2: y % 2 == 0
/// Applies a horizontal striping mask on the QR code.
/// </summary>
public static bool Pattern2(int x, int y)
{
return y % 2 == 0;
}
=> y % 2 == 0;

/// <summary>
/// Mask pattern 3: x % 3 == 0
/// Applies a vertical striping mask on the QR code.
/// </summary>
public static bool Pattern3(int x, int y)
{
return x % 3 == 0;
}
=> x % 3 == 0;

/// <summary>
/// Mask pattern 4: (x + y) % 3 == 0
/// Applies a diagonal striping mask on the QR code.
/// </summary>
public static bool Pattern4(int x, int y)
{
return (x + y) % 3 == 0;
}
=> (x + y) % 3 == 0;

/// <summary>
/// Mask pattern 5: ((y / 2) + (x / 3)) % 2 == 0
/// Applies a complex pattern mask on the QR code, mixing horizontal and vertical rules.
/// </summary>
public static bool Pattern5(int x, int y)
{
return ((int)(Math.Floor(y / 2d) + Math.Floor(x / 3d)) % 2) == 0;
}
=> ((int)(Math.Floor(y / 2d) + Math.Floor(x / 3d)) % 2) == 0;

/// <summary>
/// Mask pattern 6: ((x * y) % 2 + (x * y) % 3) == 0
/// Applies a mask based on the product of x and y coordinates modulo 2 and 3.
/// </summary>
public static bool Pattern6(int x, int y)
{
return ((x * y) % 2) + ((x * y) % 3) == 0;
}
=> ((x * y) % 2) + ((x * y) % 3) == 0;

/// <summary>
/// Mask pattern 7: (((x * y) % 2 + (x * y) % 3) % 2) == 0
/// Applies a mask based on a more complex function involving the product of x and y coordinates.
/// </summary>
public static bool Pattern7(int x, int y)
{
return (((x * y) % 2) + ((x * y) % 3)) % 2 == 0;
}
=> (((x * y) % 2) + ((x * y) % 3)) % 2 == 0;

/// <summary>
/// Mask pattern 8: (((x + y) % 2) + ((x * y) % 3) % 2) == 0
/// Combines rules of checkers and complex multiplicative masks.
/// </summary>
public static bool Pattern8(int x, int y)
{
return (((x + y) % 2) + ((x * y) % 3)) % 2 == 0;
}
=> (((x + y) % 2) + ((x * y) % 3)) % 2 == 0;

/// <summary>
/// Calculates a penalty score for a QR code to evaluate the effectiveness of a mask pattern.
Expand Down
18 changes: 4 additions & 14 deletions QRCoder/QRCodeGenerator/Polynom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ public PolynomItem this[int index]
#if NET6_0_OR_GREATER
[StackTraceHidden]
#endif
private static void ThrowIndexOutOfRangeException()
{
throw new IndexOutOfRangeException();
}
private static void ThrowIndexOutOfRangeException() => throw new IndexOutOfRangeException();


/// <summary>
Expand All @@ -83,10 +80,7 @@ private static void ThrowIndexOutOfRangeException()
/// <summary>
/// Removes all polynomial terms from the polynomial.
/// </summary>
public void Clear()
{
Count = 0;
}
public void Clear() => Count = 0;

/// <summary>
/// Clones the polynomial, creating a new instance with the same polynomial terms.
Expand Down Expand Up @@ -207,17 +201,13 @@ private void AssertCapacity(int min)
/// Rents memory for the polynomial terms from the shared memory pool.
/// </summary>
private static PolynomItem[] RentArray(int count)
{
return System.Buffers.ArrayPool<PolynomItem>.Shared.Rent(count);
}
=> System.Buffers.ArrayPool<PolynomItem>.Shared.Rent(count);

/// <summary>
/// Returns memory allocated for the polynomial terms back to the shared memory pool.
/// </summary>
private static void ReturnArray(PolynomItem[] array)
{
System.Buffers.ArrayPool<PolynomItem>.Shared.Return(array);
}
=> System.Buffers.ArrayPool<PolynomItem>.Shared.Return(array);
#else
// Implement a poor-man's array pool for .NET Framework
[ThreadStatic]
Expand Down
2 changes: 1 addition & 1 deletion QRCoderDemoUWP/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace QRCoderDemoUWP;
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
internal sealed partial class App : Application
public sealed partial class App : Application
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
Expand Down
4 changes: 1 addition & 3 deletions QRCoderTests/Helpers/HelperFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,5 @@ public static string ByteArrayToHash(byte[] data)
}

public static string StringToHash(string data)
{
return ByteArrayToHash(Encoding.UTF8.GetBytes(data));
}
=> ByteArrayToHash(Encoding.UTF8.GetBytes(data));
}

0 comments on commit 7698bf3

Please sign in to comment.