Commit 0a4d984b authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

Fixed #68 "." in key causes error

- Now is possible to access keys with dots escaping '.' with '\' lua ["g_dot.key\\.with\\.dot"];
parent 3c7a6f64
......@@ -23,6 +23,7 @@
* THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
namespace NLua.Extensions
{
......@@ -39,6 +40,7 @@ namespace NLua.Extensions
/// <c>true</c> if the specified obj is null; otherwise, <c>false</c>.
/// </returns>
///
#if USE_KOPILUA
public static bool IsNull (object obj)
{
......@@ -52,4 +54,29 @@ namespace NLua.Extensions
}
#endif
}
static class StringExtensions
{
public static IEnumerable<string> SplitWithEscape (this string input, char separator, char escapeCharacter)
{
int start = 0;
int index = 0;
while (index < input.Length) {
index = input.IndexOf (separator, index);
if (index == -1)
break;
if (input [index - 1] == escapeCharacter) {
input = input.Remove (index - 1, 1);
continue;
}
yield return input.Substring (start, index - start);
index++;
start = index;
}
yield return input.Substring (start);
}
}
}
\ No newline at end of file
......@@ -25,6 +25,7 @@
*/
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Reflection;
using System.Collections;
......@@ -539,7 +540,7 @@ end
get {
object returnValue = null;
int oldTop = LuaLib.LuaGetTop (luaState);
string[] path = fullPath.Split (new char[] { '.' });
string [] path = FullPathToArray (fullPath);
LuaLib.LuaGetGlobal (luaState, path [0]);
returnValue = translator.GetObject (luaState, -1);
LuaBase dispose = null;
......@@ -558,8 +559,7 @@ end
}
set {
int oldTop = LuaLib.LuaGetTop (luaState);
string[] path = fullPath.Split (new char[] { '.' });
string [] path = FullPathToArray (fullPath);
if (path.Length == 1) {
translator.Push (luaState, value);
LuaLib.LuaSetGlobal (luaState, fullPath);
......@@ -806,13 +806,17 @@ end
LuaLib.LuaSetTable (luaState, -3);
}
string [] FullPathToArray (string fullPath)
{
return fullPath.SplitWithEscape ('.', '\\').ToArray ();
}
/*
* Creates a new table as a global variable or as a field
* inside an existing table
*/
public void NewTable (string fullPath)
{
string[] path = fullPath.Split (new char[] { '.' });
string [] path = FullPathToArray (fullPath);
int oldTop = LuaLib.LuaGetTop (luaState);
if (path.Length == 1) {
......@@ -1089,7 +1093,7 @@ end
{
int oldTop = LuaLib.LuaGetTop (luaState);
LuaLib.LuaGetRef (luaState, reference);
object returnValue = GetObject (field.Split (new char[] {'.'}));
object returnValue = GetObject (FullPathToArray (field));
LuaLib.LuaSetTop (luaState, oldTop);
return returnValue;
}
......@@ -1117,7 +1121,7 @@ end
{
int oldTop = LuaLib.LuaGetTop (luaState);
LuaLib.LuaGetRef (luaState, reference);
SetObject (field.Split (new char[] {'.'}), val);
SetObject (FullPathToArray (field), val);
LuaLib.LuaSetTop (luaState, oldTop);
}
......
......@@ -1913,5 +1913,16 @@ namespace NLuaTest
}
}
[Test]
public void TestKeyWithDots ()
{
using (Lua lua = new Lua ()) {
lua.DoString (@"g_dot = {}
g_dot['key.with.dot'] = 42");
Assert.AreEqual (42, (int)(double)lua ["g_dot.key\\.with\\.dot"]);
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment