GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/path_to_regexp.cpp Lines: 24 24 100.0 %
Date: 2020-03-21 00:01:17 Branches: 23 36 63.9 %

Line Branch Exec Source
1
#include "expresscpp/path_to_regexp.hpp"
2
3
#include <algorithm>
4
#include <stdexcept>
5
6
#define EXPRESSCPP_CONFIG_DEBUG_PATH_TO_REGEX 0
7
8
namespace expresscpp {
9
using namespace std::string_literals;
10
11
3879
std::string pathToRegExpString(std::string_view registered_path, std::vector<Key>& keys, PathToRegExpOptions op,
12
                               std::string_view parent_path) {
13
&check;&cross;&check;&cross;
&check;&cross;
7758
  std::string regex = std::string("^") + parent_path.data() + registered_path.data();
14
3879
  size_t key_index = 0;
15
3879
  std::string::size_type start_pos = 0;
16
&check;&check;
7805
  while (start_pos != std::string::npos) {
17
3931
    start_pos = regex.find(':', start_pos);
18
&check;&check;
3931
    if (start_pos != std::string::npos) {
19
57
      const auto dash_end_pos = regex.find('-', start_pos);
20
57
      const auto slash_end_pos = regex.find('/', start_pos);
21
57
      const auto end_pos = std::min(dash_end_pos, slash_end_pos);
22
114
      Key tmp_key;
23
&check;&cross;
114
      const auto key_name = regex.substr(start_pos + 1, end_pos - start_pos - 1);
24
&check;&check;
57
      if (key_name.empty()) {
25
&check;&cross;&check;&cross;
&check;&cross;
5
        throw std::runtime_error(std::string("Empty parameter name found in path: ") + registered_path.data());
26
      }
27
52
      tmp_key.index_ = key_index++;
28
&check;&cross;
52
      tmp_key.name_ = key_name;
29
&check;&cross;
52
      regex.replace(start_pos, end_pos - start_pos, "(\\S+)");
30
&check;&cross;
52
      keys.emplace_back(tmp_key);
31
    }
32
  }
33
&check;&check;
3874
  if (op.end) {
34
&check;&cross;
2759
    regex += "$";
35
&check;&check;
1115
  } else if (op.strict) {
36
&check;&cross;
109
    regex += "/\\S*";
37
  } else {
38
&check;&cross;
1006
    regex += "\\S*";
39
  }
40
41
#if EXPRESSCPP_CONFIG_DEBUG_PATH_TO_REGEX
42
  if (keys.size() > 0) {
43
    std::cout << "printing keys ***********************" << std::endl;
44
    for (const auto& k : keys) {
45
      std::cout << "key found : "
46
                << "\"" << k.name_ << "\"" << std::endl;
47
    }
48
    std::cout << "printing keys **********************" << std::endl;
49
  }
50
#endif
51
52
3874
  return regex;
53
}
54
}  // namespace expresscpp