@@ -121,53 +121,47 @@ template <> struct string_traits<pgvector::SparseVector> {
121121 static constexpr bool converts_from_string{true };
122122
123123 static pgvector::SparseVector from_string (std::string_view text) {
124- try {
125- if (text.size () < 4 || text.front () != ' {' ) {
126- throw conversion_error (" Malformed sparsevec literal" );
127- }
128-
129- size_t n = text.find (" }/" , 1 );
130- if (n == std::string_view::npos) {
131- throw conversion_error (" Malformed sparsevec literal" );
132- }
124+ if (text.size () < 4 || text.front () != ' {' ) {
125+ throw conversion_error (" Malformed sparsevec literal" );
126+ }
133127
134- int dimensions = std::stoi ( std::string ( text.substr (n + 2 )) );
135- if (dimensions < 0 ) {
136- throw conversion_error (" Dimensions cannot be negative " );
137- }
128+ size_t n = text.find ( " }/ " , 1 );
129+ if (n == std::string_view::npos ) {
130+ throw conversion_error (" Malformed sparsevec literal " );
131+ }
138132
139- std::vector<int > indices;
140- std::vector<float > values;
133+ int dimensions = pqxx::from_string<int >(text.substr (n + 2 ));
134+ if (dimensions < 0 ) {
135+ throw conversion_error (" Dimensions cannot be negative" );
136+ }
141137
142- if (n > 1 ) {
143- std::istringstream ss (std::string (text.substr (1 , n)));
144- while (ss.good ()) {
145- std::string substr;
146- std::getline (ss, substr, ' ,' );
138+ std::vector<int > indices;
139+ std::vector<float > values;
147140
148- size_t ne = substr.find (" :" );
149- if (ne == std::string::npos) {
150- throw conversion_error (" Malformed sparsevec literal" );
151- }
141+ if (n > 1 ) {
142+ std::istringstream ss (std::string (text.substr (1 , n - 1 )));
143+ while (ss.good ()) {
144+ std::string substr;
145+ std::getline (ss, substr, ' ,' );
152146
153- int index = std::stoi (substr.substr (0 , ne));
154- float value = std::stof (substr.substr (ne + 1 ));
147+ size_t ne = substr.find (" :" );
148+ if (ne == std::string::npos) {
149+ throw conversion_error (" Malformed sparsevec literal" );
150+ }
155151
156- if (index < 1 ) {
157- throw conversion_error (" Index out of bounds" );
158- }
152+ int index = pqxx::from_string<int >(substr.substr (0 , ne));
153+ float value = pqxx::from_string<float >(substr.substr (ne + 1 ));
159154
160- indices. push_back (index - 1 );
161- values. push_back (value );
155+ if (index < 1 ) {
156+ throw conversion_error ( " Index out of bounds " );
162157 }
163- }
164158
165- return pgvector::SparseVector (dimensions, indices, values);
166- } catch (const std::invalid_argument& e) {
167- throw conversion_error (" No conversion" );
168- } catch (const std::out_of_range& e) {
169- throw conversion_error (" Out of range" );
159+ indices.push_back (index - 1 );
160+ values.push_back (value);
161+ }
170162 }
163+
164+ return pgvector::SparseVector (dimensions, indices, values);
171165 }
172166
173167 static zview to_buf (char * begin, char * end, const pgvector::SparseVector& value) {
0 commit comments