Skip to content

Commit 8a73cfa

Browse files
committed
Allows pushing binary data that is not null terminated (#4)
1 parent ffa8b10 commit 8a73cfa

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

include/LuaState.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,15 @@ namespace lua {
225225
set<const char*>(key, value);
226226
}
227227

228-
void setString(lua::String key, const std::string& string) const {
229-
stack::push_str(_luaState, string.c_str(), string.length());
228+
void setData(lua::String key, const char* value, size_t length) const {
229+
stack::push_str(_luaState, value, length);
230230
lua_setglobal(_luaState, key);
231231
}
232232

233+
void setString(lua::String key, const std::string& string) const {
234+
setData(key, string.c_str(), string.length());
235+
}
236+
233237
void set(lua::String key, const std::string& value) const {
234238
setString(key, value);
235239
}

include/LuaValue.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,13 @@ namespace lua {
331331
set<const char*>(key, value);
332332
}
333333

334+
template<typename K>
335+
void setData(K key, const char* value, size_t length) const {
336+
stack::push(_stack->state, key);
337+
stack::push_str(_stack->state, value, length);
338+
lua_settable(_stack->state, _stack->top + _stack->pushed - _stack->grouped);
339+
}
340+
334341
template<typename K>
335342
void setString(K key, const std::string& string) const {
336343
stack::push(_stack->state, key);

test/types_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@ int main(int argc, char** argv)
252252
assert((stringValue = state["text"].toString()) == "hello");
253253
assert((stringValue = state["text"].toString()) != "bannana");
254254

255+
char binaryData[3];
256+
binaryData[0] = 'a';
257+
binaryData[1] = 'b';
258+
binaryData[2] = 'c';
259+
state.setData("binary", binaryData, 3);
260+
assert(strcmp(state["binary"], "abc") == 0);
261+
255262
state.checkMemLeaks();
256263
return 0;
257264
}

0 commit comments

Comments
 (0)