mirror of https://github.com/OpenTTD/OpenTTD
Codechange: replace StripTrailingWhitespace with StrTrimView
parent
1f39d469ff
commit
04a6a55e94
|
@ -63,19 +63,13 @@ LanguageStrings ReadRawLanguageStrings(const std::string &file)
|
|||
|
||||
char buffer[2048];
|
||||
while (to_read != 0 && fgets(buffer, sizeof(buffer), *fh) != nullptr) {
|
||||
size_t len = strlen(buffer);
|
||||
std::string_view view{buffer};
|
||||
ret.lines.emplace_back(StrTrimView(view, StringConsumer::WHITESPACE_OR_NEWLINE));
|
||||
|
||||
/* Remove trailing spaces/newlines from the string. */
|
||||
size_t i = len;
|
||||
while (i > 0 && (buffer[i - 1] == '\r' || buffer[i - 1] == '\n' || buffer[i - 1] == ' ')) i--;
|
||||
buffer[i] = '\0';
|
||||
|
||||
ret.lines.emplace_back(buffer, i);
|
||||
|
||||
if (len > to_read) {
|
||||
if (view.size() > to_read) {
|
||||
to_read = 0;
|
||||
} else {
|
||||
to_read -= len;
|
||||
to_read -= view.size();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -479,13 +479,6 @@ static bool CheckCommandsMatch(std::string_view a, std::string_view b, std::stri
|
|||
return result;
|
||||
}
|
||||
|
||||
[[nodiscard]] static std::string_view StripTrailingWhitespace(std::string_view str)
|
||||
{
|
||||
auto len = str.find_last_not_of("\r\n\t ");
|
||||
if (len == std::string_view::npos) return {};
|
||||
return str.substr(0, len + 1);
|
||||
}
|
||||
|
||||
void StringReader::HandleString(std::string_view src)
|
||||
{
|
||||
/* Ignore blank lines */
|
||||
|
@ -498,7 +491,7 @@ void StringReader::HandleString(std::string_view src)
|
|||
}
|
||||
|
||||
/* Read string name */
|
||||
std::string_view str_name = StripTrailingWhitespace(consumer.ReadUntilChar(':', StringConsumer::KEEP_SEPARATOR));
|
||||
std::string_view str_name = StrTrimView(consumer.ReadUntilChar(':', StringConsumer::KEEP_SEPARATOR), StringConsumer::WHITESPACE_NO_NEWLINE);
|
||||
if (!consumer.ReadCharIf(':')) {
|
||||
StrgenError("Line has no ':' delimiter");
|
||||
return;
|
||||
|
@ -605,7 +598,7 @@ void StringReader::ParseFile()
|
|||
std::optional<std::string> line = this->ReadLine();
|
||||
if (!line.has_value()) return;
|
||||
|
||||
this->HandleString(StripTrailingWhitespace(line.value()));
|
||||
this->HandleString(StrTrimView(line.value(), StringConsumer::WHITESPACE_OR_NEWLINE));
|
||||
_strgen.cur_line++;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue