From f1a67347a6cf4b6d397fe1220b999ed7161c7097 Mon Sep 17 00:00:00 2001
From: Chris Robinson <chris.kcat@gmail.com>
Date: Fri, 25 Aug 2023 14:52:09 -0700
Subject: Use string_view in a couple more places

---
 al/eax/exception.cpp | 43 ++++++++++++-------------------------------
 1 file changed, 12 insertions(+), 31 deletions(-)

(limited to 'al/eax/exception.cpp')

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;
 }
-- 
cgit v1.2.3


From ee62638a0c1033b858de28c7cb1f9365d7cb1c56 Mon Sep 17 00:00:00 2001
From: Chris Robinson <chris.kcat@gmail.com>
Date: Sat, 26 Aug 2023 00:52:02 -0700
Subject: Simplify building a string message

---
 al/eax/exception.cpp | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)

(limited to 'al/eax/exception.cpp')

diff --git a/al/eax/exception.cpp b/al/eax/exception.cpp
index cd32e11a..e4945d88 100644
--- a/al/eax/exception.cpp
+++ b/al/eax/exception.cpp
@@ -19,20 +19,12 @@ std::string EaxException::make_message(std::string_view context, std::string_vie
     if(context.empty() && message.empty())
         return what;
 
-    static constexpr char left_prefix[] = "[";
-    static constexpr auto left_prefix_size = std::string::traits_type::length(left_prefix);
-
-    static constexpr char right_prefix[] = "] ";
-    static constexpr auto right_prefix_size = std::string::traits_type::length(right_prefix);
-
-    what.reserve((!context.empty() ? left_prefix_size + context.size() + right_prefix_size : 0) +
-        message.length() + 1);
-
+    what.reserve((!context.empty() ? context.size() + 3 : 0) + message.length() + 1);
     if(!context.empty())
     {
-        what.append(left_prefix, left_prefix_size);
+        what += "[";
         what += context;
-        what.append(right_prefix, right_prefix_size);
+        what += "] ";
     }
     what += message;
 
-- 
cgit v1.2.3