Skip to content

Commit c06a630

Browse files
authored
Use ranges v3 (#131)
* Replaced some skyr functions with range v3, and removed some redundant code * Added range-v3 to CI builds * Disabled MSVC 2017 because of range-v3 support * Resolved some missing directories from install step that were removed * Removed a lot of redundant code from percent_encoding * Fixed missing header in json.hpp * Cleaned up redundant code in unicode functions and iterators * constexpr crazy * Corrected includes
1 parent da87489 commit c06a630

37 files changed

+456
-1400
lines changed

.appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
image:
2-
- Visual Studio 2017
2+
# - Visual Studio 2017
33
- Visual Studio 2019
44

55
platform:
@@ -17,7 +17,7 @@ install:
1717
- cd C:\Tools\vcpkg
1818
- git pull
1919
- .\bootstrap-vcpkg.bat
20-
- vcpkg install tl-expected catch2 nlohmann-json fmt --triplet x64-windows
20+
- vcpkg install tl-expected range-v3 catch2 nlohmann-json fmt --triplet x64-windows
2121
- vcpkg integrate install
2222
- cd %APPVEYOR_BUILD_FOLDER%
2323

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ install:
109109
- git checkout -b master origin/master
110110
- export
111111
- ./bootstrap-vcpkg.sh
112-
- ./vcpkg install tl-expected catch2 nlohmann-json fmt
112+
- ./vcpkg install tl-expected range-v3 catch2 nlohmann-json fmt
113113
- popd
114114

115115
cache:

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
3232
set(CMAKE_CXX_STANDARD_REQUIRED ON)
3333

3434
find_package(tl-expected CONFIG REQUIRED)
35+
find_package(range-v3 CONFIG REQUIRED)
3536

3637
if (skyr_ENABLE_JSON_FUNCTIONS)
3738
find_package(nlohmann_json CONFIG REQUIRED)
@@ -119,7 +120,6 @@ install(
119120
"${PROJECT_SOURCE_DIR}/include/skyr/domain"
120121
"${PROJECT_SOURCE_DIR}/include/skyr/network"
121122
"${PROJECT_SOURCE_DIR}/include/skyr/percent_encoding"
122-
"${PROJECT_SOURCE_DIR}/include/skyr/query"
123123
DESTINATION
124124
include/skyr
125125
)
@@ -131,8 +131,6 @@ install(
131131
"${PROJECT_SOURCE_DIR}/include/skyr/v1/network"
132132
"${PROJECT_SOURCE_DIR}/include/skyr/v1/percent_encoding"
133133
"${PROJECT_SOURCE_DIR}/include/skyr/v1/platform"
134-
"${PROJECT_SOURCE_DIR}/include/skyr/v1/query"
135-
"${PROJECT_SOURCE_DIR}/include/skyr/v1/ranges"
136134
"${PROJECT_SOURCE_DIR}/include/skyr/v1/string"
137135
"${PROJECT_SOURCE_DIR}/include/skyr/v1/traits"
138136
"${PROJECT_SOURCE_DIR}/include/skyr/v1/unicode"

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Using `vcpkg`, install the library dependencies:
6363
> git fetch origin master
6464
> git checkout -b master origin/master
6565
> ./bootstrap-vcpkg.sh
66-
> ./vcpkg install tl-expected catch2 nlohmann-json fmt
66+
> ./vcpkg install tl-expected range-v3 catch2 nlohmann-json fmt
6767
```
6868

6969
### Building the project with `CMake` and `Ninja`
@@ -166,6 +166,7 @@ Here is the ``CMake`` script to build the example:
166166
project(my_project)
167167
168168
find_package(tl-expected CONFIG REQUIRED)
169+
find_package(range-v3 CONFIG REQUIRED)
169170
find_package(skyr-url CONFIG REQUIRED)
170171
171172
set(CMAKE_CXX_STANDARD 17)
@@ -193,7 +194,8 @@ Search parameters:
193194

194195
## Dependencies
195196

196-
This library uses [expected](https://github.com/TartanLlama/expected).
197+
This library uses [expected](https://github.com/TartanLlama/expected)
198+
and [Range v3](https://github.com/ericniebler/range-v3).
197199

198200
The tests use [Catch2](https://github.com/catchorg/catch2),
199201
[nlohmann-json](https://github.com/nlohmann/json) and
@@ -224,9 +226,10 @@ MacOS:
224226

225227
Windows:
226228

227-
* Microsoft Visual C++ 2017
228229
* Microsoft Visual C++ 2019
229230

231+
Microsoft Visual C++ 2017 is supported below version 0.11.
232+
230233
## License
231234

232235
This library is released under the Boost Software License (please see

include/skyr/query/query_parameter_range.hpp

Lines changed: 0 additions & 11 deletions
This file was deleted.

include/skyr/v1/json/json.hpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
#define SKYR_V1_JSON_JSON_HPP
88

99
#include <string>
10-
#include <system_error>
10+
#include <optional>
1111
#include <vector>
1212
#include <tl/expected.hpp>
13+
#include <range/v3/view/split_when.hpp>
14+
#include <range/v3/view/transform.hpp>
1315
#include <nlohmann/json.hpp>
14-
#include <skyr/v1/query/query_parameter_range.hpp>
1516
#include <skyr/v1/percent_encoding/percent_encode.hpp>
1617
#include <skyr/v1/percent_encoding/percent_decode.hpp>
1718

@@ -65,25 +66,40 @@ inline auto decode_query(std::string_view query, char separator='&', char equal=
6566
query.remove_prefix(1);
6667
}
6768

69+
static constexpr auto is_separator = [] (auto &&c) {
70+
return c == '&' || c == ';';
71+
};
72+
73+
static constexpr auto to_nvp = [] (auto &&param) -> std::pair<std::string_view, std::optional<std::string_view>> {
74+
auto element = std::string_view(std::addressof(*std::begin(param)), ranges::distance(param));
75+
auto delim = element.find_first_of("=");
76+
if (delim != std::string_view::npos) {
77+
return { element.substr(0, delim), element.substr(delim + 1) };
78+
}
79+
else {
80+
return { element, std::nullopt };
81+
}
82+
};
83+
6884
nlohmann::json object;
69-
for (auto [key, value] : query_parameter_range(query)) {
70-
const auto key_ = skyr::percent_decode(key).value();
85+
for (auto [name, value] : query | ranges::views::split_when(is_separator) | ranges::views::transform(to_nvp)) {
86+
const auto name_ = skyr::percent_decode(name).value();
7187
const auto value_ = value? skyr::percent_decode(value.value()).value() : std::string();
7288

73-
if (object.contains(key_)) {
74-
auto current_value = object[key_];
89+
if (object.contains(name_)) {
90+
auto current_value = object[name_];
7591
if (current_value.is_string()) {
7692
auto prev_value = current_value.get<std::string>();
77-
object[key_] = std::vector<std::string>{prev_value, value_};
93+
object[name_] = std::vector<std::string>{prev_value, value_};
7894
}
7995
else if (current_value.is_array()) {
8096
auto values = current_value.get<std::vector<std::string>>();
8197
values.emplace_back(value_);
82-
object[key_] = values;
98+
object[name_] = values;
8399
}
84100
}
85101
else {
86-
object[key_] = value_;
102+
object[name_] = value_;
87103
}
88104
}
89105
return object;

include/skyr/v1/percent_encoding/percent_decode.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ namespace skyr {
1515
inline namespace v1 {
1616
/// Percent decodes the input
1717
/// \returns The percent decoded output when successful, an error otherwise.
18-
inline auto percent_decode(std::string_view input) {
19-
using namespace v1::percent_encoding;
20-
return as<std::string>(input | views::decode);
18+
inline auto percent_decode(std::string_view input) -> tl::expected<std::string, percent_encoding::percent_encode_errc> {
19+
auto result = std::string{};
20+
21+
auto range = percent_encoding::percent_decode_range{input};
22+
for (auto it = std::cbegin(range); it != std::cend(range); ++it) {
23+
if (!*it) {
24+
return tl::make_unexpected((*it).error());
25+
}
26+
result.push_back((*it).value());
27+
}
28+
return result;
2129
}
2230
} // namespace v1
2331
} // namespace skyr

0 commit comments

Comments
 (0)