From aaed751bf4430bf4b9b30cef532b8753b9f639ce Mon Sep 17 00:00:00 2001 From: untodesu Date: Thu, 11 Sep 2025 13:48:31 +0500 Subject: Replace most of C strings with string_view --- core/config/number.hh | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'core/config/number.hh') diff --git a/core/config/number.hh b/core/config/number.hh index 9907993..ad79770 100644 --- a/core/config/number.hh +++ b/core/config/number.hh @@ -14,8 +14,8 @@ public: explicit Number(T default_value, T min_value, T max_value); virtual ~Number(void) = default; - virtual void set(const char* value) override; - virtual const char* get(void) const override; + virtual void set(std::string_view value) override; + virtual std::string_view get(void) const override; T get_value(void) const; void set_value(T value); @@ -79,17 +79,21 @@ inline config::Number::Number(T default_value, T min_value, T max_value) } template -inline void config::Number::set(const char* value) +inline void config::Number::set(std::string_view value) { - std::istringstream(value) >> m_value; - m_value = std::clamp(m_value, m_min_value, m_max_value); - m_string = std::to_string(m_value); + T parsed_value; + auto result = std::from_chars(value.data(), value.data() + value.size(), parsed_value); + + if(result.ec == std::errc()) { + m_value = std::clamp(parsed_value, m_min_value, m_max_value); + m_string = std::to_string(m_value); + } } template -inline const char* config::Number::get(void) const +inline std::string_view config::Number::get(void) const { - return m_string.c_str(); + return m_string; } template -- cgit