1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-11 17:51:04 +00:00

Introduce std::string_view into wider use (#14368)

This commit is contained in:
sfan5 2024-02-17 15:35:33 +01:00 committed by GitHub
parent fa47af737f
commit 6ca214fefc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
74 changed files with 501 additions and 456 deletions

View file

@ -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;
}