mirror of
https://github.com/luanti-org/luanti.git
synced 2025-07-02 16:38:41 +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
|
@ -1664,7 +1664,7 @@ Pointabilities read_pointabilities(lua_State *L, int index)
|
|||
std::string name = luaL_checkstring(L, -2);
|
||||
|
||||
// handle groups
|
||||
if(std::string_view(name).substr(0,6)=="group:") {
|
||||
if (str_starts_with(name, "group:")) {
|
||||
pointabilities.node_groups[name.substr(6)] = read_pointability_type(L, -1);
|
||||
} else {
|
||||
pointabilities.nodes[name] = read_pointability_type(L, -1);
|
||||
|
@ -1685,7 +1685,7 @@ Pointabilities read_pointabilities(lua_State *L, int index)
|
|||
std::string name = luaL_checkstring(L, -2);
|
||||
|
||||
// handle groups
|
||||
if(std::string_view(name).substr(0,6)=="group:") {
|
||||
if (str_starts_with(name, "group:")) {
|
||||
pointabilities.object_groups[name.substr(6)] = read_pointability_type(L, -1);
|
||||
} else {
|
||||
pointabilities.objects[name] = read_pointability_type(L, -1);
|
||||
|
|
|
@ -25,6 +25,7 @@ extern "C" {
|
|||
#include <cmath>
|
||||
#include <irr_v2d.h>
|
||||
#include <irr_v3d.h>
|
||||
#include <string_view>
|
||||
#include "c_converter.h"
|
||||
#include "c_types.h"
|
||||
|
||||
|
@ -78,11 +79,16 @@ v3f LuaHelper::readParam(lua_State *L, int index)
|
|||
}
|
||||
|
||||
template <>
|
||||
std::string LuaHelper::readParam(lua_State *L, int index)
|
||||
std::string_view LuaHelper::readParam(lua_State *L, int index)
|
||||
{
|
||||
size_t length;
|
||||
std::string result;
|
||||
const char *str = luaL_checklstring(L, index, &length);
|
||||
result.assign(str, length);
|
||||
return result;
|
||||
return std::string_view(str, length);
|
||||
}
|
||||
|
||||
template <>
|
||||
std::string LuaHelper::readParam(lua_State *L, int index)
|
||||
{
|
||||
auto sv = readParam<std::string_view>(L, index);
|
||||
return std::string(sv); // a copy
|
||||
}
|
||||
|
|
|
@ -19,6 +19,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
extern "C" {
|
||||
#include <lua.h>
|
||||
}
|
||||
|
@ -27,23 +29,22 @@ class LuaHelper
|
|||
{
|
||||
protected:
|
||||
/**
|
||||
* Read a value using a template type T from Lua State L and index
|
||||
*
|
||||
* Read a value using a template type T from Lua state L at index
|
||||
*
|
||||
* @tparam T type to read from Lua
|
||||
* @param L Lua state
|
||||
* @param index Lua Index to read
|
||||
* @param index Lua index to read
|
||||
* @return read value from Lua
|
||||
*/
|
||||
template <typename T>
|
||||
static T readParam(lua_State *L, int index);
|
||||
|
||||
/**
|
||||
* Read a value using a template type T from Lua State L and index
|
||||
* Read a value using a template type T from Lua state L at index
|
||||
*
|
||||
* @tparam T type to read from Lua
|
||||
* @param L Lua state
|
||||
* @param index Lua Index to read
|
||||
* @param index Lua index to read
|
||||
* @param default_value default value to apply if nil
|
||||
* @return read value from Lua or default value if nil
|
||||
*/
|
||||
|
@ -53,3 +54,18 @@ protected:
|
|||
return lua_isnoneornil(L, index) ? default_value : readParam<T>(L, index);
|
||||
}
|
||||
};
|
||||
|
||||
// (only declared for documentation purposes:)
|
||||
|
||||
/**
|
||||
* Read a string from Lua state L at index without copying it.
|
||||
*
|
||||
* Note that the returned string view is only valid as long as the value is on
|
||||
* the stack and has not been modified. Be careful.
|
||||
*
|
||||
* @param L Lua state
|
||||
* @param index Lua index to read
|
||||
* @return string view
|
||||
*/
|
||||
template <>
|
||||
std::string_view LuaHelper::readParam(lua_State *L, int index);
|
||||
|
|
|
@ -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