From 2f494003c781e91c1ea728ae6adcf7d8620498b5 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Mon, 16 Dec 2019 01:58:19 +1000 Subject: [PATCH] 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 --- .../lua/EmuLuaLibrary.String.cs | 23 ++++--------------- BizHawk.Client.Common/lua/LuaHelper.cs | 6 ++--- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs index 51ed286115..cb291111d9 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs @@ -132,23 +132,10 @@ namespace BizHawk.Client.Common } [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")] - public LuaTable Split(string str, string separator) - { - var table = Lua.NewTable(); - if (!string.IsNullOrEmpty(str)) - { - var splitStr = str.Split( - new[] { separator.FirstOrDefault() }, - StringSplitOptions.RemoveEmptyEntries); - - for (int i = 0; i < splitStr.Length; i++) - { - table[i + 1] = splitStr[i]; - } - } - - return table; - } + [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) => string.IsNullOrEmpty(str) + ? Lua.NewTable() + : str.Split(new[] { separator?.Length == 1 ? separator[0] : ',' }, StringSplitOptions.RemoveEmptyEntries) + .ToLuaTable(Lua, 1); } } diff --git a/BizHawk.Client.Common/lua/LuaHelper.cs b/BizHawk.Client.Common/lua/LuaHelper.cs index 9c4067837c..52864be0d2 100644 --- a/BizHawk.Client.Common/lua/LuaHelper.cs +++ b/BizHawk.Client.Common/lua/LuaHelper.cs @@ -8,14 +8,14 @@ namespace BizHawk.Client.Common { public static class LuaExtensions { - public static LuaTable ToLuaTable(this IList list, Lua lua) + public static LuaTable ToLuaTable(this IList list, Lua lua, int indexFrom = 0) { 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]; } - return table; }