Commit 804b7cc4 by Jonathan Wakely Committed by Jonathan Wakely

C++17 P0067R5 std::to_chars and std::from_chars (partial)

This adds the integral overloads of std::to_chars and std::from_chars,
including the changes made by P0682R0. Support for floating point types
is absent.

	* include/Makefile.am: Add new <charconv> header.
	* include/Makefile.in: Regenerate.
	* include/precompiled/stdc++.h: Include <charconv>.
	* include/std/charconv: New file.
	(to_chars_result, to_chars, from_chars_result, from_chars): Define.
	* testsuite/20_util/from_chars/1.cc: New test.
	* testsuite/20_util/from_chars/1_neg.cc: New test.
	* testsuite/20_util/from_chars/2.cc: New test.
	* testsuite/20_util/from_chars/requirements.cc: New test.
	* testsuite/20_util/to_chars/1.cc: New test.
	* testsuite/20_util/to_chars/1_neg.cc: New test.
	* testsuite/20_util/to_chars/2.cc: New test.
	* testsuite/20_util/to_chars/requirements.cc: New test.

From-SVN: r253353
parent d1453bec
2017-10-02 Jonathan Wakely <jwakely@redhat.com>
* include/Makefile.am: Add new <charconv> header.
* include/Makefile.in: Regenerate.
* include/precompiled/stdc++.h: Include <charconv>.
* include/std/charconv: New file.
(to_chars_result, to_chars, from_chars_result, from_chars): Define.
* testsuite/20_util/from_chars/1.cc: New test.
* testsuite/20_util/from_chars/1_neg.cc: New test.
* testsuite/20_util/from_chars/2.cc: New test.
* testsuite/20_util/from_chars/requirements.cc: New test.
* testsuite/20_util/to_chars/1.cc: New test.
* testsuite/20_util/to_chars/1_neg.cc: New test.
* testsuite/20_util/to_chars/2.cc: New test.
* testsuite/20_util/to_chars/requirements.cc: New test.
2017-09-27 François Dumont <fdumont@gcc.gnu.org>
* testsuite/22_locale/money_get/get/char/22131.cc: Make test less
......
......@@ -31,6 +31,7 @@ std_headers = \
${std_srcdir}/array \
${std_srcdir}/atomic \
${std_srcdir}/bitset \
${std_srcdir}/charconv \
${std_srcdir}/chrono \
${std_srcdir}/codecvt \
${std_srcdir}/complex \
......
......@@ -324,6 +324,7 @@ std_headers = \
${std_srcdir}/array \
${std_srcdir}/atomic \
${std_srcdir}/bitset \
${std_srcdir}/charconv \
${std_srcdir}/chrono \
${std_srcdir}/codecvt \
${std_srcdir}/complex \
......
......@@ -121,3 +121,7 @@
#if __cplusplus >= 201402L
#include <shared_mutex>
#endif
#if __cplusplus > 201402L
#include <charconv>
#endif
// Copyright (C) 2017 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// 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 } }
#include <charconv>
#include <string_view>
template<typename I>
bool
check_from_chars(I expected, std::string_view s, int base = 0, char term = '\0')
{
I val;
std::from_chars_result r = base == 0
? std::from_chars(s.begin(), s.end(), val)
: std::from_chars(s.begin(), s.end(), val, base);
return r.ec == std::errc{} && (r.ptr == s.end() || *r.ptr == term) && val == expected;
}
#include <climits>
#include <testsuite_hooks.h>
void
test01()
{
// Using base 10
VERIFY( check_from_chars(123, "123") );
VERIFY( check_from_chars(-123, "-123") );
VERIFY( check_from_chars(123, "123a", 10, 'a') );
VERIFY( check_from_chars(123, "0000000000000000000000000000123") );
VERIFY( check_from_chars(123, "0000000000000000000000000000123a", 10, 'a') );
}
void
test02()
{
// "0x" parsed as "0" not as hex prefix:
VERIFY( check_from_chars(0, "0x1", 10, 'x') );
VERIFY( check_from_chars(0, "0X1", 10, 'X') );
VERIFY( check_from_chars(0, "0x1", 16, 'x') );
VERIFY( check_from_chars(0, "0X1", 16, 'X') );
VERIFY( check_from_chars(1155, "xx", 34) );
VERIFY( check_from_chars(1155, "XX", 34) );
VERIFY( check_from_chars(1155, "Xx", 34) );
VERIFY( check_from_chars(1224, "yy", 35) );
VERIFY( check_from_chars(1224, "YY", 35) );
VERIFY( check_from_chars(1224, "yY", 35) );
VERIFY( check_from_chars(1295, "zz", 36) );
VERIFY( check_from_chars(1295, "ZZ", 36) );
VERIFY( check_from_chars(1295, "Zz", 36) );
// Parsing stops at first invalid digit for the given base:
VERIFY( check_from_chars(1, "01234", 2, '2') );
VERIFY( check_from_chars(27, "1234", 4, '4') );
VERIFY( check_from_chars(1155, "xxy", 34, 'y') );
VERIFY( check_from_chars(1224, "yyz", 35, 'z') );
}
int
main()
{
test01();
test02();
}
// Copyright (C) 2017 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++17" }
// { dg-do compile { target c++17 } }
#include <charconv>
void
test01(const char* first, const char* last)
{
#if _GLIBCXX_USE_WCHAR_T
wchar_t wc;
std::from_chars(first, last, wc); // { dg-error "no matching" }
std::from_chars(first, last, wc, 10); // { dg-error "no matching" }
#endif
char16_t c16;
std::from_chars(first, last, c16); // { dg-error "no matching" }
std::from_chars(first, last, c16, 10); // { dg-error "no matching" }
char32_t c32;
std::from_chars(first, last, c32); // { dg-error "no matching" }
std::from_chars(first, last, c32, 10); // { dg-error "no matching" }
}
// Copyright (C) 2017 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// 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 } }
#include <charconv>
#include <string_view>
#include <testsuite_hooks.h>
// Test std::from_chars error handling.
void
test01()
{
std::from_chars_result r;
int i = 999;
std::string_view s;
s = "";
r = std::from_chars(s.begin(), s.end(), i);
VERIFY( r.ec == std::errc::invalid_argument );
VERIFY( r.ptr == s.begin() );
VERIFY( i == 999 );
s = "*";
r = std::from_chars(s.begin(), s.end(), i);
VERIFY( r.ec == std::errc::invalid_argument );
VERIFY( r.ptr == s.begin() );
VERIFY( i == 999 );
s = "-";
r = std::from_chars(s.begin(), s.end(), i);
VERIFY( r.ec == std::errc::invalid_argument );
VERIFY( r.ptr == s.begin() );
VERIFY( i == 999 );
s = "-*";
r = std::from_chars(s.begin(), s.end(), i);
VERIFY( r.ec == std::errc::invalid_argument );
VERIFY( r.ptr == s.begin() );
VERIFY( i == 999 );
unsigned u = 888;
s = "-1";
r = std::from_chars(s.begin(), s.end(), u);
VERIFY( r.ec == std::errc::invalid_argument );
VERIFY( r.ptr == s.begin() );
s = "-a";
r = std::from_chars(s.begin(), s.end(), u);
VERIFY( r.ec == std::errc::invalid_argument );
VERIFY( r.ptr == s.begin() );
s = "-";
r = std::from_chars(s.begin(), s.end(), u);
VERIFY( r.ec == std::errc::invalid_argument );
VERIFY( r.ptr == s.begin() );
VERIFY( u == 888 );
for (int base = 2; base <= 36; ++base)
{
const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz*";
const char buf[2] = { '-', digits[base] };
r = std::from_chars(buf, buf + 1, i, base);
VERIFY( r.ec == std::errc::invalid_argument );
VERIFY( r.ptr == buf );
VERIFY( i == 999 );
r = std::from_chars(buf + 1, buf + 2, i, base);
VERIFY( r.ec == std::errc::invalid_argument );
VERIFY( r.ptr == buf + 1 );
VERIFY( i == 999 );
r = std::from_chars(buf, buf + 2, i, base);
VERIFY( r.ec == std::errc::invalid_argument );
VERIFY( r.ptr == buf );
VERIFY( i == 999 );
}
}
void
test02()
{
std::from_chars_result r;
std::string_view s;
signed char c = -5;
s = "-10000001";
r = std::from_chars(s.begin(), s.end(), c, 2);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.end() );
s = "-10000001*";
r = std::from_chars(s.begin(), s.end(), c, 2);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 9 );
s = "-10000001000*";
r = std::from_chars(s.begin(), s.end(), c, 2);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 12 );
s = "-129";
r = std::from_chars(s.begin(), s.end(), c, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.end() );
s = "-129*";
r = std::from_chars(s.begin(), s.end(), c, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 4 );
s = "-100";
r = std::from_chars(s.begin(), s.end(), c, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.end() );
s = "-100*";
r = std::from_chars(s.begin(), s.end(), c, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 4 );
s = "-81";
r = std::from_chars(s.begin(), s.end(), c, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.end() );
s = "-81*";
r = std::from_chars(s.begin(), s.end(), c, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 3 );
s = "128";
r = std::from_chars(s.begin(), s.end(), c, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.end() );
s = "128*";
r = std::from_chars(s.begin(), s.end(), c, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 3 );
s = "80";
r = std::from_chars(s.begin(), s.end(), c, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.end() );
s = "80*";
r = std::from_chars(s.begin(), s.end(), c, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 2 );
VERIFY( c == -5 );
unsigned char uc = 9;
s = "100000000";
r = std::from_chars(s.begin(), s.end(), uc, 2);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.end() );
s = "100000000*";
r = std::from_chars(s.begin(), s.end(), uc, 2);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 9 );
s = "100000000000*";
r = std::from_chars(s.begin(), s.end(), uc, 2);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 12 );
s = "256";
r = std::from_chars(s.begin(), s.end(), uc, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.end() );
s = "256**";
r = std::from_chars(s.begin(), s.end(), uc, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 3 );
s = "256000**";
r = std::from_chars(s.begin(), s.end(), uc, 10);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 6 );
s = "100";
r = std::from_chars(s.begin(), s.end(), uc, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.end() );
s = "100**";
r = std::from_chars(s.begin(), s.end(), uc, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 3 );
s = "100000**";
r = std::from_chars(s.begin(), s.end(), uc, 16);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 6 );
VERIFY( uc == 9 );
unsigned long long ull = 123;
s = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz****";
r = std::from_chars(s.begin(), s.end(), ull, 36);
VERIFY( r.ec == std::errc::result_out_of_range );
VERIFY( r.ptr == s.begin() + 42 );
VERIFY( ull == 123 );
}
int
main()
{
test01();
test02();
}
// Copyright (C) 2017 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++17" }
// { dg-do compile { target c++17 } }
#include <charconv>
namespace std
{
struct from_chars_result;
const char* from_chars_result::*pm2 = &from_chars_result::ptr;
errc from_chars_result::*pm1 = &from_chars_result::ec;
from_chars_result (*f1)(const char*, const char*, char&, int)
= &from_chars;
from_chars_result (*f2)(const char*, const char*, signed char&, int)
= &from_chars;
from_chars_result (*f3)(const char*, const char*, unsigned char&, int)
= &from_chars;
from_chars_result (*f4)(const char*, const char*, signed short&, int)
= &from_chars;
from_chars_result (*f5)(const char*, const char*, unsigned short&, int)
= &from_chars;
from_chars_result (*f6)(const char*, const char*, signed int&, int)
= &from_chars;
from_chars_result (*f7)(const char*, const char*, unsigned int&, int)
= &from_chars;
from_chars_result (*f8)(const char*, const char*, signed long&, int)
= &from_chars;
from_chars_result (*f9)(const char*, const char*, unsigned long&, int)
= &from_chars;
from_chars_result (*f10)(const char*, const char*, signed long long&, int)
= &from_chars;
from_chars_result (*f11)(const char*, const char*, unsigned long long&, int)
= &from_chars;
}
void bind()
{
const char buf[1] = "";
int i;
auto [p, e] = std::from_chars(buf, buf + 1, i, 10);
const char** pa = &p;
std::errc* ea = &e;
}
// Copyright (C) 2017 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++17" }
// { dg-do compile { target c++17 } }
#include <charconv>
void
test01(char* first, char* last)
{
#if _GLIBCXX_USE_WCHAR_T
std::to_chars(first, last, L'\x1'); // { dg-error "no matching" }
std::to_chars(first, last, L'\x1', 10); // { dg-error "no matching" }
#endif
std::to_chars(first, last, u'\x1'); // { dg-error "no matching" }
std::to_chars(first, last, u'\x1', 10); // { dg-error "no matching" }
std::to_chars(first, last, U'\x1'); // { dg-error "no matching" }
std::to_chars(first, last, U'\x1', 10); // { dg-error "no matching" }
}
// Copyright (C) 2017 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// 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 } }
#include <charconv>
#include <testsuite_hooks.h>
// Test std::to_chars error handling.
void
test01()
{
char buf[9] = "********";
std::to_chars_result r;
r = std::to_chars(buf, buf, 1);
VERIFY( r.ec == std::errc::value_too_large );
VERIFY( r.ptr == buf );
VERIFY( *r.ptr == '*' );
r = std::to_chars(buf, buf + 3, 0b1000, 2);
VERIFY( r.ec == std::errc::value_too_large );
VERIFY( r.ptr == buf + 3 );
VERIFY( *r.ptr == '*' );
r = std::to_chars(buf, buf + 4, 0b1000, 2);
VERIFY( r.ec == std::errc{} );
VERIFY( r.ptr == buf + 4 );
VERIFY( *r.ptr == '*' );
r = std::to_chars(buf, buf + 4, 010000, 8);
VERIFY( r.ec == std::errc::value_too_large );
VERIFY( r.ptr == buf + 4 );
VERIFY( *r.ptr == '*' );
r = std::to_chars(buf, buf + 5, 010000, 8);
VERIFY( r.ec == std::errc{} );
VERIFY( r.ptr == buf + 5 );
VERIFY( *r.ptr == '*' );
r = std::to_chars(buf, buf + 5, 100000, 10);
VERIFY( r.ec == std::errc::value_too_large );
VERIFY( r.ptr == buf + 5 );
VERIFY( *r.ptr == '*' );
r = std::to_chars(buf, buf + 6, 100000, 10);
VERIFY( r.ec == std::errc{} );
VERIFY( r.ptr == buf + 6 );
VERIFY( *r.ptr == '*' );
r = std::to_chars(buf, buf + 6, 0x1000000, 16);
VERIFY( r.ec == std::errc::value_too_large );
VERIFY( r.ptr == buf + 6 );
VERIFY( *r.ptr == '*' );
r = std::to_chars(buf, buf + 7, 0x1000000, 16);
VERIFY( r.ec == std::errc{} );
VERIFY( r.ptr == buf + 7 );
VERIFY( *r.ptr == '*' );
}
int
main()
{
test01();
}
// Copyright (C) 2017 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++17" }
// { dg-do compile { target c++17 } }
#include <charconv>
namespace std
{
struct to_chars_result;
char* to_chars_result::*pm2 = &to_chars_result::ptr;
errc to_chars_result::*pm1 = &to_chars_result::ec;
to_chars_result (*f1)(char*, char*, char, int) = &to_chars;
to_chars_result (*f2)(char*, char*, signed char, int) = &to_chars;
to_chars_result (*f3)(char*, char*, unsigned char, int) = &to_chars;
to_chars_result (*f4)(char*, char*, signed short, int) = &to_chars;
to_chars_result (*f5)(char*, char*, unsigned short, int) = &to_chars;
to_chars_result (*f6)(char*, char*, signed int, int) = &to_chars;
to_chars_result (*f7)(char*, char*, unsigned int, int) = &to_chars;
to_chars_result (*f8)(char*, char*, signed long, int) = &to_chars;
to_chars_result (*f9)(char*, char*, unsigned long, int) = &to_chars;
to_chars_result (*f10)(char*, char*, signed long long, int) = &to_chars;
to_chars_result (*f11)(char*, char*, unsigned long long, int) = &to_chars;
}
void bind()
{
char buf[1];
auto [p, e] = std::to_chars(buf, buf + 1, 1, 10);
char** pa = &p;
std::errc* ea = &e;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment