Skip to content

Commit f27b95f

Browse files
committed
Move photo bound parsing to separate helper class
1 parent 867a89a commit f27b95f

3 files changed

Lines changed: 39 additions & 34 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Globalization;
2+
3+
namespace Refresh.Database.Helpers;
4+
5+
public abstract class PhotoHelper
6+
{
7+
public const byte SubjectBoundCount = 4;
8+
9+
public static void ParseBoundsList(ReadOnlySpan<char> input, float[] floats)
10+
{
11+
byte start = 0;
12+
byte floatIndex = 0;
13+
14+
for (byte i = 0; i < input.Length; i++)
15+
{
16+
if(floatIndex == SubjectBoundCount - 1) break; // won't catch the last float - not worth reading
17+
if (input[i] != ',') continue;
18+
19+
if (!float.TryParse(input.Slice(start, i - start), NumberFormatInfo.InvariantInfo, out float f))
20+
throw new FormatException("Invalid format");
21+
22+
floats[floatIndex] = f;
23+
floatIndex++;
24+
start = (byte)(i + 1);
25+
}
26+
27+
// parse the last float value - bounds does not end with comma
28+
if (!float.TryParse(input[start..], NumberFormatInfo.InvariantInfo, out float lastFloat))
29+
throw new FormatException("Invalid format");
30+
31+
floats[SubjectBoundCount - 1] = lastFloat;
32+
}
33+
}
Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Globalization;
21
using System.Xml.Serialization;
32

43
namespace Refresh.Database.Models.Photos;
@@ -17,31 +16,4 @@ public class SerializedPhotoSubject
1716

1817
[XmlElement("bounds")]
1918
public string BoundsList { get; set; }
20-
21-
public const byte FloatCount = 4;
22-
23-
public static void ParseBoundsList(ReadOnlySpan<char> input, float[] floats)
24-
{
25-
byte start = 0;
26-
byte floatIndex = 0;
27-
28-
for (byte i = 0; i < input.Length; i++)
29-
{
30-
if(floatIndex == FloatCount - 1) break; // won't catch the last float - not worth reading
31-
if (input[i] != ',') continue;
32-
33-
if (!float.TryParse(input.Slice(start, i - start), NumberFormatInfo.InvariantInfo, out float f))
34-
throw new FormatException("Invalid format");
35-
36-
floats[floatIndex] = f;
37-
floatIndex++;
38-
start = (byte)(i + 1);
39-
}
40-
41-
// parse the last float value - bounds does not end with comma
42-
if (!float.TryParse(input[start..], NumberFormatInfo.InvariantInfo, out float lastFloat))
43-
throw new FormatException("Invalid format");
44-
45-
floats[FloatCount - 1] = lastFloat;
46-
}
4719
}

RefreshTests.GameServer/Tests/Parsing/PhotoBoundsTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Refresh.Database.Models.Photos;
1+
using Refresh.Database.Helpers;
22

33
namespace RefreshTests.GameServer.Tests.Parsing;
44

@@ -8,9 +8,9 @@ public class PhotoBoundsTests
88
public void ParsesBounds()
99
{
1010
ReadOnlySpan<char> bounds = "0.652369,0.710704,0.838377,0.997791";
11-
float[] floats = new float[SerializedPhotoSubject.FloatCount];
11+
float[] floats = new float[PhotoHelper.SubjectBoundCount];
1212

13-
SerializedPhotoSubject.ParseBoundsList(bounds, floats);
13+
PhotoHelper.ParseBoundsList(bounds, floats);
1414

1515
#pragma warning disable NUnit2045
1616
Assert.That(floats[0], Is.EqualTo(0.652369f));
@@ -23,14 +23,14 @@ public void ParsesBounds()
2323
[Test]
2424
public void CatchesInvalidFormat()
2525
{
26-
float[] floats = new float[SerializedPhotoSubject.FloatCount];
26+
float[] floats = new float[PhotoHelper.SubjectBoundCount];
2727
const string bounds1 = "0.652369,0.71a0704,0.838377,0.997791";
2828
const string bounds2 = "0.652369,0.710704,0.838377,0.9977a91";
2929

3030
Assert.Multiple(() =>
3131
{
32-
Assert.That(() => SerializedPhotoSubject.ParseBoundsList(bounds1, floats), Throws.TypeOf<FormatException>());
33-
Assert.That(() => SerializedPhotoSubject.ParseBoundsList(bounds2, floats), Throws.TypeOf<FormatException>());
32+
Assert.That(() => PhotoHelper.ParseBoundsList(bounds1, floats), Throws.TypeOf<FormatException>());
33+
Assert.That(() => PhotoHelper.ParseBoundsList(bounds2, floats), Throws.TypeOf<FormatException>());
3434
});
3535
}
3636
}

0 commit comments

Comments
 (0)