Skip to content

Commit 9e7dc6f

Browse files
committed
Removed unnecessary copying
1 parent 289bb91 commit 9e7dc6f

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

include/pgvector/pqxx.hpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ template <> struct string_traits<pgvector::Vector> {
3333

3434
std::vector<float> result;
3535
if (text.size() > 2) {
36-
// TODO remove all copying
37-
std::istringstream ss(std::string(text.substr(1, text.size() - 2)));
38-
while (ss.good()) {
39-
std::string substr;
40-
std::getline(ss, substr, ',');
41-
result.push_back(string_traits<float>::from_string(substr, c));
36+
size_t start = 1;
37+
for (size_t i = start; i < text.size() - 2; i++) {
38+
if (text[i] == ',') {
39+
result.push_back(string_traits<float>::from_string(text.substr(start, i - start), c));
40+
start = i + 1;
41+
}
4242
}
43+
result.push_back(string_traits<float>::from_string(text.substr(start, text.size() - start - 1), c));
4344
}
4445
return pgvector::Vector(std::move(result));
4546
}
@@ -70,13 +71,14 @@ template <> struct string_traits<pgvector::HalfVector> {
7071

7172
std::vector<float> result;
7273
if (text.size() > 2) {
73-
// TODO remove all copying
74-
std::istringstream ss(std::string(text.substr(1, text.size() - 2)));
75-
while (ss.good()) {
76-
std::string substr;
77-
std::getline(ss, substr, ',');
78-
result.push_back(string_traits<float>::from_string(substr, c));
74+
size_t start = 1;
75+
for (size_t i = start; i < text.size() - 2; i++) {
76+
if (text[i] == ',') {
77+
result.push_back(string_traits<float>::from_string(text.substr(start, i - start), c));
78+
start = i + 1;
79+
}
7980
}
81+
result.push_back(string_traits<float>::from_string(text.substr(start, text.size() - start - 1), c));
8082
}
8183
return pgvector::HalfVector(std::move(result));
8284
}

0 commit comments

Comments
 (0)