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

Use char_traits in streaming operator>> of bitset #4970

Merged
merged 5 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stl/inc/bitset
Original file line number Diff line number Diff line change
Expand Up @@ -589,13 +589,13 @@ basic_istream<_Elem, _Tr>& operator>>(basic_istream<_Elem, _Tr>& _Istr, bitset<_
if (_Tr::eq_int_type(_Tr::eof(), _Meta)) { // end of file, quit
_State |= _Istr_t::eofbit;
break;
} else if ((_Char = _Tr::to_char_type(_Meta)) != _Elem0 && _Char != _Elem1) {
} else if (!_Tr::eq((_Char = _Tr::to_char_type(_Meta)), _Elem0) && !_Tr::eq(_Char, _Elem1)) {
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
break; // invalid element
} else if (_Str.max_size() <= _Str.size()) { // no room in string, give up (unlikely)
_State |= _Istr_t::failbit;
break;
} else { // valid, append '0' or '1'
_Str.push_back('0' + (_Char == _Elem1));
_Str.push_back('0' + _Tr::eq(_Char, _Elem1));
_Changed = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <bitset>
#include <cassert>
#include <cstddef>
#include <cstring>
#include <cwchar>
#include <ios>
#include <iosfwd>
#include <sstream>
#include <string>
#include <type_traits>

Expand All @@ -22,15 +24,13 @@

using namespace std;

enum odd_char : unsigned char {};

template <>
class std::char_traits<odd_char> {
template <class T>
class odd_char_traits {
private:
static constexpr unsigned char odd_mask = 0xF;

public:
using char_type = odd_char;
using char_type = T;
using int_type = int;
using off_type = streamoff;
using pos_type = streampos;
Expand Down Expand Up @@ -161,6 +161,11 @@ class std::char_traits<odd_char> {
}
};

enum odd_char : unsigned char {};

template <>
class char_traits<odd_char> : public odd_char_traits<odd_char> {};

CONSTEXPR20 bool test_gh_4930() {
constexpr odd_char s_init[]{static_cast<odd_char>(0x55), static_cast<odd_char>(0x44), static_cast<odd_char>(0x33),
static_cast<odd_char>(0x22), static_cast<odd_char>(0x11), static_cast<odd_char>(0)};
Expand Down Expand Up @@ -280,6 +285,16 @@ CONSTEXPR20 bool test_gh_4930() {
static_assert(test_gh_4930());
#endif // _HAS_CXX20

void test_gh_4956() {
basic_string<char, odd_char_traits<char>> s("QQPPQ", 5);
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

bitset<7> bs;
std::basic_stringstream<char, odd_char_traits<char>>(s) >> bs;
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

assert(bs.to_ulong() == 0b11001);
}

int main() {
assert(test_gh_4930());
test_gh_4956();
}