|
23 | 23 |
|
24 | 24 | #include "json/base.h" |
25 | 25 |
|
| 26 | +#include <ranges> |
| 27 | + |
26 | 28 | namespace libcdoc { |
27 | 29 |
|
28 | 30 | std::string |
29 | 31 | Lock::getString(Params key) const |
30 | 32 | { |
31 | | - const std::vector<uint8_t>& bytes = params.at(key); |
32 | | - return std::string((const char *) bytes.data(), bytes.size()); |
| 33 | + const std::vector<uint8_t>& bytes = params.at(key); |
| 34 | + return {(const char *) bytes.data(), bytes.size()}; |
33 | 35 | } |
34 | 36 |
|
35 | 37 | int32_t |
@@ -59,50 +61,47 @@ Lock::parseLabel(const std::string& label) |
59 | 61 | { |
60 | 62 | std::map<std::string, std::string> parsed_label; |
61 | 63 | // Check if provided label starts with the machine generated label prefix. |
62 | | - if (!label.starts_with(CDoc2::LABELPREFIX)) |
63 | | - { |
| 64 | + if (!label.starts_with(CDoc2::LABELPREFIX)) { |
64 | 65 | return parsed_label; |
65 | 66 | } |
66 | 67 |
|
67 | | - std::string label_wo_prefix(label.substr(CDoc2::LABELPREFIX.size())); |
| 68 | + auto label_wo_prefix = std::string_view(label).substr(CDoc2::LABELPREFIX.size()); |
68 | 69 |
|
69 | 70 | // Label to be processed |
70 | | - std::string label_to_prcss; |
| 71 | + std::string decodedBase64; // Strong ref |
| 72 | + std::string_view label_to_prcss; |
71 | 73 |
|
72 | 74 | // We ignore mediatype part |
73 | 75 |
|
74 | 76 | // Check, if the label is Base64 encoded |
75 | | - auto base64IndPos = label_wo_prefix.find(CDoc2::LABELBASE64IND); |
76 | | - if (base64IndPos == std::string::npos) |
77 | | - { |
78 | | - if (label_wo_prefix.starts_with(",")) { |
79 | | - label_to_prcss = label_wo_prefix.substr(1); |
80 | | - } else { |
81 | | - label_to_prcss = std::move(label_wo_prefix); |
82 | | - } |
83 | | - } |
84 | | - else |
| 77 | + if (auto base64IndPos = label_wo_prefix.find(CDoc2::LABELBASE64IND); |
| 78 | + base64IndPos != std::string::npos) |
85 | 79 | { |
86 | 80 | std::string base64_label(label_wo_prefix.substr(base64IndPos + CDoc2::LABELBASE64IND.size())); |
87 | | - label_to_prcss = jwt::base::decode<jwt::alphabet::base64>(base64_label); |
| 81 | + decodedBase64 = jwt::base::decode<jwt::alphabet::base64>(base64_label); |
| 82 | + label_to_prcss = decodedBase64; |
| 83 | + } else if (label_wo_prefix.starts_with(",")) { |
| 84 | + label_to_prcss = label_wo_prefix.substr(1); |
| 85 | + } else { |
| 86 | + label_to_prcss = label_wo_prefix; |
88 | 87 | } |
89 | 88 |
|
90 | | - auto label_parts(split(label_to_prcss, '&')); |
91 | | - for (auto& part : label_parts) |
| 89 | + auto range_to_sv = [](auto range) constexpr { |
| 90 | + return std::string_view(&*range.begin(), std::ranges::distance(range)); |
| 91 | + }; |
| 92 | + for (const auto &part : std::ranges::split_view(label_to_prcss, '&')) |
92 | 93 | { |
93 | | - auto label_data_parts(split(part, '=')); |
94 | | - if (label_data_parts.size() != 2) |
95 | | - { |
96 | | - // Invalid label data. We just ignore them. |
| 94 | + auto label_data_parts = std::ranges::split_view(part, '='); |
| 95 | + if (label_data_parts.empty()) { |
97 | 96 | LOG_ERROR("The label '{}' is invalid", label); |
| 97 | + continue; |
98 | 98 | } |
99 | | - else |
100 | | - { |
101 | | - std::string key = urlDecode(label_data_parts[0]); |
102 | | - std::string value = urlDecode(label_data_parts[1]); |
103 | | - std::transform(key.begin(), key.end(), key.begin(), [](unsigned char c){ return std::tolower(c); }); |
104 | | - parsed_label[key] = value; |
105 | | - } |
| 99 | + auto it = label_data_parts.begin(); |
| 100 | + std::string key = urlDecode(range_to_sv(*it)); |
| 101 | + std::ranges::transform(key, key.begin(), [](unsigned char c){ return std::tolower(c); }); |
| 102 | + ++it; |
| 103 | + std::string value = urlDecode(range_to_sv(*it)); |
| 104 | + parsed_label[std::move(key)] = std::move(value); |
106 | 105 | } |
107 | 106 |
|
108 | 107 | return parsed_label; |
|
0 commit comments