LCOV - code coverage report
Current view: top level - spb/io - buffer-io.hpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 28 28
Test Date: 2026-03-06 17:56:28 Functions: 100.0 % 7 7

            Line data    Source code
       1              : /***************************************************************************\
       2              : * Name        : buffered reader                                             *
       3              : * Description : buffer between io::reader and detail::istream              *
       4              : * Author      : antonin.kriz@gmail.com                                      *
       5              : * ------------------------------------------------------------------------- *
       6              : * This is free software; you can redistribute it and/or modify it under the *
       7              : * terms of the MIT license. A copy of the license can be found in the file  *
       8              : * "LICENSE" at the root of this distribution.                               *
       9              : \***************************************************************************/
      10              : 
      11              : #pragma once
      12              : #include "io.hpp"
      13              : #include <array>
      14              : #include <cstdlib>
      15              : #include <cstring>
      16              : #include <string_view>
      17              : 
      18              : namespace spb::io
      19              : {
      20              : #ifndef SPB_READ_BUFFER_SIZE
      21              : #define SPB_READ_BUFFER_SIZE 256U
      22              : #endif
      23              : 
      24              : class buffered_reader
      25              : {
      26              : private:
      27              :     io::reader on_read;
      28              :     std::array< char, SPB_READ_BUFFER_SIZE > buffer;
      29              :     size_t begin_index = 0;
      30              :     size_t end_index   = 0;
      31              :     bool eof_reached   = false;
      32              : 
      33       182457 :     auto bytes_in_buffer( ) const noexcept -> size_t
      34              :     {
      35       182457 :         return end_index - begin_index;
      36              :     }
      37              : 
      38        26069 :     auto space_left_in_buffer( ) const noexcept -> size_t
      39              :     {
      40        26069 :         return SPB_READ_BUFFER_SIZE - end_index;
      41              :     }
      42              : 
      43        27065 :     void shift_data_to_start( ) noexcept
      44              :     {
      45        27065 :         if( begin_index > 0 )
      46              :         {
      47        46194 :             memmove( buffer.data( ), buffer.data( ) + begin_index, bytes_in_buffer( ) );
      48        23097 :             end_index -= begin_index;
      49        23097 :             begin_index = 0;
      50              :         }
      51        27065 :     }
      52              : 
      53        27065 :     void read_buffer( )
      54              :     {
      55        27065 :         shift_data_to_start( );
      56              : 
      57        53134 :         while( bytes_in_buffer( ) < SPB_READ_BUFFER_SIZE && !eof_reached )
      58              :         {
      59        26069 :             auto bytes_in = on_read( &buffer[ end_index ], space_left_in_buffer( ) );
      60        26069 :             eof_reached |= bytes_in == 0;
      61        26069 :             end_index += bytes_in;
      62              :         }
      63        27065 :     }
      64              : 
      65              : public:
      66         3809 :     explicit buffered_reader( io::reader reader )
      67         3809 :         : on_read( reader )
      68              :     {
      69         3809 :     }
      70              : 
      71        34820 :     [[nodiscard]] auto view( size_t minimal_size ) -> std::string_view
      72              :     {
      73        34820 :         minimal_size = std::max< size_t >( minimal_size, 1U );
      74        34820 :         if( bytes_in_buffer( ) < minimal_size )
      75              :         {
      76        27065 :             read_buffer( );
      77              :         }
      78        34820 :         return std::string_view( &buffer[ begin_index ], bytes_in_buffer( ) );
      79              :     }
      80              : 
      81        36586 :     void skip( size_t size ) noexcept
      82              :     {
      83        36586 :         begin_index += std::min( size, bytes_in_buffer( ) );
      84        36586 :     }
      85              : };
      86              : 
      87              : }// namespace spb::io
        

Generated by: LCOV version 2.0-1