1
0
Fork 0
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:
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

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