From 788c5d6c6e39aee876e0f9c337671ab29efef296 Mon Sep 17 00:00:00 2001 From: JailDoctor Date: Mon, 13 Dec 2021 14:52:38 +0100 Subject: [PATCH] [FEAT] substr --- ascii.cpp | 10 ++++++++-- ascii.h | 1 + lua.cpp | 8 ++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ascii.cpp b/ascii.cpp index 737ff83..af3b75d 100644 --- a/ascii.cpp +++ b/ascii.cpp @@ -423,9 +423,9 @@ int rnd(int x) { srand(x); }*/ -char tostr_tmp[256]; +char str_tmp[256]; const char* tostr(float val) { - return SDL_itoa(val, tostr_tmp, 10); + return SDL_itoa(val, str_tmp, 10); } void debug_set_prompt() { @@ -489,6 +489,12 @@ const char* chr(uint8_t ascii) { return chr_trans; } +const char* substr(const char* str, uint8_t start, uint8_t length) { + memcpy(str_tmp, &str[start], length); + str_tmp[length] = '\0'; + return str_tmp; +} + void setchar(uint8_t index, uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7) { mem[MEM_CHAR_OFFSET+index*8] = b0; mem[MEM_CHAR_OFFSET+index*8+1] = b1; diff --git a/ascii.h b/ascii.h index 086bde6..860a21c 100644 --- a/ascii.h +++ b/ascii.h @@ -163,6 +163,7 @@ void debug_get_cmd(); uint8_t ascii(const char *str, uint8_t index); const char* chr(uint8_t ascii); +const char* substr(const char* str, uint8_t start, uint8_t length); void setchar(uint8_t index, uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7); uint8_t peek(uint16_t addr); diff --git a/lua.cpp b/lua.cpp index 1fafc89..3ec6e48 100644 --- a/lua.cpp +++ b/lua.cpp @@ -183,6 +183,13 @@ extern "C" { lua_pushinteger(L, strlen(str)); return 1; } + static int cpp_substr(lua_State *L) { + const char* str = luaL_checkstring(L, 1); + int start = luaL_checknumber(L, 2); + int length = luaL_checknumber(L, 3); + lua_pushstring(L, substr(str, start, length)); + return 1; + } static int cpp_setchar(lua_State *L) { int index = luaL_checkinteger(L, 1); @@ -346,6 +353,7 @@ void lua_init(const char* filename, const bool start_playing) { lua_pushcfunction(L,cpp_ascii); lua_setglobal(L, "ascii"); lua_pushcfunction(L,cpp_chr); lua_setglobal(L, "chr"); lua_pushcfunction(L,cpp_strlen); lua_setglobal(L, "strlen"); + lua_pushcfunction(L,cpp_substr); lua_setglobal(L, "substr"); lua_pushcfunction(L,cpp_setchar); lua_setglobal(L, "setchar"); lua_pushcfunction(L,cpp_peek); lua_setglobal(L, "peek");