Skip to content

Commit 3ca955c

Browse files
authored
Merge pull request #5 from ZenAlgorithms/stable-release
Stable release added.
2 parents 2294d46 + 10ba288 commit 3ca955c

File tree

9 files changed

+127
-188
lines changed

9 files changed

+127
-188
lines changed

CMakeLists.txt

+16-8
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ INCLUDE_DIRECTORIES(lib/headers)
1313
FILE(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/lib/sources/*.cpp")
1414
FILE(GLOB_RECURSE TESTS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cc")
1515

16-
ADD_LIBRARY(expressions STATIC ${SOURCES})
16+
ADD_LIBRARY(zen_algorithms_expressions STATIC ${SOURCES})
1717

18-
TARGET_INCLUDE_DIRECTORIES(expressions PUBLIC
18+
TARGET_INCLUDE_DIRECTORIES(zen_algorithms_expressions PUBLIC
1919
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/headers>
2020
$<INSTALL_INTERFACE:include>
2121
)
2222

2323
INSTALL(DIRECTORY lib/headers/ DESTINATION include)
2424

25-
INSTALL(TARGETS expressions
25+
INSTALL(TARGETS zen_algorithms_expressions
2626
EXPORT ExpressionsTargets
2727
LIBRARY DESTINATION lib
2828
ARCHIVE DESTINATION lib
@@ -32,22 +32,30 @@ INSTALL(TARGETS expressions
3232

3333
INSTALL(EXPORT ExpressionsTargets
3434
FILE ExpressionsTargets.cmake
35-
NAMESPACE expressions::
36-
DESTINATION lib/cmake/Expressions
35+
NAMESPACE zen_algorithms::
36+
DESTINATION lib/cmake/ZenAlgorithms
3737
)
3838

3939
INCLUDE(CMakePackageConfigHelpers)
4040
CONFIGURE_PACKAGE_CONFIG_FILE(
4141
cmake/ExpressionsConfig.cmake.in
4242
${CMAKE_CURRENT_BINARY_DIR}/ExpressionsConfig.cmake
43-
INSTALL_DESTINATION lib/cmake/Expressions
43+
INSTALL_DESTINATION lib/cmake/ZenAlgorithms
4444
)
4545

4646
INSTALL(FILES
4747
${CMAKE_CURRENT_BINARY_DIR}/ExpressionsConfig.cmake
48-
DESTINATION lib/cmake/Expressions
48+
DESTINATION lib/cmake/ZenAlgorithms
4949
)
5050

51+
if(NOT CMAKE_BUILD_TYPE)
52+
set(CMAKE_BUILD_TYPE Release)
53+
endif()
54+
55+
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
56+
set(CMAKE_CXX_FLAGS_DEBUG "-g")
57+
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
58+
5159
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
5260

5361
if (BUILD_TESTS)
@@ -59,7 +67,7 @@ if (BUILD_TESTS)
5967
FETCHCONTENT_MAKEAVAILABLE(googletest)
6068
ENABLE_TESTING()
6169
ADD_EXECUTABLE(tests ${TESTS} ${SOURCES})
62-
TARGET_LINK_LIBRARIES(tests GTest::gtest_main)
70+
TARGET_LINK_LIBRARIES(tests GTest::gtest_main zen_algorithms_expressions)
6371
INCLUDE(GoogleTest)
6472
GTEST_DISCOVER_TESTS(tests)
6573
endif()

about.png

10.3 KB
Loading

lib/headers/expressions/container.hpp

-49
This file was deleted.

lib/headers/expressions/result.hpp

-37
This file was deleted.
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <utility>
5+
#include <vector>
6+
#include <memory>
7+
#include <unordered_map>
8+
#include <regex>
9+
10+
namespace zen_algorithms::expressions {
11+
12+
class expression_result : public std::enable_shared_from_this<expression_result> {
13+
bool matches_;
14+
std::unordered_map<std::string, std::string> bindings_;
15+
16+
public:
17+
expression_result(const bool matches, const std::unordered_map<std::string, std::string> & bindings) : matches_(matches), bindings_(bindings) {}
18+
19+
bool matches() const { return matches_; }
20+
std::unordered_map<std::string, std::string> get_bindings() const { return bindings_; }
21+
22+
std::string get(const std::string &name) const {
23+
if (bindings_.contains(name))
24+
return bindings_.at(name);
25+
throw std::runtime_error("The provided argument doesn't exists");
26+
}
27+
};
28+
29+
class expression : public std::enable_shared_from_this<expression> {
30+
std::string regex_;
31+
std::vector<std::string> arguments_;
32+
33+
public:
34+
35+
expression(std::string regex, const std::vector<std::string> & arguments) : regex_(std::move(regex)), arguments_(arguments) {}
36+
37+
std::vector<std::string> get_arguments() const { return arguments_; }
38+
std::string get_regex() const { return regex_; }
39+
40+
std::shared_ptr<expression_result> query(const std::string &input) const {
41+
std::unordered_map<std::string, std::string> _bindings;
42+
const std::regex _pattern(regex_);
43+
bool _matches = false;
44+
if (std::smatch _match; std::regex_match(input, _match, _pattern)) {
45+
_matches = true;
46+
auto _iterator = _match.begin();
47+
++_iterator;
48+
for (auto &_key: arguments_) {
49+
_bindings[_key] = *_iterator;
50+
++_iterator;
51+
}
52+
}
53+
return std::make_shared<expression_result>(_matches, _bindings);
54+
}
55+
};
56+
57+
static std::shared_ptr<expression> from_string(const std::string &input) {
58+
std::size_t _open = input.find('{');
59+
std::size_t _close = input.find('}');
60+
std::size_t _position = 0;
61+
62+
std::vector<std::string> _arguments;
63+
std::string _regex;
64+
65+
if (_open == std::string::npos && _close == std::string::npos)
66+
return std::make_shared<expression>(input, _arguments);
67+
68+
while (_open != std::string::npos && _close != std::string::npos) {
69+
_regex.append(input.substr(_position, _open - _position));
70+
std::string _value{input.substr(_open + 1, _close - _open - 1)};
71+
72+
if (std::find(_arguments.begin(), _arguments.end(), _value) != _arguments.end())
73+
throw std::runtime_error("The provided input contains repeated arguments.");
74+
75+
_regex.append(R"(([a-zA-Z0-9\-_]+))");
76+
_arguments.emplace_back(_value);
77+
78+
_position = _close + 1;
79+
_open = input.find('{', _close);
80+
_close = input.find('}', _open);
81+
}
82+
83+
if (_position != input.size())
84+
_regex.append(input.substr(_position, input.size() - _position));
85+
86+
87+
return std::make_shared<expression>(_regex, _arguments);
88+
}
89+
90+
}

lib/sources/container.cpp

-59
This file was deleted.

lib/sources/expressions.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <zen_algorithms/expressions.hpp>
2+
3+
namespace zen_algorithms::expressions { }

lib/sources/result.cpp

-18
This file was deleted.

tests/implementation_test.cc

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
#include <gtest/gtest.h>
2-
#include <expressions/container.hpp>
2+
#include <zen_algorithms/expressions.hpp>
33

44
TEST(Expressions, Assertions) {
5-
using namespace expressions;
5+
using namespace zen_algorithms;
66

7-
const auto _non_empty_expression = container::from_string("/users/{user}/details");
7+
const auto _expression = expressions::from_string("/users/{user}/details");
88

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");
9+
ASSERT_FALSE(_expression->get_arguments().empty());
10+
ASSERT_EQ(_expression->get_arguments().size(), 1);
11+
ASSERT_EQ(_expression->get_arguments().at(0), "user");
1212

13-
const auto _empty_expression = container::from_string("/ping");
14-
ASSERT_TRUE(_empty_expression.get_arguments().empty());
15-
ASSERT_EQ(_empty_expression.get_regex(), "/ping");
1613

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

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");
18+
const auto _non_empty_string_expression_result = _expression->query("/users/80bdc6d1-524e-411a-b316-976a65a3ed3c/details");
2219

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");
20+
ASSERT_TRUE(_non_empty_string_expression_result->matches());
21+
ASSERT_FALSE(_non_empty_string_expression_result->get_bindings().empty());
22+
ASSERT_EQ(_non_empty_string_expression_result->get("user"), "80bdc6d1-524e-411a-b316-976a65a3ed3c");
23+
24+
const auto _non_empty_integer_expression_result = _expression->query("/users/1337/details");
25+
ASSERT_TRUE(_non_empty_integer_expression_result->matches());
26+
ASSERT_FALSE(_non_empty_integer_expression_result->get_bindings().empty());
27+
ASSERT_EQ(_non_empty_integer_expression_result->get("user"), "1337");
2728
}

0 commit comments

Comments
 (0)