Commit a91904c2 authored by Joshua Ashton's avatar Joshua Ashton 🐸

[util] Implement parsing ratios from string views

parent fcc5a6c8
#include <numeric>
#include <algorithm>
#include <cstdint>
#include <string>
#include <charconv>
namespace dxvk {
......@@ -13,15 +15,39 @@ namespace dxvk {
public:
Ratio(T num, T denom) {
const T gcd = std::gcd(num, denom);
set(num, denom);
}
Ratio(std::string_view view) {
set(0, 0);
size_t colon = view.find(":");
if (colon == std::string_view::npos)
return;
std::string_view numStr = view.substr(0, colon);
std::string_view denomStr = view.substr(colon + 1);
m_num = num / gcd;
m_denom = denom / gcd;
T num = 0, denom = 0;
std::from_chars(numStr.data(), numStr.data() + numStr.size(), num);
std::from_chars(denomStr.data(), denomStr.data() + denomStr.size(), denom);
set(num, denom);
}
inline T num() const { return m_num; }
inline T denom() const { return m_denom; }
inline bool undefined() const { return m_denom == 0; }
inline void set(T num, T denom) {
const T gcd = std::gcd(num, denom);
m_num = num / gcd;
m_denom = denom / gcd;
}
inline bool operator == (const Ratio& other) const {
return num() == other.num() && denom() == other.denom();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment