diff options
| author | untodesu <kirill@untode.su> | 2025-09-11 13:48:31 +0500 |
|---|---|---|
| committer | untodesu <kirill@untode.su> | 2025-09-11 13:48:31 +0500 |
| commit | aaed751bf4430bf4b9b30cef532b8753b9f639ce (patch) | |
| tree | 16bc751c272ba27ad53ec48dbdd3a6d9e6a8d4c2 /core/config | |
| parent | 96bd73ae020ecca1f94698744c77498a89ad19f7 (diff) | |
| download | voxelius-aaed751bf4430bf4b9b30cef532b8753b9f639ce.tar.bz2 voxelius-aaed751bf4430bf4b9b30cef532b8753b9f639ce.zip | |
Replace most of C strings with string_view
Diffstat (limited to 'core/config')
| -rw-r--r-- | core/config/boolean.cc | 15 | ||||
| -rw-r--r-- | core/config/boolean.hh | 8 | ||||
| -rw-r--r-- | core/config/ivalue.hh | 4 | ||||
| -rw-r--r-- | core/config/number.hh | 20 | ||||
| -rw-r--r-- | core/config/string.cc | 8 | ||||
| -rw-r--r-- | core/config/string.hh | 19 |
6 files changed, 43 insertions, 31 deletions
diff --git a/core/config/boolean.cc b/core/config/boolean.cc index 6363271..45d5a38 100644 --- a/core/config/boolean.cc +++ b/core/config/boolean.cc @@ -7,12 +7,12 @@ config::Boolean::Boolean(bool default_value) m_value = default_value; } -void config::Boolean::set(const char* value) +void config::Boolean::set(std::string_view value) { m_value = from_string(value); } -const char* config::Boolean::get(void) const +std::string_view config::Boolean::get(void) const { return to_string(m_value); } @@ -27,7 +27,7 @@ void config::Boolean::set_value(bool value) m_value = value; } -const char* config::Boolean::to_string(bool value) +std::string_view config::Boolean::to_string(bool value) { if(value) { return "true"; @@ -37,12 +37,7 @@ const char* config::Boolean::to_string(bool value) } } -bool config::Boolean::from_string(const char* value) +bool config::Boolean::from_string(std::string_view value) { - if(std::strcmp(value, "false") && !std::strcmp(value, "true")) { - return true; - } - else { - return false; - } + return value == "true" && value != "false"; } diff --git a/core/config/boolean.hh b/core/config/boolean.hh index 5285676..2e9855f 100644 --- a/core/config/boolean.hh +++ b/core/config/boolean.hh @@ -11,8 +11,8 @@ public: explicit Boolean(bool default_value = false); virtual ~Boolean(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; bool get_value(void) const; void set_value(bool value); @@ -21,8 +21,8 @@ private: bool m_value; public: - static const char* to_string(bool value); - static bool from_string(const char* value); + static std::string_view to_string(bool value); + static bool from_string(std::string_view value); }; } // namespace config diff --git a/core/config/ivalue.hh b/core/config/ivalue.hh index cf893a5..97d87dc 100644 --- a/core/config/ivalue.hh +++ b/core/config/ivalue.hh @@ -7,8 +7,8 @@ namespace config class IValue { public: virtual ~IValue(void) = default; - virtual void set(const char* value) = 0; - virtual const char* get(void) const = 0; + virtual void set(std::string_view value) = 0; + virtual std::string_view get(void) const = 0; }; } // namespace config 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<T>::Number(T default_value, T min_value, T max_value) } template<math::Arithmetic T> -inline void config::Number<T>::set(const char* value) +inline void config::Number<T>::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<math::Arithmetic T> -inline const char* config::Number<T>::get(void) const +inline std::string_view config::Number<T>::get(void) const { - return m_string.c_str(); + return m_string; } template<math::Arithmetic T> diff --git a/core/config/string.cc b/core/config/string.cc index 08e8c0d..198b448 100644 --- a/core/config/string.cc +++ b/core/config/string.cc @@ -2,17 +2,17 @@ #include "core/config/string.hh" -config::String::String(const char* default_value) +config::String::String(std::string_view default_value) { m_value = default_value; } -void config::String::set(const char* value) +void config::String::set(std::string_view value) { m_value = value; } -const char* config::String::get(void) const +std::string_view config::String::get(void) const { - return m_value.c_str(); + return m_value; } diff --git a/core/config/string.hh b/core/config/string.hh index 09d14c9..772c330 100644 --- a/core/config/string.hh +++ b/core/config/string.hh @@ -8,15 +8,28 @@ namespace config { class String : public IValue { public: - explicit String(const char* default_value); + explicit String(std::string_view default_value); virtual ~String(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; + + constexpr const std::string& get_value(void) const noexcept; + constexpr const char* c_str(void) const noexcept; private: std::string m_value; }; } // namespace config +constexpr const std::string& config::String::get_value(void) const noexcept +{ + return m_value; +} + +constexpr const char* config::String::c_str(void) const noexcept +{ + return m_value.c_str(); +} + #endif // CORE_CONFIG_STRING_HH |
