mirror of
https://github.com/kwsch/PKHeX
synced 2024-11-14 00:07:15 +00:00
Add more string search methods
exposes some common string search operations that can be useful for parsing user entered data
This commit is contained in:
parent
35c7195d77
commit
fe8e5e3fd9
2 changed files with 76 additions and 12 deletions
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace PKHeX.Core
|
||||
|
@ -11,14 +12,23 @@ namespace PKHeX.Core
|
|||
EncounterEvent.RefreshMGDB();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Default response if there are no matches.
|
||||
/// </summary>
|
||||
public const string NoMatches = "None";
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a <see cref="species"/> can learn all input <see cref="moves"/>.
|
||||
/// </summary>
|
||||
public static bool CanLearn(string species, IEnumerable<string> moves, string lang = GameLanguage.DefaultLanguage)
|
||||
{
|
||||
var encs = GetLearn(species, moves, lang);
|
||||
return encs.Any();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a summary of all encounters where a <see cref="species"/> can learn all input <see cref="moves"/>.
|
||||
/// </summary>
|
||||
public static IEnumerable<string> GetLearnSummary(string species, IEnumerable<string> moves, string lang = GameLanguage.DefaultLanguage)
|
||||
{
|
||||
var encs = GetLearn(species, moves, lang);
|
||||
|
@ -28,36 +38,50 @@ namespace PKHeX.Core
|
|||
return msg;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all encounters where a <see cref="species"/> can learn all input <see cref="moves"/>.
|
||||
/// </summary>
|
||||
public static IEnumerable<IEncounterable> GetLearn(string species, IEnumerable<string> moves, string lang = GameLanguage.DefaultLanguage)
|
||||
{
|
||||
var str = GameInfo.GetStrings(lang);
|
||||
if (str == null)
|
||||
return Enumerable.Empty<IEncounterable>();
|
||||
return Array.Empty<IEncounterable>();
|
||||
|
||||
var spec = StringUtil.FindIndexIgnoreCase(str.specieslist, species);
|
||||
if (spec <= 0)
|
||||
return Enumerable.Empty<IEncounterable>();
|
||||
|
||||
var moveIDs = moves.Select(z => StringUtil.FindIndexIgnoreCase(str.movelist, z)).Where(z => z > 0).ToArray();
|
||||
var moveIDs = StringUtil.GetIndexes(str.movelist, moves.ToList());
|
||||
|
||||
return GetLearn(spec, moveIDs);
|
||||
}
|
||||
|
||||
public static IEnumerable<IEncounterable> GetLearn(int spec, int[] moveIDs)
|
||||
/// <summary>
|
||||
/// Gets all encounters where a <see cref="species"/> can learn all input <see cref="moves"/>.
|
||||
/// </summary>
|
||||
public static IEnumerable<IEncounterable> GetLearn(int species, int[] moves)
|
||||
{
|
||||
if (species <= 0)
|
||||
return Array.Empty<IEncounterable>();
|
||||
if (moves.Any(z => z < 0))
|
||||
return Array.Empty<IEncounterable>();
|
||||
|
||||
var blank = PKMConverter.GetBlank(PKX.Generation);
|
||||
blank.Species = spec;
|
||||
blank.Species = species;
|
||||
|
||||
var vers = GameUtil.GameVersions;
|
||||
return EncounterMovesetGenerator.GenerateEncounters(blank, moveIDs, vers);
|
||||
return EncounterMovesetGenerator.GenerateEncounters(blank, moves, vers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Summarizes all encounters by grouping by <see cref="IEncounterable.Name"/>.
|
||||
/// </summary>
|
||||
public static IEnumerable<string> Summarize(IEnumerable<IEncounterable> encounters, bool advanced = false)
|
||||
{
|
||||
var types = encounters.GroupBy(z => z.Name);
|
||||
return Summarize(types, advanced);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Summarizes groups of encounters.
|
||||
/// </summary>
|
||||
public static IEnumerable<string> Summarize(IEnumerable<IGrouping<string, IEncounterable>> types, bool advanced = false)
|
||||
{
|
||||
return types.SelectMany(g => EncounterSummary.SummarizeGroup(g, g.Key, advanced));
|
||||
|
|
|
@ -1,22 +1,62 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
namespace PKHeX.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Utility for searching strings within arrays or within another string.
|
||||
/// </summary>
|
||||
public static class StringUtil
|
||||
{
|
||||
private static readonly CultureInfo SearchCulture = CultureInfo.CurrentCulture;
|
||||
private static readonly CompareInfo CompareInfo = CultureInfo.CurrentCulture.CompareInfo;
|
||||
|
||||
/// <summary>
|
||||
/// Finds the index of the string within the array by ignoring casing, spaces, and punctuation.
|
||||
/// </summary>
|
||||
/// <param name="arr">Array of strings to search in</param>
|
||||
/// <param name="value">Value to search for</param>
|
||||
/// <returns>Index within <see cref="arr"/></returns>
|
||||
public static int FindIndexIgnoreCase(string[] arr, string value) => Array.FindIndex(arr, z => IsMatchIgnoreCase(z, value));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the indexes by calling <see cref="FindIndexIgnoreCase"/> for all <see cref="items"/>.
|
||||
/// </summary>
|
||||
/// <param name="arr">Array of strings to search in</param>
|
||||
/// <param name="items">Items to search for</param>
|
||||
/// <returns>Index within <see cref="arr"/></returns>
|
||||
public static int[] GetIndexes(string[] arr, IReadOnlyList<string> items) => GetIndexes(arr, items, 0, items.Count);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the indexes by calling <see cref="FindIndexIgnoreCase"/> for all <see cref="items"/>.
|
||||
/// </summary>
|
||||
/// <param name="arr">Array of strings to search in</param>
|
||||
/// <param name="items">Items to search for</param>
|
||||
/// <param name="start">Starting index within <see cref="items"/></param>
|
||||
/// <param name="length">Amount to convert within <see cref="items"/></param>
|
||||
/// <returns>Index within <see cref="arr"/></returns>
|
||||
public static int[] GetIndexes(string[] arr, IReadOnlyList<string> items, int start, int length)
|
||||
{
|
||||
if (length < 0)
|
||||
length = items.Count - start;
|
||||
var result = new int[length];
|
||||
for (int i = 0; i < result.Length; i++)
|
||||
result[i] = FindIndexIgnoreCase(arr, items[start + i]);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static bool IsMatchIgnoreCase(string string1, string string2)
|
||||
{
|
||||
if (string1.Length != string2.Length)
|
||||
return false;
|
||||
const CompareOptions options = CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols;
|
||||
var compare = SearchCulture.CompareInfo.Compare(string1, string2, options);
|
||||
const CompareOptions options = CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreWidth;
|
||||
var compare = CompareInfo.Compare(string1, string2, options);
|
||||
return compare == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="nth"/> string entry within the input <see cref="line"/>, based on the <see cref="separator"/> and <see cref="start"/> position.
|
||||
/// </summary>
|
||||
public static string GetNthEntry(string line, int nth, int start, char separator = ',')
|
||||
{
|
||||
if (nth != 1)
|
||||
|
|
Loading…
Reference in a new issue