// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
-// { dg-options "-std=gnu++17" }
-// { dg-do run { target c++17 } }
+// <charconv> is supported in C++14 as a GNU extension
+// { dg-do run { target c++14 } }
#include <charconv>
-#include <string_view>
+#include <string>
#include <testsuite_hooks.h>
// Test std::from_chars error handling.
{
std::from_chars_result r;
int i = 999;
- std::string_view s;
+ std::string s;
s = "";
- r = std::from_chars(s.begin(), s.end(), i);
+ r = std::from_chars(s.data(), s.data() + s.length(), i);
VERIFY( r.ec == std::errc::invalid_argument );
- VERIFY( r.ptr == s.begin() );
+ VERIFY( r.ptr == s.data() );
VERIFY( i == 999 );
s = "*";
- r = std::from_chars(s.begin(), s.end(), i);
+ r = std::from_chars(s.data(), s.data() + s.length(), i);
VERIFY( r.ec == std::errc::invalid_argument );
- VERIFY( r.ptr == s.begin() );
+ VERIFY( r.ptr == s.data() );
VERIFY( i == 999 );
s = "-";
- r = std::from_chars(s.begin(), s.end(), i);
+ r = std::from_chars(s.data(), s.data() + s.length(), i);
VERIFY( r.ec == std::errc::invalid_argument );
- VERIFY( r.ptr == s.begin() );
+ VERIFY( r.ptr == s.data() );
VERIFY( i == 999 );
s = "-*";
- r = std::from_chars(s.begin(), s.end(), i);
+ r = std::from_chars(s.data(), s.data() + s.length(), i);
VERIFY( r.ec == std::errc::invalid_argument );
- VERIFY( r.ptr == s.begin() );
+ VERIFY( r.ptr == s.data() );
VERIFY( i == 999 );
unsigned u = 888;
s = "-1";
- r = std::from_chars(s.begin(), s.end(), u);
+ r = std::from_chars(s.data(), s.data() + s.length(), u);
VERIFY( r.ec == std::errc::invalid_argument );
- VERIFY( r.ptr == s.begin() );
+ VERIFY( r.ptr == s.data() );
s = "-a";
- r = std::from_chars(s.begin(), s.end(), u);
+ r = std::from_chars(s.data(), s.data() + s.length(), u);
VERIFY( r.ec == std::errc::invalid_argument );
- VERIFY( r.ptr == s.begin() );
+ VERIFY( r.ptr == s.data() );
s = "-";
- r = std::from_chars(s.begin(), s.end(), u);
+ r = std::from_chars(s.data(), s.data() + s.length(), u);
VERIFY( r.ec == std::errc::invalid_argument );
- VERIFY( r.ptr == s.begin() );
+ VERIFY( r.ptr == s.data() );
VERIFY( u == 888 );
for (int base = 2; base <= 36; ++base)
test02()
{
std::from_chars_result r;
- std::string_view s;
+ std::string s;
signed char c = -5;
s = "-10000001";
- r = std::from_chars(s.begin(), s.end(), c, 2);
+ r = std::from_chars(s.data(), s.data() + s.length(), c, 2);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.end() );
+ VERIFY( r.ptr == s.data() + s.length() );
s = "-10000001*";
- r = std::from_chars(s.begin(), s.end(), c, 2);
+ r = std::from_chars(s.data(), s.data() + s.length(), c, 2);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 9 );
+ VERIFY( r.ptr == s.data() + 9 );
s = "-10000001000*";
- r = std::from_chars(s.begin(), s.end(), c, 2);
+ r = std::from_chars(s.data(), s.data() + s.length(), c, 2);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 12 );
+ VERIFY( r.ptr == s.data() + 12 );
s = "-129";
- r = std::from_chars(s.begin(), s.end(), c, 10);
+ r = std::from_chars(s.data(), s.data() + s.length(), c, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.end() );
+ VERIFY( r.ptr == s.data() + s.length() );
s = "-129*";
- r = std::from_chars(s.begin(), s.end(), c, 10);
+ r = std::from_chars(s.data(), s.data() + s.length(), c, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 4 );
+ VERIFY( r.ptr == s.data() + 4 );
s = "-100";
- r = std::from_chars(s.begin(), s.end(), c, 16);
+ r = std::from_chars(s.data(), s.data() + s.length(), c, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.end() );
+ VERIFY( r.ptr == s.data() + s.length() );
s = "-100*";
- r = std::from_chars(s.begin(), s.end(), c, 16);
+ r = std::from_chars(s.data(), s.data() + s.length(), c, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 4 );
+ VERIFY( r.ptr == s.data() + 4 );
s = "-81";
- r = std::from_chars(s.begin(), s.end(), c, 16);
+ r = std::from_chars(s.data(), s.data() + s.length(), c, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.end() );
+ VERIFY( r.ptr == s.data() + s.length() );
s = "-81*";
- r = std::from_chars(s.begin(), s.end(), c, 16);
+ r = std::from_chars(s.data(), s.data() + s.length(), c, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 3 );
+ VERIFY( r.ptr == s.data() + 3 );
s = "128";
- r = std::from_chars(s.begin(), s.end(), c, 10);
+ r = std::from_chars(s.data(), s.data() + s.length(), c, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.end() );
+ VERIFY( r.ptr == s.data() + s.length() );
s = "128*";
- r = std::from_chars(s.begin(), s.end(), c, 10);
+ r = std::from_chars(s.data(), s.data() + s.length(), c, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 3 );
+ VERIFY( r.ptr == s.data() + 3 );
s = "80";
- r = std::from_chars(s.begin(), s.end(), c, 16);
+ r = std::from_chars(s.data(), s.data() + s.length(), c, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.end() );
+ VERIFY( r.ptr == s.data() + s.length() );
s = "80*";
- r = std::from_chars(s.begin(), s.end(), c, 16);
+ r = std::from_chars(s.data(), s.data() + s.length(), c, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 2 );
+ VERIFY( r.ptr == s.data() + 2 );
VERIFY( c == -5 );
unsigned char uc = 9;
s = "100000000";
- r = std::from_chars(s.begin(), s.end(), uc, 2);
+ r = std::from_chars(s.data(), s.data() + s.length(), uc, 2);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.end() );
+ VERIFY( r.ptr == s.data() + s.length() );
s = "100000000*";
- r = std::from_chars(s.begin(), s.end(), uc, 2);
+ r = std::from_chars(s.data(), s.data() + s.length(), uc, 2);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 9 );
+ VERIFY( r.ptr == s.data() + 9 );
s = "100000000000*";
- r = std::from_chars(s.begin(), s.end(), uc, 2);
+ r = std::from_chars(s.data(), s.data() + s.length(), uc, 2);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 12 );
+ VERIFY( r.ptr == s.data() + 12 );
s = "256";
- r = std::from_chars(s.begin(), s.end(), uc, 10);
+ r = std::from_chars(s.data(), s.data() + s.length(), uc, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.end() );
+ VERIFY( r.ptr == s.data() + s.length() );
s = "256**";
- r = std::from_chars(s.begin(), s.end(), uc, 10);
+ r = std::from_chars(s.data(), s.data() + s.length(), uc, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 3 );
+ VERIFY( r.ptr == s.data() + 3 );
s = "256000**";
- r = std::from_chars(s.begin(), s.end(), uc, 10);
+ r = std::from_chars(s.data(), s.data() + s.length(), uc, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 6 );
+ VERIFY( r.ptr == s.data() + 6 );
s = "100";
- r = std::from_chars(s.begin(), s.end(), uc, 16);
+ r = std::from_chars(s.data(), s.data() + s.length(), uc, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.end() );
+ VERIFY( r.ptr == s.data() + s.length() );
s = "100**";
- r = std::from_chars(s.begin(), s.end(), uc, 16);
+ r = std::from_chars(s.data(), s.data() + s.length(), uc, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 3 );
+ VERIFY( r.ptr == s.data() + 3 );
s = "100000**";
- r = std::from_chars(s.begin(), s.end(), uc, 16);
+ r = std::from_chars(s.data(), s.data() + s.length(), uc, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 6 );
+ VERIFY( r.ptr == s.data() + 6 );
VERIFY( uc == 9 );
unsigned long long ull = 123;
s = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz****";
- r = std::from_chars(s.begin(), s.end(), ull, 36);
+ r = std::from_chars(s.data(), s.data() + s.length(), ull, 36);
VERIFY( r.ec == std::errc::result_out_of_range );
- VERIFY( r.ptr == s.begin() + 42 );
+ VERIFY( r.ptr == s.data() + 42 );
VERIFY( ull == 123 );
}
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
-// { dg-options "-std=gnu++17" }
-// { dg-do run { target c++17 } }
+// { dg-do run { target c++14 } }
// { dg-require-string-conversions "" }
#include <charconv>
-#include <string_view>
+#if __cplusplus >= 201703L
+# include <string_view>
+using std::string_view;
+#else
+// <charconv> is supported in C++14 as a GNU extension
+# include <string>
+using string_view = std::string;
+#endif
template<typename I>
bool
-check_to_chars(I val, std::string_view expected, int base = 0)
+check_to_chars(I val, string_view expected, int base = 0)
{
// Space for minus sign, 64 binary digits, final '*', and null terminator:
char buf[67] = "******************************************************************";
std::to_chars_result r = base == 0
? std::to_chars(buf, buf+sizeof(buf), val)
: std::to_chars(buf, buf+sizeof(buf), val, base);
- return r.ec == std::errc{} && *r.ptr == '*' && std::string_view(buf, r.ptr - buf) == expected;
+ return r.ec == std::errc{} && *r.ptr == '*' && string_view(buf, r.ptr - buf) == expected;
}
#include <string>
VERIFY( check_to_chars<signed long long>(123, "123") );
VERIFY( check_to_chars<unsigned long long>(123, "123") );
- if constexpr (std::is_signed_v<char>)
+ if (std::is_signed<char>::value)
VERIFY( check_to_chars<char>(-79, "-79") );
VERIFY( check_to_chars<signed char>(-79, "-79") );
VERIFY( check_to_chars<signed short>(-79, "-79") );
VERIFY( check_to_chars<signed long long>(123, "123", 10) );
VERIFY( check_to_chars<unsigned long long>(123, "123", 10) );
- if constexpr (std::is_signed_v<char>)
+ if (std::is_signed<char>::value)
VERIFY( check_to_chars<char>(-79, "-79", 10) );
VERIFY( check_to_chars<signed char>(-79, "-79", 10) );
VERIFY( check_to_chars<signed short>(-79, "-79", 10) );
VERIFY( check_to_chars<signed long long>(1, "1", base) );
VERIFY( check_to_chars<unsigned long long>(1, "1", base) );
- if constexpr (std::is_signed_v<char>)
+ if (std::is_signed<char>::value)
VERIFY( check_to_chars<char>(-1, "-1", base) );
VERIFY( check_to_chars<signed char>(-1, "-1", base) );
VERIFY( check_to_chars<signed short>(-1, "-1", base) );
VERIFY( check_to_chars<signed long long>(2, "2", base) );
VERIFY( check_to_chars<unsigned long long>(2, "2", base) );
- if constexpr (std::is_signed_v<char>)
+ if (std::is_signed<char>::value)
VERIFY( check_to_chars<char>(-2, "-2", base) );
VERIFY( check_to_chars<signed char>(-2, "-2", base) );
VERIFY( check_to_chars<signed short>(-2, "-2", base) );
VERIFY( check_to_chars<signed long long>(123, to_string(123), 8) );
VERIFY( check_to_chars<unsigned long long>(123, to_string(123), 8) );
- if constexpr (std::is_signed_v<char>)
+ if (std::is_signed<char>::value)
VERIFY( check_to_chars<char>(-79, to_string(-79), 8) );
VERIFY( check_to_chars<signed char>(-79, to_string(-79), 8) );
VERIFY( check_to_chars<signed short>(-79, to_string(-79), 8) );
VERIFY( check_to_chars<signed long long>(123, to_string(123), 16) );
VERIFY( check_to_chars<unsigned long long>(123, to_string(123), 16) );
- if constexpr (std::is_signed_v<char>)
+ if (std::is_signed<char>::value)
VERIFY( check_to_chars<char>(-79, to_string(-79), 16) );
VERIFY( check_to_chars<signed char>(-79, to_string(-79), 16) );
VERIFY( check_to_chars<signed short>(-79, to_string(-79), 16) );
VERIFY( check_to_chars<signed long long>(123, to_string(123), 2) );
VERIFY( check_to_chars<unsigned long long>(123, to_string(123), 2) );
- if constexpr (std::is_signed_v<char>)
+ if (std::is_signed<char>::value)
VERIFY( check_to_chars<char>(-79, to_string(-79), 2) );
VERIFY( check_to_chars<signed char>(-79, to_string(-79), 2) );
VERIFY( check_to_chars<signed short>(-79, to_string(-79), 2) );