From d1d63e3517b5e7d68f0221c8708438ac8eae7a53 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Thu, 20 Oct 2022 19:40:33 -0400
Subject: [PATCH] utf8_utils: use string_view

Change-Id: Ib939740608d112626ccce0228726e758b07d22a3
---
 src/sip/sip_utils.cpp |  5 ++---
 src/utf8_utils.cpp    | 12 +++++-------
 src/utf8_utils.h      |  9 +++------
 3 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/src/sip/sip_utils.cpp b/src/sip/sip_utils.cpp
index 36ed4d3a00..b2fcc2dab6 100644
--- a/src/sip/sip_utils.cpp
+++ b/src/sip/sip_utils.cpp
@@ -109,14 +109,13 @@ parseDisplayName(const pjsip_name_addr* sip_name_addr)
     if (not sip_name_addr->display.ptr or not sip_name_addr->display.slen)
         return {};
 
-    std::string displayName {sip_name_addr->display.ptr,
-                             static_cast<size_t>(sip_name_addr->display.slen)};
+    auto displayName = as_view(sip_name_addr->display);
 
     // Filter out invalid UTF-8 characters to avoid getting kicked from D-Bus
     if (not utf8_validate(displayName))
         return utf8_make_valid(displayName);
 
-    return displayName;
+    return std::string(displayName);
 }
 
 std::string
diff --git a/src/utf8_utils.cpp b/src/utf8_utils.cpp
index ec92e7fc29..42fd505be9 100644
--- a/src/utf8_utils.cpp
+++ b/src/utf8_utils.cpp
@@ -237,21 +237,19 @@ utf8_validate_c_str(const char* str, ssize_t max_len, const char** end)
 }
 
 bool
-utf8_validate(const std::string& str)
+utf8_validate(std::string_view str)
 {
-    const char* p;
-
-    p = fast_validate(str.c_str());
+    const char* p = fast_validate_len(str.data(), str.size());
 
     return (*p == '\0');
 }
 
 std::string
-utf8_make_valid(const std::string& name)
+utf8_make_valid(std::string_view name)
 {
     ssize_t remaining_bytes = name.size();
     ssize_t valid_bytes;
-    const char* remainder = name.c_str();
+    const char* remainder = name.data();
     const char* invalid;
     char* str = NULL;
     char* pos;
@@ -289,7 +287,7 @@ utf8_make_valid(const std::string& name)
     pos += remaining_bytes;
 
     std::string answer(str, pos - str);
-    assert(utf8_validate_c_str(answer.c_str(), -1, NULL));
+    assert(utf8_validate(answer));
 
     delete[] str;
 
diff --git a/src/utf8_utils.h b/src/utf8_utils.h
index d05042898d..8f109729da 100644
--- a/src/utf8_utils.h
+++ b/src/utf8_utils.h
@@ -20,8 +20,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
  */
 
-#ifndef H_UTF8_UTILS
-#define H_UTF8_UTILS
+#pragma once
 
 #include <cstdlib>
 #include <string>
@@ -41,7 +40,7 @@ namespace jami {
  * Returns: true if the text was valid UTF-8
  */
 
-bool utf8_validate(const std::string& str);
+bool utf8_validate(std::string_view str);
 
 /**
  * utf8_make_valid:
@@ -53,8 +52,6 @@ bool utf8_validate(const std::string& str);
  *
  * Returns: a valid utf8 string.
  */
-std::string utf8_make_valid(const std::string& name);
+std::string utf8_make_valid(std::string_view str);
 
 } // namespace jami
-
-#endif // H_UTF8_UTILS
-- 
GitLab