From 5908b8f7b0815939868b3625ed5d8ca0a7b22776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=87=E7=85=8C?= Date: Sat, 24 Aug 2019 19:30:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E5=AF=B9=E6=AF=94=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NLyric/Audio/Album.cs | 3 ++- NLyric/Audio/Track.cs | 19 +++---------------- NLyric/NLyric.csproj | 4 ++-- NLyric/NLyricImpl.cs | 19 +++++++++++++++---- NLyric/StringHelper.cs | 18 ++++++++++++++++++ 5 files changed, 40 insertions(+), 23 deletions(-) diff --git a/NLyric/Audio/Album.cs b/NLyric/Audio/Album.cs index 5fe03aa..3ceea1f 100644 --- a/NLyric/Audio/Album.cs +++ b/NLyric/Audio/Album.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; namespace NLyric.Audio { public class Album : ITrackOrAlbum { @@ -22,7 +23,7 @@ public Album(string name, string[] artists, int? trackCount, int? year) { throw new ArgumentNullException(nameof(artists)); _name = name; - _artists = artists; + _artists = artists.Select(t => t.Trim()).ToArray(); _trackCount = trackCount; _year = year; } diff --git a/NLyric/Audio/Track.cs b/NLyric/Audio/Track.cs index a667701..1966730 100644 --- a/NLyric/Audio/Track.cs +++ b/NLyric/Audio/Track.cs @@ -1,5 +1,5 @@ using System; -using System.Collections.Generic; +using System.Linq; namespace NLyric.Audio { public class Track : ITrackOrAlbum { @@ -17,7 +17,7 @@ public Track(string name, string[] artists) { throw new ArgumentNullException(nameof(artists)); _name = name; - _artists = artists; + _artists = artists.Select(t => t.Trim()).ToArray(); } public Track(ATL.Track track) { @@ -26,24 +26,11 @@ public Track(ATL.Track track) { _name = track.Title.GetSafeString(); _artists = track.Artist.GetSafeString().SplitEx(); - Array.Sort(_artists, StringComparer.Instance); + Array.Sort(_artists, StringHelper.OrdinalComparer); } public override string ToString() { return "Name:" + _name + " | Artists:" + string.Join(",", _artists); } - - private sealed class StringComparer : IComparer { - private static readonly StringComparer _instance = new StringComparer(); - - public static StringComparer Instance => _instance; - - private StringComparer() { - } - - public int Compare(string x, string y) { - return string.CompareOrdinal(x, y); - } - } } } diff --git a/NLyric/NLyric.csproj b/NLyric/NLyric.csproj index ccc20c6..60d786c 100644 --- a/NLyric/NLyric.csproj +++ b/NLyric/NLyric.csproj @@ -4,8 +4,8 @@ NLyric NLyric Copyright © 2019 Wwh - 2.2.5.0 - 2.2.5.0 + 2.2.5.1 + 2.2.5.1 ..\bin\$(Configuration) Exe netcoreapp2.1;net472 diff --git a/NLyric/NLyricImpl.cs b/NLyric/NLyricImpl.cs index 671aba7..7af2c90 100644 --- a/NLyric/NLyricImpl.cs +++ b/NLyric/NLyricImpl.cs @@ -361,24 +361,35 @@ private static async Task MapToAsync(Album album, bool withArtists) { private static void LoadLocalCaches() { if (File.Exists(_allCachesPath)) { _allCaches = JsonConvert.DeserializeObject(File.ReadAllText(_allCachesPath)); + NormalizeAllCaches(); Logger.Instance.LogInfo($"搜索缓存\"{_allCachesPath}\"加载成功。"); } - else + else { _allCaches = new AllCaches() { AlbumCaches = new List(), LyricCaches = new List(), TrackCaches = new List() }; + } } private static void SaveLocalCaches() { - _allCaches.AlbumCaches.Sort((x, y) => x.Name.CompareTo(y.Name)); - _allCaches.TrackCaches.Sort((x, y) => x.Name.CompareTo(y.Name)); - _allCaches.LyricCaches.Sort((x, y) => x.Id.CompareTo(y.Id)); + NormalizeAllCaches(); SaveLocalCachesCore(_allCachesPath); Logger.Instance.LogInfo($"搜索缓存\"{_allCachesPath}\"已被保存。"); } + private static void NormalizeAllCaches() { + _allCaches.AlbumCaches.Sort((x, y) => string.CompareOrdinal(x.Name, y.Name)); + _allCaches.TrackCaches.Sort((x, y) => string.CompareOrdinal(x.Name, y.Name)); + _allCaches.LyricCaches.Sort((x, y) => x.Id.CompareTo(y.Id)); + foreach (TrackCache cache in _allCaches.TrackCaches) { + for (int i = 0; i < cache.Artists.Length; i++) + cache.Artists[i] = cache.Artists[i].Trim(); + Array.Sort(cache.Artists, StringHelper.OrdinalComparer); + } + } + private static void SaveLocalCachesCore(string cachePath) { File.WriteAllText(cachePath, FormatJson(JsonConvert.SerializeObject(_allCaches))); } diff --git a/NLyric/StringHelper.cs b/NLyric/StringHelper.cs index 4bb0301..4aa59d0 100644 --- a/NLyric/StringHelper.cs +++ b/NLyric/StringHelper.cs @@ -10,6 +10,11 @@ internal static class StringHelper { private static readonly FuzzySettings _fuzzySettings = AllSettings.Default.Fuzzy; private static readonly MatchSettings _matchSettings = AllSettings.Default.Match; + /// + /// 二进制比较器 + /// + public static IComparer OrdinalComparer => StringOrdinalComparer.Instance; + /// /// 获取非空字符串,并且清楚首尾空格 /// @@ -123,5 +128,18 @@ public static string ToHalfWidth(this string value) { } return new string(chars); } + + private sealed class StringOrdinalComparer : IComparer { + private static readonly StringOrdinalComparer _instance = new StringOrdinalComparer(); + + public static StringOrdinalComparer Instance => _instance; + + private StringOrdinalComparer() { + } + + public int Compare(string x, string y) { + return string.CompareOrdinal(x, y); + } + } } }