Two small improvements

- Check whether `.read()` completed
- `.reserve()` expected space ahead of time
This commit is contained in:
Rangi42
2025-09-01 11:36:19 -04:00
parent 0ccdbf509a
commit cc96b4d517

View File

@@ -341,7 +341,7 @@ void LexerState::setFileAsNextState(std::string const &filePath, bool updateStat
} }
path = filePath; path = filePath;
if (size_t size = static_cast<size_t>(statBuf.st_size); statBuf.st_size > 0) { if (std::streamsize size = statBuf.st_size; statBuf.st_size > 0) {
// Read the entire file for better performance // Read the entire file for better performance
// Ideally we'd use C++20 `auto ptr = std::make_shared<char[]>(size)`, // Ideally we'd use C++20 `auto ptr = std::make_shared<char[]>(size)`,
// but it has insufficient compiler support // but it has insufficient compiler support
@@ -351,7 +351,7 @@ void LexerState::setFileAsNextState(std::string const &filePath, bool updateStat
// LCOV_EXCL_START // LCOV_EXCL_START
fatal("Failed to open file \"%s\": %s", path.c_str(), strerror(errno)); fatal("Failed to open file \"%s\": %s", path.c_str(), strerror(errno));
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
} else if (!fs.read(ptr.get(), size)) { } else if (!fs.read(ptr.get(), size) || fs.gcount() != size) {
// LCOV_EXCL_START // LCOV_EXCL_START
fatal("Failed to read file \"%s\": %s", path.c_str(), strerror(errno)); fatal("Failed to read file \"%s\": %s", path.c_str(), strerror(errno));
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
@@ -1375,6 +1375,7 @@ static void appendExpandedString(std::string &str, std::string const &expanded)
return; return;
} }
str.reserve(str.length() + expanded.length());
for (char c : expanded) { for (char c : expanded) {
// Escape characters that need escaping // Escape characters that need escaping
switch (c) { switch (c) {