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 @@ ...@@ -23,6 +23,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
using System; using System;
using System.Collections.Generic;
namespace NLua.Extensions namespace NLua.Extensions
{ {
...@@ -39,6 +40,7 @@ namespace NLua.Extensions ...@@ -39,6 +40,7 @@ namespace NLua.Extensions
/// <c>true</c> if the specified obj is null; otherwise, <c>false</c>. /// <c>true</c> if the specified obj is null; otherwise, <c>false</c>.
/// </returns> /// </returns>
/// ///
#if USE_KOPILUA #if USE_KOPILUA
public static bool IsNull (object obj) public static bool IsNull (object obj)
{ {
...@@ -52,4 +54,29 @@ namespace NLua.Extensions ...@@ -52,4 +54,29 @@ namespace NLua.Extensions
} }
#endif #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 @@ ...@@ -25,6 +25,7 @@
*/ */
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Threading; using System.Threading;
using System.Reflection; using System.Reflection;
using System.Collections; using System.Collections;
...@@ -539,7 +540,7 @@ end ...@@ -539,7 +540,7 @@ end
get { get {
object returnValue = null; object returnValue = null;
int oldTop = LuaLib.LuaGetTop (luaState); int oldTop = LuaLib.LuaGetTop (luaState);
string[] path = fullPath.Split (new char[] { '.' }); string [] path = FullPathToArray (fullPath);
LuaLib.LuaGetGlobal (luaState, path [0]); LuaLib.LuaGetGlobal (luaState, path [0]);
returnValue = translator.GetObject (luaState, -1); returnValue = translator.GetObject (luaState, -1);
LuaBase dispose = null; LuaBase dispose = null;
...@@ -558,8 +559,7 @@ end ...@@ -558,8 +559,7 @@ end
} }
set { set {
int oldTop = LuaLib.LuaGetTop (luaState); int oldTop = LuaLib.LuaGetTop (luaState);
string[] path = fullPath.Split (new char[] { '.' }); string [] path = FullPathToArray (fullPath);
if (path.Length == 1) { if (path.Length == 1) {
translator.Push (luaState, value); translator.Push (luaState, value);
LuaLib.LuaSetGlobal (luaState, fullPath); LuaLib.LuaSetGlobal (luaState, fullPath);
...@@ -806,13 +806,17 @@ end ...@@ -806,13 +806,17 @@ end
LuaLib.LuaSetTable (luaState, -3); LuaLib.LuaSetTable (luaState, -3);
} }
string [] FullPathToArray (string fullPath)
{
return fullPath.SplitWithEscape ('.', '\\').ToArray ();
}
/* /*
* Creates a new table as a global variable or as a field * Creates a new table as a global variable or as a field
* inside an existing table * inside an existing table
*/ */
public void NewTable (string fullPath) public void NewTable (string fullPath)
{ {
string[] path = fullPath.Split (new char[] { '.' }); string [] path = FullPathToArray (fullPath);
int oldTop = LuaLib.LuaGetTop (luaState); int oldTop = LuaLib.LuaGetTop (luaState);
if (path.Length == 1) { if (path.Length == 1) {
...@@ -1089,7 +1093,7 @@ end ...@@ -1089,7 +1093,7 @@ end
{ {
int oldTop = LuaLib.LuaGetTop (luaState); int oldTop = LuaLib.LuaGetTop (luaState);
LuaLib.LuaGetRef (luaState, reference); LuaLib.LuaGetRef (luaState, reference);
object returnValue = GetObject (field.Split (new char[] {'.'})); object returnValue = GetObject (FullPathToArray (field));
LuaLib.LuaSetTop (luaState, oldTop); LuaLib.LuaSetTop (luaState, oldTop);
return returnValue; return returnValue;
} }
...@@ -1117,7 +1121,7 @@ end ...@@ -1117,7 +1121,7 @@ end
{ {
int oldTop = LuaLib.LuaGetTop (luaState); int oldTop = LuaLib.LuaGetTop (luaState);
LuaLib.LuaGetRef (luaState, reference); LuaLib.LuaGetRef (luaState, reference);
SetObject (field.Split (new char[] {'.'}), val); SetObject (FullPathToArray (field), val);
LuaLib.LuaSetTop (luaState, oldTop); LuaLib.LuaSetTop (luaState, oldTop);
} }
......
...@@ -1913,5 +1913,16 @@ namespace NLuaTest ...@@ -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