mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Introduce std::string_view
into wider use (#14368)
This commit is contained in:
parent
fa47af737f
commit
6ca214fefc
74 changed files with 501 additions and 456 deletions
|
@ -163,6 +163,9 @@ int LuaItemStack::l_get_metadata(lua_State *L)
|
|||
NO_MAP_LOCK_REQUIRED;
|
||||
LuaItemStack *o = checkObject<LuaItemStack>(L, 1);
|
||||
ItemStack &item = o->m_stack;
|
||||
|
||||
log_deprecated(L, "ItemStack:get_metadata is deprecated", 1, true);
|
||||
|
||||
const std::string &value = item.metadata.getString("");
|
||||
lua_pushlstring(L, value.c_str(), value.size());
|
||||
return 1;
|
||||
|
@ -176,6 +179,8 @@ int LuaItemStack::l_set_metadata(lua_State *L)
|
|||
LuaItemStack *o = checkObject<LuaItemStack>(L, 1);
|
||||
ItemStack &item = o->m_stack;
|
||||
|
||||
log_deprecated(L, "ItemStack:set_metadata is deprecated", 1, true);
|
||||
|
||||
size_t len = 0;
|
||||
const char *ptr = luaL_checklstring(L, 2, &len);
|
||||
item.metadata.setString("", std::string(ptr, len));
|
||||
|
|
|
@ -115,9 +115,7 @@ int MetaDataRef::l_set_string(lua_State *L)
|
|||
|
||||
MetaDataRef *ref = checkAnyMetadata(L, 1);
|
||||
std::string name = luaL_checkstring(L, 2);
|
||||
size_t len = 0;
|
||||
const char *s = lua_tolstring(L, 3, &len);
|
||||
std::string str(s, len);
|
||||
auto str = readParam<std::string_view>(L, 3);
|
||||
|
||||
IMetadata *meta = ref->getmeta(!str.empty());
|
||||
if (meta != NULL && meta->setString(name, str))
|
||||
|
@ -300,9 +298,8 @@ bool MetaDataRef::handleFromTable(lua_State *L, int table, IMetadata *meta)
|
|||
while (lua_next(L, fieldstable) != 0) {
|
||||
// key at index -2 and value at index -1
|
||||
std::string name = readParam<std::string>(L, -2);
|
||||
size_t cl;
|
||||
const char *cs = lua_tolstring(L, -1, &cl);
|
||||
meta->setString(name, std::string(cs, cl));
|
||||
auto value = readParam<std::string_view>(L, -1);
|
||||
meta->setString(name, value);
|
||||
lua_pop(L, 1); // Remove value, keep key for next iteration
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
|
|
|
@ -318,8 +318,7 @@ int ModApiUtil::l_compress(lua_State *L)
|
|||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
|
||||
size_t size;
|
||||
const char *data = luaL_checklstring(L, 1, &size);
|
||||
auto data = readParam<std::string_view>(L, 1);
|
||||
|
||||
LuaCompressMethod method = get_compress_method(L, 2);
|
||||
|
||||
|
@ -330,13 +329,13 @@ int ModApiUtil::l_compress(lua_State *L)
|
|||
if (!lua_isnoneornil(L, 3))
|
||||
level = readParam<int>(L, 3);
|
||||
|
||||
compressZlib(reinterpret_cast<const u8 *>(data), size, os, level);
|
||||
compressZlib(data, os, level);
|
||||
} else if (method == LUA_COMPRESS_METHOD_ZSTD) {
|
||||
int level = ZSTD_CLEVEL_DEFAULT;
|
||||
if (!lua_isnoneornil(L, 3))
|
||||
level = readParam<int>(L, 3);
|
||||
|
||||
compressZstd(reinterpret_cast<const u8 *>(data), size, os, level);
|
||||
compressZstd(data, os, level);
|
||||
}
|
||||
|
||||
std::string out = os.str();
|
||||
|
@ -350,12 +349,12 @@ int ModApiUtil::l_decompress(lua_State *L)
|
|||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
|
||||
size_t size;
|
||||
const char *data = luaL_checklstring(L, 1, &size);
|
||||
auto data = readParam<std::string_view>(L, 1);
|
||||
|
||||
LuaCompressMethod method = get_compress_method(L, 2);
|
||||
|
||||
std::istringstream is(std::string(data, size), std::ios_base::binary);
|
||||
// FIXME: zero copy possible in c++26 or with custom rdbuf
|
||||
std::istringstream is(std::string(data), std::ios_base::binary);
|
||||
std::ostringstream os(std::ios_base::binary);
|
||||
|
||||
if (method == LUA_COMPRESS_METHOD_DEFLATE) {
|
||||
|
@ -375,10 +374,9 @@ int ModApiUtil::l_encode_base64(lua_State *L)
|
|||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
|
||||
size_t size;
|
||||
const char *data = luaL_checklstring(L, 1, &size);
|
||||
auto data = readParam<std::string_view>(L, 1);
|
||||
|
||||
std::string out = base64_encode((const unsigned char *)(data), size);
|
||||
std::string out = base64_encode(data);
|
||||
|
||||
lua_pushlstring(L, out.data(), out.size());
|
||||
return 1;
|
||||
|
@ -389,9 +387,7 @@ int ModApiUtil::l_decode_base64(lua_State *L)
|
|||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
|
||||
size_t size;
|
||||
const char *d = luaL_checklstring(L, 1, &size);
|
||||
const std::string data = std::string(d, size);
|
||||
auto data = readParam<std::string_view>(L, 1);
|
||||
|
||||
if (!base64_is_valid(data)) {
|
||||
lua_pushnil(L);
|
||||
|
@ -487,12 +483,11 @@ int ModApiUtil::l_safe_file_write(lua_State *L)
|
|||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
size_t size;
|
||||
const char *content = luaL_checklstring(L, 2, &size);
|
||||
auto content = readParam<std::string_view>(L, 2);
|
||||
|
||||
CHECK_SECURE_PATH(L, path, true);
|
||||
|
||||
bool ret = fs::safeWriteToFile(path, std::string(content, size));
|
||||
bool ret = fs::safeWriteToFile(path, content);
|
||||
lua_pushboolean(L, ret);
|
||||
|
||||
return 1;
|
||||
|
@ -549,18 +544,16 @@ int ModApiUtil::l_get_version(lua_State *L)
|
|||
int ModApiUtil::l_sha1(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
size_t size;
|
||||
const char *data = luaL_checklstring(L, 1, &size);
|
||||
|
||||
auto data = readParam<std::string_view>(L, 1);
|
||||
bool hex = !lua_isboolean(L, 2) || !readParam<bool>(L, 2);
|
||||
|
||||
// Compute actual checksum of data
|
||||
std::string data_sha1;
|
||||
{
|
||||
SHA1 ctx;
|
||||
ctx.addBytes(data, size);
|
||||
unsigned char *data_tmpdigest = ctx.getDigest();
|
||||
data_sha1.assign((char*) data_tmpdigest, 20);
|
||||
free(data_tmpdigest);
|
||||
ctx.addBytes(data);
|
||||
data_sha1 = ctx.getDigest();
|
||||
}
|
||||
|
||||
if (hex) {
|
||||
|
@ -654,8 +647,8 @@ int ModApiUtil::l_urlencode(lua_State *L)
|
|||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
|
||||
const char *value = luaL_checkstring(L, 1);
|
||||
lua_pushstring(L, urlencode(value).c_str());
|
||||
auto s = readParam<std::string_view>(L, 1);
|
||||
lua_pushstring(L, urlencode(s).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue