Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Dec 3, 2024
1 parent 26b8a43 commit 52f6833
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ set(taopq_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/src/lib/pq/parameter_traits.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/pq/result.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/pq/result_traits.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/pq/result_traits_array.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/pq/row.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/pq/table_field.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/pq/table_reader.cpp
Expand Down
24 changes: 4 additions & 20 deletions include/tao/pq/result_traits_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,15 @@ namespace tao::pq

namespace internal
{
[[nodiscard]] auto parse_quoted( const char*& value ) -> std::string;

template< typename T >
requires( !pq::is_array_result< T > ) && ( result_traits_size< T > == 1 )
[[nodiscard]] auto parse( const char*& value ) -> T
{
if( *value == '"' ) {
++value;
std::string input;
while( const auto* pos = std::strpbrk( value, "\\\"" ) ) {
switch( *pos ) {
case '\\':
input.append( value, pos++ );
input += *pos++;
value = pos;
break;

case '"':
input.append( value, pos++ );
value = pos;
return result_traits< T >::from( input.c_str() );

default:
TAO_PQ_INTERNAL_UNREACHABLE;
}
}
throw std::invalid_argument( "unterminated quoted string" );
const std::string input = parse_quoted( ++value );
return result_traits< T >::from( input.c_str() );
}
else {
if( const auto* end = std::strpbrk( value, ",;}" ) ) {
Expand Down
38 changes: 38 additions & 0 deletions src/lib/pq/result_traits_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2024 Daniel Frey and Dr. Colin Hirsch
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)

#include <tao/pq/result_traits_array.hpp>

#include <cstring>
#include <stdexcept>
#include <string>

#include <tao/pq/internal/unreachable.hpp>

namespace tao::pq::internal
{
auto parse_quoted( const char*& value ) -> std::string
{
std::string result;
while( const auto* pos = std::strpbrk( value, "\\\"" ) ) {
switch( *pos ) {
case '\\':
result.append( value, pos++ );
result += *pos++;
value = pos;
break;

case '"':
result.append( value, pos++ );
value = pos;
return result;

default:
TAO_PQ_INTERNAL_UNREACHABLE;
}
}
throw std::invalid_argument( "unterminated quoted string" );
}

} // namespace tao::pq::internal

0 comments on commit 52f6833

Please sign in to comment.