Add param to IList.ToLuaTable extension and refactor bizstring.split

Changed behaviour when said Lua function is called with a misformatted separator
param, and updated its docs
This commit is contained in:
YoshiRulz 2019-12-16 01:58:19 +10:00
parent d2f21f84e3
commit 2f494003c7
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
2 changed files with 8 additions and 21 deletions

View File

@ -132,23 +132,10 @@ namespace BizHawk.Client.Common
} }
[LuaMethodExample("local nlbizspl = bizstring.split( \"Some, string\", \", \" );")] [LuaMethodExample("local nlbizspl = bizstring.split( \"Some, string\", \", \" );")]
[LuaMethod("split", "Splits str based on separator into a LuaTable. Separator must be one character!. Same functionality as .NET string.Split() using the RemoveEmptyEntries option")] [LuaMethod("split", "Splits str into a Lua-style array using the given separator (consecutive separators in str will NOT create empty entries in the array). If the separator is not a string exactly one char long, ',' will be used.")]
public LuaTable Split(string str, string separator) public LuaTable Split(string str, string separator) => string.IsNullOrEmpty(str)
{ ? Lua.NewTable()
var table = Lua.NewTable(); : str.Split(new[] { separator?.Length == 1 ? separator[0] : ',' }, StringSplitOptions.RemoveEmptyEntries)
if (!string.IsNullOrEmpty(str)) .ToLuaTable(Lua, 1);
{
var splitStr = str.Split(
new[] { separator.FirstOrDefault() },
StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < splitStr.Length; i++)
{
table[i + 1] = splitStr[i];
}
}
return table;
}
} }
} }

View File

@ -8,14 +8,14 @@ namespace BizHawk.Client.Common
{ {
public static class LuaExtensions public static class LuaExtensions
{ {
public static LuaTable ToLuaTable<T>(this IList<T> list, Lua lua) public static LuaTable ToLuaTable<T>(this IList<T> list, Lua lua, int indexFrom = 0)
{ {
var table = lua.NewTable(); var table = lua.NewTable();
for (int i = 0; i < list.Count; i++) var indexAfterLast = indexFrom + list.Count;
for (var i = indexFrom; i != indexAfterLast; i++)
{ {
table[i] = list[i]; table[i] = list[i];
} }
return table; return table;
} }