Skip to content

Commit

Permalink
feat: Merge branch 'main' into check-source-location-file-name-result
Browse files Browse the repository at this point in the history
  • Loading branch information
ToruNiina committed Jan 5, 2025
2 parents 247796c + f042b38 commit 5bea350
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 89 deletions.
16 changes: 10 additions & 6 deletions include/toml11/fwd/location_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class location

location(source_ptr src, std::string src_name)
: source_(std::move(src)), source_name_(std::move(src_name)),
location_(0), line_number_(1)
location_(0), line_number_(1), column_number_(1)
{}

location(const location&) = default;
Expand All @@ -41,7 +41,7 @@ class location
~location() = default;

void advance(std::size_t n = 1) noexcept;
void retrace(std::size_t n = 1) noexcept;
void retrace() noexcept;

bool is_ok() const noexcept { return static_cast<bool>(this->source_); }

Expand All @@ -54,22 +54,25 @@ class location
{
return this->location_;
}
void set_location(const std::size_t loc) noexcept;

std::size_t line_number() const noexcept
{
return this->line_number_;
}
std::size_t column_number() const noexcept
{
return this->column_number_;
}
std::string get_line() const;
std::size_t column_number() const noexcept;

source_ptr const& source() const noexcept {return this->source_;}
std::string const& source_name() const noexcept {return this->source_name_;}

private:

void advance_line_number(const std::size_t n);
void retrace_line_number(const std::size_t n);
void advance_impl(const std::size_t n);
void retrace_impl();
std::size_t calc_column_number() const noexcept;

private:

Expand All @@ -81,6 +84,7 @@ class location
std::string source_name_;
std::size_t location_; // std::vector<>::difference_type is signed
std::size_t line_number_;
std::size_t column_number_;
};

bool operator==(const location& lhs, const location& rhs) noexcept;
Expand Down
7 changes: 6 additions & 1 deletion include/toml11/fwd/region_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,16 @@ class region
const_iterator cend() const noexcept;

std::string as_string() const;
std::vector<std::string> as_lines() const;
std::vector<std::pair<std::string, std::size_t>> as_lines() const;

source_ptr const& source() const noexcept {return this->source_;}
std::string const& source_name() const noexcept {return this->source_name_;}

private:

std::pair<std::string, std::size_t>
take_line(const_iterator begin, const_iterator end) const;

private:

source_ptr source_;
Expand Down
37 changes: 35 additions & 2 deletions include/toml11/fwd/source_location_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,34 @@
namespace toml
{

//
// A struct to contain location in a toml file.
//
// To reduce memory consumption, it omits unrelated parts of long lines. like:
//
// 1. one long line, short region
// ```
// |
// 1 | ... "foo", "bar", baz, "qux", "foobar", ...
// | ^-- unknown value
// ```
// 2. long region
// ```
// |
// 1 | array = [ "foo", ... "bar" ]
// | ^^^^^^^^^^^^^^^^^^^^- in this array
// ```
// 3. many lines
// |
// 1 | array = [ "foo",
// | ^^^^^^^^
// | ...
// | ^^^
// |
// 10 | , "bar"]
// | ^^^^^^^^- in this array
// ```
//
struct source_location
{
public:
Expand Down Expand Up @@ -39,13 +66,19 @@ struct source_location

std::vector<std::string> const& lines() const noexcept {return line_str_;}

// for internal use
std::size_t first_column_offset() const noexcept {return this->first_offset_;}
std::size_t last_column_offset() const noexcept {return this->last_offset_;}

private:

bool is_ok_;
std::size_t first_line_;
std::size_t first_column_;
std::size_t first_column_; // column num in the actual file
std::size_t first_offset_; // column num in the shown line
std::size_t last_line_;
std::size_t last_column_;
std::size_t last_column_; // column num in the actual file
std::size_t last_offset_; // column num in the shown line
std::size_t length_;
std::string file_name_;
std::vector<std::string> line_str_;
Expand Down
91 changes: 37 additions & 54 deletions include/toml11/impl/location_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ TOML11_INLINE void location::advance(std::size_t n) noexcept
assert(this->is_ok());
if(this->location_ + n < this->source_->size())
{
this->advance_line_number(n);
this->location_ += n;
this->advance_impl(n);
}
else
{
this->advance_line_number(this->source_->size() - this->location_);
this->location_ = this->source_->size();
this->advance_impl(this->source_->size() - this->location_);

assert(this->location_ == this->source_->size());
}
}
TOML11_INLINE void location::retrace(std::size_t n) noexcept
TOML11_INLINE void location::retrace(/*restricted to n=1*/) noexcept
{
assert(this->is_ok());
if(this->location_ < n)
if(this->location_ == 0)
{
this->location_ = 0;
this->line_number_ = 1;
this->column_number_ = 1;
}
else
{
this->retrace_line_number(n);
this->location_ -= n;
this->retrace_impl();
}
}

Expand Down Expand Up @@ -66,30 +66,6 @@ TOML11_INLINE location::char_type location::peek()
}
}

TOML11_INLINE void location::set_location(const std::size_t loc) noexcept
{
if(this->location_ == loc)
{
return ;
}

if(loc == 0)
{
this->line_number_ = 1;
}
else if(this->location_ < loc)
{
const auto d = loc - this->location_;
this->advance_line_number(d);
}
else
{
const auto d = this->location_ - loc;
this->retrace_line_number(d);
}
this->location_ = loc;
}

TOML11_INLINE std::string location::get_line() const
{
assert(this->is_ok());
Expand All @@ -101,7 +77,8 @@ TOML11_INLINE std::string location::get_line() const

return make_string(std::next(prev.base()), next);
}
TOML11_INLINE std::size_t location::column_number() const noexcept

TOML11_INLINE std::size_t location::calc_column_number() const noexcept
{
assert(this->is_ok());
const auto iter = std::next(this->source_->cbegin(), static_cast<difference_type>(this->location_));
Expand All @@ -112,38 +89,44 @@ TOML11_INLINE std::size_t location::column_number() const noexcept
return static_cast<std::size_t>(std::distance(prev.base(), iter) + 1); // 1-origin
}


TOML11_INLINE void location::advance_line_number(const std::size_t n)
TOML11_INLINE void location::advance_impl(const std::size_t n)
{
assert(this->is_ok());
assert(this->location_ + n <= this->source_->size());

const auto iter = this->source_->cbegin();
this->line_number_ += static_cast<std::size_t>(std::count(
std::next(iter, static_cast<difference_type>(this->location_)),
std::next(iter, static_cast<difference_type>(this->location_ + n)),
char_type('\n')));
auto iter = this->source_->cbegin();
std::advance(iter, static_cast<difference_type>(this->location_));

for(std::size_t i=0; i<n; ++i)
{
const auto c = *iter;
if(c == char_type('\n'))
{
this->line_number_ += 1;
this->column_number_ = 1;
}
else
{
this->column_number_ += 1;
}
iter++;
}
this->location_ += n;
return;
}
TOML11_INLINE void location::retrace_line_number(const std::size_t n)
TOML11_INLINE void location::retrace_impl(/*n == 1*/)
{
assert(this->is_ok());
assert(n <= this->location_); // loc - n >= 0
assert(this->location_ != 0);

const auto iter = this->source_->cbegin();
const auto dline_num = static_cast<std::size_t>(std::count(
std::next(iter, static_cast<difference_type>(this->location_ - n)),
std::next(iter, static_cast<difference_type>(this->location_)),
char_type('\n')));
this->location_ -= 1;

if(this->line_number_ <= dline_num)
{
this->line_number_ = 1;
}
else
auto iter = this->source_->cbegin();
std::advance(iter, static_cast<difference_type>(this->location_));
if(*iter == '\n')
{
this->line_number_ -= dline_num;
this->line_number_ -= 1;
this->column_number_ = this->calc_column_number();
}
return;
}
Expand All @@ -166,7 +149,7 @@ TOML11_INLINE bool operator!=(const location& lhs, const location& rhs)
TOML11_INLINE location prev(const location& loc)
{
location p(loc);
p.retrace(1);
p.retrace();
return p;
}
TOML11_INLINE location next(const location& loc)
Expand Down
Loading

0 comments on commit 5bea350

Please sign in to comment.