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
|
@ -25,7 +25,7 @@ Dummy database class
|
|||
#include "remoteplayer.h"
|
||||
|
||||
|
||||
bool Database_Dummy::saveBlock(const v3s16 &pos, const std::string &data)
|
||||
bool Database_Dummy::saveBlock(const v3s16 &pos, std::string_view data)
|
||||
{
|
||||
m_database[getBlockAsInteger(pos)] = data;
|
||||
return true;
|
||||
|
@ -128,11 +128,12 @@ bool Database_Dummy::hasModEntry(const std::string &modname, const std::string &
|
|||
}
|
||||
|
||||
bool Database_Dummy::setModEntry(const std::string &modname,
|
||||
const std::string &key, const std::string &value)
|
||||
const std::string &key, std::string_view value)
|
||||
{
|
||||
auto mod_pair = m_mod_storage_database.find(modname);
|
||||
if (mod_pair == m_mod_storage_database.end()) {
|
||||
m_mod_storage_database[modname] = StringMap({{key, value}});
|
||||
auto &map = m_mod_storage_database[modname];
|
||||
map[key] = value;
|
||||
} else {
|
||||
mod_pair->second[key] = value;
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include "database.h"
|
||||
#include "irrlichttypes.h"
|
||||
|
@ -27,7 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
class Database_Dummy : public MapDatabase, public PlayerDatabase, public ModStorageDatabase
|
||||
{
|
||||
public:
|
||||
bool saveBlock(const v3s16 &pos, const std::string &data);
|
||||
bool saveBlock(const v3s16 &pos, std::string_view data);
|
||||
void loadBlock(const v3s16 &pos, std::string *block);
|
||||
bool deleteBlock(const v3s16 &pos);
|
||||
void listAllLoadableBlocks(std::vector<v3s16> &dst);
|
||||
|
@ -43,7 +44,7 @@ public:
|
|||
const std::string &key, std::string *value);
|
||||
bool hasModEntry(const std::string &modname, const std::string &key);
|
||||
bool setModEntry(const std::string &modname,
|
||||
const std::string &key, const std::string &value);
|
||||
const std::string &key,std::string_view value);
|
||||
bool removeModEntry(const std::string &modname, const std::string &key);
|
||||
bool removeModEntries(const std::string &modname);
|
||||
void listMods(std::vector<std::string> *res);
|
||||
|
|
|
@ -428,13 +428,14 @@ bool ModStorageDatabaseFiles::hasModEntry(const std::string &modname, const std:
|
|||
}
|
||||
|
||||
bool ModStorageDatabaseFiles::setModEntry(const std::string &modname,
|
||||
const std::string &key, const std::string &value)
|
||||
const std::string &key, std::string_view value)
|
||||
{
|
||||
Json::Value *meta = getOrCreateJson(modname);
|
||||
if (!meta)
|
||||
return false;
|
||||
|
||||
(*meta)[key] = Json::Value(value);
|
||||
Json::Value value_v(value.data(), value.data() + value.size());
|
||||
(*meta)[key] = std::move(value_v);
|
||||
m_modified.insert(modname);
|
||||
|
||||
return true;
|
||||
|
|
|
@ -84,7 +84,7 @@ public:
|
|||
const std::string &key, std::string *value);
|
||||
virtual bool hasModEntry(const std::string &modname, const std::string &key);
|
||||
virtual bool setModEntry(const std::string &modname,
|
||||
const std::string &key, const std::string &value);
|
||||
const std::string &key, std::string_view value);
|
||||
virtual bool removeModEntry(const std::string &modname, const std::string &key);
|
||||
virtual bool removeModEntries(const std::string &modname);
|
||||
virtual void listMods(std::vector<std::string> *res);
|
||||
|
|
|
@ -53,10 +53,11 @@ Database_LevelDB::Database_LevelDB(const std::string &savedir)
|
|||
m_database.reset(db);
|
||||
}
|
||||
|
||||
bool Database_LevelDB::saveBlock(const v3s16 &pos, const std::string &data)
|
||||
bool Database_LevelDB::saveBlock(const v3s16 &pos, std::string_view data)
|
||||
{
|
||||
leveldb::Slice data_s(data.data(), data.size());
|
||||
leveldb::Status status = m_database->Put(leveldb::WriteOptions(),
|
||||
i64tos(getBlockAsInteger(pos)), data);
|
||||
i64tos(getBlockAsInteger(pos)), data_s);
|
||||
if (!status.ok()) {
|
||||
warningstream << "saveBlock: LevelDB error saving block "
|
||||
<< pos << ": " << status.ToString() << std::endl;
|
||||
|
|
|
@ -34,7 +34,7 @@ public:
|
|||
Database_LevelDB(const std::string &savedir);
|
||||
~Database_LevelDB() = default;
|
||||
|
||||
bool saveBlock(const v3s16 &pos, const std::string &data);
|
||||
bool saveBlock(const v3s16 &pos, std::string_view data);
|
||||
void loadBlock(const v3s16 &pos, std::string *block);
|
||||
bool deleteBlock(const v3s16 &pos);
|
||||
void listAllLoadableBlocks(std::vector<v3s16> &dst);
|
||||
|
|
|
@ -223,7 +223,7 @@ void MapDatabasePostgreSQL::initStatements()
|
|||
"SELECT posX, posY, posZ FROM blocks");
|
||||
}
|
||||
|
||||
bool MapDatabasePostgreSQL::saveBlock(const v3s16 &pos, const std::string &data)
|
||||
bool MapDatabasePostgreSQL::saveBlock(const v3s16 &pos, std::string_view data)
|
||||
{
|
||||
// Verify if we don't overflow the platform integer with the mapblock size
|
||||
if (data.size() > INT_MAX) {
|
||||
|
@ -240,7 +240,7 @@ bool MapDatabasePostgreSQL::saveBlock(const v3s16 &pos, const std::string &data)
|
|||
y = htonl(pos.Y);
|
||||
z = htonl(pos.Z);
|
||||
|
||||
const void *args[] = { &x, &y, &z, data.c_str() };
|
||||
const void *args[] = { &x, &y, &z, data.data() };
|
||||
const int argLen[] = {
|
||||
sizeof(x), sizeof(y), sizeof(z), (int)data.size()
|
||||
};
|
||||
|
@ -940,11 +940,11 @@ bool ModStorageDatabasePostgreSQL::hasModEntry(const std::string &modname,
|
|||
}
|
||||
|
||||
bool ModStorageDatabasePostgreSQL::setModEntry(const std::string &modname,
|
||||
const std::string &key, const std::string &value)
|
||||
const std::string &key, std::string_view value)
|
||||
{
|
||||
verifyDatabase();
|
||||
|
||||
const void *args[] = { modname.c_str(), key.c_str(), value.c_str() };
|
||||
const void *args[] = { modname.c_str(), key.c_str(), value.data() };
|
||||
const int argLen[] = {
|
||||
-1,
|
||||
(int)MYMIN(key.size(), INT_MAX),
|
||||
|
|
|
@ -120,7 +120,7 @@ public:
|
|||
MapDatabasePostgreSQL(const std::string &connect_string);
|
||||
virtual ~MapDatabasePostgreSQL() = default;
|
||||
|
||||
bool saveBlock(const v3s16 &pos, const std::string &data);
|
||||
bool saveBlock(const v3s16 &pos, std::string_view data);
|
||||
void loadBlock(const v3s16 &pos, std::string *block);
|
||||
bool deleteBlock(const v3s16 &pos);
|
||||
void listAllLoadableBlocks(std::vector<v3s16> &dst);
|
||||
|
@ -186,7 +186,7 @@ public:
|
|||
bool getModEntry(const std::string &modname, const std::string &key, std::string *value);
|
||||
bool hasModEntry(const std::string &modname, const std::string &key);
|
||||
bool setModEntry(const std::string &modname,
|
||||
const std::string &key, const std::string &value);
|
||||
const std::string &key, std::string_view value);
|
||||
bool removeModEntry(const std::string &modname, const std::string &key);
|
||||
bool removeModEntries(const std::string &modname);
|
||||
void listMods(std::vector<std::string> *res);
|
||||
|
|
|
@ -91,12 +91,12 @@ void Database_Redis::endSave() {
|
|||
freeReplyObject(reply);
|
||||
}
|
||||
|
||||
bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data)
|
||||
bool Database_Redis::saveBlock(const v3s16 &pos, std::string_view data)
|
||||
{
|
||||
std::string tmp = i64tos(getBlockAsInteger(pos));
|
||||
|
||||
redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HSET %s %s %b",
|
||||
hash.c_str(), tmp.c_str(), data.c_str(), data.size()));
|
||||
hash.c_str(), tmp.c_str(), data.data(), data.size()));
|
||||
if (!reply) {
|
||||
warningstream << "saveBlock: redis command 'HSET' failed on "
|
||||
"block " << pos << ": " << ctx->errstr << std::endl;
|
||||
|
|
|
@ -38,7 +38,7 @@ public:
|
|||
void beginSave();
|
||||
void endSave();
|
||||
|
||||
bool saveBlock(const v3s16 &pos, const std::string &data);
|
||||
bool saveBlock(const v3s16 &pos, std::string_view data);
|
||||
void loadBlock(const v3s16 &pos, std::string *block);
|
||||
bool deleteBlock(const v3s16 &pos);
|
||||
void listAllLoadableBlocks(std::vector<v3s16> &dst);
|
||||
|
|
|
@ -258,7 +258,7 @@ bool MapDatabaseSQLite3::deleteBlock(const v3s16 &pos)
|
|||
return good;
|
||||
}
|
||||
|
||||
bool MapDatabaseSQLite3::saveBlock(const v3s16 &pos, const std::string &data)
|
||||
bool MapDatabaseSQLite3::saveBlock(const v3s16 &pos, std::string_view data)
|
||||
{
|
||||
verifyDatabase();
|
||||
|
||||
|
@ -283,13 +283,8 @@ void MapDatabaseSQLite3::loadBlock(const v3s16 &pos, std::string *block)
|
|||
return;
|
||||
}
|
||||
|
||||
const char *data = (const char *) sqlite3_column_blob(m_stmt_read, 0);
|
||||
size_t len = sqlite3_column_bytes(m_stmt_read, 0);
|
||||
|
||||
if (data)
|
||||
block->assign(data, len);
|
||||
else
|
||||
block->clear();
|
||||
auto data = sqlite_to_blob(m_stmt_read, 0);
|
||||
block->assign(data);
|
||||
|
||||
sqlite3_step(m_stmt_read);
|
||||
// We should never get more than 1 row, so ok to reset
|
||||
|
@ -553,7 +548,7 @@ bool PlayerDatabaseSQLite3::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
|
|||
int_to_sqlite(m_stmt_player_load_inventory_items, 2, invId);
|
||||
while (sqlite3_step(m_stmt_player_load_inventory_items) == SQLITE_ROW) {
|
||||
const std::string itemStr = sqlite_to_string(m_stmt_player_load_inventory_items, 1);
|
||||
if (itemStr.length() > 0) {
|
||||
if (!itemStr.empty()) {
|
||||
ItemStack stack;
|
||||
stack.deSerialize(itemStr);
|
||||
invList->changeItem(sqlite_to_uint(m_stmt_player_load_inventory_items, 0), stack);
|
||||
|
@ -567,7 +562,7 @@ bool PlayerDatabaseSQLite3::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
|
|||
str_to_sqlite(m_stmt_player_metadata_load, 1, sao->getPlayer()->getName());
|
||||
while (sqlite3_step(m_stmt_player_metadata_load) == SQLITE_ROW) {
|
||||
std::string attr = sqlite_to_string(m_stmt_player_metadata_load, 0);
|
||||
std::string value = sqlite_to_string(m_stmt_player_metadata_load, 1);
|
||||
auto value = sqlite_to_string_view(m_stmt_player_metadata_load, 1);
|
||||
|
||||
sao->getMeta().setString(attr, value);
|
||||
}
|
||||
|
@ -592,7 +587,7 @@ void PlayerDatabaseSQLite3::listPlayers(std::vector<std::string> &res)
|
|||
verifyDatabase();
|
||||
|
||||
while (sqlite3_step(m_stmt_player_list) == SQLITE_ROW)
|
||||
res.push_back(sqlite_to_string(m_stmt_player_list, 0));
|
||||
res.emplace_back(sqlite_to_string_view(m_stmt_player_list, 0));
|
||||
|
||||
sqlite3_reset(m_stmt_player_list);
|
||||
}
|
||||
|
@ -669,14 +664,14 @@ bool AuthDatabaseSQLite3::getAuth(const std::string &name, AuthEntry &res)
|
|||
return false;
|
||||
}
|
||||
res.id = sqlite_to_uint(m_stmt_read, 0);
|
||||
res.name = sqlite_to_string(m_stmt_read, 1);
|
||||
res.password = sqlite_to_string(m_stmt_read, 2);
|
||||
res.name = sqlite_to_string_view(m_stmt_read, 1);
|
||||
res.password = sqlite_to_string_view(m_stmt_read, 2);
|
||||
res.last_login = sqlite_to_int64(m_stmt_read, 3);
|
||||
sqlite3_reset(m_stmt_read);
|
||||
|
||||
int64_to_sqlite(m_stmt_read_privs, 1, res.id);
|
||||
while (sqlite3_step(m_stmt_read_privs) == SQLITE_ROW) {
|
||||
res.privileges.emplace_back(sqlite_to_string(m_stmt_read_privs, 0));
|
||||
res.privileges.emplace_back(sqlite_to_string_view(m_stmt_read_privs, 0));
|
||||
}
|
||||
sqlite3_reset(m_stmt_read_privs);
|
||||
|
||||
|
@ -741,7 +736,7 @@ void AuthDatabaseSQLite3::listNames(std::vector<std::string> &res)
|
|||
verifyDatabase();
|
||||
|
||||
while (sqlite3_step(m_stmt_list_names) == SQLITE_ROW) {
|
||||
res.push_back(sqlite_to_string(m_stmt_list_names, 0));
|
||||
res.emplace_back(sqlite_to_string_view(m_stmt_list_names, 0));
|
||||
}
|
||||
sqlite3_reset(m_stmt_list_names);
|
||||
}
|
||||
|
@ -815,11 +810,9 @@ void ModStorageDatabaseSQLite3::getModEntries(const std::string &modname, String
|
|||
|
||||
str_to_sqlite(m_stmt_get_all, 1, modname);
|
||||
while (sqlite3_step(m_stmt_get_all) == SQLITE_ROW) {
|
||||
const char *key_data = (const char *) sqlite3_column_blob(m_stmt_get_all, 0);
|
||||
size_t key_len = sqlite3_column_bytes(m_stmt_get_all, 0);
|
||||
const char *value_data = (const char *) sqlite3_column_blob(m_stmt_get_all, 1);
|
||||
size_t value_len = sqlite3_column_bytes(m_stmt_get_all, 1);
|
||||
(*storage)[std::string(key_data, key_len)] = std::string(value_data, value_len);
|
||||
auto key = sqlite_to_blob(m_stmt_get_all, 0);
|
||||
auto value = sqlite_to_blob(m_stmt_get_all, 1);
|
||||
(*storage)[std::string(key)].assign(value);
|
||||
}
|
||||
sqlite3_vrfy(sqlite3_errcode(m_database), SQLITE_DONE);
|
||||
|
||||
|
@ -833,9 +826,8 @@ void ModStorageDatabaseSQLite3::getModKeys(const std::string &modname,
|
|||
|
||||
str_to_sqlite(m_stmt_get_keys, 1, modname);
|
||||
while (sqlite3_step(m_stmt_get_keys) == SQLITE_ROW) {
|
||||
const char *key_data = (const char *) sqlite3_column_blob(m_stmt_get_keys, 0);
|
||||
size_t key_len = sqlite3_column_bytes(m_stmt_get_keys, 0);
|
||||
storage->emplace_back(key_data, key_len);
|
||||
auto key = sqlite_to_blob(m_stmt_get_keys, 0);
|
||||
storage->emplace_back(key);
|
||||
}
|
||||
sqlite3_vrfy(sqlite3_errcode(m_database), SQLITE_DONE);
|
||||
|
||||
|
@ -852,9 +844,8 @@ bool ModStorageDatabaseSQLite3::getModEntry(const std::string &modname,
|
|||
"Internal error: failed to bind query at " __FILE__ ":" TOSTRING(__LINE__));
|
||||
bool found = sqlite3_step(m_stmt_get) == SQLITE_ROW;
|
||||
if (found) {
|
||||
const char *value_data = (const char *) sqlite3_column_blob(m_stmt_get, 0);
|
||||
size_t value_len = sqlite3_column_bytes(m_stmt_get, 0);
|
||||
value->assign(value_data, value_len);
|
||||
auto sv = sqlite_to_blob(m_stmt_get, 0);
|
||||
value->assign(sv);
|
||||
sqlite3_step(m_stmt_get);
|
||||
}
|
||||
|
||||
|
@ -881,7 +872,7 @@ bool ModStorageDatabaseSQLite3::hasModEntry(const std::string &modname,
|
|||
}
|
||||
|
||||
bool ModStorageDatabaseSQLite3::setModEntry(const std::string &modname,
|
||||
const std::string &key, const std::string &value)
|
||||
const std::string &key, std::string_view value)
|
||||
{
|
||||
verifyDatabase();
|
||||
|
||||
|
|
|
@ -44,14 +44,9 @@ protected:
|
|||
void verifyDatabase();
|
||||
|
||||
// Convertors
|
||||
inline void str_to_sqlite(sqlite3_stmt *s, int iCol, const std::string &str) const
|
||||
inline void str_to_sqlite(sqlite3_stmt *s, int iCol, std::string_view str) const
|
||||
{
|
||||
sqlite3_vrfy(sqlite3_bind_text(s, iCol, str.c_str(), str.size(), NULL));
|
||||
}
|
||||
|
||||
inline void str_to_sqlite(sqlite3_stmt *s, int iCol, const char *str) const
|
||||
{
|
||||
sqlite3_vrfy(sqlite3_bind_text(s, iCol, str, strlen(str), NULL));
|
||||
sqlite3_vrfy(sqlite3_bind_text(s, iCol, str.data(), str.size(), NULL));
|
||||
}
|
||||
|
||||
inline void int_to_sqlite(sqlite3_stmt *s, int iCol, int val) const
|
||||
|
@ -69,10 +64,28 @@ protected:
|
|||
sqlite3_vrfy(sqlite3_bind_double(s, iCol, val));
|
||||
}
|
||||
|
||||
inline std::string sqlite_to_string(sqlite3_stmt *s, int iCol)
|
||||
// Note that the return value is only valid until the statement is stepped or reset.
|
||||
inline std::string_view sqlite_to_string_view(sqlite3_stmt *s, int iCol)
|
||||
{
|
||||
const char* text = reinterpret_cast<const char*>(sqlite3_column_text(s, iCol));
|
||||
return std::string(text ? text : "");
|
||||
return text ? std::string_view(text) : std::string_view();
|
||||
}
|
||||
|
||||
// Avoid using this in favor of `sqlite_to_string_view`.
|
||||
inline std::string sqlite_to_string(sqlite3_stmt *s, int iCol)
|
||||
{
|
||||
return std::string(sqlite_to_string_view(s, iCol));
|
||||
}
|
||||
|
||||
// Converts a BLOB-type column into a string_view (null byte safe).
|
||||
// Note that the return value is only valid until the statement is stepped or reset.
|
||||
inline std::string_view sqlite_to_blob(sqlite3_stmt *s, int iCol)
|
||||
{
|
||||
const char *data = reinterpret_cast<const char*>(sqlite3_column_blob(s, iCol));
|
||||
if (!data)
|
||||
return std::string_view();
|
||||
size_t len = sqlite3_column_bytes(s, iCol);
|
||||
return std::string_view(data, len);
|
||||
}
|
||||
|
||||
inline s32 sqlite_to_int(sqlite3_stmt *s, int iCol)
|
||||
|
@ -107,13 +120,16 @@ protected:
|
|||
}
|
||||
|
||||
// Query verifiers helpers
|
||||
inline void sqlite3_vrfy(int s, const std::string &m = "", int r = SQLITE_OK) const
|
||||
inline void sqlite3_vrfy(int s, std::string_view m = "", int r = SQLITE_OK) const
|
||||
{
|
||||
if (s != r)
|
||||
throw DatabaseException(m + ": " + sqlite3_errmsg(m_database));
|
||||
if (s != r) {
|
||||
std::string msg(m);
|
||||
msg.append(": ").append(sqlite3_errmsg(m_database));
|
||||
throw DatabaseException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
inline void sqlite3_vrfy(const int s, const int r, const std::string &m = "") const
|
||||
inline void sqlite3_vrfy(const int s, const int r, std::string_view m = "") const
|
||||
{
|
||||
sqlite3_vrfy(s, m, r);
|
||||
}
|
||||
|
@ -146,7 +162,7 @@ public:
|
|||
MapDatabaseSQLite3(const std::string &savedir);
|
||||
virtual ~MapDatabaseSQLite3();
|
||||
|
||||
bool saveBlock(const v3s16 &pos, const std::string &data);
|
||||
bool saveBlock(const v3s16 &pos, std::string_view data);
|
||||
void loadBlock(const v3s16 &pos, std::string *block);
|
||||
bool deleteBlock(const v3s16 &pos);
|
||||
void listAllLoadableBlocks(std::vector<v3s16> &dst);
|
||||
|
@ -245,7 +261,7 @@ public:
|
|||
const std::string &key, std::string *value);
|
||||
virtual bool hasModEntry(const std::string &modname, const std::string &key);
|
||||
virtual bool setModEntry(const std::string &modname,
|
||||
const std::string &key, const std::string &value);
|
||||
const std::string &key,std::string_view value);
|
||||
virtual bool removeModEntry(const std::string &modname, const std::string &key);
|
||||
virtual bool removeModEntries(const std::string &modname);
|
||||
virtual void listMods(std::vector<std::string> *res);
|
||||
|
|
|
@ -19,12 +19,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include "irr_v3d.h"
|
||||
#include "irrlichttypes.h"
|
||||
#include "util/basic_macros.h"
|
||||
#include "util/string.h"
|
||||
|
||||
class Database
|
||||
|
@ -40,7 +39,7 @@ class MapDatabase : public Database
|
|||
public:
|
||||
virtual ~MapDatabase() = default;
|
||||
|
||||
virtual bool saveBlock(const v3s16 &pos, const std::string &data) = 0;
|
||||
virtual bool saveBlock(const v3s16 &pos, std::string_view data) = 0;
|
||||
virtual void loadBlock(const v3s16 &pos, std::string *block) = 0;
|
||||
virtual bool deleteBlock(const v3s16 &pos) = 0;
|
||||
|
||||
|
@ -97,7 +96,7 @@ public:
|
|||
virtual bool getModEntry(const std::string &modname,
|
||||
const std::string &key, std::string *value) = 0;
|
||||
virtual bool setModEntry(const std::string &modname,
|
||||
const std::string &key, const std::string &value) = 0;
|
||||
const std::string &key, std::string_view value) = 0;
|
||||
virtual bool removeModEntry(const std::string &modname, const std::string &key) = 0;
|
||||
virtual bool removeModEntries(const std::string &modname) = 0;
|
||||
virtual void listMods(std::vector<std::string> *res) = 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue