fix #180: Merge branch 'linefeed-at-eof'

This commit is contained in:
ToruNiina 2021-12-11 23:13:42 +09:00
commit 1b5107e5e3
2 changed files with 28 additions and 0 deletions

View File

@ -905,6 +905,22 @@ BOOST_AUTO_TEST_CASE(test_files_end_with_empty_lines)
}
}
BOOST_AUTO_TEST_CASE(test_file_ends_without_lf)
{
{
const std::string table(
"key = \"value\"\n"
"[table]\n"
"key = \"value\""
);
std::istringstream iss(table);
const auto data = toml::parse(iss,
"test_files_end_without_lf.toml");
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
}
}
BOOST_AUTO_TEST_CASE(test_parse_function_compiles)
{

View File

@ -2209,12 +2209,24 @@ parse(std::istream& is, const std::string& fname = "unknown file")
std::vector<char> letters(static_cast<std::size_t>(fsize));
is.read(letters.data(), fsize);
// remove null character if exists
while(!letters.empty() && letters.back() == '\0')
{
letters.pop_back();
}
assert(letters.empty() || letters.back() != '\0');
// append LF.
// Although TOML does not require LF at the EOF, to make parsing logic
// simpler, we "normalize" the content by adding LF if it does not exist.
// It also checks if the last char is CR, to avoid changing the meaning.
// This is not the *best* way to deal with the last character, but is a
// simple and quick fix.
if(!letters.empty() && letters.back() != '\n' && letters.back() != '\r')
{
letters.push_back('\n');
}
detail::location loc(std::move(fname), std::move(letters));
// skip BOM if exists.