Skip to content

Commit

Permalink
Use TryParse over exception-wrapped Parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Emik03 committed May 9, 2024
1 parent eb13497 commit 6537654
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 33 deletions.
49 changes: 49 additions & 0 deletions Quaver.API/Helpers/Drain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: MPL-2.0
using System;

namespace Quaver.API.Helpers
{
/// <summary>
/// Represents a sequence of items separated by a separator.
/// </summary>
/// <typeparam name="T">The type of items.</typeparam>
public readonly ref struct Drain<T>
where T : IEquatable<T>
{
readonly ReadOnlySpan<T> _remaining;

readonly T _separator;

/// <summary>
/// Creates a new <see cref="Drain{T}"/> instance.
/// </summary>
/// <param name="remaining">The remaining items.</param>
/// <param name="separator">The separator.</param>
public Drain(ReadOnlySpan<T> remaining, T separator)
{
_remaining = remaining;
_separator = separator;
}

/// <summary>
/// Collects the next <see cref="ReadOnlySpan{T}"/>,
/// while also initializing the next <see cref="Drain{T}"/>.
/// </summary>
/// <param name="next">The next <see cref="ReadOnlySpan{T}"/>.</param>
/// <param name="rest">The rest as <see cref="Drain{T}"/>.</param>
public void Deconstruct(out ReadOnlySpan<T> next, out Drain<T> rest)
{
var i = _remaining.IndexOf(_separator);

if (i is -1)
{
next = _remaining;
rest = default;
return;
}

next = _remaining[..i];
rest = new Drain<T>(_remaining[(i + 1)..], _separator);
}
}
}
25 changes: 8 additions & 17 deletions Quaver.API/Maps/Structures/EditorLayerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Drawing;
using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Interop;
using YamlDotNet.Serialization;
using Quaver.API.Helpers;

namespace Quaver.API.Maps.Structures
{
Expand Down Expand Up @@ -31,22 +31,13 @@ public class EditorLayerInfo
/// </summary>
/// <returns></returns>
[MoonSharpVisible(false)]
public Color GetColor()
{
if (ColorRgb == null)
return Color.White;

var split = ColorRgb.Split(',');

try
{
return Color.FromArgb(byte.Parse(split[0]), byte.Parse(split[1]), byte.Parse(split[2]));
}
catch (Exception)
{
return Color.White;
}
}
public Color GetColor() =>
new Drain<char>(ColorRgb, ',') is var (tr, (tg, (tb, _))) &&
byte.TryParse(tr, out var r) &&
byte.TryParse(tg, out var g) &&
byte.TryParse(tb, out var b)
? Color.FromArgb(r, g, b)
: Color.White;

/// <summary>
/// By-value comparer, auto-generated by Rider.
Expand Down
24 changes: 8 additions & 16 deletions Quaver.API/Replays/Replay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,22 +239,14 @@ public Replay(string path, bool readHeaderless = false)

// Add all the replay frames to the object
foreach (var frame in frames)
try
{
// Split up the frame string by SongTime|KeyPressState
var frameSplit = frame.Split('|');

Frames.Add(
new ReplayFrame(
int.Parse(frameSplit[0]),
(ReplayKeyPressState)Enum.Parse(typeof(ReplayKeyPressState), frameSplit[1])
)
);
}
catch (Exception)
{
// ignored
}
{
// Split up the frame string by SongTime|KeyPressState
var (tTime, (tKeys, _)) = new Drain<char>(frame, '|');

if (int.TryParse(tTime, out var time) &&
Enum.TryParse(tKeys.ToString(), out ReplayKeyPressState keys))
Frames.Add(new ReplayFrame(time, keys));
}
}
}

Expand Down

0 comments on commit 6537654

Please sign in to comment.