Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid output when using wbuffer_convert with ostringstream #59083

Open
ldionne opened this issue Nov 19, 2022 · 0 comments
Open

Invalid output when using wbuffer_convert with ostringstream #59083

ldionne opened this issue Nov 19, 2022 · 0 comments
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. locale issues related to localization

Comments

@ldionne
Copy link
Member

ldionne commented Nov 19, 2022

The following test case was extracted from libcxx/test/std/localization/locales/locale.convenience/conversions/conversions.buffer/overflow.pass.cpp: https://godbolt.org/z/Wc73box63

#include <locale>
#include <codecvt>
#include <fstream>
#include <cassert>

#include <iostream>
#include <sstream>


struct test_buf
    : public std::wbuffer_convert<std::codecvt_utf8<wchar_t> >
{
    typedef std::wbuffer_convert<std::codecvt_utf8<wchar_t> > base;
    typedef base::char_type   char_type;
    typedef base::int_type    int_type;
    typedef base::traits_type traits_type;

    explicit test_buf(std::streambuf* sb) : base(sb) {}

    char_type* pbase() const {return base::pbase();}
    char_type* pptr()  const {return base::pptr();}
    char_type* epptr() const {return base::epptr();}
    void gbump(int n) {base::gbump(n);}

    virtual int_type overflow(int_type c = traits_type::eof()) {return base::overflow(c);}
};

int main() {
    std::ostringstream out;
    test_buf f(out.rdbuf());
    assert(f.sputc(0x4E51) == 0x4E51);
    assert(f.sputc(0x4E52) == 0x4E52);
    assert(f.sputc(0x4E53) == 0x4E53);

    std::string s = out.str();
    std::cout << "s = '" << s << "', s.size() = " << s.size() << std::endl;
}

MSVC output: s = '乑乒乓', s.size() = 9
GCC output: s = '', s.size() = 0
Libc++ output: s = '乑', s.size() = 3

Only the MSVC output makes sense IMO.

@ldionne ldionne added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Nov 19, 2022
@philnik777 philnik777 added the locale issues related to localization label Jul 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. locale issues related to localization
Projects
None yet
Development

No branches or pull requests

2 participants