File: examples.rst

package info (click to toggle)
ctre 3.10.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,092 kB
  • sloc: cpp: 80,502; makefile: 136; javascript: 69; python: 31
file content (77 lines) | stat: -rw-r--r-- 2,047 bytes parent folder | download | duplicates (2)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Examples
========

Extracting a number from input
------------------------------
::

  std::optional<std::string_view> extract_number(std::string_view s) noexcept {
  	if (auto m = ctre::match<"[a-z]+([0-9]+)">(s)) {
          return m.get<1>().to_view();
      } else {
          return std::nullopt;
      }
  }

`link to compiler explorer <https://gcc.godbolt.org/z/5U67_e>`_

Extracting values from date
---------------------------
::


  struct date { std::string_view year; std::string_view month; std::string_view day; };
  std::optional<date> extract_date(std::string_view s) noexcept {
      using namespace ctre::literals;
      if (auto [whole, year, month, day] = ctre::match<"(\\d{4})/(\\d{1,2})/(\\d{1,2})">(s); whole) {
          return date{year, month, day};
      } else {
          return std::nullopt;
      }
  }
  
  //static_assert(extract_date("2018/08/27"sv).has_value());
  //static_assert((*extract_date("2018/08/27"sv)).year == "2018"sv);
  //static_assert((*extract_date("2018/08/27"sv)).month == "08"sv);
  //static_assert((*extract_date("2018/08/27"sv)).day == "27"sv);

`link to compiler explorer <https://gcc.godbolt.org/z/x64CVp>`_

Lexer
-----
::

  enum class type {
      unknown, identifier, number
  };
  
  struct lex_item {
      type t;
      std::string_view c;
  };
  
  std::optional<lex_item> lexer(std::string_view v) noexcept {
      if (auto [m,id,num] = ctre::match<"([a-z]+)|([0-9]+)">(v); m) {
          if (id) {
              return lex_item{type::identifier, id};
          } else if (num) {
              return lex_item{type::number, num};
          }
      }
      return std::nullopt;
  }

`link to compiler explorer <https://gcc.godbolt.org/z/PKTiCC>`_

Range over input
----------------

This support is preliminary and probably the API will be changed.

::

  auto input = "123,456,768"sv;
  
  for (auto match: ctre::range<"([0-9]+),?">(input)) {
  	std::cout << std::string_view{match.get<0>()} << "\n";
  }