Skip to content

Commit

Permalink
修复字符串对比错误
Browse files Browse the repository at this point in the history
  • Loading branch information
wwh1004 committed Aug 24, 2019
1 parent 79957a8 commit 5908b8f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
3 changes: 2 additions & 1 deletion NLyric/Audio/Album.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace NLyric.Audio {
public class Album : ITrackOrAlbum {
Expand All @@ -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;
}
Expand Down
19 changes: 3 additions & 16 deletions NLyric/Audio/Track.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace NLyric.Audio {
public class Track : ITrackOrAlbum {
Expand All @@ -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) {
Expand All @@ -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<string> {
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);
}
}
}
}
4 changes: 2 additions & 2 deletions NLyric/NLyric.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<Title>NLyric</Title>
<Product>NLyric</Product>
<Copyright>Copyright © 2019 Wwh</Copyright>
<AssemblyVersion>2.2.5.0</AssemblyVersion>
<FileVersion>2.2.5.0</FileVersion>
<AssemblyVersion>2.2.5.1</AssemblyVersion>
<FileVersion>2.2.5.1</FileVersion>
<OutputPath>..\bin\$(Configuration)</OutputPath>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.1;net472</TargetFrameworks>
Expand Down
19 changes: 15 additions & 4 deletions NLyric/NLyricImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,24 +361,35 @@ private static async Task<NcmAlbum> MapToAsync(Album album, bool withArtists) {
private static void LoadLocalCaches() {
if (File.Exists(_allCachesPath)) {
_allCaches = JsonConvert.DeserializeObject<AllCaches>(File.ReadAllText(_allCachesPath));
NormalizeAllCaches();
Logger.Instance.LogInfo($"搜索缓存\"{_allCachesPath}\"加载成功。");
}
else
else {
_allCaches = new AllCaches() {
AlbumCaches = new List<AlbumCache>(),
LyricCaches = new List<LyricCache>(),
TrackCaches = new List<TrackCache>()
};
}
}

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)));
}
Expand Down
18 changes: 18 additions & 0 deletions NLyric/StringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ internal static class StringHelper {
private static readonly FuzzySettings _fuzzySettings = AllSettings.Default.Fuzzy;
private static readonly MatchSettings _matchSettings = AllSettings.Default.Match;

/// <summary>
/// 二进制比较器
/// </summary>
public static IComparer<string> OrdinalComparer => StringOrdinalComparer.Instance;

/// <summary>
/// 获取非空字符串,并且清楚首尾空格
/// </summary>
Expand Down Expand Up @@ -123,5 +128,18 @@ public static string ToHalfWidth(this string value) {
}
return new string(chars);
}

private sealed class StringOrdinalComparer : IComparer<string> {
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);
}
}
}
}

0 comments on commit 5908b8f

Please sign in to comment.