mirror of https://github.com/OpenTTD/OpenTTD
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
parent
23bcd592a4
commit
aee04e7bc6
|
@ -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';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue