Simplify `SerializeTable` in `ConsoleLuaLibrary`
This commit is contained in:
parent
f3799fbb0e
commit
b6bcd083a3
|
@ -1,3 +1,4 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
|
@ -66,23 +67,9 @@ namespace BizHawk.Client.EmuHawk
|
|||
{
|
||||
static string SerializeTable(LuaTable lti)
|
||||
{
|
||||
var keyObjs = lti.Keys;
|
||||
var valueObjs = lti.Values;
|
||||
if (keyObjs.Count != valueObjs.Count)
|
||||
{
|
||||
throw new ArgumentException(message: "each value must be paired with one key, they differ in number", paramName: nameof(lti));
|
||||
}
|
||||
|
||||
var values = new object[keyObjs.Count];
|
||||
var kvpIndex = 0;
|
||||
foreach (var valueObj in valueObjs)
|
||||
{
|
||||
values[kvpIndex++] = valueObj;
|
||||
}
|
||||
|
||||
return string.Concat(keyObjs.Cast<object>()
|
||||
.Select((kObj, i) => $"\"{kObj}\": \"{values[i]}\"\n")
|
||||
.Order());
|
||||
var entries = ((IEnumerator<KeyValuePair<object, object/*?*/>>) lti.GetEnumerator()).AsEnumerable()
|
||||
.ToArray();
|
||||
return string.Concat(entries.Select(static kvp => $"\"{kvp.Key}\": \"{kvp.Value}\"\n").Order());
|
||||
}
|
||||
|
||||
if (!Tools.Has<LuaConsole>())
|
||||
|
|
|
@ -10,6 +10,30 @@ namespace BizHawk.Common.CollectionExtensions
|
|||
public static class CollectionExtensions
|
||||
#pragma warning restore MA0104
|
||||
{
|
||||
private struct EnumeratorAsEnumerable<T> : IEnumerable<T>
|
||||
{
|
||||
private IEnumerator<T>? _wrapped;
|
||||
|
||||
public EnumeratorAsEnumerable(IEnumerator<T> wrapped)
|
||||
=> _wrapped = wrapped;
|
||||
|
||||
public override bool Equals(object? other)
|
||||
=> other is EnumeratorAsEnumerable<T> wrapper && object.Equals(_wrapped, wrapper._wrapped);
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
var temp = _wrapped ?? throw new InvalidOperationException("double enumeration (or `default`/zeroed struct)");
|
||||
_wrapped = null;
|
||||
return temp;
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
=> GetEnumerator();
|
||||
|
||||
public override int GetHashCode()
|
||||
=> _wrapped?.GetHashCode() ?? default;
|
||||
}
|
||||
|
||||
private const string ERR_MSG_IMMUTABLE_LIST = "immutable list passed to mutating method";
|
||||
|
||||
private const string WARN_NONGENERIC = "use generic overload";
|
||||
|
@ -119,6 +143,9 @@ namespace BizHawk.Common.CollectionExtensions
|
|||
foreach (var item in collection) list.Add(item);
|
||||
}
|
||||
|
||||
public static IEnumerable<T> AsEnumerable<T>(this IEnumerator<T> enumerator)
|
||||
=> new EnumeratorAsEnumerable<T>(enumerator);
|
||||
|
||||
/// <remarks>
|
||||
/// Contains method for arrays which does not need Linq, but rather uses Array.IndexOf
|
||||
/// similar to <see cref="ICollection{T}.Contains">ICollection's Contains</see>
|
||||
|
|
Loading…
Reference in New Issue