|
8 | 8 |
|
9 | 9 | #include <cstddef> |
10 | 10 | #include <limits> |
| 11 | +#include <ranges> |
11 | 12 | #include <string_view> |
12 | 13 | #include <vector> |
13 | 14 |
|
@@ -35,14 +36,10 @@ template<> struct string_traits<pgvector::Vector> { |
35 | 36 | std::vector<float> values; |
36 | 37 | if (text.size() > 2) { |
37 | 38 | std::string_view inner = text.substr(1, text.size() - 2); |
38 | | - size_t start = 0; |
39 | | - for (size_t i = 0; i < inner.size(); i++) { |
40 | | - if (inner[i] == ',') { |
41 | | - values.push_back(pqxx::from_string<float>(inner.substr(start, i - start), c)); |
42 | | - start = i + 1; |
43 | | - } |
| 39 | + for (const auto& v : std::views::split(inner, ',')) { |
| 40 | + std::string_view sv{v.begin(), v.end()}; |
| 41 | + values.push_back(pqxx::from_string<float>(sv, c)); |
44 | 42 | } |
45 | | - values.push_back(pqxx::from_string<float>(inner.substr(start), c)); |
46 | 43 | } |
47 | 44 | return pgvector::Vector{std::move(values)}; |
48 | 45 | } |
@@ -108,14 +105,10 @@ template<> struct string_traits<pgvector::HalfVector> { |
108 | 105 | std::vector<pgvector::Half> values; |
109 | 106 | if (text.size() > 2) { |
110 | 107 | std::string_view inner = text.substr(1, text.size() - 2); |
111 | | - size_t start = 0; |
112 | | - for (size_t i = 0; i < inner.size(); i++) { |
113 | | - if (inner[i] == ',') { |
114 | | - values.push_back(static_cast<pgvector::Half>(pqxx::from_string<float>(inner.substr(start, i - start), c))); |
115 | | - start = i + 1; |
116 | | - } |
| 108 | + for (const auto& v : std::views::split(inner, ',')) { |
| 109 | + std::string_view sv{v.begin(), v.end()}; |
| 110 | + values.push_back(static_cast<pgvector::Half>(pqxx::from_string<float>(sv, c))); |
117 | 111 | } |
118 | | - values.push_back(static_cast<pgvector::Half>(pqxx::from_string<float>(inner.substr(start), c))); |
119 | 112 | } |
120 | 113 | return pgvector::HalfVector{std::move(values)}; |
121 | 114 | } |
@@ -187,32 +180,25 @@ template<> struct string_traits<pgvector::SparseVector> { |
187 | 180 | int dimensions = pqxx::from_string<int>(text.substr(n + 2), c); |
188 | 181 |
|
189 | 182 | if (n > 1) { |
190 | | - auto add_element = [&](std::string_view substr) { |
191 | | - size_t ne = substr.find(":"); |
| 183 | + std::string_view inner = text.substr(1, n - 1); |
| 184 | + for (const auto& v : std::views::split(inner, ',')) { |
| 185 | + std::string_view sv{v.begin(), v.end()}; |
| 186 | + |
| 187 | + size_t ne = sv.find(":"); |
192 | 188 | if (ne == std::string::npos) { |
193 | 189 | throw conversion_error{"Malformed sparsevec literal"}; |
194 | 190 | } |
195 | 191 |
|
196 | | - int index = pqxx::from_string<int>(substr.substr(0, ne), c); |
197 | | - float value = pqxx::from_string<float>(substr.substr(ne + 1), c); |
| 192 | + int index = pqxx::from_string<int>(sv.substr(0, ne), c); |
| 193 | + float value = pqxx::from_string<float>(sv.substr(ne + 1), c); |
198 | 194 |
|
199 | 195 | // check to avoid undefined behavior |
200 | 196 | if (index > std::numeric_limits<int>::min()) { |
201 | 197 | index -= 1; |
202 | 198 | } |
203 | 199 |
|
204 | 200 | map.insert({index, value}); |
205 | | - }; |
206 | | - |
207 | | - std::string_view inner = text.substr(1, n - 1); |
208 | | - size_t start = 0; |
209 | | - for (size_t i = 0; i < inner.size(); i++) { |
210 | | - if (inner[i] == ',') { |
211 | | - add_element(inner.substr(start, i - start)); |
212 | | - start = i + 1; |
213 | | - } |
214 | 201 | } |
215 | | - add_element(inner.substr(start)); |
216 | 202 | } |
217 | 203 |
|
218 | 204 | try { |
|
0 commit comments