Skip to content

Commit

Permalink
v1.5.2 Parseers for Point and Point3d
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Dec 22, 2023
1 parent 51abf5d commit 24dcc67
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/Smab.Helpers/GridHelpers/Point.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Smab.Helpers;
using System.Diagnostics.CodeAnalysis;

public record struct Point(int X, int Y) {
namespace Smab.Helpers;

public record struct Point(int X, int Y) : IParsable<Point> {

public Point((int X, int Y) point) : this(point.X, point.Y) { }

Expand Down Expand Up @@ -78,5 +80,14 @@ public IEnumerable<Point> AllAdjacent() {
];

//public override string ToString() => $"({X}, {Y})";
public static Point Parse(string s, IFormatProvider? provider) {
char[] splitBy = [',', '(', ')'];
string[] tokens = s.TrimmedSplit(splitBy);
return new(tokens[0].As<int>(), tokens[1].As<int>());
}

public static Point Parse(string s) => Parse(s, null);
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out Point result)
=> ISimpleParsable<Point>.TryParse(s, provider, out result);

}
16 changes: 14 additions & 2 deletions src/Smab.Helpers/GridHelpers/Point3d.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Smab.Helpers;
using System.Diagnostics.CodeAnalysis;

public record struct Point3d(int X, int Y, int Z) {
namespace Smab.Helpers;

public record struct Point3d(int X, int Y, int Z) : IParsable<Point3d> {

public Point3d((int X, int Y, int Z) point) : this(point.X, point.Y, point.Z) { }

Expand Down Expand Up @@ -75,4 +77,14 @@ public IEnumerable<Point3d> AllAdjacent() {

//public override string ToString() => $"({X}, {Y}, {Z})";

public static Point3d Parse(string s, IFormatProvider? provider) {
char[] splitBy = [',', '(', ')'];
string[] tokens = s.TrimmedSplit(splitBy);
return new(tokens[0].As<int>(), tokens[1].As<int>(), tokens[2].As<int>());
}

public static Point3d Parse(string s) => Parse(s, null);
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out Point3d result)
=> ISimpleParsable<Point3d>.TryParse(s, provider, out result);

}
2 changes: 1 addition & 1 deletion src/Smab.Helpers/Smab.Helpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<PackageReleaseNotes>.NET 8.0</PackageReleaseNotes>
<VersionPrefix>1.5.1</VersionPrefix>
<VersionPrefix>1.5.2</VersionPrefix>
<Preview></Preview>
<VersionSuffix Condition="'$(Preview)' != '' And '$(BUILD_BUILDNUMBER)' == ''">$(Preview).$([System.DateTime]::get_Now().get_Year())$([System.DateTime]::get_Now().get_Month().ToString("D2"))$([System.DateTime]::get_Now().get_Day().ToString("D2"))-$([System.DateTime]::get_Now().get_Hour().ToString("D2"))$([System.DateTime]::get_Now().get_Minute().ToString("D2"))</VersionSuffix>
<VersionSuffix Condition="'$(Preview)' != '' And '$(BUILD_BUILDNUMBER)' != ''">$(Preview).$(BUILD_BUILDNUMBER)</VersionSuffix>
Expand Down
11 changes: 11 additions & 0 deletions test/Smab.Helpers.Test/HelperMethodTests/Point3dTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
namespace Smab.Helpers.Test.HelperMethodTests;

public class Point3dTests {
[Theory]
[InlineData("0,0,0", 0, 0, 0)]
[InlineData("(8,9,10)", 8, 9, 10)]
[InlineData(" 8 , -6, 3 ", 8, -6, 3)]
public void Point3d_Parses(string parseString, int p1x, int p1y, int p1z) {
Point3d actual = Point3d.Parse(parseString);
actual.X.ShouldBe(p1x);
actual.Y.ShouldBe(p1y);
actual.Z.ShouldBe(p1z);
}

[Theory]
[InlineData(0, 0, 0, 1, 1, 1, 1, 1, 1)]
[InlineData(8, 9, 3, 12, 4, 7, 20, 13, 10)]
Expand Down
10 changes: 10 additions & 0 deletions test/Smab.Helpers.Test/HelperMethodTests/PointTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
namespace Smab.Helpers.Test.HelperMethodTests;

public class PointTests {
[Theory]
[InlineData("0,0", 0, 0)]
[InlineData("(8,9)", 8, 9)]
[InlineData(" 8 , -6 ", 8, -6)]
public void Point_Parses(string parseString, int p1x, int p1y) {
Point actual = Point.Parse(parseString);
actual.X.ShouldBe(p1x);
actual.Y.ShouldBe(p1y);
}

[Theory]
[InlineData(0, 0, 1, 1, 1, 1)]
[InlineData(8, 9, 12, 4, 20, 13)]
Expand Down

0 comments on commit 24dcc67

Please sign in to comment.