Page MenuHomePhabricator

D59178/new/.diff
No OneTemporary

File Metadata

Created
Mon, May 25, 6:02 PM

D59178/new/.diff

Index: libcxx/src/string.cpp
===================================================================
--- libcxx/src/string.cpp
+++ libcxx/src/string.cpp
@@ -7,12 +7,14 @@
//===----------------------------------------------------------------------===//
#include "string"
+#include "charconv"
#include "cstdlib"
#include "cwchar"
#include "cerrno"
#include "limits"
#include "stdexcept"
#include <stdio.h>
+#include "__debug"
_LIBCPP_BEGIN_NAMESPACE_STD
@@ -375,11 +377,11 @@
return s;
}
-template <class S, class V, bool = is_floating_point<V>::value>
+template <class S>
struct initial_string;
-template <class V, bool b>
-struct initial_string<string, V, b>
+template <>
+struct initial_string<string>
{
string
operator()() const
@@ -390,23 +392,8 @@
}
};
-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>
+template <>
+struct initial_string<wstring>
{
wstring
operator()() const
@@ -430,95 +417,108 @@
#endif
}
+template <typename S, typename V>
+S i_to_string(const V v)
+{
+ // numeric_limits::digits10 returns value less on 1 than desired for unsigned numbers.
+ // For example, for 1-byte unsigned value digits10 is 2 (999 can not be represented),
+ // so we need +1 here.
+ constexpr size_t bufsize = numeric_limits<V>::digits10 + 2; // +1 for minus, +1 for digits10
+ char buf[bufsize];
+ const auto res = to_chars(buf, buf + bufsize, v);
+ _LIBCPP_ASSERT(res.ec == errc(), "bufsize must be large enough to accomodate the value");
+ return S(buf, res.ptr);
+}
+
} // unnamed namespace
string to_string(int val)
{
- return as_string(snprintf, initial_string<string, int>()(), "%d", val);
+ return i_to_string<string>(val);
}
string to_string(unsigned val)
{
- return as_string(snprintf, initial_string<string, unsigned>()(), "%u", val);
+ return i_to_string<string>(val);
}
string to_string(long val)
{
- return as_string(snprintf, initial_string<string, long>()(), "%ld", val);
+ return i_to_string<string>(val);
}
string to_string(unsigned long val)
{
- return as_string(snprintf, initial_string<string, unsigned long>()(), "%lu", val);
+ return i_to_string<string>(val);
}
string to_string(long long val)
{
- return as_string(snprintf, initial_string<string, long long>()(), "%lld", val);
+ return i_to_string<string>(val);
}
string to_string(unsigned long long val)
{
- return as_string(snprintf, initial_string<string, unsigned long long>()(), "%llu", val);
+ return i_to_string<string>(val);
}
string to_string(float val)
{
- return as_string(snprintf, initial_string<string, float>()(), "%f", val);
+ return as_string(snprintf, initial_string<string>()(), "%f", val);
}
string to_string(double val)
{
- return as_string(snprintf, initial_string<string, double>()(), "%f", val);
+ return as_string(snprintf, initial_string<string>()(), "%f", val);
}
string to_string(long double val)
{
- return as_string(snprintf, initial_string<string, long double>()(), "%Lf", val);
+ return as_string(snprintf, initial_string<string>()(), "%Lf", val);
}
wstring to_wstring(int val)
{
- return as_string(get_swprintf(), initial_string<wstring, int>()(), L"%d", val);
+ return i_to_string<wstring>(val);
}
wstring to_wstring(unsigned val)
{
- return as_string(get_swprintf(), initial_string<wstring, unsigned>()(), L"%u", val);
+ return i_to_string<wstring>(val);
}
wstring to_wstring(long val)
{
- return as_string(get_swprintf(), initial_string<wstring, long>()(), L"%ld", val);
+ return i_to_string<wstring>(val);
}
wstring to_wstring(unsigned long val)
{
- return as_string(get_swprintf(), initial_string<wstring, unsigned long>()(), L"%lu", val);
+ return i_to_string<wstring>(val);
}
wstring to_wstring(long long val)
{
- return as_string(get_swprintf(), initial_string<wstring, long long>()(), L"%lld", val);
+ return i_to_string<wstring>(val);
}
wstring to_wstring(unsigned long long val)
{
- return as_string(get_swprintf(), initial_string<wstring, unsigned long long>()(), L"%llu", val);
+ return i_to_string<wstring>(val);
}
wstring to_wstring(float val)
{
- return as_string(get_swprintf(), initial_string<wstring, float>()(), L"%f", val);
+ return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val);
}
wstring to_wstring(double val)
{
- return as_string(get_swprintf(), initial_string<wstring, double>()(), L"%f", val);
+ return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val);
}
wstring to_wstring(long double val)
{
- return as_string(get_swprintf(), initial_string<wstring, long double>()(), L"%Lf", val);
+ return as_string(get_swprintf(), initial_string<wstring>()(), L"%Lf", val);
}
_LIBCPP_END_NAMESPACE_STD

Event Timeline