1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-19 02:12:37 +01:00

Codechange: replace char* in GetOptData with std::string_view

This commit is contained in:
Rubidium
2025-04-30 22:19:11 +02:00
committed by rubidium42
parent c1a287ad17
commit fbe80f31fe
9 changed files with 58 additions and 57 deletions

View File

@@ -21,30 +21,30 @@
*/
int GetOptData::GetOpt()
{
const char *s = this->cont;
if (s == nullptr) {
std::string_view s = this->cont;
if (s.empty()) {
if (this->arguments.empty()) return -1; // No arguments left -> finished.
s = this->arguments[0];
if (*s != '-') return -1; // No leading '-' -> not an option -> finished.
if (s[0] != '-') return -1; // No leading '-' -> not an option -> finished.
this->arguments = this->arguments.subspan(1);
/* Is it a long option? */
for (auto &option : this->options) {
if (option.longname != nullptr && !strcmp(option.longname, s)) { // Long options always use the entire argument.
this->cont = nullptr;
if (option.longname == s) { // Long options always use the entire argument.
this->cont = {};
return this->GetOpt(option);
}
}
s++; // Skip leading '-'.
s.remove_prefix(1); // Skip leading '-'.
}
/* Is it a short option? */
for (auto &option : this->options) {
if (option.shortname != '\0' && *s == option.shortname) {
this->cont = (s[1] != '\0') ? s + 1 : nullptr;
if (option.shortname != '\0' && s[0] == option.shortname) {
this->cont = s.substr(1);
return this->GetOpt(option);
}
}
@@ -54,16 +54,16 @@ int GetOptData::GetOpt()
int GetOptData::GetOpt(const OptionData &option)
{
this->opt = nullptr;
this->opt = {};
switch (option.type) {
case ODF_NO_VALUE:
return option.id;
case ODF_HAS_VALUE:
case ODF_OPTIONAL_VALUE:
if (this->cont != nullptr) { // Remainder of the argument is the option value.
if (!this->cont.empty()) { // Remainder of the argument is the option value.
this->opt = this->cont;
this->cont = nullptr;
this->cont = {};
return option.id;
}
/* No more arguments, either return an error or a value-less option. */