Skip to content

Commit

Permalink
Merge branch 'fix-format-location-first-line'
Browse files Browse the repository at this point in the history
  • Loading branch information
ToruNiina committed Jun 20, 2024
2 parents 6ba0772 + d023a93 commit 9e27fd2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
6 changes: 2 additions & 4 deletions include/toml11/fwd/source_location_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,8 @@ std::string format_location(
{
const auto lnw = detail::line_width(loc, msg, tail...);

std::ostringstream oss;
detail::format_filename(oss, loc);

return oss.str() + detail::format_location_rec(lnw, loc.file_name(), loc, msg, tail...);
const std::string f(""); // at the 1st iteration, no prev_filename is given
return detail::format_location_rec(lnw, f, loc, msg, tail...);
}

} // toml
Expand Down
47 changes: 42 additions & 5 deletions include/toml11/impl/region_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,53 @@ TOML11_INLINE std::vector<std::string> region::as_lines() const
assert(this->is_ok());
if(this->length_ == 0)
{
return {""};
return std::vector<std::string>{""};
}

const auto begin = std::next(this->source_->cbegin(), static_cast<difference_type>(this->first_));
const auto end = std::next(this->source_->cbegin(), static_cast<difference_type>(this->last_ ));
// Consider the following toml file
// ```
// array = [
// ] # comment
// ```
// and the region represnets
// ```
// [
// ]
// ```
// but we want to show the following.
// ```
// array = [
// ] # comment
// ```
// So we need to find LFs before `begin` and after `end`.
//
// But, if region ends with LF, it should not include the next line.
// ```
// a = 42
// ^^^- with the last LF
// ```
// So we start from `end-1` when looking for LF.

const auto line_begin = std::find(cxx::make_reverse_iterator(begin), this->source_->crend(), char_type('\n'));
const auto begin_idx = static_cast<difference_type>(this->first_);
const auto end_idx = static_cast<difference_type>(this->last_) - 1;

// length_ != 0, so begin < end. then begin <= end-1
assert(begin_idx <= end_idx);

const auto begin = std::next(this->source_->cbegin(), begin_idx);
const auto end = std::next(this->source_->cbegin(), end_idx);

const auto line_begin = std::find(cxx::make_reverse_iterator(begin), this->source_->crend(), char_type('\n')).base();
const auto line_end = std::find(end, this->source_->cend(), char_type('\n'));

std::istringstream iss(make_string(line_begin.base(), line_end));
const auto reg_lines = make_string(line_begin, line_end);

if(reg_lines == "") // the region is an empty line that only contains LF
{
return std::vector<std::string>{""};
}

std::istringstream iss(reg_lines);

std::vector<std::string> lines;
std::string line;
Expand Down

0 comments on commit 9e27fd2

Please sign in to comment.