algorithm.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "base/algorithm.h"
  8. #include "base/debug_log.h"
  9. #include <cfenv>
  10. namespace base {
  11. [[nodiscard]] double SafeRound(double value) {
  12. Expects(!std::isnan(value));
  13. if (const auto result = std::round(value); !std::isnan(result)) {
  14. return result;
  15. }
  16. const auto errors = std::fetestexcept(FE_ALL_EXCEPT);
  17. if (const auto result = std::round(value); !std::isnan(result)) {
  18. return result;
  19. }
  20. LOG(("Streaming Error: Got second NAN in std::round(%1), fe: %2."
  21. ).arg(value
  22. ).arg(errors));
  23. std::feclearexcept(FE_ALL_EXCEPT);
  24. if (const auto result = std::round(value); !std::isnan(result)) {
  25. return result;
  26. }
  27. Unexpected("NAN after third std::round.");
  28. }
  29. QString CleanAndSimplify(QString text) {
  30. for (auto &ch : text) {
  31. if (ch.unicode() < 32) {
  32. ch = QChar(' ');
  33. }
  34. }
  35. return text.simplified();
  36. }
  37. } // namespace base