fune/xpcom/string/nsTString.cpp
Nika Layzell adfb1a6344 Bug 840706 - Part 2: Make ToDouble and ToFloat more spec-compliant, r=smaug,xpcom-reviewers,barret
The main use of this method appears to be when parsing attributes with floating
point values in HTML. By changing from PR_strtod, this change does have some
semantic differences around edge-cases, though I've tried to keep it compliant
with the wording in the standard.

Here are some of the edge-cases I investigated:

1. We appear to build nspr with INFNAN_CHECK undefined, meaning that we did not
   parse strings like "inf", "infinity" and "nan" both before and after this
   change.
2. PR_strtod already ignored locale (built with USE_LOCALE undefined), so
   locale behaviour shouldn't have changed.
3. Neither PR_strtod nor the spec support hex floats, so I didn't enable them
   in double-conversion.
4. Leading whitespace is skipped by both PR_strtod and in the spec, so is
   skipped after this change.
5. Numbers too large to fit in a double (e.g. 2E308) were previously returned
   as inf or -inf by PR_strtod. The spec specifies that these values should return
   an error instead, so they have been changed to produce an error with this
   change.
   This is a web-visible change (<progress value=2E308 max=10> is indeterminate
   in Chromium and complete in Firefox before this change, it will be
   indeterminate for both after the change).
6. Parsing for floats & doubles are now handled seperately due to
   differences in the maximum and minimum representable values.

Differential Revision: https://phabricator.services.mozilla.com/D148305
2022-12-06 20:27:49 +00:00

42 lines
1.1 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsTString.h"
#include "nsString.h"
#include "prdtoa.h"
/**
* nsTString::SetCharAt
*/
template <typename T>
bool nsTString<T>::SetCharAt(char16_t aChar, index_type aIndex) {
if (aIndex >= this->mLength) {
return false;
}
if (!this->EnsureMutable()) {
this->AllocFailed(this->mLength);
}
this->mData[aIndex] = char_type(aChar);
return true;
}
template <typename T>
void nsTString<T>::Rebind(const char_type* data, size_type length) {
// If we currently own a buffer, release it.
this->Finalize();
this->SetData(const_cast<char_type*>(data), length, DataFlags::TERMINATED);
this->AssertValidDependentString();
}
template class nsTString<char>;
template class nsTString<char16_t>;
template class nsTAutoStringN<char, 64>;
template class nsTAutoStringN<char16_t, 64>;