[RFC PATCH 5/8] libcamera: base: utils: Use std::string_view
Laurent Pinchart
laurent.pinchart at ideasonboard.com
Mon Dec 16 00:02:03 CET 2024
Replace usage of const std::string references passed to class member
functions with std::string_view. This allows using static C string
literals in the callers without the overhead of constructing a
std::string instance.
As std::string can't be implicitly constructed from std::string_view, an
explicit construction is added at the end of dirname().
Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
---
include/libcamera/base/utils.h | 15 ++++++++-------
src/libcamera/base/utils.cpp | 14 +++++++-------
2 files changed, 15 insertions(+), 14 deletions(-)
diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
index dd012fd58501cd8d..3ef1c0e87383f8d2 100644
--- a/include/libcamera/base/utils.h
+++ b/include/libcamera/base/utils.h
@@ -15,6 +15,7 @@
#include <sstream>
#include <string.h>
#include <string>
+#include <string_view>
#include <sys/time.h>
#include <type_traits>
#include <utility>
@@ -38,7 +39,7 @@ namespace utils {
const char *basename(const char *path);
char *secure_getenv(const char *name);
-std::string dirname(const std::string &path);
+std::string dirname(std::string_view path);
template<typename T>
std::vector<typename T::key_type> map_keys(const T &map)
@@ -143,7 +144,7 @@ size_t strlcpy(char *dst, const char *src, size_t size);
#ifndef __DOXYGEN__
template<typename Container, typename UnaryOp>
-std::string join(const Container &items, const std::string &sep, UnaryOp op)
+std::string join(const Container &items, std::string_view sep, UnaryOp op)
{
std::ostringstream ss;
bool first = true;
@@ -162,7 +163,7 @@ std::string join(const Container &items, const std::string &sep, UnaryOp op)
}
template<typename Container>
-std::string join(const Container &items, const std::string &sep)
+std::string join(const Container &items, std::string_view sep)
{
std::ostringstream ss;
bool first = true;
@@ -181,7 +182,7 @@ std::string join(const Container &items, const std::string &sep)
}
#else
template<typename Container, typename UnaryOp>
-std::string join(const Container &items, const std::string &sep, UnaryOp op = nullptr);
+std::string join(const Container &items, std::string_view sep, UnaryOp op = nullptr);
#endif
namespace details {
@@ -189,7 +190,7 @@ namespace details {
class StringSplitter
{
public:
- StringSplitter(const std::string &str, const std::string &delim);
+ StringSplitter(std::string_view str, std::string_view delim);
class iterator
{
@@ -238,9 +239,9 @@ private:
} /* namespace details */
-details::StringSplitter split(const std::string &str, const std::string &delim);
+details::StringSplitter split(std::string_view str, std::string_view delim);
-std::string toAscii(const std::string &str);
+std::string toAscii(std::string_view str);
std::string libcameraBuildPath();
std::string libcameraSourcePath();
diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp
index bcfc1941a92a3d69..886aa9ecdc2bddfe 100644
--- a/src/libcamera/base/utils.cpp
+++ b/src/libcamera/base/utils.cpp
@@ -80,7 +80,7 @@ char *secure_getenv(const char *name)
*
* \return A string of the directory component of the path
*/
-std::string dirname(const std::string &path)
+std::string dirname(std::string_view path)
{
if (path.empty())
return ".";
@@ -116,7 +116,7 @@ std::string dirname(const std::string &path)
pos--;
}
- return path.substr(0, pos + 1);
+ return std::string(path.substr(0, pos + 1));
}
/**
@@ -247,7 +247,7 @@ size_t strlcpy(char *dst, const char *src, size_t size)
return strlen(src);
}
-details::StringSplitter::StringSplitter(const std::string &str, const std::string &delim)
+details::StringSplitter::StringSplitter(std::string_view str, std::string_view delim)
: str_(str), delim_(delim)
{
}
@@ -278,7 +278,7 @@ std::string details::StringSplitter::iterator::operator*() const
/**
* \fn template<typename Container, typename UnaryOp> \
- * std::string utils::join(const Container &items, const std::string &sep, UnaryOp op)
+ * std::string utils::join(const Container &items, std::string_view sep, UnaryOp op)
* \brief Join elements of a container in a string with a separator
* \param[in] items The container
* \param[in] sep The separator to add between elements
@@ -293,7 +293,7 @@ std::string details::StringSplitter::iterator::operator*() const
*/
/**
- * \fn split(const std::string &str, const std::string &delim)
+ * \fn split(std::string_view str, std::string_view delim)
* \brief Split a string based on a delimiter
* \param[in] str The string to split
* \param[in] delim The delimiter string
@@ -305,7 +305,7 @@ std::string details::StringSplitter::iterator::operator*() const
* \return An object that can be used in a range-based for loop to iterate over
* the substrings
*/
-details::StringSplitter split(const std::string &str, const std::string &delim)
+details::StringSplitter split(std::string_view str, std::string_view delim)
{
/** \todo Try to avoid copies of str and delim */
return details::StringSplitter(str, delim);
@@ -319,7 +319,7 @@ details::StringSplitter split(const std::string &str, const std::string &delim)
*
* \return A string equal to \a str stripped out of all non-ASCII characters
*/
-std::string toAscii(const std::string &str)
+std::string toAscii(std::string_view str)
{
std::string ret;
for (const char &c : str)
--
Regards,
Laurent Pinchart
More information about the libcamera-devel
mailing list