Skip to content

Instantly share code, notes, and snippets.

@leha-bot
Last active April 10, 2019 12:57
Show Gist options
  • Save leha-bot/dbe7ace1111f1e1fd37612dd265e3d3c to your computer and use it in GitHub Desktop.
Save leha-bot/dbe7ace1111f1e1fd37612dd265e3d3c to your computer and use it in GitHub Desktop.
class line_iterator_view {
const char *line_beg;
const char *line_end;
const char *current;
const char *current_eol;
// Передвигает наш итератор на след. позицию.
void advance_to_next_eol()
{
current_eol = strchr(current, '\n');
if (!current_eol) {
current_eol = line_end;
}
}
public:
class line {
const char *beg, *end_;
public:
line(const char *b, const char *e) : beg(b), end_(e)
{}
const char *begin()
{
return beg;
}
const char *end()
{
return end_;
}
};
line_iterator_view() : line_beg(nullptr), line_end(nullptr),
current(nullptr), current_eol(nullptr)
{}
line_iterator_view(const char *line_beg, const char *line_end)
: line_beg(line_beg), line_end(line_end), current(line_beg),
current_eol(current)
{
assert(line_end > line_beg);
advance_to_next_eol();
}
line operator *()
{
return { current, current_eol };
}
line_iterator_view &operator ++()
{
if (current_eol >= line_end) {
current = current_eol = nullptr;
return *this;
}
current = ++current_eol;
advance_to_next_eol();
return *this;
}
bool operator ==(const line_iterator_view &v) const
{
return this->current == v.current &&
this->current_eol == v.current_eol;
}
bool operator !=(const line_iterator_view &v) const
{
return !this->operator==(v);
}
};
class line_iterator_pair {
line_iterator_view iter;
public:
line_iterator_pair(const char *line_beg, const char *line_end)
: iter(line_beg, line_end)
{}
line_iterator_view &begin()
{
return iter;
}
static line_iterator_view end()
{
return {};
}
};
line_iterator_pair make_line_iterator_range(const char *line_beg,
const char *line_end)
{
return { line_beg, line_end };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment