Skip to content

Commit 622bff2

Browse files
committed
specify get_line behavior on out of range versus empty line
When the specified line is empty, the returned `string_view` will still have a valid (non-null) data pointer. When the line number is out of range, the data pointer will be null.
1 parent 8b86a24 commit 622bff2

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[
290290
- **[@levicki](https://github.com/levicki)** - Helped design some new features
291291
- **[@moorereason](https://github.com/moorereason)** - Reported a whole bunch of bugs
292292
- **[@mosra](https://github.com/mosra)** - Created the awesome [m.css] used to generate the API docs
293-
- **[@N-Dekker](https://github.com/N-Dekker)** - Added a workaround for the legacy lambda processor of MSVC 2019/2022
293+
- **[@N-Dekker](https://github.com/N-Dekker)** - Added a workaround for the legacy lambda processor of MSVC 2019/2022, added `get_line`
294294
- **[@ned14](https://github.com/ned14)** - Reported a bunch of bugs and helped design some new features
295295
- **[@okureta](https://github.com/okureta)** - Reported a bug
296296
- **[@prince-chrismc](https://github.com/prince-chrismc)** - Added toml++ to ConanCenter, and fixed some typos

include/toml++/impl/source_region.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,11 @@ TOML_NAMESPACE_START
235235
/// \param line_num The line number (1-based).
236236
///
237237
/// \returns The specified line, excluding any possible trailing carriage return or line feed character.
238-
/// \remarks Returns an empty string_view when there is no line at the specified line number, in the specified document.
238+
/// \remarks The data pointer of the returned `string_view` will be null when the specified line
239+
/// number is out of range (i.e., when the line number is zero, or greater than the
240+
/// total number of lines of the specified document). Otherwise, when the line
241+
/// number is within the document and the line is empty, the returned `string_view` will
242+
/// also be empty, but then its data pointer will not be null.
239243
TOML_NODISCARD
240244
constexpr std::string_view get_line(std::string_view doc, source_index line_num) noexcept
241245
{

tests/user_feedback.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,16 @@ b = []
455455
SECTION("tomlplusplus/issues/254") // https://github.com/marzer/tomlplusplus/issues/254
456456
{
457457
// Check constexpr support.
458-
static_assert(toml::get_line(""sv, 1) == std::string_view{});
458+
static_assert(toml::get_line(""sv, 1).data() == nullptr);
459459
static_assert(toml::get_line("alpha = 1\nbeta = 2\n # last line # "sv, 1) == "alpha = 1"sv);
460460

461+
constexpr auto expected_empty_line = toml::get_line("# line one\n\n# line three"sv, 2);
462+
static_assert(expected_empty_line.empty());
463+
static_assert(expected_empty_line.data() != nullptr);
464+
461465
for (const toml::source_index line_num : { 0u, 1u, 2u })
462466
{
463-
CHECK(toml::get_line(""sv, line_num) == std::string_view{});
467+
CHECK(toml::get_line(""sv, line_num).data() == nullptr);
464468
}
465469

466470
for (const auto input : {
@@ -469,9 +473,9 @@ b = []
469473
"# \r (embedded carriage return)\r\n"sv,
470474
})
471475
{
472-
CHECK(toml::get_line(input, 0) == std::string_view{});
476+
CHECK(toml::get_line(input, 0).data() == nullptr);
473477
CHECK(toml::get_line(input, 1) == "# \r (embedded carriage return)"sv);
474-
CHECK(toml::get_line(input, 2) == std::string_view{});
478+
CHECK(toml::get_line(input, 2).data() == nullptr);
475479
}
476480

477481
for (const auto input : {
@@ -480,11 +484,11 @@ b = []
480484
"alpha = 1\r\nbeta = 2\r\n # last line # \r\n"sv,
481485
})
482486
{
483-
CHECK(toml::get_line(input, 0) == std::string_view{});
487+
CHECK(toml::get_line(input, 0).data() == nullptr);
484488
CHECK(toml::get_line(input, 1) == "alpha = 1"sv);
485489
CHECK(toml::get_line(input, 2) == "beta = 2"sv);
486490
CHECK(toml::get_line(input, 3) == " # last line # "sv);
487-
CHECK(toml::get_line(input, 4) == std::string_view{});
491+
CHECK(toml::get_line(input, 4).data() == nullptr);
488492
}
489493
}
490494
}

0 commit comments

Comments
 (0)