Skip to content

Commit

Permalink
Merge pull request #305 from andy840119/io/fix-regex-issue
Browse files Browse the repository at this point in the history
Using better regular expression to parse ruby, romaji and time-tag
  • Loading branch information
andy840119 authored Dec 12, 2020
2 parents 507cfcf + 02636f8 commit ff779c3
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using Newtonsoft.Json;
using NUnit.Framework;
using osu.Framework.Graphics.Sprites;
using osu.Game.Rulesets.Karaoke.IO.Serialization.Converters;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Tests.IO.Serialization.Converters
{
[TestFixture]
public class TimeTagConverterTest : BaseSingleConverterTest<TimeTagConverter>
{
[TestCase(1, TimeTagIndex.IndexState.Start, 1000, "[1,start]:1000")]
[TestCase(1, TimeTagIndex.IndexState.End, 1000, "[1,end]:1000")]
[TestCase(-1, TimeTagIndex.IndexState.Start, 1000, "[-1,start]:1000")] // Should not check index is out of range in here.
[TestCase(1, TimeTagIndex.IndexState.Start, -1000, "[1,start]:-1000")] // Should not check if time is negative.
[TestCase(1, TimeTagIndex.IndexState.Start, null, "[1,start]:")]
public void TestSerialize(int index, TimeTagIndex.IndexState state, int? time, string json)
{
var timeTag = new TimeTag(new TimeTagIndex(index, state), time);

var result = JsonConvert.SerializeObject(timeTag, CreateSettings());
Assert.AreEqual(result, $"\"{json}\"");
}

[TestCase("[1,start]:1000", 1, TimeTagIndex.IndexState.Start, 1000)]
[TestCase("[1,end]:1000", 1, TimeTagIndex.IndexState.End, 1000)]
[TestCase("[-1,start]:1000", -1, TimeTagIndex.IndexState.Start, 1000)] // Should not check index is out of range in here.
[TestCase("[1,start]:-1000", 1, TimeTagIndex.IndexState.Start, -1000)] // Should not check if time is negative.
[TestCase("[1,start]:", 1, TimeTagIndex.IndexState.Start, null)]
[TestCase("", 0, TimeTagIndex.IndexState.Start, null)] // Test deal with format is not right below.
[TestCase("[1,???]:", 0, TimeTagIndex.IndexState.Start, null)]
[TestCase("[1,]", 0, TimeTagIndex.IndexState.Start, null)]
[TestCase("[,start]", 0, TimeTagIndex.IndexState.Start, null)]
[TestCase("[]", 0, TimeTagIndex.IndexState.Start, null)]
public void TestDeserialize(string json, int index, TimeTagIndex.IndexState state, int? time)
{
var result = JsonConvert.DeserializeObject<TimeTag>($"\"{json}\"", CreateSettings());
var actual = new TimeTag(new TimeTagIndex(index, state), time);
Assert.AreEqual(result.Index, actual.Index);
Assert.AreEqual(result.Time, actual.Time);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public override RomajiTag ReadJson(JsonReader reader, Type objectType, RomajiTag
if (value == null || value == "")
return new RomajiTag();

var regux = new Regex("([-0-9]+),([-0-9]+)]:(.*$)");
var regux = new Regex("(?<start>[-0-9]+),(?<end>[-0-9]+)]:(?<romaji>.*$)");
var result = regux.Match(value);
if (!result.Success)
return new RomajiTag();

var startIndex = int.Parse(result.Groups[1].Value);
var endIndex = int.Parse(result.Groups[2].Value);
var text = result.Groups[3].Value;
var startIndex = int.Parse(result.Groups["start"]?.Value);
var endIndex = int.Parse(result.Groups["end"]?.Value);
var text = result.Groups["romaji"]?.Value;

return new RomajiTag
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public override RubyTag ReadJson(JsonReader reader, Type objectType, RubyTag exi
if (value == null || value == "")
return new RubyTag();

var regux = new Regex("([-0-9]+),([-0-9]+)]:(.*$)");
var regux = new Regex("(?<start>[-0-9]+),(?<end>[-0-9]+)]:(?<ruby>.*$)");
var result = regux.Match(value);
if (!result.Success)
return new RubyTag();

var startIndex = int.Parse(result.Groups[1].Value);
var endIndex = int.Parse(result.Groups[2].Value);
var text = result.Groups[3].Value;
var startIndex = int.Parse(result.Groups["start"]?.Value);
var endIndex = int.Parse(result.Groups["end"]?.Value);
var text = result.Groups["ruby"]?.Value;

return new RubyTag
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using osu.Framework.Graphics.Sprites;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.IO.Serialization.Converters
{
public class TimeTagConverter : JsonConverter<TimeTag>
{
public override TimeTag ReadJson(JsonReader reader, Type objectType, TimeTag existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var obj = JToken.Load(reader);
var value = obj.Value<string>();

if (value == null || value == "")
return new TimeTag(new TimeTagIndex());

var regux = new Regex("(?<index>[-0-9]+),(?<state>start|end)]:(?<time>[-0-9]+|s*|)");
var result = regux.Match(value);
if (!result.Success)
return new TimeTag(new TimeTagIndex());

var index = int.Parse(result.Groups["index"]?.Value);
var state = result.Groups["state"]?.Value == "start" ? TimeTagIndex.IndexState.Start : TimeTagIndex.IndexState.End;
var timeStr = result.Groups["time"]?.Value;
var time = timeStr == "" ? default(int?) : int.Parse(timeStr);

return new TimeTag(new TimeTagIndex(index, state), time);
}

public override void WriteJson(JsonWriter writer, TimeTag value, JsonSerializer serializer)
{
var tag = value.Index;
var state = tag.State == TimeTagIndex.IndexState.Start ? "start" : "end";
var time = value.Time;

var str = $"[{tag.Index},{state}]:{time}";
writer.WriteValue(str);
}
}
}

This file was deleted.

2 changes: 0 additions & 2 deletions osu.Game.Rulesets.Karaoke/Objects/Lyric.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json;
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics.Sprites;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Rulesets.Judgements;
Expand Down

0 comments on commit ff779c3

Please sign in to comment.