Skip to content

Commit 2294d46

Browse files
authored
Merge pull request #4 from ZenAlgorithms/improvements
Improvements added.
2 parents 181ccd5 + fcd5902 commit 2294d46

File tree

6 files changed

+46
-59
lines changed

6 files changed

+46
-59
lines changed

about.png

14.2 KB
Loading

lib/headers/expressions/container.hpp

+4-16
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,10 @@
88
#include <expressions/result.hpp>
99

1010
namespace expressions {
11-
class container : public std::enable_shared_from_this<container> {
11+
struct container {
1212
std::string regex_;
1313
std::vector<std::string> arguments_{};
1414

15-
public:
16-
/**
17-
* Expression constructor
18-
*
19-
* @param regex
20-
* @param arguments
21-
*/
22-
container(
23-
std::string regex,
24-
std::vector<std::string> arguments
25-
);
26-
2715
/**
2816
* Get the regex
2917
*
@@ -46,16 +34,16 @@ namespace expressions {
4634
* @param input
4735
* @return
4836
*/
49-
std::shared_ptr<result>
50-
query(const std::string &input);
37+
result
38+
query(const std::string &input) const;
5139

5240
/**
5341
* Creates a expression from strings
5442
*
5543
* @param input
5644
* @return std::shared_ptr<expression>
5745
*/
58-
static std::shared_ptr<container>
46+
static container
5947
from_string(const std::string &input);
6048
};
6149
}

lib/headers/expressions/result.hpp

+11-13
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,13 @@
22

33
#include <memory>
44
#include <unordered_map>
5+
#include <optional>
56

67
namespace expressions {
7-
class result : public std::enable_shared_from_this<result> {
8+
struct result {
89
bool _matches = false;
910
std::unordered_map<std::string, std::string> _bindings;
1011

11-
public:
12-
/**
13-
* Expression result constructor
14-
*
15-
* @param matches
16-
* @param bindings
17-
*/
18-
result(
19-
bool matches,
20-
std::unordered_map<std::string, std::string> bindings
21-
);
22-
2312
/**
2413
* Get matches
2514
*
@@ -35,5 +24,14 @@ namespace expressions {
3524
*/
3625
std::unordered_map<std::string, std::string>
3726
bindings() const;
27+
28+
/**
29+
* Get parameter
30+
*
31+
* @param name
32+
* @return
33+
*/
34+
std::string
35+
get(const std::string & name) const;
3836
};
3937
}

lib/sources/container.cpp

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#include <expressions/container.hpp>
22

33
namespace expressions {
4-
container::container(std::string regex, std::vector<std::string> arguments)
5-
: regex_(std::move(regex)), arguments_(std::move(arguments)) {
6-
}
7-
84
std::string container::get_regex() const {
95
return regex_;
106
}
@@ -13,7 +9,7 @@ namespace expressions {
139
return arguments_;
1410
}
1511

16-
std::shared_ptr<result> container::query(const std::string &input) {
12+
result container::query(const std::string &input) const {
1713
std::unordered_map<std::string, std::string> _bindings;
1814
const std::regex _pattern(regex_);
1915
bool _matches = false;
@@ -26,10 +22,10 @@ namespace expressions {
2622
++_iterator;
2723
}
2824
}
29-
return std::make_shared<result>(_matches, _bindings);
25+
return { ._matches = _matches, ._bindings = _bindings };
3026
}
3127

32-
std::shared_ptr<container> container::from_string(const std::string &input) {
28+
container container::from_string(const std::string &input) {
3329
std::size_t _open = input.find('{');
3430
std::size_t _close = input.find('}');
3531
std::size_t _position = 0;
@@ -38,26 +34,26 @@ namespace expressions {
3834
std::string _regex;
3935

4036
if (_open == std::string::npos && _close == std::string::npos)
41-
return std::make_shared<container>(std::string{input.data()}, _arguments);
37+
return { .regex_ = std::string{ input }, .arguments_ = _arguments };
4238

4339
while (_open != std::string::npos && _close != std::string::npos) {
4440
_regex.append(input.substr(_position, _open - _position));
4541
std::string _value{input.substr(_open + 1, _close - _open - 1)};
4642

4743
if (std::find(_arguments.begin(), _arguments.end(), _value) != _arguments.end())
48-
throw std::runtime_error("groups can't be repeated ... ");
44+
throw std::runtime_error("The provided input contains repeated arguments.");
4945

5046
_regex.append(R"(([a-zA-Z0-9\-_]+))");
5147
_arguments.emplace_back(_value);
5248

5349
_position = _close + 1;
5450
_open = input.find('{', _close);
5551
_close = input.find('}', _open);
56-
57-
if (_open == std::string::npos && _close == std::string::npos && _position != input.size())
58-
_regex.append(input.substr(_position, input.size() - _position));
5952
}
6053

61-
return std::make_shared<container>(_regex, _arguments);
54+
if (_position != input.size())
55+
_regex.append(input.substr(_position, input.size() - _position));
56+
57+
return { .regex_ = _regex , .arguments_ = _arguments };
6258
}
6359
}

lib/sources/result.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#include <expressions/result.hpp>
22

33
namespace expressions {
4-
result::result(const bool matches, std::unordered_map<std::string, std::string> bindings)
5-
: _matches(matches), _bindings(std::move(bindings)) {
6-
}
7-
84
bool result::matches() const {
95
return _matches;
106
}
117

128
std::unordered_map<std::string, std::string> result::bindings() const {
139
return _bindings;
1410
}
11+
12+
std::string
13+
result::get(const std::string &name) const {
14+
if (_bindings.contains(name))
15+
return _bindings.at(name);
16+
throw std::runtime_error("The provided argument doesn't exists");
17+
}
1518
}

tests/implementation_test.cc

+15-13
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@ TEST(Expressions, Assertions) {
55
using namespace expressions;
66

77
const auto _non_empty_expression = container::from_string("/users/{user}/details");
8-
ASSERT_FALSE(_non_empty_expression->get_arguments().empty());
9-
ASSERT_EQ(_non_empty_expression->get_arguments().size(), 1);
10-
ASSERT_EQ(_non_empty_expression->get_arguments().at(0), "user");
8+
9+
ASSERT_FALSE(_non_empty_expression.get_arguments().empty());
10+
ASSERT_EQ(_non_empty_expression.get_arguments().size(), 1);
11+
ASSERT_EQ(_non_empty_expression.get_arguments().at(0), "user");
1112

1213
const auto _empty_expression = container::from_string("/ping");
13-
ASSERT_TRUE(_empty_expression->get_arguments().empty());
14-
ASSERT_EQ(_empty_expression->get_regex(), "/ping");
14+
ASSERT_TRUE(_empty_expression.get_arguments().empty());
15+
ASSERT_EQ(_empty_expression.get_regex(), "/ping");
16+
17+
const auto _non_empty_string_expression_result = _non_empty_expression.query("/users/80bdc6d1-524e-411a-b316-976a65a3ed3c/details");
1518

16-
const auto _non_empty_string_expression_result = _non_empty_expression->query("/users/80bdc6d1-524e-411a-b316-976a65a3ed3c/details");
17-
ASSERT_TRUE(_non_empty_string_expression_result->matches());
18-
ASSERT_FALSE(_non_empty_string_expression_result->bindings().empty());
19-
ASSERT_EQ(_non_empty_string_expression_result->bindings().at("user"), "80bdc6d1-524e-411a-b316-976a65a3ed3c");
19+
ASSERT_TRUE(_non_empty_string_expression_result.matches());
20+
ASSERT_FALSE(_non_empty_string_expression_result.bindings().empty());
21+
ASSERT_EQ(_non_empty_string_expression_result.get("user"), "80bdc6d1-524e-411a-b316-976a65a3ed3c");
2022

21-
const auto _non_empty_integer_expression_result = _non_empty_expression->query("/users/1337/details");
22-
ASSERT_TRUE(_non_empty_integer_expression_result->matches());
23-
ASSERT_FALSE(_non_empty_integer_expression_result->bindings().empty());
24-
ASSERT_EQ(_non_empty_integer_expression_result->bindings().at("user"), "1337");
23+
const auto _non_empty_integer_expression_result = _non_empty_expression.query("/users/1337/details");
24+
ASSERT_TRUE(_non_empty_integer_expression_result.matches());
25+
ASSERT_FALSE(_non_empty_integer_expression_result.bindings().empty());
26+
ASSERT_EQ(_non_empty_integer_expression_result.get("user"), "1337");
2527
}

0 commit comments

Comments
 (0)