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 |
✓✗✓✗ ✓✗ |
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 |
✓✓ |
7805 |
while (start_pos != std::string::npos) { |
17 |
|
3931 |
start_pos = regex.find(':', start_pos); |
18 |
✓✓ |
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 |
✓✗ |
114 |
const auto key_name = regex.substr(start_pos + 1, end_pos - start_pos - 1); |
24 |
✓✓ |
57 |
if (key_name.empty()) { |
25 |
✓✗✓✗ ✓✗ |
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 |
✓✗ |
52 |
tmp_key.name_ = key_name; |
29 |
✓✗ |
52 |
regex.replace(start_pos, end_pos - start_pos, "(\\S+)"); |
30 |
✓✗ |
52 |
keys.emplace_back(tmp_key); |
31 |
|
|
} |
32 |
|
|
} |
33 |
✓✓ |
3874 |
if (op.end) { |
34 |
✓✗ |
2759 |
regex += "$"; |
35 |
✓✓ |
1115 |
} else if (op.strict) { |
36 |
✓✗ |
109 |
regex += "/\\S*"; |
37 |
|
|
} else { |
38 |
✓✗ |
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 |