From a6a062df66fe49704bd074464af30bf8c3db6ea5 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Wed, 2 Jun 2010 18:20:39 +0000 Subject: [PATCH] [string.conversions] git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@105336 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/string | 136 +++- include/thread | 4 +- lib/buildit | 2 +- src/ios.cpp | 55 -- src/string.cpp | 690 ++++++++++++++++++ test/strings/string.conversions/stod.pass.cpp | 166 +++++ test/strings/string.conversions/stof.pass.cpp | 166 +++++ test/strings/string.conversions/stoi.pass.cpp | 108 +++ test/strings/string.conversions/stol.pass.cpp | 89 +++ .../strings/string.conversions/stold.pass.cpp | 168 +++++ .../strings/string.conversions/stoll.pass.cpp | 89 +++ .../strings/string.conversions/stoul.pass.cpp | 87 +++ .../string.conversions/stoull.pass.cpp | 88 +++ .../string.conversions/to_string.pass.cpp | 126 ++++ .../string.conversions/to_wstring.pass.cpp | 126 ++++ .../strings.general/nothing_to_do.pass.cpp | 13 + 16 files changed, 2037 insertions(+), 76 deletions(-) create mode 100644 src/string.cpp create mode 100644 test/strings/string.conversions/stod.pass.cpp create mode 100644 test/strings/string.conversions/stof.pass.cpp create mode 100644 test/strings/string.conversions/stoi.pass.cpp create mode 100644 test/strings/string.conversions/stol.pass.cpp create mode 100644 test/strings/string.conversions/stold.pass.cpp create mode 100644 test/strings/string.conversions/stoll.pass.cpp create mode 100644 test/strings/string.conversions/stoul.pass.cpp create mode 100644 test/strings/string.conversions/stoull.pass.cpp create mode 100644 test/strings/string.conversions/to_string.pass.cpp create mode 100644 test/strings/string.conversions/to_wstring.pass.cpp create mode 100644 test/strings/strings.general/nothing_to_do.pass.cpp diff --git a/include/string b/include/string index 15713f07..3428fb84 100644 --- a/include/string +++ b/include/string @@ -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 - 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, 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 basic_string& append(InputIterator first, InputIterator last); + template + basic_string& append(InputIterator first, InputIterator last); basic_string& append(initializer_list); 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 basic_string& assign(InputIterator first, InputIterator last); + template + basic_string& assign(InputIterator first, InputIterator last); basic_string& assign(initializer_list); 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 iterator insert(const_iterator p, InputIterator first, InputIterator last); + template + iterator insert(const_iterator p, InputIterator first, InputIterator last); iterator insert(const_iterator p, initializer_list); 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 basic_string& replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2); + template + basic_string& replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2); basic_string& replace(iterator i1, iterator i2, initializer_list); 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 basic_string -operator+(const basic_string& lhs, const basic_string& rhs); +operator+(const basic_string& lhs, + const basic_string& rhs); template basic_string @@ -271,7 +281,8 @@ basic_string operator+(const basic_string& lhs, charT rhs); template -bool operator==(const basic_string& lhs, const basic_string& rhs); +bool operator==(const basic_string& lhs, + const basic_string& rhs); template bool operator==(const charT* lhs, const basic_string& rhs); @@ -280,7 +291,8 @@ template bool operator==(const basic_string& lhs, const charT* rhs); template -bool operator!=(const basic_string& lhs, const basic_string& rhs); +bool operator!=(const basic_string& lhs, + const basic_string& rhs); template bool operator!=(const charT* lhs, const basic_string& rhs); @@ -289,7 +301,8 @@ template bool operator!=(const basic_string& lhs, const charT* rhs); template -bool operator< (const basic_string& lhs, const basic_string& rhs); +bool operator< (const basic_string& lhs, + const basic_string& rhs); template bool operator< (const basic_string& lhs, const charT* rhs); @@ -298,7 +311,8 @@ template bool operator< (const charT* lhs, const basic_string& rhs); template -bool operator> (const basic_string& lhs, const basic_string& rhs); +bool operator> (const basic_string& lhs, + const basic_string& rhs); template bool operator> (const basic_string& lhs, const charT* rhs); @@ -307,7 +321,8 @@ template bool operator> (const charT* lhs, const basic_string& rhs); template -bool operator<=(const basic_string& lhs, const basic_string& rhs); +bool operator<=(const basic_string& lhs, + const basic_string& rhs); template bool operator<=(const basic_string& lhs, const charT* rhs); @@ -316,7 +331,8 @@ template bool operator<=(const charT* lhs, const basic_string& rhs); template -bool operator>=(const basic_string& lhs, const basic_string& rhs); +bool operator>=(const basic_string& lhs, + const basic_string& rhs); template bool operator>=(const basic_string& lhs, const charT* rhs); @@ -325,7 +341,8 @@ template bool operator>=(const charT* lhs, const basic_string& rhs); template -void swap(basic_string& lhs, basic_string& rhs); +void swap(basic_string& lhs, + basic_string& rhs); template basic_istream& @@ -337,7 +354,8 @@ operator<<(basic_ostream& os, const basic_string basic_istream& -getline(basic_istream& is, basic_string& str, charT delim); +getline(basic_istream& is, basic_string& str, + charT delim); template basic_istream& @@ -345,6 +363,48 @@ getline(basic_istream& is, basic_string typedef basic_string string; typedef basic_string wstring; +typedef basic_string u16string; +typedef basic_string 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; template <> struct hash; @@ -3449,6 +3509,46 @@ typedef basic_string 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 const typename basic_string<_CharT, _Traits, _Allocator>::size_type basic_string<_CharT, _Traits, _Allocator>::npos; diff --git a/include/thread b/include/thread index 16faed93..5fcd2c98 100644 --- a/include/thread +++ b/include/thread @@ -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; } diff --git a/lib/buildit b/lib/buildit index 43341f97..19d19598 100755 --- a/lib/buildit +++ b/lib/buildit @@ -12,7 +12,7 @@ then exit 1 fi -if [ -z $CXX ] +if [ -z "$CXX" ] then CXX=g++ fi diff --git a/src/ios.cpp b/src/ios.cpp index 4d1261e3..eb597bfb 100644 --- a/src/ios.cpp +++ b/src/ios.cpp @@ -20,61 +20,6 @@ _LIBCPP_BEGIN_NAMESPACE_STD -template class __basic_string_common; - -template class basic_string; -template class basic_string; - -template enable_if<__is_forward_iterator::value, void>::type - basic_string, allocator > - ::__init(char const*, char const*); - -template enable_if<__is_forward_iterator::value, void>::type - basic_string, allocator > - ::__init(wchar_t const*, wchar_t const*); - -template - enable_if<__is_forward_iterator::value, - basic_string, allocator >&>::type - basic_string, allocator >:: - append(char*, char*); - -template - enable_if<__is_forward_iterator::value, - basic_string, allocator >&>::type - basic_string, allocator >:: - append(wchar_t*, wchar_t*); - -template - enable_if<__is_forward_iterator::value, - string::iterator>::type - string:: - insert(string::const_iterator, char const*, char const*); - -template - enable_if<__is_forward_iterator::value, - wstring::iterator>::type - wstring:: - insert(wstring::const_iterator, wchar_t const*, wchar_t const*); - -template - enable_if<__is_input_iterator::value, string&>::type - string:: - replace(string::iterator, string::iterator, char const*, char const*); - -template - enable_if<__is_input_iterator::value, wstring&>::type - wstring:: - replace(wstring::iterator, wstring::iterator, wchar_t const*, wchar_t const*); - -template - enable_if<__is_forward_iterator::value, wstring&>::type - wstring::assign(wchar_t*, wchar_t*); - -template - string - operator+, allocator >(char const*, string const&); - template class basic_ios; template class basic_ios; diff --git a/src/string.cpp b/src/string.cpp new file mode 100644 index 00000000..819935c0 --- /dev/null +++ b/src/string.cpp @@ -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; + +template class basic_string; +template class basic_string; + +template enable_if<__is_forward_iterator::value, void>::type + basic_string, allocator > + ::__init(char const*, char const*); + +template enable_if<__is_forward_iterator::value, void>::type + basic_string, allocator > + ::__init(wchar_t const*, wchar_t const*); + +template + enable_if<__is_forward_iterator::value, + basic_string, allocator >&>::type + basic_string, allocator >:: + append(char*, char*); + +template + enable_if<__is_forward_iterator::value, + basic_string, allocator >&>::type + basic_string, allocator >:: + append(wchar_t*, wchar_t*); + +template + enable_if<__is_forward_iterator::value, + string::iterator>::type + string:: + insert(string::const_iterator, char const*, char const*); + +template + enable_if<__is_forward_iterator::value, + wstring::iterator>::type + wstring:: + insert(wstring::const_iterator, wchar_t const*, wchar_t const*); + +template + enable_if<__is_input_iterator::value, string&>::type + string:: + replace(string::iterator, string::iterator, char const*, char const*); + +template + enable_if<__is_input_iterator::value, wstring&>::type + wstring:: + replace(wstring::iterator, wstring::iterator, wchar_t const*, wchar_t const*); + +template + enable_if<__is_forward_iterator::value, wstring&>::type + wstring::assign(wchar_t*, wchar_t*); + +template + string + operator+, allocator >(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::min() || numeric_limits::max() < r) + ptr = const_cast(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(ptr - p); + return static_cast(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::min() || numeric_limits::max() < r) + ptr = const_cast(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(ptr - p); + return static_cast(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(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(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(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(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(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(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(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(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(ptr - p); + return static_cast(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(ptr - p); + return static_cast(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(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(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(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(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::digits / 3) + + ((numeric_limits::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::digits / 3) + + ((numeric_limits::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::digits / 3) + + ((numeric_limits::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::digits / 3) + + ((numeric_limits::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::digits / 3) + + ((numeric_limits::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::digits / 3) + + ((numeric_limits::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 diff --git a/test/strings/string.conversions/stod.pass.cpp b/test/strings/string.conversions/stod.pass.cpp new file mode 100644 index 00000000..4c75f0a3 --- /dev/null +++ b/test/strings/string.conversions/stod.pass.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// double stod(const string& str, size_t *idx = 0); +// double stod(const wstring& str, size_t *idx = 0); + +#include +#include +#include + +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); + } +} diff --git a/test/strings/string.conversions/stof.pass.cpp b/test/strings/string.conversions/stof.pass.cpp new file mode 100644 index 00000000..14a5bd03 --- /dev/null +++ b/test/strings/string.conversions/stof.pass.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// float stof(const string& str, size_t *idx = 0); +// float stof(const wstring& str, size_t *idx = 0); + +#include +#include +#include + +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); + } +} diff --git a/test/strings/string.conversions/stoi.pass.cpp b/test/strings/string.conversions/stoi.pass.cpp new file mode 100644 index 00000000..68451806 --- /dev/null +++ b/test/strings/string.conversions/stoi.pass.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// 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 +#include + +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::max() > std::numeric_limits::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); + } +} diff --git a/test/strings/string.conversions/stol.pass.cpp b/test/strings/string.conversions/stol.pass.cpp new file mode 100644 index 00000000..f3132631 --- /dev/null +++ b/test/strings/string.conversions/stol.pass.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// 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 +#include + +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); + } +} diff --git a/test/strings/string.conversions/stold.pass.cpp b/test/strings/string.conversions/stold.pass.cpp new file mode 100644 index 00000000..86666d80 --- /dev/null +++ b/test/strings/string.conversions/stold.pass.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// long double stold(const string& str, size_t *idx = 0); +// long double stold(const wstring& str, size_t *idx = 0); + +#include + +#include +#include +#include + +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); + } +} diff --git a/test/strings/string.conversions/stoll.pass.cpp b/test/strings/string.conversions/stoll.pass.cpp new file mode 100644 index 00000000..8e904037 --- /dev/null +++ b/test/strings/string.conversions/stoll.pass.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// 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 +#include + +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); + } +} diff --git a/test/strings/string.conversions/stoul.pass.cpp b/test/strings/string.conversions/stoul.pass.cpp new file mode 100644 index 00000000..c31b2cc2 --- /dev/null +++ b/test/strings/string.conversions/stoul.pass.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// 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 +#include + +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); + } +} diff --git a/test/strings/string.conversions/stoull.pass.cpp b/test/strings/string.conversions/stoull.pass.cpp new file mode 100644 index 00000000..9432750b --- /dev/null +++ b/test/strings/string.conversions/stoull.pass.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// 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 +#include + +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); + } +} diff --git a/test/strings/string.conversions/to_string.pass.cpp b/test/strings/string.conversions/to_string.pass.cpp new file mode 100644 index 00000000..86e2b6b4 --- /dev/null +++ b/test/strings/string.conversions/to_string.pass.cpp @@ -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 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 +#include +#include + +template +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::max()); + assert(s.size() == std::numeric_limits::digits10 + 1); + std::istringstream is(s); + T t(0); + is >> t; + assert(t == std::numeric_limits::max()); + } + { + std::string s = std::to_string(std::numeric_limits::min()); + std::istringstream is(s); + T t(0); + is >> t; + assert(t == std::numeric_limits::min()); + } +} + +template +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::max()); + assert(s.size() == std::numeric_limits::digits10 + 1); + std::istringstream is(s); + T t(0); + is >> t; + assert(t == std::numeric_limits::max()); + } +} + +template +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(); + test_signed(); + test_signed(); + test_unsigned(); + test_unsigned(); + test_unsigned(); + test_float(); + test_float(); + test_float(); +} diff --git a/test/strings/string.conversions/to_wstring.pass.cpp b/test/strings/string.conversions/to_wstring.pass.cpp new file mode 100644 index 00000000..a96abf2e --- /dev/null +++ b/test/strings/string.conversions/to_wstring.pass.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// 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 +#include +#include + +template +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::max()); + assert(s.size() == std::numeric_limits::digits10 + 1); + std::wistringstream is(s); + T t(0); + is >> t; + assert(t == std::numeric_limits::max()); + } + { + std::wstring s = std::to_wstring(std::numeric_limits::min()); + std::wistringstream is(s); + T t(0); + is >> t; + assert(t == std::numeric_limits::min()); + } +} + +template +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::max()); + assert(s.size() == std::numeric_limits::digits10 + 1); + std::wistringstream is(s); + T t(0); + is >> t; + assert(t == std::numeric_limits::max()); + } +} + +template +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(); + test_signed(); + test_signed(); + test_unsigned(); + test_unsigned(); + test_unsigned(); + test_float(); + test_float(); + test_float(); +} diff --git a/test/strings/strings.general/nothing_to_do.pass.cpp b/test/strings/strings.general/nothing_to_do.pass.cpp new file mode 100644 index 00000000..b85601fc --- /dev/null +++ b/test/strings/strings.general/nothing_to_do.pass.cpp @@ -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() +{ +}