* @brief Locale-independent QString::toDouble() replacement built on fast_float. */
| 302 | * @brief Locale-independent QString::toDouble() replacement built on fast_float. |
| 303 | */ |
| 304 | [[nodiscard]] static Q_ALWAYS_INLINE double toDouble(QStringView text, |
| 305 | bool* ok = nullptr) noexcept |
| 306 | { |
| 307 | const char16_t* first = text.utf16(); |
| 308 | const char16_t* last = first + text.size(); |
| 309 | |
| 310 | for (; first < last && QChar::isSpace(*first); ++first) |
| 311 | continue; |
| 312 | |
| 313 | for (; last > first && QChar::isSpace(*(last - 1)); --last) |
| 314 | continue; |
| 315 | |
| 316 | constexpr auto format = |
| 317 | fast_float::chars_format::general | fast_float::chars_format::allow_leading_plus; |
| 318 | |
| 319 | double value = 0.0; |
| 320 | const auto result = fast_float::from_chars(first, last, value, format); |
| 321 | const bool good = (result.ec == std::errc()) && (result.ptr == last); |
| 322 | if (ok) |
| 323 | *ok = good; |
| 324 | |
| 325 | return good ? value : 0.0; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * @brief Byte-level toDouble() for QByteArray / raw char data (same grammar as above). |