Browse Source

Add optional byte offset and size parameters to Data:getString.

Sasha Szpakowski 2 years ago
parent
commit
f6416dd584
1 changed files with 13 additions and 1 deletions
  1. 13 1
      src/modules/data/wrap_Data.cpp

+ 13 - 1
src/modules/data/wrap_Data.cpp

@@ -19,6 +19,7 @@
  **/
  **/
 
 
 #include "wrap_Data.h"
 #include "wrap_Data.h"
+#include "common/int.h"
 
 
 // Put the Lua code directly into a raw string literal.
 // Put the Lua code directly into a raw string literal.
 static const char data_lua[] =
 static const char data_lua[] =
@@ -38,7 +39,18 @@ Data *luax_checkdata(lua_State *L, int idx)
 int w_Data_getString(lua_State *L)
 int w_Data_getString(lua_State *L)
 {
 {
 	Data *t = luax_checkdata(L, 1);
 	Data *t = luax_checkdata(L, 1);
-	lua_pushlstring(L, (const char *) t->getData(), t->getSize());
+	int64 offset = (int64) luaL_optnumber(L, 2, 0);
+
+	int64 size = lua_isnoneornil(L, 3)
+		? ((int64) t->getSize() - offset)
+		: (int64) luaL_checknumber(L, 3);
+
+	if (offset < 0 || offset + size > (int64) t->getSize())
+		return luaL_error(L, "The given offset and size parameters don't fit within the Data's size.");
+
+	auto data = (const char *) t->getData() + offset;
+
+	lua_pushlstring(L, data, size);
 	return 1;
 	return 1;
 }
 }