Refactor NameList and improve case-insensitive checks.

Replaced `ContainsCaseInsensitive` with a more versatile `Contains` method leveraging `StringComparison`. Simplified name collection assignment logic and ensured consistent access to name dictionaries based on the presence of spaces in names.

Signed-off-by: Ivandro Jao <Ivandrofly@gmail.com>
This commit is contained in:
Ivandro Jao 2024-08-03 14:12:24 +01:00
parent 82254ff1f8
commit 1394cc697a
2 changed files with 23 additions and 16 deletions

View File

@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
@ -248,8 +249,7 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
private bool TryAdd(string name)
{
var collection = name.Contains(" ") ? _namesMultiList : _namesList;
return collection.Add(name);
return GetCollectionFromName(name).Add(name);
}
private static XmlDocument CreateDocument(string fileName)
@ -274,6 +274,7 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
return false;
}
// todo:
var text = input.Replace(Environment.NewLine, " ");
text = text.FixExtraSpaces();
@ -303,24 +304,30 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
return false;
}
public bool ContainsCaseInsensitive(string name, out string newName)
public bool Contains(string name) => Contains(name, StringComparison.Ordinal);
public bool Contains(string name, StringComparison comparison)
{
return GetCollectionFromName(name).Any(n => name.Equals(n, comparison));
}
/// <summary>
/// Returns the dictionary name of a given name (case-insensitive).
/// </summary>
public string GetDictionaryName(string name)
{
return GetCollectionFromName(name).FirstOrDefault(n => n.Equals(name, StringComparison.OrdinalIgnoreCase));
}
private readonly ISet<string> _emptySet = new HashSet<string>();
private ISet<string> GetCollectionFromName(string name)
{
newName = null;
if (string.IsNullOrEmpty(name))
{
return false;
return _emptySet;
}
foreach (var n in name.IndexOf(' ') >= 0 ? _namesMultiList : _namesList)
{
if (name.Equals(n, StringComparison.OrdinalIgnoreCase))
{
newName = n;
return true;
}
}
return false;
return name.IndexOf(' ') >= 0 ? _namesMultiList : _namesList;
}
public static async Task<NameList> CreateAsync(string dictionaryFolder, string languageName, bool useOnlineNameList, string namesUrl)

View File

@ -757,7 +757,7 @@ namespace Nikse.SubtitleEdit.Core.Forms
{
var s = line.Substring(lastIndexOfPeriod, indexOfColon - lastIndexOfPeriod);
s = s.Trim('.', '-', ' ', '!', '?', '"', '\'');
if (IsHIDescription(s) || Settings.NameList != null && Settings.NameList.ContainsCaseInsensitive(s, out var _))
if (IsHIDescription(s) || Settings.NameList != null && Settings.NameList.Contains(s, StringComparison.OrdinalIgnoreCase))
{
var partialRemove = false;
if (Settings.RemoveTextBeforeColonOnlyUppercase)