From 05d80f9b3283d7686a27e5d3ed0bac6086669368 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 6 Aug 2023 01:29:04 -0700 Subject: More properly print string_views --- al/debug.cpp | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/al/debug.cpp b/al/debug.cpp index af67859a..2d8819f3 100644 --- a/al/debug.cpp +++ b/al/debug.cpp @@ -174,8 +174,8 @@ void ALCcontext::sendDebugMessage(std::unique_lock &debuglock, Debug if(message.length() >= MaxDebugMessageLength) UNLIKELY { - ERR("Debug message too long (%zu >= %d):\n-> %s\n", message.length(), - MaxDebugMessageLength, message.data()); + ERR("Debug message too long (%zu >= %d):\n-> %.*s\n", message.length(), + MaxDebugMessageLength, static_cast(message.length()), message.data()); return; } @@ -214,9 +214,10 @@ void ALCcontext::sendDebugMessage(std::unique_lock &debuglock, Debug " Type: %s\n" " ID: %u\n" " Severity: %s\n" - " Message: \"%s\"\n", + " Message: \"%.*s\"\n", GetDebugSourceName(source), GetDebugTypeName(type), id, - GetDebugSeverityName(severity), message.data()); + GetDebugSeverityName(severity), static_cast(message.length()), + message.data()); } } @@ -245,21 +246,8 @@ FORCE_ALIGN void AL_APIENTRY alDebugMessageInsertDirectEXT(ALCcontext *context, return context->setError(AL_INVALID_VALUE, "Debug message too long (%d >= %d)", length, MaxDebugMessageLength); - std::string tmpmessage; - std::string_view msgview; - if(length < 0) - msgview = message; - /* Testing if the message is null terminated like this is kind of ugly, but - * it's the only way to avoid an otherwise unnecessary copy since the - * callback and trace calls need a null-terminated message string. - */ - else if(message[length] == '\0') - msgview = {message, static_cast(length)}; - else - { - tmpmessage.assign(message, static_cast(length)); - msgview = tmpmessage; - } + auto msgview = (length < 0) ? std::string_view{message} + : std::string_view{message, static_cast(length)}; if(msgview.length() >= MaxDebugMessageLength) UNLIKELY return context->setError(AL_INVALID_VALUE, "Debug message too long (%zu >= %d)", -- cgit v1.2.3