Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2019-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <util/string.h>7#include <iterator>8#include <memory>9#include <stdexcept>10#include <string>11#include <string_view>13namespace util {14void ReplaceAll(std::string& in_out, std::string_view search, std::string_view substitute)15{16if (search.empty()) return;17auto pos{in_out.find(search)};18if (pos == std::string::npos) return;20// Build separately because repeated std::string::replace() calls move the remaining suffix when sizes differ21std::string result;22result.reserve(in_out.size());23std::string::size_type start{0};24for (; pos != std::string::npos; pos = in_out.find(search, start)) {25result.append(in_out, start, pos - start).append(substitute);26start = pos + search.size();27}28result.append(in_out, start);29in_out.swap(result);30}32LineReader::LineReader(std::string_view str, size_t max_line_length)33: m_str{str}, m_max_line_length{max_line_length}, m_it{str.begin()} {}35std::optional<std::string_view> LineReader::ReadLine()36{37if (m_it == m_str.end()) {38return std::nullopt;39}41const auto line_start = m_it;42while (m_it != m_str.end()) {43// Read a character from the incoming buffer and increment the iterator44const bool new_line{*m_it == '\n'};45++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.48if (new_line) {49std::string_view line{line_start, m_it - 1};50if (!line.empty() && line.back() == '\r')51line.remove_suffix(1);52return line;53}54// If the character we just consumed gives us a line length greater55// 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.57if (static_cast<size_t>(std::distance(line_start, m_it)) > m_max_line_length) {58// Reset iterator59m_it = line_start;60throw 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 granularly65// with ReadLength() and return null to indicate a line was not found.66m_it = line_start;67return std::nullopt;68}70// Ignores max_line_length but won't overflow71std::string_view LineReader::ReadLength(size_t len)72{73if (len == 0) return {};74if (Remaining() < len) throw std::runtime_error("Not enough data in buffer");75std::string_view out(std::to_address(m_it), len);76m_it += len;77return out;78}80size_t LineReader::Remaining() const81{82return std::distance(m_it, m_str.end());83}85size_t LineReader::Consumed() const86{87return std::distance(m_str.begin(), m_it);88}89} // namespace util
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.