aboutsummaryrefslogtreecommitdiff
path: root/include/cru/common/StringUtil.hpp
blob: a44ae6b41f96a2841b80613de67c7f37d4fd658d (plain)
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
31
32
33
34
35
36
37
38
39
#pragma once
#include "Base.hpp"

namespace cru {
using CodePoint = std::int32_t;
constexpr CodePoint k_code_point_end = -1;

class TextEncodeException : public std::runtime_error {
 public:
  using runtime_error::runtime_error;
};

class Utf8Iterator : public Object {
 public:
  explicit Utf8Iterator(const std::string_view& string) : string_(string) {}
  Utf8Iterator(const std::string_view& string, Index position)
      : string_(string), position_(position) {}

  CRU_DEFAULT_COPY(Utf8Iterator)
  CRU_DEFAULT_MOVE(Utf8Iterator)

  ~Utf8Iterator() = default;

 public:
  void SetToHead() { position_ = 0; }
  void SetPosition(Index position) { position_ = position; }

  // Advance current position and get next code point. Return k_code_point_end
  // if there is no next code unit(point). Throw TextEncodeException if decoding
  // fails.
  CodePoint Next();

  Index CurrentPosition() const { return this->position_; }

 private:
  std::string_view string_;
  Index position_ = 0;
};
}  // namespace cru