1
0
Fork 0

Codechange: Pass span to StrValid instead of first and last - 1. (#12846)

`std::span` is used instead of `std::string_view` as this is only used for fixed-length buffers.

This removes some callers of `lastof()`
pull/12849/head
Peter Nelson 2024-07-08 08:36:57 +01:00 committed by GitHub
parent 23bcd592a4
commit aee04e7bc6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 17 deletions

View File

@ -226,32 +226,35 @@ std::string StrMakeValid(std::string_view str, StringValidationSettings settings
/** /**
* Checks whether the given string is valid, i.e. contains only * Checks whether the given string is valid, i.e. contains only
* valid (printable) characters and is properly terminated. * valid (printable) characters and is properly terminated.
* @param str The string to validate. * @note std::span is used instead of std::string_view as we are validating fixed-length string buffers, and
* @param last The last character of the string, i.e. the string * std::string_view's constructor will assume a C-string that ends with a NUL terminator, which is one of the things
* must be terminated here or earlier. * we are checking.
* @param str Span of chars to validate.
*/ */
bool StrValid(const char *str, const char *last) bool StrValid(std::span<const char> str)
{ {
/* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */ /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
auto it = std::begin(str);
auto last = std::prev(std::end(str));
while (str <= last && *str != '\0') { while (it <= last && *it != '\0') {
size_t len = Utf8EncodedCharLen(*str); size_t len = Utf8EncodedCharLen(*it);
/* Encoded length is 0 if the character isn't known. /* Encoded length is 0 if the character isn't known.
* The length check is needed to prevent Utf8Decode to read * The length check is needed to prevent Utf8Decode to read
* over the terminating '\0' if that happens to be placed * over the terminating '\0' if that happens to be placed
* within the encoding of an UTF8 character. */ * within the encoding of an UTF8 character. */
if (len == 0 || str + len > last) return false; if (len == 0 || it + len > last) return false;
char32_t c; char32_t c;
len = Utf8Decode(&c, str); len = Utf8Decode(&c, &*it);
if (!IsPrintable(c) || (c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) { if (!IsPrintable(c) || (c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
return false; return false;
} }
str += len; it += len;
} }
return *str == '\0'; return *it == '\0';
} }
/** /**

View File

@ -27,7 +27,7 @@ void StrMakeValidInPlace(char *str, StringValidationSettings settings = SVS_REPL
bool strtolower(std::string &str, std::string::size_type offs = 0); bool strtolower(std::string &str, std::string::size_type offs = 0);
[[nodiscard]] bool StrValid(const char *str, const char *last) NOACCESS(2); [[nodiscard]] bool StrValid(std::span<const char> str);
void StrTrimInPlace(std::string &str); void StrTrimInPlace(std::string &str);
std::string_view StrTrimView(std::string_view str); std::string_view StrTrimView(std::string_view str);

View File

@ -1878,12 +1878,12 @@ bool LanguagePackHeader::IsValid() const
this->newgrflangid < MAX_LANG && this->newgrflangid < MAX_LANG &&
this->num_genders < MAX_NUM_GENDERS && this->num_genders < MAX_NUM_GENDERS &&
this->num_cases < MAX_NUM_CASES && this->num_cases < MAX_NUM_CASES &&
StrValid(this->name, lastof(this->name)) && StrValid(this->name) &&
StrValid(this->own_name, lastof(this->own_name)) && StrValid(this->own_name) &&
StrValid(this->isocode, lastof(this->isocode)) && StrValid(this->isocode) &&
StrValid(this->digit_group_separator, lastof(this->digit_group_separator)) && StrValid(this->digit_group_separator) &&
StrValid(this->digit_group_separator_currency, lastof(this->digit_group_separator_currency)) && StrValid(this->digit_group_separator_currency) &&
StrValid(this->digit_decimal_separator, lastof(this->digit_decimal_separator)); StrValid(this->digit_decimal_separator);
} }
/** /**