Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Dec 3, 2024
1 parent 52f6833 commit 2df02a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
28 changes: 10 additions & 18 deletions include/tao/pq/result_traits_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#ifndef TAO_PQ_RESULT_TRAITS_ARRAY_HPP
#define TAO_PQ_RESULT_TRAITS_ARRAY_HPP

#include <cassert>
#include <cstring>
#include <list>
#include <set>
#include <stdexcept>
Expand Down Expand Up @@ -48,6 +46,7 @@ namespace tao::pq
namespace internal
{
[[nodiscard]] auto parse_quoted( const char*& value ) -> std::string;
[[nodiscard]] auto parse_unquoted( const char*& value ) -> std::string;

template< typename T >
requires( !pq::is_array_result< T > ) && ( result_traits_size< T > == 1 )
Expand All @@ -57,24 +56,17 @@ namespace tao::pq
const std::string input = parse_quoted( ++value );
return result_traits< T >::from( input.c_str() );
}
else {
if( const auto* end = std::strpbrk( value, ",;}" ) ) {
const std::string input( value, end );
value = end;
if( input == "NULL" ) {
if constexpr( requires { result_traits< T >::null(); } ) {
return result_traits< T >::null();
}
else {
throw std::invalid_argument( "unexpected NULL value" );
}
}
else {
return result_traits< T >::from( input.c_str() );
}

const std::string input = parse_unquoted( value );
if( input == "NULL" ) {
if constexpr( requires { result_traits< T >::null(); } ) {
return result_traits< T >::null();
}
else {
throw std::invalid_argument( "unexpected NULL value" );
}
throw std::invalid_argument( "unterminated unquoted string" );
}
return result_traits< T >::from( input.c_str() );
}

template< typename T >
Expand Down
10 changes: 10 additions & 0 deletions src/lib/pq/result_traits_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ namespace tao::pq::internal
throw std::invalid_argument( "unterminated quoted string" );
}

auto parse_unquoted( const char*& value ) -> std::string
{
if( const auto* end = std::strpbrk( value, ",;}" ) ) {
const std::string result( value, end );
value = end;
return result;
}
throw std::invalid_argument( "unterminated unquoted string" );
}

} // namespace tao::pq::internal

0 comments on commit 2df02a1

Please sign in to comment.