<commit_before>#pragma once

#include <functional>
#include <ostream>
#include <string>

#include <boost/assert.hpp>
#include <boost/config.hpp>

#include "blackhole/detail/config/nullptr.hpp"
#include "blackhole/detail/support/char.hpp"

#include "streambuf.hpp"

namespace blackhole {

namespace aux {

template<typename Char, class Traits = std::char_traits<Char>, class Allocator = std::allocator<Char> >
class attachable_basic_ostringstream : public std::basic_ostream<Char, Traits> {
public:
    typedef Char char_type;
    typedef Traits traits_type;
    typedef Allocator allocator_type;
    typedef basic_ostringstreambuf<Char, Traits, Allocator> streambuf_type;
    typedef typename streambuf_type::string_type string_type;

    typedef std::basic_ostream<Char, Traits> ostream_type;
    typedef typename ostream_type::int_type int_type;
    typedef typename ostream_type::pos_type pos_type;
    typedef typename ostream_type::off_type off_type;

private:
    typedef std::ios_base&(*ios_base_manip)(std::ios_base&);
    typedef std::basic_ios<char_type, traits_type>&(*basic_ios_manip)(std::basic_ios<char_type, traits_type>&);
    typedef ostream_type&(*stream_manip)(ostream_type&);

    streambuf_type streambuf;

public:
    //!@note: According to the 27.7.3.2/4 `std::basic_ostream` dtor does not perform any
    //!       operations on rdbuf(), so it can be in broken state. However there is no mention
    //!       about `std::basic_ostream` ctor behaviour on passing non-initialized `streambuf`
    //!       member pointer.
    //!       I am pretty sure it's safe, because `std::ostringstream` does the same way.
    attachable_basic_ostringstream() :
        ostream_type(&streambuf)
    {
        initialize();
    }

    explicit attachable_basic_ostringstream(string_type& string) :
        ostream_type(&streambuf),
        streambuf(std::ref(string))
    {
        initialize();
    }

    attachable_basic_ostringstream(const attachable_basic_ostringstream & other) = delete;
    attachable_basic_ostringstream& operator=(const attachable_basic_ostringstream& other) = delete;

    ~attachable_basic_ostringstream() {
        if (streambuf.storage()) {
            this->flush();
        }
    }

    void attach(string_type& string) {
        streambuf.attach(string);
        ostream_type::clear(ostream_type::goodbit);
    }

    void detach() {
        streambuf.detach();
        ostream_type::clear(ostream_type::badbit);
    }

    const string_type& str() {
        this->flush();

        string_type* storage = streambuf.storage();
        BOOST_ASSERT(storage != nullptr);<commit_msg>[Code Style] Style.<commit_after>#pragma once

#include <functional>
#include <ostream>
#include <string>

#include <boost/assert.hpp>
#include <boost/config.hpp>

#include "blackhole/detail/config/nullptr.hpp"
#include "blackhole/detail/support/char.hpp"

#include "streambuf.hpp"

namespace blackhole {

namespace aux {

template<typename Char, class Traits = std::char_traits<Char>, class Allocator = std::allocator<Char>>
class attachable_basic_ostringstream : public std::basic_ostream<Char, Traits> {
public:
    typedef Char char_type;
    typedef Traits traits_type;
    typedef Allocator allocator_type;
    typedef basic_ostringstreambuf<Char, Traits, Allocator> streambuf_type;
    typedef typename streambuf_type::string_type string_type;

    typedef std::basic_ostream<Char, Traits> ostream_type;
    typedef typename ostream_type::int_type int_type;
    typedef typename ostream_type::pos_type pos_type;
    typedef typename ostream_type::off_type off_type;

private:
    typedef std::ios_base&(*ios_base_manip)(std::ios_base&);
    typedef std::basic_ios<char_type, traits_type>&(*basic_ios_manip)(std::basic_ios<char_type, traits_type>&);
    typedef ostream_type&(*stream_manip)(ostream_type&);

    streambuf_type streambuf;

public:
    //!@note: According to the 27.7.3.2/4 `std::basic_ostream` dtor does not perform any
    //!       operations on rdbuf(), so it can be in broken state. However there is no mention
    //!       about `std::basic_ostream` ctor behaviour on passing non-initialized `streambuf`
    //!       member pointer.
    //!       I am pretty sure it's safe, because `std::ostringstream` does the same way.
    attachable_basic_ostringstream() :
        ostream_type(&streambuf)
    {
        initialize();
    }

    explicit attachable_basic_ostringstream(string_type& string) :
        ostream_type(&streambuf),
        streambuf(std::ref(string))
    {
        initialize();
    }

    attachable_basic_ostringstream(const attachable_basic_ostringstream& other) = delete;
    attachable_basic_ostringstream& operator=(const attachable_basic_ostringstream& other) = delete;

    ~attachable_basic_ostringstream() {
        if (streambuf.storage()) {
            this->flush();
        }
    }

    void attach(string_type& string) {
        streambuf.attach(string);
        ostream_type::clear(ostream_type::goodbit);
    }

    void detach() {
        streambuf.detach();
        ostream_type::clear(ostream_type::badbit);
    }

    const string_type& str() {
        this->flush();

        string_type* storage = streambuf.storage();
        BOOST_ASSERT(storage != nullptr);
{'fraction_non_alphanumeric': 0.10948210205635948, 'fraction_numerical': 0.002284843869002285, 'mean_word_length': 3.900186567164179, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4425840', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1608, 'n_words': 341}