48 if (!traits_type::eq_int_type(
c, traits_type::eof())) {
49 *pptr() = traits_type::to_char_type(
c);
52 return sync() == 0 ? traits_type::not_eof(
c) : traits_type::eof();
59 const auto rbase = std::reverse_iterator<char *>(pbase());
60 const auto rpptr = std::reverse_iterator<char *>(pptr());
61 auto is_ascii = [](
char c) {
return (
static_cast<unsigned char>(
c) & 0x80) == 0x00; };
62 auto is_leading = [](
char c) {
return (
static_cast<unsigned char>(
c) & 0xC0) == 0xC0; };
63 auto is_leading_2b = [](
char c) {
return static_cast<unsigned char>(
c) <= 0xDF; };
64 auto is_leading_3b = [](
char c) {
return static_cast<unsigned char>(
c) <= 0xEF; };
66 if (is_ascii(*rpptr)) {
71 const auto rpend = rbase - rpptr >= 3 ? rpptr + 3 : rbase;
72 const auto leading = std::find_if(rpptr, rpend, is_leading);
73 if (leading == rbase) {
76 const auto dist =
static_cast<size_t>(leading - rpptr);
81 }
else if (dist == 1) {
82 remainder = is_leading_2b(*leading) ? 0 : dist + 1;
83 }
else if (dist == 2) {
84 remainder = is_leading_3b(*leading) ? 0 : dist + 1;
95 if (pbase() != pptr()) {
98 auto size =
static_cast<size_t>(pptr() - pbase());
99 size_t remainder = utf8_remainder();
101 if (
size > remainder) {
102 str line(pbase(),
size - remainder);
103 pywrite(std::move(line));
109 std::memmove(pbase(), pptr() - remainder, remainder);
111 setp(pbase(), epptr());
112 pbump(
static_cast<int>(remainder));
117 int sync()
override {
return _sync(); }
120 explicit pythonbuf(
const object &pyostream,
size_t buffer_size = 1024)
121 : buf_size(buffer_size), d_buffer(new char[buf_size]), pywrite(pyostream.attr(
"write")),
122 pyflush(pyostream.attr(
"flush")) {
123 setp(d_buffer.get(), d_buffer.get() + buf_size - 1);
167 const object &pyostream
169 : costream(costream),
buffer(pyostream) {
170 old = costream.rdbuf(&
buffer);
195 const object &pyostream
211 : do_stdout_(do_stdout), do_stderr_(do_stderr) {}
223 redirect_stdout.reset();
224 redirect_stderr.reset();
261 .
def(
"__enter__", &detail::OstreamRedirect::enter)
262 .
def(
"__exit__", [](detail::OstreamRedirect &self_,
const args &) { self_.exit(); });