stream_counter.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #pragma once
  2. #include <library/cpp/yson_pull/position_info.h>
  3. #include <cstddef>
  4. namespace NYsonPull {
  5. namespace NDetail {
  6. template <bool EnableLinePositionInfo>
  7. class stream_counter;
  8. template <>
  9. class stream_counter<true> {
  10. private:
  11. size_t offset_ = 0;
  12. size_t line_ = 1;
  13. size_t column_ = 1;
  14. public:
  15. TPositionInfo info() const {
  16. return {offset_, line_, column_};
  17. }
  18. void update(const ui8* begin, const ui8* end) {
  19. offset_ += end - begin;
  20. for (auto current = begin; current != end; ++current) {
  21. ++column_;
  22. if (*current == '\n') { //TODO: memchr
  23. ++line_;
  24. column_ = 1;
  25. }
  26. }
  27. }
  28. };
  29. template <>
  30. class stream_counter<false> {
  31. private:
  32. size_t offset_ = 0;
  33. public:
  34. TPositionInfo info() const {
  35. return {offset_, {}, {}};
  36. }
  37. void update(const ui8* begin, const ui8* end) {
  38. offset_ += end - begin;
  39. }
  40. };
  41. }
  42. }