diff options
author | Chris Robinson <[email protected]> | 2023-08-25 14:52:09 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2023-08-25 14:52:09 -0700 |
commit | f1a67347a6cf4b6d397fe1220b999ed7161c7097 (patch) | |
tree | da48b7cd24ef0622e6c47f4bb368ac21bb71d776 /al | |
parent | ca3bc1bd80fdff511e83d563a4ee94d6cd885473 (diff) |
Use string_view in a couple more places
Diffstat (limited to 'al')
-rw-r--r-- | al/eax/exception.cpp | 43 | ||||
-rw-r--r-- | al/eax/exception.h | 6 | ||||
-rw-r--r-- | al/eax/utils.cpp | 6 | ||||
-rw-r--r-- | al/eax/utils.h | 3 |
4 files changed, 20 insertions, 38 deletions
diff --git a/al/eax/exception.cpp b/al/eax/exception.cpp index 435e7442..cd32e11a 100644 --- a/al/eax/exception.cpp +++ b/al/eax/exception.cpp @@ -6,54 +6,35 @@ #include <string> -EaxException::EaxException(const char *context, const char *message) +EaxException::EaxException(std::string_view context, std::string_view message) : std::runtime_error{make_message(context, message)} { } EaxException::~EaxException() = default; -std::string EaxException::make_message(const char *context, const char *message) +std::string EaxException::make_message(std::string_view context, std::string_view message) { - const auto context_size = (context ? std::string::traits_type::length(context) : 0); - const auto has_contex = (context_size > 0); - - const auto message_size = (message ? std::string::traits_type::length(message) : 0); - const auto has_message = (message_size > 0); - - if (!has_contex && !has_message) - { - return std::string{}; - } + auto what = std::string{}; + if(context.empty() && message.empty()) + return what; static constexpr char left_prefix[] = "["; - const auto left_prefix_size = std::string::traits_type::length(left_prefix); + static constexpr auto left_prefix_size = std::string::traits_type::length(left_prefix); static constexpr char right_prefix[] = "] "; - const auto right_prefix_size = std::string::traits_type::length(right_prefix); + static constexpr auto right_prefix_size = std::string::traits_type::length(right_prefix); - const auto what_size = - ( - has_contex ? - left_prefix_size + context_size + right_prefix_size : - 0) + - message_size + - 1; + what.reserve((!context.empty() ? left_prefix_size + context.size() + right_prefix_size : 0) + + message.length() + 1); - auto what = std::string{}; - what.reserve(what_size); - - if (has_contex) + if(!context.empty()) { what.append(left_prefix, left_prefix_size); - what.append(context, context_size); + what += context; what.append(right_prefix, right_prefix_size); } - - if (has_message) - { - what.append(message, message_size); - } + what += message; return what; } diff --git a/al/eax/exception.h b/al/eax/exception.h index 3ae88cdc..336654f0 100644 --- a/al/eax/exception.h +++ b/al/eax/exception.h @@ -1,16 +1,16 @@ #ifndef EAX_EXCEPTION_INCLUDED #define EAX_EXCEPTION_INCLUDED - #include <stdexcept> #include <string> +#include <string_view> class EaxException : public std::runtime_error { - static std::string make_message(const char *context, const char *message); + static std::string make_message(std::string_view context, std::string_view message); public: - EaxException(const char *context, const char *message); + EaxException(std::string_view context, std::string_view message); ~EaxException() override; }; // EaxException diff --git a/al/eax/utils.cpp b/al/eax/utils.cpp index b3ed6ca1..53599ac5 100644 --- a/al/eax/utils.cpp +++ b/al/eax/utils.cpp @@ -8,7 +8,7 @@ #include "core/logging.h" -void eax_log_exception(const char *message) noexcept +void eax_log_exception(std::string_view message) noexcept { const auto exception_ptr = std::current_exception(); assert(exception_ptr); @@ -18,9 +18,9 @@ void eax_log_exception(const char *message) noexcept } catch(const std::exception& ex) { const auto ex_message = ex.what(); - ERR("%s %s\n", message ? message : "", ex_message); + ERR("%.*s %s\n", static_cast<int>(message.length()), message.data(), ex_message); } catch(...) { - ERR("%s %s\n", message ? message : "", "Generic exception."); + ERR("%.*s %s\n", static_cast<int>(message.length()), message.data(), "Generic exception."); } } diff --git a/al/eax/utils.h b/al/eax/utils.h index 8ff75a18..e5581bd6 100644 --- a/al/eax/utils.h +++ b/al/eax/utils.h @@ -4,6 +4,7 @@ #include <algorithm> #include <cstdint> #include <string> +#include <string_view> #include <type_traits> using EaxDirtyFlags = unsigned int; @@ -13,7 +14,7 @@ struct EaxAlLowPassParam { float gain_hf; }; -void eax_log_exception(const char *message) noexcept; +void eax_log_exception(std::string_view message) noexcept; template<typename TException, typename TValue> void eax_validate_range( |