Implement bizstring.split()

This commit is contained in:
adelikat 2014-05-18 21:06:16 +00:00
parent 3127e6d36c
commit 3d10d67fea
2 changed files with 31 additions and 1 deletions

View File

@ -1,10 +1,21 @@
using System;
using System.Linq;
using LuaInterface;
namespace BizHawk.Client.Common
{
public class StringLuaLibrary : LuaLibraryBase
{
private readonly Lua _lua;
public override string Name { get { return "bizstring"; } }
public StringLuaLibrary(Lua lua)
{
_lua = lua;
}
[LuaMethodAttributes(
"hex",
"Converts the number to a string representation of the hexadecimal value of the given number"
@ -126,5 +137,24 @@ namespace BizHawk.Client.Common
{
return str.EndsWith(str2);
}
[LuaMethodAttributes(
"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();
var splitStr = str.Split(
new char[] { separator.FirstOrDefault() },
StringSplitOptions.RemoveEmptyEntries);
for(int i = 0; i < splitStr.Length; i++)
{
table[i] = splitStr[i];
}
return table;
}
}
}

View File

@ -101,7 +101,7 @@ namespace BizHawk.Client.EmuHawk
new NesLuaLibrary().LuaRegister(lua, Docs);
new SavestateLuaLibrary().LuaRegister(lua, Docs);
new SnesLuaLibrary().LuaRegister(lua, Docs);
new StringLuaLibrary().LuaRegister(lua, Docs);
new StringLuaLibrary(_lua).LuaRegister(lua, Docs);
new GameInfoLuaLibrary(_lua).LuaRegister(lua, Docs);
Docs.Sort();
}