File: line_iterator.h

package info (click to toggle)
nodejs 20.19.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 219,072 kB
  • sloc: cpp: 1,277,408; javascript: 565,332; ansic: 129,476; python: 58,536; sh: 3,841; makefile: 2,725; asm: 1,732; perl: 248; lisp: 222; xml: 42
file content (30 lines) | stat: -rw-r--r-- 813 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef LINE_ITERATOR_H
#define LINE_ITERATOR_H

#include <string_view>

struct line_iterator {
  std::string_view all_text{};
  size_t next_end_of_line{0};
  line_iterator(const char *_buffer, size_t _len) : all_text(_buffer, _len) {}

  inline bool find_another_complete_line() noexcept {
    next_end_of_line = all_text.find('\n');
    return next_end_of_line != std::string_view::npos;
  }

  inline operator bool() const noexcept {
    return next_end_of_line != std::string_view::npos;
  }

  inline std::string_view grab_line() noexcept {
    auto line = all_text.substr(0, next_end_of_line);  // advance to next EOL
    // remove anything prior to said EOL
    all_text.remove_prefix(next_end_of_line + 1);
    return line;
  }

  inline size_t tail() const noexcept { return all_text.size(); }
};

#endif