Merge "Fix wcsto* where strings begin with whitespace."

This commit is contained in:
Treehugger Robot 2016-12-02 23:04:04 +00:00 committed by Gerrit Code Review
commit 297ad4cbb8
3 changed files with 27 additions and 5 deletions

View file

@ -32,8 +32,14 @@
#include "local.h"
template <typename float_type> float_type wcstod(const wchar_t* str, wchar_t** end,
float_type strtod_fn(const char*, char**)) {
/// Performs wide-character string to floating point conversion.
template <typename float_type>
float_type wcstod(const wchar_t* str, wchar_t** end, float_type strtod_fn(const char*, char**)) {
const wchar_t* original_str = str;
while (iswspace(*str)) {
str++;
}
// What's the longest span of the input that might be part of the float?
size_t max_len = wcsspn(str, L"-+0123456789.xXeEpP()nNaAiIfFtTyY");
@ -70,7 +76,15 @@ template <typename float_type> float_type wcstod(const wchar_t* str, wchar_t** e
float_type result = strtod_fn(ascii_str, &ascii_end);
if (ascii_end != ascii_str + actual_len) abort();
if (end) *end = const_cast<wchar_t*>(str) + actual_len;
if (end) {
if (actual_len == 0) {
// There was an error. We need to set the end pointer back to the original string, not the
// one we advanced past the leading whitespace.
*end = const_cast<wchar_t*>(original_str);
} else {
*end = const_cast<wchar_t*>(str) + actual_len;
}
}
delete[] ascii_str;
return result;

View file

@ -298,6 +298,11 @@ static void CheckStrToFloat(T fn(const char* s, char** end)) {
EXPECT_PRED_FORMAT2(pred, 9.0, fn("0.9e1", nullptr));
EXPECT_PRED_FORMAT2(pred, 9.0, fn("0x1.2p3", nullptr));
const char* s = " \t\v\f\r\n9.0";
char* p;
EXPECT_PRED_FORMAT2(pred, 9.0, fn(s, &p));
EXPECT_EQ(s + strlen(s), p);
EXPECT_TRUE(isnan(fn("+nan", nullptr)));
EXPECT_TRUE(isnan(fn("nan", nullptr)));
EXPECT_TRUE(isnan(fn("-nan", nullptr)));
@ -306,7 +311,6 @@ static void CheckStrToFloat(T fn(const char* s, char** end)) {
EXPECT_TRUE(isnan(fn("nan(0xff)", nullptr)));
EXPECT_TRUE(isnan(fn("-nan(0xff)", nullptr)));
char* p;
EXPECT_TRUE(isnan(fn("+nanny", &p)));
EXPECT_STREQ("ny", p);
EXPECT_TRUE(isnan(fn("nanny", &p)));

View file

@ -686,6 +686,11 @@ static void CheckWcsToFloat(T fn(const wchar_t* s, wchar_t** end)) {
EXPECT_PRED_FORMAT2(pred, 9.0, fn(L"0.9e1", nullptr));
EXPECT_PRED_FORMAT2(pred, 9.0, fn(L"0x1.2p3", nullptr));
const wchar_t* s = L" \t\v\f\r\n9.0";
wchar_t* p;
EXPECT_PRED_FORMAT2(pred, 9.0, fn(s, &p));
EXPECT_EQ(s + wcslen(s), p);
EXPECT_TRUE(isnan(fn(L"+nan", nullptr)));
EXPECT_TRUE(isnan(fn(L"nan", nullptr)));
EXPECT_TRUE(isnan(fn(L"-nan", nullptr)));
@ -694,7 +699,6 @@ static void CheckWcsToFloat(T fn(const wchar_t* s, wchar_t** end)) {
EXPECT_TRUE(isnan(fn(L"nan(0xff)", nullptr)));
EXPECT_TRUE(isnan(fn(L"-nan(0xff)", nullptr)));
wchar_t* p;
EXPECT_TRUE(isnan(fn(L"+nanny", &p)));
EXPECT_STREQ(L"ny", p);
EXPECT_TRUE(isnan(fn(L"nanny", &p)));