Skip to content

Commit

Permalink
C# 12 goodies
Browse files Browse the repository at this point in the history
  • Loading branch information
kleisauke committed Sep 12, 2024
1 parent 5146ecb commit 8e06cf2
Show file tree
Hide file tree
Showing 32 changed files with 289 additions and 314 deletions.
6 changes: 3 additions & 3 deletions build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ partial class Build : NukeBuild

string VipsVersion => Environment.GetEnvironmentVariable("VIPS_VERSION");

string[] NuGetArchitectures => new[]
{
string[] NuGetArchitectures =>
[
"win-x64",
"win-x86",
"win-arm64",
Expand All @@ -50,7 +50,7 @@ partial class Build : NukeBuild
"linux-arm64",
"osx-x64",
"osx-arm64"
};
];

protected override void OnBuildInitialized()
{
Expand Down
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<CLSCompliant>false</CLSCompliant>
<ComVisible>false</ComVisible>

<LangVersion>11</LangVersion>
<LangVersion>12</LangVersion>

<Major>2</Major>
<Minor>4</Minor>
Expand Down
14 changes: 8 additions & 6 deletions samples/NetVips.Samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ namespace NetVips;

internal class Program
{
private static readonly List<ISample> Samples = Assembly.GetExecutingAssembly().GetTypes()
.Where(x => x.GetInterfaces().Contains(typeof(ISample)) && x.GetConstructor(Type.EmptyTypes) != null)
.Select(x => Activator.CreateInstance(x) as ISample)
.OrderBy(s => s?.Category)
.ToList();
private static readonly List<ISample> Samples =
[
.. Assembly.GetExecutingAssembly().GetTypes()
.Where(x => x.GetInterfaces().Contains(typeof(ISample)) && x.GetConstructor(Type.EmptyTypes) != null)
.Select(x => Activator.CreateInstance(x) as ISample)
.OrderBy(s => s?.Category)
];

private static void Main(string[] args)
{
Expand Down Expand Up @@ -60,7 +62,7 @@ private static void Main(string[] args)
}

// Clear any arguments
args = Array.Empty<string>();
args = [];
} while (!string.IsNullOrEmpty(input) && !string.Equals(input, "Q", StringComparison.OrdinalIgnoreCase));
}

Expand Down
2 changes: 1 addition & 1 deletion samples/NetVips.Samples/SampleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal static string FirstLetterToLower(this string str)
/// <returns>A new camel cased string.</returns>
internal static string ToPascalCase(this string str)
{
return str.Split(new[] { "_" }, StringSplitOptions.RemoveEmptyEntries)
return str.Split(["_"], StringSplitOptions.RemoveEmptyEntries)
.Select(s => char.ToUpperInvariant(s[0]) + s[1..])
.Aggregate(string.Empty, (s1, s2) => s1 + s2);
}
Expand Down
2 changes: 1 addition & 1 deletion samples/NetVips.Samples/Samples/CaptchaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void Execute(string[] args)
}

// remove any unused edges
var trim = textLayer.FindTrim(background: new double[] { 0 });
var trim = textLayer.FindTrim(background: [0]);
using (textLayer)
{
textLayer = textLayer.Crop((int)trim[0], (int)trim[1], (int)trim[2], (int)trim[3]);
Expand Down
4 changes: 2 additions & 2 deletions samples/NetVips.Samples/Samples/Duotone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class Duotone : ISample
public const string Filename = "images/equus_quagga.jpg";

// #C83658 as CIELAB triple
public double[] Start = { 46.479, 58.976, 15.052 };
public double[] Start = [46.479, 58.976, 15.052];

// #D8E74F as CIELAB triple
public double[] Stop = { 88.12, -23.952, 69.178 };
public double[] Stop = [88.12, -23.952, 69.178];

public void Execute(string[] args)
{
Expand Down
6 changes: 3 additions & 3 deletions samples/NetVips.Samples/Samples/GenerateImageClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ private string GenerateFunction(string operationName, string indent = "
}

string[] reservedKeywords =
{
[
"in", "ref", "out", "ushort"
};
];

string SafeIdentifier(string name) =>
reservedKeywords.Contains(name)
Expand Down Expand Up @@ -739,7 +739,7 @@ string ToCref(string name) =>
if (optionalOutput.Length > 0 && outParameters == null)
{
result.AppendLine()
.Append(GenerateFunction(operationName, indent, mutable, new[] { optionalOutput[0] }));
.Append(GenerateFunction(operationName, indent, mutable, [optionalOutput[0]]));
}
else if (outParameters != null && outParameters.Count != optionalOutput.Length)
{
Expand Down
32 changes: 16 additions & 16 deletions samples/NetVips.Samples/Samples/GenerateImageOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,22 +267,22 @@ public string GenerateOperators()
// generate list of all operator overloads and supported types
var allOverloads = new Dictionary<string, string[]>
{
{"+", new[] {"Image", "double", "double[]", "int[]"}},
{"-", new[] {"Image", "double", "double[]", "int[]"}},
{"*", new[] {"Image", "double", "double[]", "int[]"}},
{"/", new[] {"Image", "double", "double[]", "int[]"}},
{"%", new[] {"Image", "double", "double[]", "int[]"}},
{"&", new[] {"Image", "double", "double[]", "int[]"}},
{"|", new[] {"Image", "double", "double[]", "int[]"}},
{"^", new[] {"Image", "double", "double[]", "int[]"}},
{"<<", new[] {"int"}},
{">>", new[] {"int"}},
{"==", new[] {"double", "double[]", "int[]"}},
{"!=", new[] {"double", "double[]", "int[]"}},
{"<", new[] {"Image", "double", "double[]", "int[]"}},
{">", new[] {"Image", "double", "double[]", "int[]"}},
{"<=", new[] {"Image", "double", "double[]", "int[]"}},
{">=", new[] {"Image", "double", "double[]", "int[]"}}
{"+", ["Image", "double", "double[]", "int[]"]},
{"-", ["Image", "double", "double[]", "int[]"]},
{"*", ["Image", "double", "double[]", "int[]"]},
{"/", ["Image", "double", "double[]", "int[]"]},
{"%", ["Image", "double", "double[]", "int[]"]},
{"&", ["Image", "double", "double[]", "int[]"]},
{"|", ["Image", "double", "double[]", "int[]"]},
{"^", ["Image", "double", "double[]", "int[]"]},
{"<<", ["int"]},
{">>", ["int"]},
{"==", ["double", "double[]", "int[]"]},
{"!=", ["double", "double[]", "int[]"]},
{"<", ["Image", "double", "double[]", "int[]"]},
{">", ["Image", "double", "double[]", "int[]"]},
{"<=", ["Image", "double", "double[]", "int[]"]},
{">=", ["Image", "double", "double[]", "int[]"]}
};

const string preamble = """
Expand Down
2 changes: 1 addition & 1 deletion samples/NetVips.Samples/Samples/Indexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Indexer : ISample

public void Execute(string[] args)
{
using var r = Image.NewFromArray(new[] { 1, 2, 3 });
using var r = Image.NewFromArray([1, 2, 3]);
using var g = r + 1;
using var b = r + 2;
using var image = r.Bandjoin(g, b);
Expand Down
2 changes: 1 addition & 1 deletion samples/NetVips.Samples/Samples/MutableImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Execute(string[] args)
for (var i = 0; i <= 100; i++)
{
var j = i / 100.0;
x.DrawLine(new[] { 255.0 }, (int)(x.Width * j), 0, 0, (int)(x.Height * (1 - j)));
x.DrawLine([255.0], (int)(x.Width * j), 0, 0, (int)(x.Height * (1 - j)));
}
});
mutated.WriteToFile("mutated.jpg");
Expand Down
56 changes: 25 additions & 31 deletions samples/NetVips.Samples/Samples/OnePointMosaic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@ public class OnePointMosaic : ISample
public string Name => "1 Point Mosaic";
public string Category => "Mosaicing";

public struct Point
public struct Point(int x, int y)
{
public int X, Y;

public Point(int x, int y)
{
X = x;
Y = y;
}
public int X = x, Y = y;
}

public List<string> Images = new()
{
public List<string> Images =
[
"images/cd1.1.jpg",
"images/cd1.2.jpg",
"images/cd2.1.jpg",
Expand All @@ -32,29 +26,29 @@ public Point(int x, int y)
"images/cd3.2.jpg",
"images/cd4.1.jpg",
"images/cd4.2.jpg"
};
];

public List<Point> HorizontalMarks = new()
{
new Point(489, 140),
new Point(66, 141),
new Point(453, 40),
new Point(15, 43),
new Point(500, 122),
new Point(65, 121),
new Point(495, 58),
new Point(40, 57)
};
public List<Point> HorizontalMarks =
[
new(489, 140),
new(66, 141),
new(453, 40),
new(15, 43),
new(500, 122),
new(65, 121),
new(495, 58),
new(40, 57)
];

public List<Point> VerticalMarks = new()
{
new Point(364, 346),
new Point(388, 44),
new Point(385, 629),
new Point(384, 17),
new Point(503, 959),
new Point(527, 42)
};
public List<Point> VerticalMarks =
[
new(364, 346),
new(388, 44),
new(385, 629),
new(384, 17),
new(503, 959),
new(527, 42)
];

public void Execute(string[] args)
{
Expand Down
2 changes: 1 addition & 1 deletion samples/NetVips.Samples/Samples/ShapeCropping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public int[] ResolveShapeTrim(int width, int height, int maskWidth, int maskHeig
var left = (int)Math.Round((width - trimWidth) / 2);
var top = (int)Math.Round((height - trimHeight) / 2);

return new[] { left, top, (int)Math.Round(trimWidth), (int)Math.Round(trimHeight) };
return [left, top, (int)Math.Round(trimWidth), (int)Math.Round(trimHeight)];
}

#region helpers
Expand Down
4 changes: 2 additions & 2 deletions src/NetVips/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal static object[] PrependImage<T>(this T[] args, Image image)
{
if (args == null)
{
return new object[] { image };
return [image];
}

var newValues = new object[args.Length + 1];
Expand Down Expand Up @@ -152,7 +152,7 @@ internal static string ToUtf8String(this nint utf8Str, bool freePtr = false, int
/// <returns>The readable format of the bytes.</returns>
internal static string ToReadableBytes(this ulong value)
{
string[] sizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
string[] sizeSuffixes = ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];

var i = 0;
decimal dValue = value;
Expand Down
2 changes: 1 addition & 1 deletion src/NetVips/GObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class GObject : SafeHandle
/// <remarks>
/// All recorded delegates are freed in <see cref="ReleaseDelegates"/>.
/// </remarks>
private readonly ICollection<GCHandle> _handles = new List<GCHandle>();
private readonly ICollection<GCHandle> _handles = [];

/// <summary>
/// Hint of how much native memory is actually occupied by the object.
Expand Down
26 changes: 13 additions & 13 deletions src/NetVips/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,7 @@ public string[] GetFields()

GLib.GFree(ptrArr);

return names.ToArray();
return [.. names];
}

/// <summary>
Expand Down Expand Up @@ -1378,7 +1378,7 @@ public Image Case(params int[] ints) =>
/// <param name="images">Array of case images.</param>
/// <returns>A new <see cref="Image"/>.</returns>
public Image Case(params Image[] images) =>
this.Call("case", new object[] { images }) as Image;
this.Call("case", [images]) as Image;

/// <summary>
/// Use pixel values to pick cases from an a set of mixed images and constants.
Expand All @@ -1391,7 +1391,7 @@ public Image Case(params Image[] images) =>
/// <param name="objects">Array of mixed images and constants.</param>
/// <returns>A new <see cref="Image"/>.</returns>
public Image Case(params object[] objects) =>
this.Call("case", new object[] { objects }) as Image;
this.Call("case", [objects]) as Image;

/// <summary>
/// Append a set of constants bandwise.
Expand Down Expand Up @@ -1430,7 +1430,7 @@ public Image Bandjoin(params int[] ints) =>
/// <param name="images">Array of images.</param>
/// <returns>A new <see cref="Image"/>.</returns>
public Image Bandjoin(params Image[] images) =>
this.Call("bandjoin", new object[] { images.PrependImage(this) }) as Image;
this.Call("bandjoin", [images.PrependImage(this)]) as Image;

/// <summary>
/// Append a set of mixed images and constants bandwise.
Expand All @@ -1443,7 +1443,7 @@ public Image Bandjoin(params Image[] images) =>
/// <param name="objects">Array of mixed images and constants.</param>
/// <returns>A new <see cref="Image"/>.</returns>
public Image Bandjoin(params object[] objects) =>
this.Call("bandjoin", new object[] { objects.PrependImage(this) }) as Image;
this.Call("bandjoin", [objects.PrependImage(this)]) as Image;

/// <summary>
/// Band-wise rank a set of constants.
Expand Down Expand Up @@ -1496,7 +1496,7 @@ public Image Bandrank(Image[] images, int? index = null)

options.AddIfPresent(nameof(index), index);

return this.Call("bandrank", options, new object[] { images.PrependImage(this) }) as Image;
return this.Call("bandrank", options, [images.PrependImage(this)]) as Image;
}

/// <summary>
Expand All @@ -1511,7 +1511,7 @@ public Image Bandrank(Image[] images, int? index = null)
/// <param name="index">Select this band element from sorted list.</param>
/// <returns>A new <see cref="Image"/>.</returns>
public Image Bandrank(Image other, int? index = null) =>
Bandrank(new[] { other }, index);
Bandrank([other], index);

/// <summary>
/// Band-wise rank a set of mixed images and constants.
Expand All @@ -1530,7 +1530,7 @@ public Image Bandrank(object[] objects, int? index = null)

options.AddIfPresent(nameof(index), index);

return this.Call("bandrank", options, new object[] { objects.PrependImage(this) }) as Image;
return this.Call("bandrank", options, [objects.PrependImage(this)]) as Image;
}

/// <summary>
Expand Down Expand Up @@ -1603,7 +1603,7 @@ public Image Crop(int left, int top, int width, int height) =>
public double[] MaxPos()
{
var v = Max(out var x, out var y);
return new[] { v, x, y };
return [v, x, y];
}

/// <summary>
Expand All @@ -1613,7 +1613,7 @@ public double[] MaxPos()
public double[] MinPos()
{
var v = Min(out var x, out var y);
return new[] { v, x, y };
return [v, x, y];
}

/// <summary>
Expand Down Expand Up @@ -1754,7 +1754,7 @@ public double[] MinPos()
/// </summary>
/// <param name="exp">To the power of this.</param>
/// <returns>A new <see cref="Image"/>.</returns>
public Image Pow(double exp) => Math2Const(Enums.OperationMath2.Pow, new[] { exp });
public Image Pow(double exp) => Math2Const(Enums.OperationMath2.Pow, [exp]);

/// <summary>
/// Raise to power of an array.
Expand Down Expand Up @@ -1783,7 +1783,7 @@ public Image Pow(int[] exp) =>
/// </summary>
/// <param name="base">To the base of this.</param>
/// <returns>A new <see cref="Image"/>.</returns>
public Image Wop(double @base) => Math2Const(Enums.OperationMath2.Wop, new[] { @base });
public Image Wop(double @base) => Math2Const(Enums.OperationMath2.Wop, [@base]);

/// <summary>
/// Raise to power of an array, but with the arguments reversed.
Expand Down Expand Up @@ -1812,7 +1812,7 @@ public Image Wop(int[] @base) =>
/// </summary>
/// <param name="x">Arc tangent of y / <paramref name="x"/>.</param>
/// <returns>A new <see cref="Image"/>.</returns>
public Image Atan2(double x) => Math2Const(Enums.OperationMath2.Atan2, new[] { x });
public Image Atan2(double x) => Math2Const(Enums.OperationMath2.Atan2, [x]);

/// <summary>
/// Arc tangent of an array in degrees.
Expand Down
Loading

0 comments on commit 8e06cf2

Please sign in to comment.