Skip to content

Commit ac25191

Browse files
committed
All stuff fixed.
1 parent 109e514 commit ac25191

12 files changed

+229
-183
lines changed

CMakeLists.txt

+31-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,43 @@ INCLUDE(FetchContent)
1111
INCLUDE_DIRECTORIES(lib/headers)
1212

1313
FILE(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/lib/sources/*.cpp")
14-
file(GLOB_RECURSE TESTS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cc")
14+
FILE(GLOB_RECURSE TESTS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cc")
1515

1616
ADD_LIBRARY(expressions STATIC ${SOURCES})
1717

18-
TARGET_INCLUDE_DIRECTORIES(expressions PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib/headers)
18+
TARGET_INCLUDE_DIRECTORIES(expressions PUBLIC
19+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/headers>
20+
$<INSTALL_INTERFACE:include>
21+
)
1922

20-
INSTALL(TARGETS expressions DESTINATION lib)
2123
INSTALL(DIRECTORY lib/headers/ DESTINATION include)
2224

25+
INSTALL(TARGETS expressions
26+
EXPORT ExpressionsTargets
27+
LIBRARY DESTINATION lib
28+
ARCHIVE DESTINATION lib
29+
RUNTIME DESTINATION bin
30+
INCLUDES DESTINATION include
31+
)
32+
33+
INSTALL(EXPORT ExpressionsTargets
34+
FILE ExpressionsTargets.cmake
35+
NAMESPACE expressions::
36+
DESTINATION lib/cmake/Expressions
37+
)
38+
39+
INCLUDE(CMakePackageConfigHelpers)
40+
CONFIGURE_PACKAGE_CONFIG_FILE(
41+
cmake/ExpressionsConfig.cmake.in
42+
${CMAKE_CURRENT_BINARY_DIR}/ExpressionsConfig.cmake
43+
INSTALL_DESTINATION lib/cmake/Expressions
44+
)
45+
46+
INSTALL(FILES
47+
${CMAKE_CURRENT_BINARY_DIR}/ExpressionsConfig.cmake
48+
DESTINATION lib/cmake/Expressions
49+
)
50+
2351
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
2452

2553
if (BUILD_TESTS)

README.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ make install
1515
## Usage
1616

1717
```c++
18-
#include <expression/expression.hpp>
18+
#include <expressions/container.hpp>
1919

2020
using namespace std;
2121

22-
auto it = expression::from_string("/api/servers/{server}/status");
22+
auto _container = expressions::container::from_string("/api/servers/{server}/status");
2323

24-
auto result = it->query("/api/servers/production/status");
24+
auto _result = _container->query("/api/servers/production/status");
2525

26-
cout << result->matches() << endl;
26+
cout << _result->matches() << endl;
2727
// 1
2828

29-
cout << result->bindings()->at("server") << endl;
29+
cout << _result->bindings()->at("server") << endl;
3030
// production
31-
```
31+
```
32+
33+
See more [examples](https://github.com/ZenAlgorithms/Expressions/blob/master/tests/implementation_test.cc) ...

cmake/ExpressionsConfig.cmake.in

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@PACKAGE_INIT@
2+
3+
INCLUDE(CMakeFindDependencyMacro)
4+
5+
# Incluir el archivo de objetivos
6+
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/ExpressionsTargets.cmake")

lib/headers/expression.hpp

-59
This file was deleted.

lib/headers/expression_result.hpp

-37
This file was deleted.

lib/headers/expressions/container.hpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
#include <memory>
6+
#include <regex>
7+
8+
#include <expressions/result.hpp>
9+
10+
namespace expressions {
11+
class container : public std::enable_shared_from_this<container> {
12+
std::string regex_;
13+
std::vector<std::string> arguments_{};
14+
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+
27+
/**
28+
* Get the regex
29+
*
30+
* @return std::string
31+
*/
32+
std::string
33+
get_regex() const;
34+
35+
/**
36+
* Get the arguments
37+
*
38+
* @return std::vector<std::string>
39+
*/
40+
std::vector<std::string>
41+
get_arguments() const;
42+
43+
/**
44+
* Query the expression
45+
*
46+
* @param input
47+
* @return
48+
*/
49+
std::shared_ptr<result>
50+
query(const std::string &input);
51+
52+
/**
53+
* Creates a expression from strings
54+
*
55+
* @param input
56+
* @return std::shared_ptr<expression>
57+
*/
58+
static std::shared_ptr<container>
59+
from_string(const std::string &input);
60+
};
61+
}

lib/headers/expressions/result.hpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <unordered_map>
5+
6+
namespace expressions {
7+
class result : public std::enable_shared_from_this<result> {
8+
bool _matches = false;
9+
std::unordered_map<std::string, std::string> _bindings;
10+
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+
23+
/**
24+
* Get matches
25+
*
26+
* @return
27+
*/
28+
bool
29+
matches() const;
30+
31+
/**
32+
* Get bindings
33+
*
34+
* @return
35+
*/
36+
std::unordered_map<std::string, std::string>
37+
bindings() const;
38+
};
39+
}

lib/sources/container.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <expressions/container.hpp>
2+
3+
namespace expressions {
4+
container::container(std::string regex, std::vector<std::string> arguments)
5+
: regex_(std::move(regex)), arguments_(std::move(arguments)) {
6+
}
7+
8+
std::string container::get_regex() const {
9+
return regex_;
10+
}
11+
12+
std::vector<std::string> container::get_arguments() const {
13+
return arguments_;
14+
}
15+
16+
std::shared_ptr<result> container::query(const std::string &input) {
17+
std::unordered_map<std::string, std::string> _bindings;
18+
const std::regex _pattern(regex_);
19+
bool _matches = false;
20+
if (std::smatch _match; std::regex_match(input, _match, _pattern)) {
21+
_matches = true;
22+
auto _iterator = _match.begin();
23+
++_iterator;
24+
for (auto &_key: arguments_) {
25+
_bindings[_key] = *_iterator;
26+
++_iterator;
27+
}
28+
}
29+
return std::make_shared<result>(_matches, _bindings);
30+
}
31+
32+
std::shared_ptr<container> container::from_string(const std::string &input) {
33+
std::size_t _open = input.find('{');
34+
std::size_t _close = input.find('}');
35+
std::size_t _position = 0;
36+
37+
std::vector<std::string> _arguments;
38+
std::string _regex;
39+
40+
if (_open == std::string::npos && _close == std::string::npos)
41+
return std::make_shared<container>(std::string{input.data()}, _arguments);
42+
43+
while (_open != std::string::npos && _close != std::string::npos) {
44+
_regex.append(input.substr(_position, _open - _position));
45+
std::string _value{input.substr(_open + 1, _close - _open - 1)};
46+
47+
if (std::find(_arguments.begin(), _arguments.end(), _value) != _arguments.end())
48+
throw std::runtime_error("groups can't be repeated ... ");
49+
50+
_regex.append(R"(([a-zA-Z0-9\-_]+))");
51+
_arguments.emplace_back(_value);
52+
53+
_position = _close + 1;
54+
_open = input.find('{', _close);
55+
_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));
59+
}
60+
61+
return std::make_shared<container>(_regex, _arguments);
62+
}
63+
}

0 commit comments

Comments
 (0)