[string.conversions]

llvm-svn: 105336
This commit is contained in:
Howard Hinnant 2010-06-02 18:20:39 +00:00
parent 94801a47f8
commit cbbf633edb
16 changed files with 2037 additions and 76 deletions

View File

@ -95,12 +95,14 @@ public:
explicit basic_string(const allocator_type& a = allocator_type());
basic_string(const basic_string& str);
basic_string(basic_string&& str);
basic_string(const basic_string& str, size_type pos, size_type n = npos, const allocator_type& a = allocator_type());
basic_string(const basic_string& str, size_type pos, size_type n = npos,
const allocator_type& a = allocator_type());
basic_string(const_pointer s, const allocator_type& a = allocator_type());
basic_string(const_pointer s, size_type n, const allocator_type& a = allocator_type());
basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
template<class InputIterator>
basic_string(InputIterator begin, InputIterator end, const allocator_type& a = allocator_type());
basic_string(InputIterator begin, InputIterator end,
const allocator_type& a = allocator_type());
basic_string(initializer_list<value_type>, const Allocator& = Allocator());
basic_string(const basic_string&, const Allocator&);
basic_string(basic_string&&, const Allocator&);
@ -156,7 +158,8 @@ public:
basic_string& append(const_pointer s, size_type n);
basic_string& append(const_pointer s);
basic_string& append(size_type n, value_type c);
template<class InputIterator> basic_string& append(InputIterator first, InputIterator last);
template<class InputIterator>
basic_string& append(InputIterator first, InputIterator last);
basic_string& append(initializer_list<value_type>);
void push_back(value_type c);
@ -171,17 +174,20 @@ public:
basic_string& assign(const_pointer s, size_type n);
basic_string& assign(const_pointer s);
basic_string& assign(size_type n, value_type c);
template<class InputIterator> basic_string& assign(InputIterator first, InputIterator last);
template<class InputIterator>
basic_string& assign(InputIterator first, InputIterator last);
basic_string& assign(initializer_list<value_type>);
basic_string& insert(size_type pos1, const basic_string& str);
basic_string& insert(size_type pos1, const basic_string& str, size_type pos2, size_type n);
basic_string& insert(size_type pos1, const basic_string& str,
size_type pos2, size_type n);
basic_string& insert(size_type pos, const_pointer s, size_type n);
basic_string& insert(size_type pos, const_pointer s);
basic_string& insert(size_type pos, size_type n, value_type c);
iterator insert(const_iterator p, value_type c);
iterator insert(const_iterator p, size_type n, value_type c);
template<class InputIterator> iterator insert(const_iterator p, InputIterator first, InputIterator last);
template<class InputIterator>
iterator insert(const_iterator p, InputIterator first, InputIterator last);
iterator insert(const_iterator p, initializer_list<value_type>);
basic_string& erase(size_type pos = 0, size_type n = npos);
@ -189,7 +195,8 @@ public:
iterator erase(const_iterator first, const_iterator last);
basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
basic_string& replace(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2);
basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
size_type pos2, size_type n2);
basic_string& replace(size_type pos, size_type n1, const_pointer s, size_type n2);
basic_string& replace(size_type pos, size_type n1, const_pointer s);
basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
@ -197,7 +204,8 @@ public:
basic_string& replace(iterator i1, iterator i2, const_pointer s, size_type n);
basic_string& replace(iterator i1, iterator i2, const_pointer s);
basic_string& replace(iterator i1, iterator i2, size_type n, value_type c);
template<class InputIterator> basic_string& replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2);
template<class InputIterator>
basic_string& replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2);
basic_string& replace(iterator i1, iterator i2, initializer_list<value_type>);
size_type copy(pointer s, size_type n, size_type pos = 0) const;
@ -242,7 +250,8 @@ public:
int compare(const basic_string& str) const;
int compare(size_type pos1, size_type n1, const basic_string& str) const;
int compare(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2) const;
int compare(size_type pos1, size_type n1, const basic_string& str,
size_type pos2, size_type n2) const;
int compare(const_pointer s) const;
int compare(size_type pos1, size_type n1, const_pointer s) const;
int compare(size_type pos1, size_type n1, const_pointer s, size_type n2) const;
@ -252,7 +261,8 @@ public:
template<class charT, class traits, class Allocator>
basic_string<charT, traits, Allocator>
operator+(const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs);
operator+(const basic_string<charT, traits, Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
basic_string<charT, traits, Allocator>
@ -271,7 +281,8 @@ basic_string<charT, traits, Allocator>
operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs);
bool operator==(const basic_string<charT, traits, Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);
@ -280,7 +291,8 @@ template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs);
template<class charT, class traits, class Allocator>
bool operator!=(const basic_string<charT,traits,Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs);
bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);
@ -289,7 +301,8 @@ template<class charT, class traits, class Allocator>
bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
template<class charT, class traits, class Allocator>
bool operator< (const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs);
bool operator< (const basic_string<charT, traits, Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
@ -298,7 +311,8 @@ template<class charT, class traits, class Allocator>
bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator> (const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs);
bool operator> (const basic_string<charT, traits, Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
@ -307,7 +321,8 @@ template<class charT, class traits, class Allocator>
bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs);
bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
@ -316,7 +331,8 @@ template<class charT, class traits, class Allocator>
bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs);
bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
@ -325,7 +341,8 @@ template<class charT, class traits, class Allocator>
bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
void swap(basic_string<charT, traits, Allocator>& lhs, basic_string<charT, traits, Allocator>& rhs);
void swap(basic_string<charT, traits, Allocator>& lhs,
basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
basic_istream<charT, traits>&
@ -337,7 +354,8 @@ operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, A
template<class charT, class traits, class Allocator>
basic_istream<charT, traits>&
getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str, charT delim);
getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
charT delim);
template<class charT, class traits, class Allocator>
basic_istream<charT, traits>&
@ -345,6 +363,48 @@ getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
typedef basic_string<char16_t> u16string;
typedef basic_string<char32_t> u32string;
int stoi (const string& str, size_t* idx = 0, int base = 10);
long stol (const string& str, size_t* idx = 0, int base = 10);
unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
long long stoll (const string& str, size_t* idx = 0, int base = 10);
unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
float stof (const string& str, size_t* idx = 0);
double stod (const string& str, size_t* idx = 0);
long double stold(const string& str, size_t* idx = 0);
string to_string(int val);
string to_string(unsigned val);
string to_string(long val);
string to_string(unsigned long val);
string to_string(long long val);
string to_string(unsigned long long val);
string to_string(float val);
string to_string(double val);
string to_string(long double val);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);
long stol (const wstring& str, size_t* idx = 0, int base = 10);
unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);
float stof (const wstring& str, size_t* idx = 0);
double stod (const wstring& str, size_t* idx = 0);
long double stold(const wstring& str, size_t* idx = 0);
wstring to_wstring(int val);
wstring to_wstring(unsigned val);
wstring to_wstring(long val);
wstring to_wstring(unsigned long val);
wstring to_wstring(long long val);
wstring to_wstring(unsigned long long val);
wstring to_wstring(float val);
wstring to_wstring(double val);
wstring to_wstring(long double val);
template <> struct hash<string>;
template <> struct hash<u16string>;
@ -3449,6 +3509,46 @@ typedef basic_string<char32_t> u32string;
#endif
int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
long stol (const string& __str, size_t* __idx = 0, int __base = 10);
unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
float stof (const string& __str, size_t* __idx = 0);
double stod (const string& __str, size_t* __idx = 0);
long double stold(const string& __str, size_t* __idx = 0);
string to_string(int __val);
string to_string(unsigned __val);
string to_string(long __val);
string to_string(unsigned long __val);
string to_string(long long __val);
string to_string(unsigned long long __val);
string to_string(float __val);
string to_string(double __val);
string to_string(long double __val);
int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
float stof (const wstring& __str, size_t* __idx = 0);
double stod (const wstring& __str, size_t* __idx = 0);
long double stold(const wstring& __str, size_t* __idx = 0);
wstring to_wstring(int __val);
wstring to_wstring(unsigned __val);
wstring to_wstring(long __val);
wstring to_wstring(unsigned long __val);
wstring to_wstring(long long __val);
wstring to_wstring(unsigned long long __val);
wstring to_wstring(float __val);
wstring to_wstring(double __val);
wstring to_wstring(long double __val);
template<class _CharT, class _Traits, class _Allocator>
const typename basic_string<_CharT, _Traits, _Allocator>::size_type
basic_string<_CharT, _Traits, _Allocator>::npos;

View File

@ -265,10 +265,10 @@ inline
thread&
thread::operator=(thread&& __t)
{
if (__t_ != nullptr)
if (__t_ != 0)
terminate();
__t_ = __t.__t_;
__t.__t_ = nullptr;
__t.__t_ = 0;
return *this;
}

View File

@ -12,7 +12,7 @@ then
exit 1
fi
if [ -z $CXX ]
if [ -z "$CXX" ]
then
CXX=g++
fi

View File

@ -20,61 +20,6 @@
_LIBCPP_BEGIN_NAMESPACE_STD
template class __basic_string_common<true>;
template class basic_string<char>;
template class basic_string<wchar_t>;
template enable_if<__is_forward_iterator<char const*>::value, void>::type
basic_string<char, char_traits<char>, allocator<char> >
::__init<char const*>(char const*, char const*);
template enable_if<__is_forward_iterator<wchar_t const*>::value, void>::type
basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >
::__init<wchar_t const*>(wchar_t const*, wchar_t const*);
template
enable_if<__is_forward_iterator<char*>::value,
basic_string<char, char_traits<char>, allocator<char> >&>::type
basic_string<char, char_traits<char>, allocator<char> >::
append<char*>(char*, char*);
template
enable_if<__is_forward_iterator<wchar_t*>::value,
basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >&>::type
basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >::
append<wchar_t*>(wchar_t*, wchar_t*);
template
enable_if<__is_forward_iterator<char const*>::value,
string::iterator>::type
string::
insert<char const*>(string::const_iterator, char const*, char const*);
template
enable_if<__is_forward_iterator<wchar_t const*>::value,
wstring::iterator>::type
wstring::
insert<wchar_t const*>(wstring::const_iterator, wchar_t const*, wchar_t const*);
template
enable_if<__is_input_iterator<char const*>::value, string&>::type
string::
replace<char const*>(string::iterator, string::iterator, char const*, char const*);
template
enable_if<__is_input_iterator<wchar_t const*>::value, wstring&>::type
wstring::
replace<wchar_t const*>(wstring::iterator, wstring::iterator, wchar_t const*, wchar_t const*);
template
enable_if<__is_forward_iterator<wchar_t*>::value, wstring&>::type
wstring::assign<wchar_t*>(wchar_t*, wchar_t*);
template
string
operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
template class basic_ios<char>;
template class basic_ios<wchar_t>;

690
libcxx/src/string.cpp Normal file
View File

@ -0,0 +1,690 @@
//===------------------------- string.cpp ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "string"
#include "cstdlib"
#include "cwchar"
#include "cerrno"
_LIBCPP_BEGIN_NAMESPACE_STD
template class __basic_string_common<true>;
template class basic_string<char>;
template class basic_string<wchar_t>;
template enable_if<__is_forward_iterator<char const*>::value, void>::type
basic_string<char, char_traits<char>, allocator<char> >
::__init<char const*>(char const*, char const*);
template enable_if<__is_forward_iterator<wchar_t const*>::value, void>::type
basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >
::__init<wchar_t const*>(wchar_t const*, wchar_t const*);
template
enable_if<__is_forward_iterator<char*>::value,
basic_string<char, char_traits<char>, allocator<char> >&>::type
basic_string<char, char_traits<char>, allocator<char> >::
append<char*>(char*, char*);
template
enable_if<__is_forward_iterator<wchar_t*>::value,
basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >&>::type
basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >::
append<wchar_t*>(wchar_t*, wchar_t*);
template
enable_if<__is_forward_iterator<char const*>::value,
string::iterator>::type
string::
insert<char const*>(string::const_iterator, char const*, char const*);
template
enable_if<__is_forward_iterator<wchar_t const*>::value,
wstring::iterator>::type
wstring::
insert<wchar_t const*>(wstring::const_iterator, wchar_t const*, wchar_t const*);
template
enable_if<__is_input_iterator<char const*>::value, string&>::type
string::
replace<char const*>(string::iterator, string::iterator, char const*, char const*);
template
enable_if<__is_input_iterator<wchar_t const*>::value, wstring&>::type
wstring::
replace<wchar_t const*>(wstring::iterator, wstring::iterator, wchar_t const*, wchar_t const*);
template
enable_if<__is_forward_iterator<wchar_t*>::value, wstring&>::type
wstring::assign<wchar_t*>(wchar_t*, wchar_t*);
template
string
operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
int
stoi(const string& str, size_t* idx, int base)
{
char* ptr;
const char* const p = str.c_str();
long r = strtol(p, &ptr, base);
if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
ptr = const_cast<char*>(p);
if (ptr == p)
{
if (r == 0)
throw invalid_argument("stoi: no conversion");
throw out_of_range("stoi: out of range");
}
if (idx)
*idx = static_cast<size_t>(ptr - p);
return static_cast<int>(r);
}
int
stoi(const wstring& str, size_t* idx, int base)
{
wchar_t* ptr;
const wchar_t* const p = str.c_str();
long r = wcstol(p, &ptr, base);
if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
ptr = const_cast<wchar_t*>(p);
if (ptr == p)
{
if (r == 0)
throw invalid_argument("stoi: no conversion");
throw out_of_range("stoi: out of range");
}
if (idx)
*idx = static_cast<size_t>(ptr - p);
return static_cast<int>(r);
}
long
stol(const string& str, size_t* idx, int base)
{
char* ptr;
const char* const p = str.c_str();
long r = strtol(p, &ptr, base);
if (ptr == p)
{
if (r == 0)
throw invalid_argument("stol: no conversion");
throw out_of_range("stol: out of range");
}
if (idx)
*idx = static_cast<size_t>(ptr - p);
return r;
}
long
stol(const wstring& str, size_t* idx, int base)
{
wchar_t* ptr;
const wchar_t* const p = str.c_str();
long r = wcstol(p, &ptr, base);
if (ptr == p)
{
if (r == 0)
throw invalid_argument("stol: no conversion");
throw out_of_range("stol: out of range");
}
if (idx)
*idx = static_cast<size_t>(ptr - p);
return r;
}
unsigned long
stoul(const string& str, size_t* idx, int base)
{
char* ptr;
const char* const p = str.c_str();
unsigned long r = strtoul(p, &ptr, base);
if (ptr == p)
{
if (r == 0)
throw invalid_argument("stoul: no conversion");
throw out_of_range("stoul: out of range");
}
if (idx)
*idx = static_cast<size_t>(ptr - p);
return r;
}
unsigned long
stoul(const wstring& str, size_t* idx, int base)
{
wchar_t* ptr;
const wchar_t* const p = str.c_str();
unsigned long r = wcstoul(p, &ptr, base);
if (ptr == p)
{
if (r == 0)
throw invalid_argument("stoul: no conversion");
throw out_of_range("stoul: out of range");
}
if (idx)
*idx = static_cast<size_t>(ptr - p);
return r;
}
long long
stoll(const string& str, size_t* idx, int base)
{
char* ptr;
const char* const p = str.c_str();
long long r = strtoll(p, &ptr, base);
if (ptr == p)
{
if (r == 0)
throw invalid_argument("stoll: no conversion");
throw out_of_range("stoll: out of range");
}
if (idx)
*idx = static_cast<size_t>(ptr - p);
return r;
}
long long
stoll(const wstring& str, size_t* idx, int base)
{
wchar_t* ptr;
const wchar_t* const p = str.c_str();
long long r = wcstoll(p, &ptr, base);
if (ptr == p)
{
if (r == 0)
throw invalid_argument("stoll: no conversion");
throw out_of_range("stoll: out of range");
}
if (idx)
*idx = static_cast<size_t>(ptr - p);
return r;
}
unsigned long long
stoull(const string& str, size_t* idx, int base)
{
char* ptr;
const char* const p = str.c_str();
unsigned long long r = strtoull(p, &ptr, base);
if (ptr == p)
{
if (r == 0)
throw invalid_argument("stoull: no conversion");
throw out_of_range("stoull: out of range");
}
if (idx)
*idx = static_cast<size_t>(ptr - p);
return r;
}
unsigned long long
stoull(const wstring& str, size_t* idx, int base)
{
wchar_t* ptr;
const wchar_t* const p = str.c_str();
unsigned long long r = wcstoull(p, &ptr, base);
if (ptr == p)
{
if (r == 0)
throw invalid_argument("stoull: no conversion");
throw out_of_range("stoull: out of range");
}
if (idx)
*idx = static_cast<size_t>(ptr - p);
return r;
}
float
stof(const string& str, size_t* idx)
{
char* ptr;
const char* const p = str.c_str();
int errno_save = errno;
errno = 0;
double r = strtod(p, &ptr);
swap(errno, errno_save);
if (errno_save == ERANGE)
throw out_of_range("stof: out of range");
if (ptr == p)
throw invalid_argument("stof: no conversion");
if (idx)
*idx = static_cast<size_t>(ptr - p);
return static_cast<float>(r);
}
float
stof(const wstring& str, size_t* idx)
{
wchar_t* ptr;
const wchar_t* const p = str.c_str();
int errno_save = errno;
errno = 0;
double r = wcstod(p, &ptr);
swap(errno, errno_save);
if (errno_save == ERANGE)
throw out_of_range("stof: out of range");
if (ptr == p)
throw invalid_argument("stof: no conversion");
if (idx)
*idx = static_cast<size_t>(ptr - p);
return static_cast<float>(r);
}
double
stod(const string& str, size_t* idx)
{
char* ptr;
const char* const p = str.c_str();
int errno_save = errno;
errno = 0;
double r = strtod(p, &ptr);
swap(errno, errno_save);
if (errno_save == ERANGE)
throw out_of_range("stod: out of range");
if (ptr == p)
throw invalid_argument("stod: no conversion");
if (idx)
*idx = static_cast<size_t>(ptr - p);
return r;
}
double
stod(const wstring& str, size_t* idx)
{
wchar_t* ptr;
const wchar_t* const p = str.c_str();
int errno_save = errno;
errno = 0;
double r = wcstod(p, &ptr);
swap(errno, errno_save);
if (errno_save == ERANGE)
throw out_of_range("stod: out of range");
if (ptr == p)
throw invalid_argument("stod: no conversion");
if (idx)
*idx = static_cast<size_t>(ptr - p);
return r;
}
long double
stold(const string& str, size_t* idx)
{
char* ptr;
const char* const p = str.c_str();
int errno_save = errno;
errno = 0;
long double r = strtold(p, &ptr);
swap(errno, errno_save);
if (errno_save == ERANGE)
throw out_of_range("stold: out of range");
if (ptr == p)
throw invalid_argument("stold: no conversion");
if (idx)
*idx = static_cast<size_t>(ptr - p);
return r;
}
long double
stold(const wstring& str, size_t* idx)
{
wchar_t* ptr;
const wchar_t* const p = str.c_str();
int errno_save = errno;
errno = 0;
long double r = wcstold(p, &ptr);
swap(errno, errno_save);
if (errno_save == ERANGE)
throw out_of_range("stold: out of range");
if (ptr == p)
throw invalid_argument("stold: no conversion");
if (idx)
*idx = static_cast<size_t>(ptr - p);
return r;
}
string to_string(int val)
{
string s;
s.resize(s.capacity());
while (true)
{
int n2 = snprintf(&s[0], s.size()+1, "%d", val);
if (n2 <= s.size())
{
s.resize(n2);
break;
}
s.resize(n2);
}
return s;
}
string to_string(unsigned val)
{
string s;
s.resize(s.capacity());
while (true)
{
int n2 = snprintf(&s[0], s.size()+1, "%u", val);
if (n2 <= s.size())
{
s.resize(n2);
break;
}
s.resize(n2);
}
return s;
}
string to_string(long val)
{
string s;
s.resize(s.capacity());
while (true)
{
int n2 = snprintf(&s[0], s.size()+1, "%ld", val);
if (n2 <= s.size())
{
s.resize(n2);
break;
}
s.resize(n2);
}
return s;
}
string to_string(unsigned long val)
{
string s;
s.resize(s.capacity());
while (true)
{
int n2 = snprintf(&s[0], s.size()+1, "%lu", val);
if (n2 <= s.size())
{
s.resize(n2);
break;
}
s.resize(n2);
}
return s;
}
string to_string(long long val)
{
string s;
s.resize(s.capacity());
while (true)
{
int n2 = snprintf(&s[0], s.size()+1, "%lld", val);
if (n2 <= s.size())
{
s.resize(n2);
break;
}
s.resize(n2);
}
return s;
}
string to_string(unsigned long long val)
{
string s;
s.resize(s.capacity());
while (true)
{
int n2 = snprintf(&s[0], s.size()+1, "%llu", val);
if (n2 <= s.size())
{
s.resize(n2);
break;
}
s.resize(n2);
}
return s;
}
string to_string(float val)
{
string s;
s.resize(s.capacity());
while (true)
{
int n2 = snprintf(&s[0], s.size()+1, "%f", val);
if (n2 <= s.size())
{
s.resize(n2);
break;
}
s.resize(n2);
}
return s;
}
string to_string(double val)
{
string s;
s.resize(s.capacity());
while (true)
{
int n2 = snprintf(&s[0], s.size()+1, "%f", val);
if (n2 <= s.size())
{
s.resize(n2);
break;
}
s.resize(n2);
}
return s;
}
string to_string(long double val)
{
string s;
s.resize(s.capacity());
while (true)
{
int n2 = snprintf(&s[0], s.size()+1, "%Lf", val);
if (n2 <= s.size())
{
s.resize(n2);
break;
}
s.resize(n2);
}
return s;
}
wstring to_wstring(int val)
{
const size_t n = (numeric_limits<int>::digits / 3)
+ ((numeric_limits<int>::digits % 3) != 0)
+ 1;
wstring s(n, wchar_t());
s.resize(s.capacity());
while (true)
{
int n2 = swprintf(&s[0], s.size()+1, L"%d", val);
if (n2 > 0)
{
s.resize(n2);
break;
}
s.resize(2*s.size());
s.resize(s.capacity());
}
return s;
}
wstring to_wstring(unsigned val)
{
const size_t n = (numeric_limits<unsigned>::digits / 3)
+ ((numeric_limits<unsigned>::digits % 3) != 0)
+ 1;
wstring s(n, wchar_t());
s.resize(s.capacity());
while (true)
{
int n2 = swprintf(&s[0], s.size()+1, L"%u", val);
if (n2 > 0)
{
s.resize(n2);
break;
}
s.resize(2*s.size());
s.resize(s.capacity());
}
return s;
}
wstring to_wstring(long val)
{
const size_t n = (numeric_limits<long>::digits / 3)
+ ((numeric_limits<long>::digits % 3) != 0)
+ 1;
wstring s(n, wchar_t());
s.resize(s.capacity());
while (true)
{
int n2 = swprintf(&s[0], s.size()+1, L"%ld", val);
if (n2 > 0)
{
s.resize(n2);
break;
}
s.resize(2*s.size());
s.resize(s.capacity());
}
return s;
}
wstring to_wstring(unsigned long val)
{
const size_t n = (numeric_limits<unsigned long>::digits / 3)
+ ((numeric_limits<unsigned long>::digits % 3) != 0)
+ 1;
wstring s(n, wchar_t());
s.resize(s.capacity());
while (true)
{
int n2 = swprintf(&s[0], s.size()+1, L"%lu", val);
if (n2 > 0)
{
s.resize(n2);
break;
}
s.resize(2*s.size());
s.resize(s.capacity());
}
return s;
}
wstring to_wstring(long long val)
{
const size_t n = (numeric_limits<long long>::digits / 3)
+ ((numeric_limits<long long>::digits % 3) != 0)
+ 1;
wstring s(n, wchar_t());
s.resize(s.capacity());
while (true)
{
int n2 = swprintf(&s[0], s.size()+1, L"%lld", val);
if (n2 > 0)
{
s.resize(n2);
break;
}
s.resize(2*s.size());
s.resize(s.capacity());
}
return s;
}
wstring to_wstring(unsigned long long val)
{
const size_t n = (numeric_limits<unsigned long long>::digits / 3)
+ ((numeric_limits<unsigned long long>::digits % 3) != 0)
+ 1;
wstring s(n, wchar_t());
s.resize(s.capacity());
while (true)
{
int n2 = swprintf(&s[0], s.size()+1, L"%llu", val);
if (n2 > 0)
{
s.resize(n2);
break;
}
s.resize(2*s.size());
s.resize(s.capacity());
}
return s;
}
wstring to_wstring(float val)
{
const size_t n = 20;
wstring s(n, wchar_t());
s.resize(s.capacity());
while (true)
{
int n2 = swprintf(&s[0], s.size()+1, L"%f", val);
if (n2 > 0)
{
s.resize(n2);
break;
}
s.resize(2*s.size());
s.resize(s.capacity());
}
return s;
}
wstring to_wstring(double val)
{
const size_t n = 20;
wstring s(n, wchar_t());
s.resize(s.capacity());
while (true)
{
int n2 = swprintf(&s[0], s.size()+1, L"%f", val);
if (n2 > 0)
{
s.resize(n2);
break;
}
s.resize(2*s.size());
s.resize(s.capacity());
}
return s;
}
wstring to_wstring(long double val)
{
const size_t n = 20;
wstring s(n, wchar_t());
s.resize(s.capacity());
while (true)
{
int n2 = swprintf(&s[0], s.size()+1, L"%Lf", val);
if (n2 > 0)
{
s.resize(n2);
break;
}
s.resize(2*s.size());
s.resize(s.capacity());
}
return s;
}
_LIBCPP_END_NAMESPACE_STD

View File

@ -0,0 +1,166 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <string>
// double stod(const string& str, size_t *idx = 0);
// double stod(const wstring& str, size_t *idx = 0);
#include <string>
#include <cmath>
#include <cassert>
int main()
{
assert(std::stod("0") == 0);
assert(std::stod(L"0") == 0);
assert(std::stod("-0") == 0);
assert(std::stod(L"-0") == 0);
assert(std::stod("-10") == -10);
assert(std::stod(L"-10.5") == -10.5);
assert(std::stod(" 10") == 10);
assert(std::stod(L" 10") == 10);
size_t idx = 0;
assert(std::stod("10g", &idx) == 10);
assert(idx == 2);
idx = 0;
assert(std::stod(L"10g", &idx) == 10);
assert(idx == 2);
try
{
assert(std::stod("1.e60", &idx) == 1.e60);
assert(idx == 5);
}
catch (const std::out_of_range&)
{
assert(false);
}
try
{
assert(std::stod(L"1.e60", &idx) == 1.e60);
assert(idx == 5);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
assert(std::stod("1.e360", &idx) == INFINITY);
assert(false);
}
catch (const std::out_of_range&)
{
assert(idx == 0);
}
try
{
assert(std::stod(L"1.e360", &idx) == INFINITY);
assert(false);
}
catch (const std::out_of_range&)
{
assert(idx == 0);
}
try
{
assert(std::stod("INF", &idx) == INFINITY);
assert(idx == 3);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
assert(std::stod(L"INF", &idx) == INFINITY);
assert(idx == 3);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
assert(std::isnan(std::stod("NAN", &idx)));
assert(idx == 3);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
assert(std::isnan(std::stod(L"NAN", &idx)));
assert(idx == 3);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
std::stod("", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stod(L"", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stod(" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stod(L" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stod("a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stod(L"a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
}

View File

@ -0,0 +1,166 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <string>
// float stof(const string& str, size_t *idx = 0);
// float stof(const wstring& str, size_t *idx = 0);
#include <string>
#include <cmath>
#include <cassert>
int main()
{
assert(std::stof("0") == 0);
assert(std::stof(L"0") == 0);
assert(std::stof("-0") == 0);
assert(std::stof(L"-0") == 0);
assert(std::stof("-10") == -10);
assert(std::stof(L"-10.5") == -10.5);
assert(std::stof(" 10") == 10);
assert(std::stof(L" 10") == 10);
size_t idx = 0;
assert(std::stof("10g", &idx) == 10);
assert(idx == 2);
idx = 0;
assert(std::stof(L"10g", &idx) == 10);
assert(idx == 2);
try
{
assert(std::stof("1.e60", &idx) == INFINITY);
assert(idx == 5);
}
catch (const std::out_of_range&)
{
assert(false);
}
try
{
assert(std::stof(L"1.e60", &idx) == INFINITY);
assert(idx == 5);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
assert(std::stof("1.e360", &idx) == INFINITY);
assert(false);
}
catch (const std::out_of_range&)
{
assert(idx == 0);
}
try
{
assert(std::stof(L"1.e360", &idx) == INFINITY);
assert(false);
}
catch (const std::out_of_range&)
{
assert(idx == 0);
}
try
{
assert(std::stof("INF", &idx) == INFINITY);
assert(idx == 3);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
assert(std::stof(L"INF", &idx) == INFINITY);
assert(idx == 3);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
assert(std::isnan(std::stof("NAN", &idx)));
assert(idx == 3);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
assert(std::isnan(std::stof(L"NAN", &idx)));
assert(idx == 3);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
std::stof("", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stof(L"", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stof(" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stof(L" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stof("a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stof(L"a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
}

View File

@ -0,0 +1,108 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <string>
// int stoi(const string& str, size_t *idx = 0, int base = 10);
// int stoi(const wstring& str, size_t *idx = 0, int base = 10);
#include <string>
#include <cassert>
int main()
{
assert(std::stoi("0") == 0);
assert(std::stoi(L"0") == 0);
assert(std::stoi("-0") == 0);
assert(std::stoi(L"-0") == 0);
assert(std::stoi("-10") == -10);
assert(std::stoi(L"-10") == -10);
assert(std::stoi(" 10") == 10);
assert(std::stoi(L" 10") == 10);
size_t idx = 0;
assert(std::stoi("10g", &idx, 16) == 16);
assert(idx == 2);
idx = 0;
assert(std::stoi(L"10g", &idx, 16) == 16);
assert(idx == 2);
if (std::numeric_limits<long>::max() > std::numeric_limits<int>::max())
{
try
{
std::stoi("0x100000000", &idx, 16);
assert(false);
}
catch (const std::out_of_range&)
{
}
try
{
std::stoi(L"0x100000000", &idx, 16);
assert(false);
}
catch (const std::out_of_range&)
{
}
}
idx = 0;
try
{
std::stoi("", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoi(L"", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoi(" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoi(L" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoi("a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoi(L"a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
}

View File

@ -0,0 +1,89 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <string>
// long stol(const string& str, size_t *idx = 0, int base = 10);
// long stol(const wstring& str, size_t *idx = 0, int base = 10);
#include <string>
#include <cassert>
int main()
{
assert(std::stol("0") == 0);
assert(std::stol(L"0") == 0);
assert(std::stol("-0") == 0);
assert(std::stol(L"-0") == 0);
assert(std::stol("-10") == -10);
assert(std::stol(L"-10") == -10);
assert(std::stol(" 10") == 10);
assert(std::stol(L" 10") == 10);
size_t idx = 0;
assert(std::stol("10g", &idx, 16) == 16);
assert(idx == 2);
idx = 0;
assert(std::stol(L"10g", &idx, 16) == 16);
assert(idx == 2);
idx = 0;
try
{
std::stol("", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stol(L"", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stol(" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stol(L" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stol("a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stol(L"a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
}

View File

@ -0,0 +1,168 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <string>
// long double stold(const string& str, size_t *idx = 0);
// long double stold(const wstring& str, size_t *idx = 0);
#include <iostream>
#include <string>
#include <cmath>
#include <cassert>
int main()
{
assert(std::stold("0") == 0);
assert(std::stold(L"0") == 0);
assert(std::stold("-0") == 0);
assert(std::stold(L"-0") == 0);
assert(std::stold("-10") == -10);
assert(std::stold(L"-10.5") == -10.5);
assert(std::stold(" 10") == 10);
assert(std::stold(L" 10") == 10);
size_t idx = 0;
assert(std::stold("10g", &idx) == 10);
assert(idx == 2);
idx = 0;
assert(std::stold(L"10g", &idx) == 10);
assert(idx == 2);
try
{
assert(std::stold("1.e60", &idx) == 1.e60L);
assert(idx == 5);
}
catch (const std::out_of_range&)
{
assert(false);
}
try
{
assert(std::stold(L"1.e60", &idx) == 1.e60L);
assert(idx == 5);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
assert(std::stold("1.e6000", &idx) == INFINITY);
assert(false);
}
catch (const std::out_of_range&)
{
assert(idx == 0);
}
try
{
assert(std::stold(L"1.e6000", &idx) == INFINITY);
assert(false);
}
catch (const std::out_of_range&)
{
assert(idx == 0);
}
try
{
assert(std::stold("INF", &idx) == INFINITY);
assert(idx == 3);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
assert(std::stold(L"INF", &idx) == INFINITY);
assert(idx == 3);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
assert(std::isnan(std::stold("NAN", &idx)));
assert(idx == 3);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
assert(std::isnan(std::stold(L"NAN", &idx)));
assert(idx == 3);
}
catch (const std::out_of_range&)
{
assert(false);
}
idx = 0;
try
{
std::stold("", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stold(L"", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stold(" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stold(L" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stold("a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stold(L"a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
}

View File

@ -0,0 +1,89 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <string>
// long long stoll(const string& str, size_t *idx = 0, int base = 10);
// long long stoll(const wstring& str, size_t *idx = 0, int base = 10);
#include <string>
#include <cassert>
int main()
{
assert(std::stoll("0") == 0);
assert(std::stoll(L"0") == 0);
assert(std::stoll("-0") == 0);
assert(std::stoll(L"-0") == 0);
assert(std::stoll("-10") == -10);
assert(std::stoll(L"-10") == -10);
assert(std::stoll(" 10") == 10);
assert(std::stoll(L" 10") == 10);
size_t idx = 0;
assert(std::stoll("10g", &idx, 16) == 16);
assert(idx == 2);
idx = 0;
assert(std::stoll(L"10g", &idx, 16) == 16);
assert(idx == 2);
idx = 0;
try
{
std::stoll("", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoll(L"", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoll(" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoll(L" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoll("a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoll(L"a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
}

View File

@ -0,0 +1,87 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <string>
// unsigned long stoul(const string& str, size_t *idx = 0, int base = 10);
// unsigned long stoul(const wstring& str, size_t *idx = 0, int base = 10);
#include <string>
#include <cassert>
int main()
{
assert(std::stoul("0") == 0);
assert(std::stoul(L"0") == 0);
assert(std::stoul("-0") == 0);
assert(std::stoul(L"-0") == 0);
assert(std::stoul(" 10") == 10);
assert(std::stoul(L" 10") == 10);
size_t idx = 0;
assert(std::stoul("10g", &idx, 16) == 16);
assert(idx == 2);
idx = 0;
assert(std::stoul(L"10g", &idx, 16) == 16);
assert(idx == 2);
idx = 0;
try
{
std::stoul("", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoul(L"", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoul(" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoul(L" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoul("a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoul(L"a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
}

View File

@ -0,0 +1,88 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <string>
// unsigned long long stoull(const string& str, size_t *idx = 0, int base = 10);
// unsigned long long stoull(const wstring& str, size_t *idx = 0, int base = 10);
#include <string>
#include <cassert>
int main()
{
assert(std::stoull("0") == 0);
assert(std::stoull(L"0") == 0);
assert(std::stoull("-0") == 0);
assert(std::stoull(L"-0") == 0);
assert(std::stoull(" 10") == 10);
assert(std::stoull(L" 10") == 10);
size_t idx = 0;
assert(std::stoull("10g", &idx, 16) == 16);
assert(idx == 2);
idx = 0;
assert(std::stoull(L"10g", &idx, 16) == 16);
assert(idx == 2);
idx = 0;
try
{
std::stoull("", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
idx = 0;
try
{
std::stoull(L"", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoull(" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoull(L" - 8", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoull("a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
try
{
std::stoull(L"a1", &idx);
assert(false);
}
catch (const std::invalid_argument&)
{
assert(idx == 0);
}
}

View File

@ -0,0 +1,126 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <string>
// string to_string(int val);
// string to_string(unsigned val);
// string to_string(long val);
// string to_string(unsigned long val);
// string to_string(long long val);
// string to_string(unsigned long long val);
// string to_string(float val);
// string to_string(double val);
// string to_string(long double val);
#include <string>
#include <cassert>
#include <sstream>
template <class T>
void
test_signed()
{
{
std::string s = std::to_string(T(0));
assert(s.size() == 1);
assert(s[s.size()] == 0);
assert(s == "0");
}
{
std::string s = std::to_string(T(12345));
assert(s.size() == 5);
assert(s[s.size()] == 0);
assert(s == "12345");
}
{
std::string s = std::to_string(T(-12345));
assert(s.size() == 6);
assert(s[s.size()] == 0);
assert(s == "-12345");
}
{
std::string s = std::to_string(std::numeric_limits<T>::max());
assert(s.size() == std::numeric_limits<T>::digits10 + 1);
std::istringstream is(s);
T t(0);
is >> t;
assert(t == std::numeric_limits<T>::max());
}
{
std::string s = std::to_string(std::numeric_limits<T>::min());
std::istringstream is(s);
T t(0);
is >> t;
assert(t == std::numeric_limits<T>::min());
}
}
template <class T>
void
test_unsigned()
{
{
std::string s = std::to_string(T(0));
assert(s.size() == 1);
assert(s[s.size()] == 0);
assert(s == "0");
}
{
std::string s = std::to_string(T(12345));
assert(s.size() == 5);
assert(s[s.size()] == 0);
assert(s == "12345");
}
{
std::string s = std::to_string(std::numeric_limits<T>::max());
assert(s.size() == std::numeric_limits<T>::digits10 + 1);
std::istringstream is(s);
T t(0);
is >> t;
assert(t == std::numeric_limits<T>::max());
}
}
template <class T>
void
test_float()
{
{
std::string s = std::to_string(T(0));
assert(s.size() == 8);
assert(s[s.size()] == 0);
assert(s == "0.000000");
}
{
std::string s = std::to_string(T(12345));
assert(s.size() == 12);
assert(s[s.size()] == 0);
assert(s == "12345.000000");
}
{
std::string s = std::to_string(T(-12345));
assert(s.size() == 13);
assert(s[s.size()] == 0);
assert(s == "-12345.000000");
}
}
int main()
{
test_signed<int>();
test_signed<long>();
test_signed<long long>();
test_unsigned<unsigned>();
test_unsigned<unsigned long>();
test_unsigned<unsigned long long>();
test_float<float>();
test_float<double>();
test_float<long double>();
}

View File

@ -0,0 +1,126 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <string>
// wstring to_wstring(int val);
// wstring to_wstring(unsigned val);
// wstring to_wstring(long val);
// wstring to_wstring(unsigned long val);
// wstring to_wstring(long long val);
// wstring to_wstring(unsigned long long val);
// wstring to_wstring(float val);
// wstring to_wstring(double val);
// wstring to_wstring(long double val);
#include <string>
#include <cassert>
#include <sstream>
template <class T>
void
test_signed()
{
{
std::wstring s = std::to_wstring(T(0));
assert(s.size() == 1);
assert(s[s.size()] == 0);
assert(s == L"0");
}
{
std::wstring s = std::to_wstring(T(12345));
assert(s.size() == 5);
assert(s[s.size()] == 0);
assert(s == L"12345");
}
{
std::wstring s = std::to_wstring(T(-12345));
assert(s.size() == 6);
assert(s[s.size()] == 0);
assert(s == L"-12345");
}
{
std::wstring s = std::to_wstring(std::numeric_limits<T>::max());
assert(s.size() == std::numeric_limits<T>::digits10 + 1);
std::wistringstream is(s);
T t(0);
is >> t;
assert(t == std::numeric_limits<T>::max());
}
{
std::wstring s = std::to_wstring(std::numeric_limits<T>::min());
std::wistringstream is(s);
T t(0);
is >> t;
assert(t == std::numeric_limits<T>::min());
}
}
template <class T>
void
test_unsigned()
{
{
std::wstring s = std::to_wstring(T(0));
assert(s.size() == 1);
assert(s[s.size()] == 0);
assert(s == L"0");
}
{
std::wstring s = std::to_wstring(T(12345));
assert(s.size() == 5);
assert(s[s.size()] == 0);
assert(s == L"12345");
}
{
std::wstring s = std::to_wstring(std::numeric_limits<T>::max());
assert(s.size() == std::numeric_limits<T>::digits10 + 1);
std::wistringstream is(s);
T t(0);
is >> t;
assert(t == std::numeric_limits<T>::max());
}
}
template <class T>
void
test_float()
{
{
std::wstring s = std::to_wstring(T(0));
assert(s.size() == 8);
assert(s[s.size()] == 0);
assert(s == L"0.000000");
}
{
std::wstring s = std::to_wstring(T(12345));
assert(s.size() == 12);
assert(s[s.size()] == 0);
assert(s == L"12345.000000");
}
{
std::wstring s = std::to_wstring(T(-12345));
assert(s.size() == 13);
assert(s[s.size()] == 0);
assert(s == L"-12345.000000");
}
}
int main()
{
test_signed<int>();
test_signed<long>();
test_signed<long long>();
test_unsigned<unsigned>();
test_unsigned<unsigned long>();
test_unsigned<unsigned long long>();
test_float<float>();
test_float<double>();
test_float<long double>();
}

View File

@ -0,0 +1,13 @@
// -*- C++ -*-
//===-------------------------- algorithm ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
int main()
{
}