[libcxx-commits] [libcxx] ac08e2b - [libc++] Make chars_format a bitmask type.
Mark de Wever via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Apr 14 09:17:55 PDT 2021
Author: Mark de Wever
Date: 2021-04-14T18:17:38+02:00
New Revision: ac08e2bb98e6ecc6f56f553109b889abe3ee614e
URL: https://github.com/llvm/llvm-project/commit/ac08e2bb98e6ecc6f56f553109b889abe3ee614e
DIFF: https://github.com/llvm/llvm-project/commit/ac08e2bb98e6ecc6f56f553109b889abe3ee614e.diff
LOG: [libc++] Make chars_format a bitmask type.
Some of Microsoft's unit tests in D70631 fail because libc++'s
implementation of std::chars_format isn't a proper bitmask type. Adding
the required functions to make std::chars_format a proper bitmask type.
Implements parts of P0067: Elementary string conversions
Differential Revision: https://reviews.llvm.org/D97115
Added:
libcxx/test/std/utilities/charconv/charconv.syn/chars_format.pass.cpp
Modified:
libcxx/include/charconv
Removed:
################################################################################
diff --git a/libcxx/include/charconv b/libcxx/include/charconv
index 4666c5c51db66..6397c56f61d0c 100644
--- a/libcxx/include/charconv
+++ b/libcxx/include/charconv
@@ -81,6 +81,7 @@ namespace std {
#include <cstring>
#include <limits>
#include <type_traits>
+#include <utility>
#include <__debug>
@@ -108,6 +109,47 @@ enum class _LIBCPP_ENUM_VIS chars_format
general = fixed | scientific
};
+inline _LIBCPP_INLINE_VISIBILITY constexpr chars_format
+operator~(chars_format __x) {
+ return chars_format(~_VSTD::__to_underlying(__x));
+}
+
+inline _LIBCPP_INLINE_VISIBILITY constexpr chars_format
+operator&(chars_format __x, chars_format __y) {
+ return chars_format(_VSTD::__to_underlying(__x) &
+ _VSTD::__to_underlying(__y));
+}
+
+inline _LIBCPP_INLINE_VISIBILITY constexpr chars_format
+operator|(chars_format __x, chars_format __y) {
+ return chars_format(_VSTD::__to_underlying(__x) |
+ _VSTD::__to_underlying(__y));
+}
+
+inline _LIBCPP_INLINE_VISIBILITY constexpr chars_format
+operator^(chars_format __x, chars_format __y) {
+ return chars_format(_VSTD::__to_underlying(__x) ^
+ _VSTD::__to_underlying(__y));
+}
+
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 chars_format&
+operator&=(chars_format& __x, chars_format __y) {
+ __x = __x & __y;
+ return __x;
+}
+
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 chars_format&
+operator|=(chars_format& __x, chars_format __y) {
+ __x = __x | __y;
+ return __x;
+}
+
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 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
index 0000000000000..475852f81339d
--- /dev/null
+++ b/libcxx/test/std/utilities/charconv/charconv.syn/chars_format.pass.cpp
@@ -0,0 +1,78 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// Note: chars_format is a C++17 feature backported to C++11. Assert isn't
+// allowed in a constexpr function in C++11. To keep the code readable, C++11
+// support is untested.
+// UNSUPPORTED: c++03, c++11
+
+// <charconv>
+
+// Bitmask type
+// enum class chars_format {
+// scientific = unspecified,
+// fixed = unspecified,
+// hex = unspecified,
+// general = fixed | scientific
+// };
+
+#include <charconv>
+#include <cassert>
+
+#include "test_macros.h"
+
+constexpr bool test() {
+ using cf = std::chars_format;
+ using ut = std::underlying_type<cf>::type;
+
+ {
+ cf x = cf::scientific;
+ x |= cf::fixed;
+ assert(x == cf::general);
+ }
+ {
+ cf x = cf::general;
+ x &= cf::fixed;
+ assert(x == cf::fixed);
+ }
+ {
+ cf x = cf::general;
+ x ^= cf::fixed;
+ assert(x == cf::scientific);
+ }
+
+ assert(static_cast<ut>(cf::scientific & (cf::fixed | cf::hex)) == 0);
+ assert(static_cast<ut>(cf::fixed & (cf::scientific | cf::hex)) == 0);
+ assert(static_cast<ut>(cf::hex & (cf::scientific | cf::fixed)) == 0);
+
+ assert((cf::scientific | cf::fixed) == cf::general);
+
+ assert(static_cast<ut>(cf::scientific & cf::fixed) == 0);
+
+ assert((cf::general ^ cf::fixed) == cf::scientific);
+
+ assert((~cf::hex & cf::general) == cf::general);
+
+ return true;
+}
+
+std::chars_format x;
+static_assert(std::is_same<std::chars_format, decltype(~x)>::value, "");
+static_assert(std::is_same<std::chars_format, decltype(x & x)>::value, "");
+static_assert(std::is_same<std::chars_format, decltype(x | x)>::value, "");
+static_assert(std::is_same<std::chars_format, decltype(x ^ x)>::value, "");
+static_assert(std::is_same<std::chars_format&, decltype(x &= x)>::value, "");
+static_assert(std::is_same<std::chars_format&, decltype(x |= x)>::value, "");
+static_assert(std::is_same<std::chars_format&, decltype(x ^= x)>::value, "");
+
+int main(int, char**) {
+ assert(test());
+ static_assert(test(), "");
+
+ return 0;
+}
More information about the libcxx-commits
mailing list