public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r13-3042] libstdc++: Enable std::bitset<N>::bitset(const char*) for freestanding
Date: Mon,  3 Oct 2022 14:46:12 +0000 (GMT)	[thread overview]
Message-ID: <20221003144612.B3802385482D@sourceware.org> (raw)

https://gcc.gnu.org/g:7f38b5c4c10ebf9b0346bd8c3e9df0bbd0372647

commit r13-3042-g7f38b5c4c10ebf9b0346bd8c3e9df0bbd0372647
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Sep 30 13:36:30 2022 +0100

    libstdc++: Enable std::bitset<N>::bitset(const char*) for freestanding
    
    The std::bitset constructor from a character string is specified in
    terms of std::basic_string and std::char_traits, but doesn't need to be.
    This makes it available for freestanding.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/bitset (bitset(const C*, size_type n, C, C))
            [!_GLIBCXX_HOSTED]: Remove std::basic_string dependency.
            (_M_copy_from_ptr) [!_GLIBCXX_HOSTED]: Abort instead of
            throwing.

Diff:
---
 libstdc++-v3/include/std/bitset | 62 +++++++++++++++++++++++++++++++++--------
 1 file changed, 50 insertions(+), 12 deletions(-)

diff --git a/libstdc++-v3/include/std/bitset b/libstdc++-v3/include/std/bitset
index 10fb05a1b90..afabeb4ba01 100644
--- a/libstdc++-v3/include/std/bitset
+++ b/libstdc++-v3/include/std/bitset
@@ -697,7 +697,38 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       _S_do_sanitize_val(unsigned long long __val)
       { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); }
     };
-#endif
+
+  namespace __bitset
+  {
+#if _GLIBCXX_HOSTED
+    template<typename _CharT>
+      using __string = std::basic_string<_CharT>;
+#else
+    template<typename _CharT>
+      struct __string
+      {
+	using size_type = size_t;
+	static constexpr size_type npos = size_type(-1);
+
+	struct traits_type
+	{
+	  static _GLIBCXX14_CONSTEXPR size_t
+	  length(const _CharT* __s) noexcept
+	  {
+	    size_t __n = 0;
+	    while (__s[__n])
+	      __n++;
+	    return __n;
+	  }
+
+	  static constexpr bool
+	  eq(_CharT __l, _CharT __r) noexcept
+	  { return __l == __r; }
+	};
+      };
+#endif // HOSTED
+  } // namespace __bitset
+#endif // C++11
 
   /**
    *  @brief The %bitset class represents a @e fixed-size sequence of bits.
@@ -963,6 +994,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	  _M_check_initial_position(__s, __position);
 	  _M_copy_from_string(__s, __position, __n, __zero, __one);
 	}
+#endif // HOSTED
 
 #if __cplusplus >= 201103L
       /**
@@ -978,23 +1010,23 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	[[__gnu__::__nonnull__]]
 	_GLIBCXX23_CONSTEXPR
         explicit
-        bitset(const _CharT* __str,
-	       typename std::basic_string<_CharT>::size_type __n
-	       = std::basic_string<_CharT>::npos,
+	bitset(const _CharT* __str,
+	       typename __bitset::__string<_CharT>::size_type __n
+		 = __bitset::__string<_CharT>::npos,
 	       _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
         : _Base()
         {
+#if _GLIBCXX_HOSTED
 	  if (!__str)
 	    __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
+#endif
+	  using _Traits = typename __bitset::__string<_CharT>::traits_type;
 
-	  if (__n == std::basic_string<_CharT>::npos)
-	    __n = std::char_traits<_CharT>::length(__str);
-	  _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0,
-							     __n, __zero,
-							     __one);
+	  if (__n == __bitset::__string<_CharT>::npos)
+	    __n = _Traits::length(__str);
+	  _M_copy_from_ptr<_CharT, _Traits>(__str, __n, 0, __n, __zero, __one);
 	}
 #endif // C++11
-#endif // HOSTED
 
       // 23.3.5.2 bitset operations:
       ///@{
@@ -1463,7 +1495,6 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 #endif
     };
 
-#if _GLIBCXX_HOSTED
   // Definitions of non-inline member functions.
   template<size_t _Nb>
     template<class _CharT, class _Traits>
@@ -1483,10 +1514,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	    else if (_Traits::eq(__c, __one))
 	      _Unchecked_set(__i - 1);
 	    else
-	      __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
+	      {
+#if _GLIBCXX_HOSTED
+		__throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
+#else
+		__builtin_abort();
+#endif
+	      }
 	  }
       }
 
+#if _GLIBCXX_HOSTED
   template<size_t _Nb>
     template<class _CharT, class _Traits, class _Alloc>
       _GLIBCXX23_CONSTEXPR

                 reply	other threads:[~2022-10-03 14:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221003144612.B3802385482D@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).