123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- //===------------------------- string.cpp ---------------------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is dual licensed under the MIT and the University of Illinois Open
- // Source Licenses. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- #include "string"
- #include "cstdlib"
- #include "cwchar"
- #include "cerrno"
- #include "limits"
- #include "stdexcept"
- #ifdef _LIBCPP_MSVCRT
- #include "support/win32/support.h"
- #endif // _LIBCPP_MSVCRT
- #include <stdio.h>
- _LIBCPP_BEGIN_NAMESPACE_STD
- template class __basic_string_common<true>;
- template class basic_string<char>;
- template class basic_string<wchar_t>;
- template
- string
- operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
- namespace
- {
- template<typename T>
- inline
- void throw_helper( const string& msg )
- {
- #ifndef _LIBCPP_NO_EXCEPTIONS
- throw T( msg );
- #else
- #if !defined(_LIBCPP_SGX_NO_IOSTREAMS)
- fprintf(stderr, "%s\n", msg.c_str());
- #endif // !defined(_LIBCPP_SGX_NO_IOSTREAMS)
- abort();
- #endif
- }
- inline
- void throw_from_string_out_of_range( const string& func )
- {
- throw_helper<out_of_range>(func + ": out of range");
- }
- inline
- void throw_from_string_invalid_arg( const string& func )
- {
- throw_helper<invalid_argument>(func + ": no conversion");
- }
- // as_integer
- template<typename V, typename S, typename F>
- inline
- V
- as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f)
- {
- typename S::value_type* ptr = nullptr;
- const typename S::value_type* const p = str.c_str();
- typename remove_reference<decltype(errno)>::type errno_save = errno;
- errno = 0;
- V r = f(p, &ptr, base);
- swap(errno, errno_save);
- if (errno_save == ERANGE)
- throw_from_string_out_of_range(func);
- if (ptr == p)
- throw_from_string_invalid_arg(func);
- if (idx)
- *idx = static_cast<size_t>(ptr - p);
- return r;
- }
- template<typename V, typename S>
- inline
- V
- as_integer(const string& func, const S& s, size_t* idx, int base);
- // string
- template<>
- inline
- int
- as_integer(const string& func, const string& s, size_t* idx, int base )
- {
- // Use long as no Standard string to integer exists.
- long r = as_integer_helper<long>( func, s, idx, base, strtol );
- if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
- throw_from_string_out_of_range(func);
- return static_cast<int>(r);
- }
- template<>
- inline
- long
- as_integer(const string& func, const string& s, size_t* idx, int base )
- {
- return as_integer_helper<long>( func, s, idx, base, strtol );
- }
- template<>
- inline
- unsigned long
- as_integer( const string& func, const string& s, size_t* idx, int base )
- {
- return as_integer_helper<unsigned long>( func, s, idx, base, strtoul );
- }
- template<>
- inline
- long long
- as_integer( const string& func, const string& s, size_t* idx, int base )
- {
- return as_integer_helper<long long>( func, s, idx, base, strtoll );
- }
- template<>
- inline
- unsigned long long
- as_integer( const string& func, const string& s, size_t* idx, int base )
- {
- return as_integer_helper<unsigned long long>( func, s, idx, base, strtoull );
- }
- // wstring
- // Implement support for wcstoxxx api in wchar.h
- #if !defined(_LIBCPP_SGX_CONFIG)
- template<>
- inline
- int
- as_integer( const string& func, const wstring& s, size_t* idx, int base )
- {
- // Use long as no Stantard string to integer exists.
- long r = as_integer_helper<long>( func, s, idx, base, wcstol );
- if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
- throw_from_string_out_of_range(func);
- return static_cast<int>(r);
- }
- template<>
- inline
- long
- as_integer( const string& func, const wstring& s, size_t* idx, int base )
- {
- return as_integer_helper<long>( func, s, idx, base, wcstol );
- }
- template<>
- inline
- unsigned long
- as_integer( const string& func, const wstring& s, size_t* idx, int base )
- {
- return as_integer_helper<unsigned long>( func, s, idx, base, wcstoul );
- }
- template<>
- inline
- long long
- as_integer( const string& func, const wstring& s, size_t* idx, int base )
- {
- return as_integer_helper<long long>( func, s, idx, base, wcstoll );
- }
- template<>
- inline
- unsigned long long
- as_integer( const string& func, const wstring& s, size_t* idx, int base )
- {
- return as_integer_helper<unsigned long long>( func, s, idx, base, wcstoull );
- }
- #endif // !defined(_LIBCPP_SGX_CONFIG)
- // as_float
- template<typename V, typename S, typename F>
- inline
- V
- as_float_helper(const string& func, const S& str, size_t* idx, F f )
- {
- typename S::value_type* ptr = nullptr;
- const typename S::value_type* const p = str.c_str();
- typename remove_reference<decltype(errno)>::type errno_save = errno;
- errno = 0;
- V r = f(p, &ptr);
- swap(errno, errno_save);
- if (errno_save == ERANGE)
- throw_from_string_out_of_range(func);
- if (ptr == p)
- throw_from_string_invalid_arg(func);
- if (idx)
- *idx = static_cast<size_t>(ptr - p);
- return r;
- }
- template<typename V, typename S>
- inline
- V as_float( const string& func, const S& s, size_t* idx = nullptr );
- template<>
- inline
- float
- as_float( const string& func, const string& s, size_t* idx )
- {
- return as_float_helper<float>( func, s, idx, strtof );
- }
- template<>
- inline
- double
- as_float(const string& func, const string& s, size_t* idx )
- {
- return as_float_helper<double>( func, s, idx, strtod );
- }
- template<>
- inline
- long double
- as_float( const string& func, const string& s, size_t* idx )
- {
- return as_float_helper<long double>( func, s, idx, strtold );
- }
- // Implement support for wcstoxxx api in wchar.h
- #if !defined(_LIBCPP_SGX_CONFIG)
- template<>
- inline
- float
- as_float( const string& func, const wstring& s, size_t* idx )
- {
- return as_float_helper<float>( func, s, idx, wcstof );
- }
- template<>
- inline
- double
- as_float( const string& func, const wstring& s, size_t* idx )
- {
- return as_float_helper<double>( func, s, idx, wcstod );
- }
- template<>
- inline
- long double
- as_float( const string& func, const wstring& s, size_t* idx )
- {
- return as_float_helper<long double>( func, s, idx, wcstold );
- }
- #endif // !defined(_LIBCPP_SGX_CONFIG)
- } // unnamed namespace
- int
- stoi(const string& str, size_t* idx, int base)
- {
- return as_integer<int>( "stoi", str, idx, base );
- }
- // Implement support for wcstoxxx api in wchar.h
- #if !defined(_LIBCPP_SGX_CONFIG)
- int
- stoi(const wstring& str, size_t* idx, int base)
- {
- return as_integer<int>( "stoi", str, idx, base );
- }
- #endif // !defined(_LIBCPP_SGX_CONFIG)
- long
- stol(const string& str, size_t* idx, int base)
- {
- return as_integer<long>( "stol", str, idx, base );
- }
- // Implement support for wcstoxxx api in wchar.h
- #if !defined(_LIBCPP_SGX_CONFIG)
- long
- stol(const wstring& str, size_t* idx, int base)
- {
- return as_integer<long>( "stol", str, idx, base );
- }
- #endif // !defined(_LIBCPP_SGX_CONFIG)
- unsigned long
- stoul(const string& str, size_t* idx, int base)
- {
- return as_integer<unsigned long>( "stoul", str, idx, base );
- }
- // Implement support for wcstoxxx api in wchar.h
- #if !defined(_LIBCPP_SGX_CONFIG)
- unsigned long
- stoul(const wstring& str, size_t* idx, int base)
- {
- return as_integer<unsigned long>( "stoul", str, idx, base );
- }
- #endif // !defined(_LIBCPP_SGX_CONFIG)
- long long
- stoll(const string& str, size_t* idx, int base)
- {
- return as_integer<long long>( "stoll", str, idx, base );
- }
- // Implement support for wcstoxxx api in wchar.h
- #if !defined(_LIBCPP_SGX_CONFIG)
- long long
- stoll(const wstring& str, size_t* idx, int base)
- {
- return as_integer<long long>( "stoll", str, idx, base );
- }
- #endif // !defined(_LIBCPP_SGX_CONFIG)
- unsigned long long
- stoull(const string& str, size_t* idx, int base)
- {
- return as_integer<unsigned long long>( "stoull", str, idx, base );
- }
- // Implement support for wcstoxxx api in wchar.h
- #if !defined(_LIBCPP_SGX_CONFIG)
- unsigned long long
- stoull(const wstring& str, size_t* idx, int base)
- {
- return as_integer<unsigned long long>( "stoull", str, idx, base );
- }
- #endif // !defined(_LIBCPP_SGX_CONFIG)
- float
- stof(const string& str, size_t* idx)
- {
- return as_float<float>( "stof", str, idx );
- }
- // Implement support for wcstoxxx api in wchar.h
- #if !defined(_LIBCPP_SGX_CONFIG)
- float
- stof(const wstring& str, size_t* idx)
- {
- return as_float<float>( "stof", str, idx );
- }
- #endif // !defined(_LIBCPP_SGX_CONFIG)
- double
- stod(const string& str, size_t* idx)
- {
- return as_float<double>( "stod", str, idx );
- }
- // Implement support for wcstoxxx api in wchar.h
- #if !defined(_LIBCPP_SGX_CONFIG)
- double
- stod(const wstring& str, size_t* idx)
- {
- return as_float<double>( "stod", str, idx );
- }
- #endif // !defined(_LIBCPP_SGX_CONFIG)
- long double
- stold(const string& str, size_t* idx)
- {
- return as_float<long double>( "stold", str, idx );
- }
- // Implement support for wcstoxxx api in wchar.h
- #if !defined(_LIBCPP_SGX_CONFIG)
- long double
- stold(const wstring& str, size_t* idx)
- {
- return as_float<long double>( "stold", str, idx );
- }
- #endif // !defined(_LIBCPP_SGX_CONFIG)
- // to_string
- namespace
- {
- // as_string
- template<typename S, typename P, typename V >
- inline
- S
- as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)
- {
- typedef typename S::size_type size_type;
- size_type available = s.size();
- while (true)
- {
- int status = sprintf_like(&s[0], available + 1, fmt, a);
- if ( status >= 0 )
- {
- size_type used = static_cast<size_type>(status);
- if ( used <= available )
- {
- s.resize( used );
- break;
- }
- available = used; // Assume this is advice of how much space we need.
- }
- else
- available = available * 2 + 1;
- s.resize(available);
- }
- return s;
- }
- template <class S, class V, bool = is_floating_point<V>::value>
- struct initial_string;
- template <class V, bool b>
- struct initial_string<string, V, b>
- {
- string
- operator()() const
- {
- string s;
- s.resize(s.capacity());
- return s;
- }
- };
- template <class V>
- struct initial_string<wstring, V, false>
- {
- wstring
- operator()() const
- {
- 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());
- return s;
- }
- };
- template <class V>
- struct initial_string<wstring, V, true>
- {
- wstring
- operator()() const
- {
- wstring s(20, wchar_t());
- s.resize(s.capacity());
- return s;
- }
- };
- typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...);
- inline
- wide_printf
- get_swprintf()
- {
- #ifndef _LIBCPP_MSVCRT
- return swprintf;
- #else
- return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(swprintf);
- #endif
- }
- } // unnamed namespace
- string to_string(int val)
- {
- return as_string(snprintf, initial_string<string, int>()(), "%d", val);
- }
- string to_string(unsigned val)
- {
- return as_string(snprintf, initial_string<string, unsigned>()(), "%u", val);
- }
- string to_string(long val)
- {
- return as_string(snprintf, initial_string<string, long>()(), "%ld", val);
- }
- string to_string(unsigned long val)
- {
- return as_string(snprintf, initial_string<string, unsigned long>()(), "%lu", val);
- }
- string to_string(long long val)
- {
- return as_string(snprintf, initial_string<string, long long>()(), "%lld", val);
- }
- string to_string(unsigned long long val)
- {
- return as_string(snprintf, initial_string<string, unsigned long long>()(), "%llu", val);
- }
- string to_string(float val)
- {
- return as_string(snprintf, initial_string<string, float>()(), "%f", val);
- }
- string to_string(double val)
- {
- return as_string(snprintf, initial_string<string, double>()(), "%f", val);
- }
- string to_string(long double val)
- {
- return as_string(snprintf, initial_string<string, long double>()(), "%Lf", val);
- }
- wstring to_wstring(int val)
- {
- return as_string(get_swprintf(), initial_string<wstring, int>()(), L"%d", val);
- }
- wstring to_wstring(unsigned val)
- {
- return as_string(get_swprintf(), initial_string<wstring, unsigned>()(), L"%u", val);
- }
- wstring to_wstring(long val)
- {
- return as_string(get_swprintf(), initial_string<wstring, long>()(), L"%ld", val);
- }
- wstring to_wstring(unsigned long val)
- {
- return as_string(get_swprintf(), initial_string<wstring, unsigned long>()(), L"%lu", val);
- }
- wstring to_wstring(long long val)
- {
- return as_string(get_swprintf(), initial_string<wstring, long long>()(), L"%lld", val);
- }
- wstring to_wstring(unsigned long long val)
- {
- return as_string(get_swprintf(), initial_string<wstring, unsigned long long>()(), L"%llu", val);
- }
- wstring to_wstring(float val)
- {
- return as_string(get_swprintf(), initial_string<wstring, float>()(), L"%f", val);
- }
- wstring to_wstring(double val)
- {
- return as_string(get_swprintf(), initial_string<wstring, double>()(), L"%f", val);
- }
- wstring to_wstring(long double val)
- {
- return as_string(get_swprintf(), initial_string<wstring, long double>()(), L"%Lf", val);
- }
- _LIBCPP_END_NAMESPACE_STD
|