From 35399ce8fef0559f65bee4f519c582d192a04e52 Mon Sep 17 00:00:00 2001 From: 1hitsong <3330318+1hitsong@users.noreply.github.com> Date: Wed, 21 Sep 2022 17:49:28 -0400 Subject: [PATCH] Update summaries, Use spans --- MediaBrowser.Controller/Lyrics/LyricLine.cs | 12 ++++++------ .../Lyrics/LyricMetadata.cs | 18 +++++++++--------- .../Lyrics/LyricResponse.cs | 4 ++-- .../Lyric/LrcLyricProvider.cs | 16 ++++++++-------- .../Lyric/TxtLyricProvider.cs | 10 ++++++---- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/MediaBrowser.Controller/Lyrics/LyricLine.cs b/MediaBrowser.Controller/Lyrics/LyricLine.cs index eb5ff9972..c406f92fc 100644 --- a/MediaBrowser.Controller/Lyrics/LyricLine.cs +++ b/MediaBrowser.Controller/Lyrics/LyricLine.cs @@ -12,17 +12,17 @@ public class LyricLine /// The lyric start time in ticks. public LyricLine(string text, long? start = null) { - Start = start; Text = text; + Start = start; } + /// + /// Gets the text of this lyric line. + /// + public string Text { get; } + /// /// Gets the start time in ticks. /// public long? Start { get; } - - /// - /// Gets the text. - /// - public string Text { get; } } diff --git a/MediaBrowser.Controller/Lyrics/LyricMetadata.cs b/MediaBrowser.Controller/Lyrics/LyricMetadata.cs index 0ba777975..6091ede52 100644 --- a/MediaBrowser.Controller/Lyrics/LyricMetadata.cs +++ b/MediaBrowser.Controller/Lyrics/LyricMetadata.cs @@ -8,47 +8,47 @@ namespace MediaBrowser.Controller.Lyrics; public class LyricMetadata { /// - /// Gets or sets Artist - The song artist. + /// Gets or sets the song artist. /// public string? Artist { get; set; } /// - /// Gets or sets Album - The album this song is on. + /// Gets or sets the album this song is on. /// public string? Album { get; set; } /// - /// Gets or sets Title - The title of the song. + /// Gets or sets the title of the song. /// public string? Title { get; set; } /// - /// Gets or sets Author - Creator of the lyric data. + /// Gets or sets the author of the lyric data. /// public string? Author { get; set; } /// - /// Gets or sets Length - How long the song is. + /// Gets or sets the length of the song in ticks. /// public long? Length { get; set; } /// - /// Gets or sets By - Creator of the LRC file. + /// Gets or sets who the LRC file was created by. /// public string? By { get; set; } /// - /// Gets or sets Offset - Offset:+/- Timestamp adjustment in milliseconds. + /// Gets or sets the lyric offset compared to audio in ticks. /// public long? Offset { get; set; } /// - /// Gets or sets Creator - The Software used to create the LRC file. + /// Gets or sets the software used to create the LRC file. /// public string? Creator { get; set; } /// - /// Gets or sets Version - The version of the Creator used. + /// Gets or sets the version of the creator used. /// public string? Version { get; set; } } diff --git a/MediaBrowser.Controller/Lyrics/LyricResponse.cs b/MediaBrowser.Controller/Lyrics/LyricResponse.cs index 56a569645..0d52b5ec5 100644 --- a/MediaBrowser.Controller/Lyrics/LyricResponse.cs +++ b/MediaBrowser.Controller/Lyrics/LyricResponse.cs @@ -9,12 +9,12 @@ namespace MediaBrowser.Controller.Lyrics; public class LyricResponse { /// - /// Gets or sets Metadata. + /// Gets or sets Metadata for the lyrics. /// public LyricMetadata Metadata { get; set; } = new(); /// - /// Gets or sets Lyrics. + /// Gets or sets a collection of individual lyric lines. /// public IReadOnlyList Lyrics { get; set; } = Array.Empty(); } diff --git a/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs b/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs index 8ed0552cc..1dbe5958e 100644 --- a/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs +++ b/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.IO; using System.Linq; using LrcParser.Model; using LrcParser.Parser; @@ -59,7 +60,7 @@ public class LrcLyricProvider : ILyricProvider } var fileMetaData = new Dictionary(StringComparer.OrdinalIgnoreCase); - string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath); + string lrcFileContent = File.ReadAllText(lyricFilePath); Song lyricData; @@ -84,25 +85,24 @@ public class LrcLyricProvider : ILyricProvider foreach (string metaDataRow in metaDataRows) { - if (!metaDataRow.Contains(':', StringComparison.OrdinalIgnoreCase)) + var index = metaDataRow.IndexOf(':', StringComparison.OrdinalIgnoreCase); + if (index == -1) { continue; } - string[] metaDataField = metaDataRow.Split(':', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); - // Remove square bracket before field name, and after field value // Example 1: [au: 1hitsong] // Example 2: [ar: Calabrese] - string metaDataFieldName = metaDataField[0][1..]; - string metaDataFieldValue = metaDataField[1][..^1]; + var metaDataFieldNameSpan = metaDataRow.AsSpan(1, index - 1).Trim(); + var metaDataFieldValueSpan = metaDataRow.AsSpan(index + 1, metaDataRow.Length - index - 2).Trim(); - if (string.IsNullOrEmpty(metaDataFieldName) || string.IsNullOrEmpty(metaDataFieldValue)) + if (metaDataFieldValueSpan.IsEmpty || metaDataFieldValueSpan.IsEmpty) { continue; } - fileMetaData[metaDataFieldName] = metaDataFieldValue; + fileMetaData[metaDataFieldNameSpan.ToString()] = metaDataFieldValueSpan.ToString(); } if (sortedLyricData.Count == 0) diff --git a/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs b/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs index df6d52630..bce881054 100644 --- a/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs +++ b/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using System.IO; +using System.Linq; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Lyrics; using MediaBrowser.Controller.Resolvers; @@ -36,18 +38,18 @@ public class TxtLyricProvider : ILyricProvider return null; } - string[] lyricTextLines = System.IO.File.ReadAllLines(lyricFilePath); + string[] lyricTextLines = File.ReadAllLines(lyricFilePath); if (lyricTextLines.Length == 0) { return null; } - List lyricList = new(lyricTextLines.Length); + LyricLine[] lyricList = new LyricLine[lyricTextLines.Length]; - foreach (string lyricTextLine in lyricTextLines) + for (int lyricLine = 0; lyricLine < lyricTextLines.Length; lyricLine++) { - lyricList.Add(new LyricLine(lyricTextLine)); + lyricList[lyricLine] = new LyricLine(lyricTextLines[lyricLine]); } return new LyricResponse { Lyrics = lyricList };