From 254ecffacf151c934f8a08a6be93e66d5ab1d54a Mon Sep 17 00:00:00 2001 From: Mirko Bonadei Date: Wed, 16 Jan 2019 12:14:29 +0100 Subject: [PATCH] Using absl::string_view to stringify an RTCErrorType. Bug: webrtc:10198 Change-Id: Ie7fdba08df219a03ebe2ee5521c2840f28571bba Reviewed-on: https://webrtc-review.googlesource.com/c/117162 Reviewed-by: Karl Wiberg Commit-Queue: Mirko Bonadei Cr-Commit-Position: refs/heads/master@{#26277} --- api/rtc_error.cc | 8 ++++---- api/rtc_error.h | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/api/rtc_error.cc b/api/rtc_error.cc index eccc891e1b..51fd07f699 100644 --- a/api/rtc_error.cc +++ b/api/rtc_error.cc @@ -10,11 +10,12 @@ #include "api/rtc_error.h" +#include "absl/strings/string_view.h" #include "rtc_base/arraysize.h" namespace { -static const char* const kRTCErrorTypeNames[] = { +const absl::string_view kRTCErrorTypeNames[] = { "NONE", "UNSUPPORTED_OPERATION", "UNSUPPORTED_PARAMETER", @@ -52,10 +53,9 @@ void RTCError::set_message(std::string message) { message_ = std::move(message); } -// TODO(jonasolsson): Change to use absl::string_view when it's available. -std::string ToString(RTCErrorType error) { +absl::string_view ToString(RTCErrorType error) { int index = static_cast(error); - return std::string(kRTCErrorTypeNames[index]); + return kRTCErrorTypeNames[index]; } } // namespace webrtc diff --git a/api/rtc_error.h b/api/rtc_error.h index d8db1f2cbb..673a79a2a1 100644 --- a/api/rtc_error.h +++ b/api/rtc_error.h @@ -17,6 +17,7 @@ #include #include // For std::move. +#include "absl/strings/string_view.h" #include "rtc_base/checks.h" #include "rtc_base/logging.h" @@ -128,8 +129,9 @@ class RTCError { // Outputs the error as a friendly string. Update this method when adding a new // error type. // -// Only intended to be used for logging/disagnostics. -std::string ToString(RTCErrorType error); +// Only intended to be used for logging/diagnostics. The string_view points +// to literal string that lives for the whole duration of the program. +absl::string_view ToString(RTCErrorType error); #ifdef UNIT_TEST inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)