diff --git a/libcxx/include/charconv b/libcxx/include/charconv --- a/libcxx/include/charconv +++ b/libcxx/include/charconv @@ -108,6 +108,49 @@ general = fixed | scientific }; +using __chars_format_underlying_type = underlying_type::type; + +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR chars_format +operator~(chars_format __x) { + return chars_format(~static_cast<__chars_format_underlying_type>(__x) & 0x7); +} + +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR chars_format +operator&(chars_format __x, chars_format __y) { + return chars_format(static_cast<__chars_format_underlying_type>(__x) & + static_cast<__chars_format_underlying_type>(__y)); +} + +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR chars_format +operator|(chars_format __x, chars_format __y) { + return chars_format(static_cast<__chars_format_underlying_type>(__x) | + static_cast<__chars_format_underlying_type>(__y)); +} + +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR chars_format +operator^(chars_format __x, chars_format __y) { + return chars_format(static_cast<__chars_format_underlying_type>(__x) ^ + static_cast<__chars_format_underlying_type>(__y)); +} + +inline _LIBCPP_INLINE_VISIBILITY chars_format& operator&=(chars_format& __x, + chars_format __y) { + __x = __x & __y; + return __x; +} + +inline _LIBCPP_INLINE_VISIBILITY chars_format& operator|=(chars_format& __x, + chars_format __y) { + __x = __x | __y; + return __x; +} + +inline _LIBCPP_INLINE_VISIBILITY chars_format& operator^=(chars_format& __x, + chars_format __y) { + __x = __x ^ __y; + return __x; +} + struct _LIBCPP_TYPE_VIS to_chars_result { char* ptr; diff --git a/libcxx/test/std/utilities/charconv/charconv.syn/chars_format.pass.cpp b/libcxx/test/std/utilities/charconv/charconv.syn/chars_format.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/charconv/charconv.syn/chars_format.pass.cpp @@ -0,0 +1,72 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03 + +// + +// Bitmask type +// enum class chars_format { +// scientific = unspecified, +// fixed = unspecified, +// hex = unspecified, +// general = fixed | scientific +// }; + +#include +#include + +#include "test_macros.h" + +void test() { + { + std::chars_format x = std::chars_format::scientific; + x |= std::chars_format::fixed; + assert(x == std::chars_format::general); + } + + { + std::chars_format x = std::chars_format::general; + x &= std::chars_format::fixed; + assert(x == std::chars_format::fixed); + } + { + std::chars_format x = std::chars_format::general; + x ^= std::chars_format::fixed; + assert(x == std::chars_format::scientific); + } +} + +constexpr bool test_constexpr() { + // This construction is used to make the function constexpr in C++11. + return (static_cast( + std::chars_format::scientific) == 1) && + (static_cast( + std::chars_format::fixed) == 2) && + (static_cast( + std::chars_format::hex) == 4) && + + ((std::chars_format::scientific | std::chars_format::fixed) == + std::chars_format::general) && + + (static_cast( + std::chars_format::scientific & std::chars_format::fixed) == 0) && + + ((std::chars_format::general ^ std::chars_format::fixed) == + std::chars_format::scientific) && + + (~std::chars_format::hex == std::chars_format::general); +} + +int main(int, char**) { + test(); + test_constexpr(); + static_assert(test_constexpr(), ""); + + return 0; +}