mirror of
https://github.com/OpenTTD/OpenTTD
synced 2026-01-17 01:12:39 +01:00
Codechange: use std::string_view for sq_getstring
This commit is contained in:
@@ -45,10 +45,10 @@ bool ScriptAdminMakeJSON(nlohmann::json &json, HSQUIRRELVM vm, SQInteger index,
|
||||
}
|
||||
|
||||
case OT_STRING: {
|
||||
const SQChar *buf;
|
||||
sq_getstring(vm, index, &buf);
|
||||
std::string_view view;
|
||||
sq_getstring(vm, index, view);
|
||||
|
||||
json = std::string(buf);
|
||||
json = view;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -78,9 +78,9 @@ bool ScriptAdminMakeJSON(nlohmann::json &json, HSQUIRRELVM vm, SQInteger index,
|
||||
sq_pushnull(vm);
|
||||
while (SQ_SUCCEEDED(sq_next(vm, index - 1))) {
|
||||
sq_tostring(vm, -2);
|
||||
const SQChar *buf;
|
||||
sq_getstring(vm, -1, &buf);
|
||||
std::string key = std::string(buf);
|
||||
std::string_view view;
|
||||
sq_getstring(vm, -1, view);
|
||||
std::string key{view};
|
||||
sq_pop(vm, 1);
|
||||
|
||||
nlohmann::json value;
|
||||
|
||||
@@ -60,10 +60,10 @@ SQInteger ScriptText::_SetParam(int parameter, HSQUIRRELVM vm)
|
||||
|
||||
switch (sq_gettype(vm, -1)) {
|
||||
case OT_STRING: {
|
||||
const SQChar *value;
|
||||
sq_getstring(vm, -1, &value);
|
||||
std::string_view view;
|
||||
sq_getstring(vm, -1, view);
|
||||
|
||||
this->param[parameter] = StrMakeValid(value);
|
||||
this->param[parameter] = StrMakeValid(view);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -135,10 +135,10 @@ SQInteger ScriptText::_set(HSQUIRRELVM vm)
|
||||
int32_t k;
|
||||
|
||||
if (sq_gettype(vm, 2) == OT_STRING) {
|
||||
const SQChar *key_string;
|
||||
sq_getstring(vm, 2, &key_string);
|
||||
std::string_view view;
|
||||
sq_getstring(vm, 2, view);
|
||||
|
||||
std::string str = StrMakeValid(key_string);
|
||||
std::string str = StrMakeValid(view);
|
||||
if (!str.starts_with("param_") || str.size() > 8) return SQ_ERROR;
|
||||
|
||||
k = stoi(str.substr(6));
|
||||
|
||||
@@ -105,13 +105,13 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
|
||||
/* Read the table, and find all properties we care about */
|
||||
sq_pushnull(vm);
|
||||
while (SQ_SUCCEEDED(sq_next(vm, -2))) {
|
||||
const SQChar *key_string;
|
||||
if (SQ_FAILED(sq_getstring(vm, -2, &key_string))) return SQ_ERROR;
|
||||
std::string_view key_string;
|
||||
if (SQ_FAILED(sq_getstring(vm, -2, key_string))) return SQ_ERROR;
|
||||
std::string key = StrMakeValid(key_string);
|
||||
|
||||
if (key == "name") {
|
||||
const SQChar *sqvalue;
|
||||
if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR;
|
||||
std::string_view sqvalue;
|
||||
if (SQ_FAILED(sq_getstring(vm, -1, sqvalue))) return SQ_ERROR;
|
||||
|
||||
/* Don't allow '=' and ',' in configure setting names, as we need those
|
||||
* 2 chars to nicely store the settings as a string. */
|
||||
@@ -120,8 +120,8 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
|
||||
std::replace_if(config.name.begin(), config.name.end(), replace_with_underscore, '_');
|
||||
present.Set(ScriptConfigItemKey::Name);
|
||||
} else if (key == "description") {
|
||||
const SQChar *sqdescription;
|
||||
if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
|
||||
std::string_view sqdescription;
|
||||
if (SQ_FAILED(sq_getstring(vm, -1, sqdescription))) return SQ_ERROR;
|
||||
config.description = StrMakeValid(sqdescription);
|
||||
present.Set(ScriptConfigItemKey::Description);
|
||||
} else if (key == "min_value") {
|
||||
@@ -199,9 +199,9 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
|
||||
|
||||
SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
|
||||
{
|
||||
const SQChar *setting_name_str;
|
||||
if (SQ_FAILED(sq_getstring(vm, -2, &setting_name_str))) return SQ_ERROR;
|
||||
std::string setting_name = StrMakeValid(setting_name_str);
|
||||
std::string_view setting_name_view;
|
||||
if (SQ_FAILED(sq_getstring(vm, -2, setting_name_view))) return SQ_ERROR;
|
||||
std::string setting_name = StrMakeValid(setting_name_view);
|
||||
|
||||
ScriptConfigItem *config = nullptr;
|
||||
for (auto &item : this->config_list) {
|
||||
@@ -217,18 +217,18 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
|
||||
/* Read the table and find all labels */
|
||||
sq_pushnull(vm);
|
||||
while (SQ_SUCCEEDED(sq_next(vm, -2))) {
|
||||
const SQChar *key_string;
|
||||
const SQChar *label;
|
||||
if (SQ_FAILED(sq_getstring(vm, -2, &key_string))) return SQ_ERROR;
|
||||
if (SQ_FAILED(sq_getstring(vm, -1, &label))) return SQ_ERROR;
|
||||
std::string_view key_string;
|
||||
std::string_view label;
|
||||
if (SQ_FAILED(sq_getstring(vm, -2, key_string))) return SQ_ERROR;
|
||||
if (SQ_FAILED(sq_getstring(vm, -1, label))) return SQ_ERROR;
|
||||
/* Because squirrel doesn't support identifiers starting with a digit,
|
||||
* we skip the first character. */
|
||||
key_string++;
|
||||
key_string.remove_prefix(1);
|
||||
int sign = 1;
|
||||
if (*key_string == '_') {
|
||||
if (key_string.starts_with('_')) {
|
||||
/* When the second character is '_', it indicates the value is negative. */
|
||||
sign = -1;
|
||||
key_string++;
|
||||
key_string.remove_prefix(1);
|
||||
}
|
||||
auto key = ParseInteger<int>(key_string);
|
||||
if (!key.has_value()) return SQ_ERROR;
|
||||
|
||||
@@ -40,10 +40,10 @@ ScriptStorage::~ScriptStorage() = default;
|
||||
* @param error_msg Is this an error message?
|
||||
* @param message The actual message text.
|
||||
*/
|
||||
static void PrintFunc(bool error_msg, const std::string &message)
|
||||
static void PrintFunc(bool error_msg, std::string_view message)
|
||||
{
|
||||
/* Convert to OpenTTD internal capable string */
|
||||
ScriptController::Print(error_msg, message);
|
||||
ScriptController::Print(error_msg, std::string{message});
|
||||
}
|
||||
|
||||
ScriptInstance::ScriptInstance(std::string_view api_name)
|
||||
@@ -383,9 +383,9 @@ static const SaveLoad _script_byte[] = {
|
||||
_script_sl_byte = SQSL_STRING;
|
||||
SlObject(nullptr, _script_byte);
|
||||
}
|
||||
const SQChar *buf;
|
||||
sq_getstring(vm, index, &buf);
|
||||
size_t len = strlen(buf) + 1;
|
||||
std::string_view view;
|
||||
sq_getstring(vm, index, view);
|
||||
size_t len = view.size() + 1;
|
||||
if (len >= 255) {
|
||||
ScriptLog::Error("Maximum string length is 254 chars. No data saved.");
|
||||
return false;
|
||||
@@ -393,7 +393,7 @@ static const SaveLoad _script_byte[] = {
|
||||
if (!test) {
|
||||
_script_sl_byte = (uint8_t)len;
|
||||
SlObject(nullptr, _script_byte);
|
||||
SlCopy(const_cast<char *>(buf), len, SLE_CHAR);
|
||||
SlCopy(const_cast<char *>(view.data()), len, SLE_CHAR);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -678,10 +678,10 @@ bool ScriptInstance::IsPaused()
|
||||
case SQSL_INSTANCE: {
|
||||
SQInteger top = sq_gettop(this->vm);
|
||||
LoadObjects(this->vm, this->data);
|
||||
const SQChar *buf;
|
||||
sq_getstring(this->vm, -1, &buf);
|
||||
std::string_view view;
|
||||
sq_getstring(this->vm, -1, view);
|
||||
Squirrel *engine = static_cast<Squirrel *>(sq_getforeignptr(this->vm));
|
||||
std::string class_name = fmt::format("{}{}", engine->GetAPIName(), buf);
|
||||
std::string class_name = fmt::format("{}{}", engine->GetAPIName(), view);
|
||||
sq_pushroottable(this->vm);
|
||||
sq_pushstring(this->vm, class_name);
|
||||
if (SQ_FAILED(sq_get(this->vm, -2))) throw Script_FatalError(fmt::format("'{}' doesn't exist", class_name));
|
||||
|
||||
@@ -194,7 +194,7 @@ void Squirrel::CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *so
|
||||
}
|
||||
}
|
||||
|
||||
void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, const std::string &s)
|
||||
void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, std::string_view s)
|
||||
{
|
||||
/* Check if we have a custom print function */
|
||||
SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
|
||||
@@ -205,7 +205,7 @@ void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, const std::string &s)
|
||||
}
|
||||
}
|
||||
|
||||
void Squirrel::RunError(HSQUIRRELVM vm, const SQChar *error)
|
||||
void Squirrel::RunError(HSQUIRRELVM vm, std::string_view error)
|
||||
{
|
||||
/* Set the print function to something that prints to stderr */
|
||||
SQPRINTFUNCTION pf = sq_getprintfunc(vm);
|
||||
@@ -229,11 +229,11 @@ void Squirrel::RunError(HSQUIRRELVM vm, const SQChar *error)
|
||||
|
||||
SQInteger Squirrel::_RunError(HSQUIRRELVM vm)
|
||||
{
|
||||
const SQChar *sErr = nullptr;
|
||||
std::string_view view;
|
||||
|
||||
if (sq_gettop(vm) >= 1) {
|
||||
if (SQ_SUCCEEDED(sq_getstring(vm, -1, &sErr))) {
|
||||
Squirrel::RunError(vm, sErr);
|
||||
if (SQ_SUCCEEDED(sq_getstring(vm, -1, view))) {
|
||||
Squirrel::RunError(vm, view);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -242,7 +242,7 @@ SQInteger Squirrel::_RunError(HSQUIRRELVM vm)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Squirrel::PrintFunc(HSQUIRRELVM vm, const std::string &s)
|
||||
void Squirrel::PrintFunc(HSQUIRRELVM vm, std::string_view s)
|
||||
{
|
||||
/* Check if we have a custom print function */
|
||||
SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
|
||||
|
||||
@@ -26,7 +26,7 @@ class Squirrel {
|
||||
friend class ScriptInstance;
|
||||
|
||||
private:
|
||||
typedef void (SQPrintFunc)(bool error_msg, const std::string &message);
|
||||
using SQPrintFunc = void (bool error_msg, std::string_view message);
|
||||
|
||||
HSQUIRRELVM vm; ///< The VirtualMachine instance for squirrel
|
||||
void *global_pointer; ///< Can be set by who ever initializes Squirrel
|
||||
@@ -60,17 +60,17 @@ protected:
|
||||
/**
|
||||
* The RunError handler.
|
||||
*/
|
||||
static void RunError(HSQUIRRELVM vm, const SQChar *error);
|
||||
static void RunError(HSQUIRRELVM vm, std::string_view error);
|
||||
|
||||
/**
|
||||
* If a user runs 'print' inside a script, this function gets the params.
|
||||
*/
|
||||
static void PrintFunc(HSQUIRRELVM vm, const std::string &s);
|
||||
static void PrintFunc(HSQUIRRELVM vm, std::string_view s);
|
||||
|
||||
/**
|
||||
* If an error has to be print, this function is called.
|
||||
*/
|
||||
static void ErrorPrintFunc(HSQUIRRELVM vm, const std::string &s);
|
||||
static void ErrorPrintFunc(HSQUIRRELVM vm, std::string_view s);
|
||||
|
||||
public:
|
||||
Squirrel(std::string_view api_name);
|
||||
|
||||
@@ -110,9 +110,9 @@ namespace SQConvert {
|
||||
/* Convert what-ever there is as parameter to a string */
|
||||
sq_tostring(vm, index);
|
||||
|
||||
const SQChar *tmp;
|
||||
sq_getstring(vm, -1, &tmp);
|
||||
std::string result = StrMakeValid(tmp);
|
||||
std::string_view view;
|
||||
sq_getstring(vm, -1, view);
|
||||
std::string result = StrMakeValid(view);
|
||||
sq_poptop(vm);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -41,9 +41,9 @@ SQInteger SquirrelStd::max(HSQUIRRELVM vm)
|
||||
SQInteger SquirrelStd::require(HSQUIRRELVM vm)
|
||||
{
|
||||
SQInteger top = sq_gettop(vm);
|
||||
const SQChar *filename;
|
||||
std::string_view filename;
|
||||
|
||||
sq_getstring(vm, 2, &filename);
|
||||
sq_getstring(vm, 2, filename);
|
||||
|
||||
/* Get the script-name of the current file, so we can work relative from it */
|
||||
SQStackInfos si;
|
||||
|
||||
Reference in New Issue
Block a user