2023-11-04 01:01:18 +00:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System ;
2023-11-20 12:27:24 +00:00
using System.Diagnostics.CodeAnalysis ;
2023-11-04 01:01:18 +00:00
using System.Text.RegularExpressions ;
2023-11-07 00:36:58 +00:00
namespace osu.Game.Rulesets.Edit
2023-11-04 01:01:18 +00:00
{
public static class EditorTimestampParser
{
2023-11-12 14:09:15 +00:00
// 00:00:000 (...) - test
// original osu-web regex: https://github.com/ppy/osu-web/blob/3b1698639244cfdaf0b41c68bfd651ea729ec2e3/resources/js/utils/beatmapset-discussion-helper.ts#L78
2023-11-20 12:27:24 +00:00
public static readonly Regex TIME_REGEX = new Regex ( @"\b(((?<minutes>\d{2,}):(?<seconds>[0-5]\d)[:.](?<milliseconds>\d{3}))(?<selection>\s\([^)]+\))?)" , RegexOptions . Compiled ) ;
2023-11-04 01:01:18 +00:00
2023-11-20 12:27:24 +00:00
public static bool TryParse ( string timestamp , [ NotNullWhen ( true ) ] out TimeSpan ? parsedTime , out string? parsedSelection )
2023-11-04 01:01:18 +00:00
{
2023-11-07 00:36:58 +00:00
Match match = TIME_REGEX . Match ( timestamp ) ;
2023-11-12 14:09:15 +00:00
2023-11-20 12:27:24 +00:00
if ( ! match . Success )
{
parsedTime = null ;
parsedSelection = null ;
return false ;
}
bool result = true ;
result & = int . TryParse ( match . Groups [ @"minutes" ] . Value , out int timeMin ) ;
result & = int . TryParse ( match . Groups [ @"seconds" ] . Value , out int timeSec ) ;
result & = int . TryParse ( match . Groups [ @"milliseconds" ] . Value , out int timeMsec ) ;
// somewhat sane limit for timestamp duration (10 hours).
result & = timeMin < 600 ;
if ( ! result )
{
parsedTime = null ;
parsedSelection = null ;
return false ;
}
parsedTime = TimeSpan . FromMinutes ( timeMin ) + TimeSpan . FromSeconds ( timeSec ) + TimeSpan . FromMilliseconds ( timeMsec ) ;
parsedSelection = match . Groups [ @"selection" ] . Value . Trim ( ) ;
if ( ! string . IsNullOrEmpty ( parsedSelection ) )
parsedSelection = parsedSelection [ 1. . ^ 1 ] ;
return true ;
2023-11-04 01:01:18 +00:00
}
}
}