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];
|
char buffer[2048];
|
||||||
while (to_read != 0 && fgets(buffer, sizeof(buffer), *fh) != nullptr) {
|
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. */
|
if (view.size() > to_read) {
|
||||||
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) {
|
|
||||||
to_read = 0;
|
to_read = 0;
|
||||||
} else {
|
} 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;
|
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)
|
void StringReader::HandleString(std::string_view src)
|
||||||
{
|
{
|
||||||
/* Ignore blank lines */
|
/* Ignore blank lines */
|
||||||
|
@ -498,7 +491,7 @@ void StringReader::HandleString(std::string_view src)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read string name */
|
/* 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(':')) {
|
if (!consumer.ReadCharIf(':')) {
|
||||||
StrgenError("Line has no ':' delimiter");
|
StrgenError("Line has no ':' delimiter");
|
||||||
return;
|
return;
|
||||||
|
@ -605,7 +598,7 @@ void StringReader::ParseFile()
|
||||||
std::optional<std::string> line = this->ReadLine();
|
std::optional<std::string> line = this->ReadLine();
|
||||||
if (!line.has_value()) return;
|
if (!line.has_value()) return;
|
||||||
|
|
||||||
this->HandleString(StripTrailingWhitespace(line.value()));
|
this->HandleString(StrTrimView(line.value(), StringConsumer::WHITESPACE_OR_NEWLINE));
|
||||||
_strgen.cur_line++;
|
_strgen.cur_line++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue