LCOV - code coverage report
Current view: top level - src/util - string.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 40 40
Test Date: 2026-09-25 06:37:43 Functions: 100.0 % 6 6
Branches: 80.0 % 40 32

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2019-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <util/string.h>
       6                 :             : 
       7                 :             : #include <iterator>
       8                 :             : #include <memory>
       9                 :             : #include <stdexcept>
      10                 :             : #include <string>
      11                 :             : #include <string_view>
      12                 :             : 
      13                 :             : namespace util {
      14                 :          30 : void ReplaceAll(std::string& in_out, std::string_view search, std::string_view substitute)
      15                 :             : {
      16         [ +  + ]:          30 :     if (search.empty()) return;
      17                 :          28 :     auto pos{in_out.find(search)};
      18         [ +  + ]:          28 :     if (pos == std::string::npos) return;
      19                 :             : 
      20                 :             :     // Build separately because repeated std::string::replace() calls move the remaining suffix when sizes differ
      21         [ -  + ]:          26 :     std::string result;
      22   [ -  +  +  - ]:          26 :     result.reserve(in_out.size());
      23                 :             :     std::string::size_type start{0};
      24         [ +  + ]:          60 :     for (; pos != std::string::npos; pos = in_out.find(search, start)) {
      25   [ +  -  +  - ]:          34 :         result.append(in_out, start, pos - start).append(substitute);
      26                 :          34 :         start = pos + search.size();
      27                 :             :     }
      28         [ +  - ]:          26 :     result.append(in_out, start);
      29                 :          26 :     in_out.swap(result);
      30                 :          26 : }
      31                 :             : 
      32                 :          83 : LineReader::LineReader(std::string_view str, size_t max_line_length)
      33                 :          83 :     : m_str{str}, m_max_line_length{max_line_length}, m_it{str.begin()} {}
      34                 :             : 
      35                 :        2669 : std::optional<std::string_view> LineReader::ReadLine()
      36                 :             : {
      37         [ +  + ]:        2669 :     if (m_it == m_str.end()) {
      38                 :          19 :         return std::nullopt;
      39                 :             :     }
      40                 :             : 
      41                 :             :     const auto line_start = m_it;
      42         [ +  + ]:       27806 :     while (m_it != m_str.end()) {
      43                 :             :         // Read a character from the incoming buffer and increment the iterator
      44                 :       27803 :         const bool new_line{*m_it == '\n'};
      45                 :       27803 :         ++m_it;
      46                 :             :         // If the character we just consumed was \n, the line is terminated.
      47                 :             :         // The \n itself does not count against max_line_length.
      48         [ +  + ]:       27803 :         if (new_line) {
      49         [ +  + ]:        2643 :             std::string_view line{line_start, m_it - 1};
      50   [ +  +  +  + ]:        2643 :             if (!line.empty() && line.back() == '\r')
      51                 :          76 :                 line.remove_suffix(1);
      52                 :        2643 :             return line;
      53                 :             :         }
      54                 :             :         // If the character we just consumed gives us a line length greater
      55                 :             :         // than max_line_length, and we are not at the end of the line (or buffer) yet,
      56                 :             :         // that means the line we are currently reading is too long, and we throw.
      57         [ +  + ]:       25160 :         if (static_cast<size_t>(std::distance(line_start, m_it)) > m_max_line_length) {
      58                 :             :             // Reset iterator
      59                 :           4 :             m_it = line_start;
      60         [ +  - ]:           4 :             throw std::runtime_error("max_line_length exceeded by LineReader");
      61                 :             :         }
      62                 :             :     }
      63                 :             :     // End of buffer reached without finding a \n or exceeding max_line_length.
      64                 :             :     // Reset the iterator so the rest of the buffer can be read granularly
      65                 :             :     // with ReadLength() and return null to indicate a line was not found.
      66                 :           3 :     m_it = line_start;
      67                 :           3 :     return std::nullopt;
      68                 :             : }
      69                 :             : 
      70                 :             : // Ignores max_line_length but won't overflow
      71                 :          40 : std::string_view LineReader::ReadLength(size_t len)
      72                 :             : {
      73         [ +  + ]:          40 :     if (len == 0) return {};
      74   [ +  +  +  - ]:          37 :     if (Remaining() < len) throw std::runtime_error("Not enough data in buffer");
      75                 :          36 :     std::string_view out(std::to_address(m_it), len);
      76                 :          36 :     m_it += len;
      77                 :          36 :     return out;
      78                 :             : }
      79                 :             : 
      80                 :         102 : size_t LineReader::Remaining() const
      81                 :             : {
      82                 :         102 :     return std::distance(m_it, m_str.end());
      83                 :             : }
      84                 :             : 
      85                 :        2696 : size_t LineReader::Consumed() const
      86                 :             : {
      87                 :        2696 :     return std::distance(m_str.begin(), m_it);
      88                 :             : }
      89                 :             : } // namespace util
        

Generated by: LCOV version 2.0-1