Use std::string_view in several APIs
Change-Id: I2c13b229162b247738cbf46d6bf71fc69d45816f
diff --git a/ndn-cxx/util/indented-stream.cpp b/ndn-cxx/util/indented-stream.cpp
index ba78f33..e178c13 100644
--- a/ndn-cxx/util/indented-stream.cpp
+++ b/ndn-cxx/util/indented-stream.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2018 Regents of the University of California.
+ * Copyright (c) 2013-2023 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -30,7 +30,7 @@
namespace ndn {
namespace util {
-IndentedStream::IndentedStream(std::ostream& os, const std::string& indent)
+IndentedStream::IndentedStream(std::ostream& os, std::string_view indent)
: std::ostream(&m_buffer)
, m_buffer(os, indent)
{
@@ -41,7 +41,7 @@
flush();
}
-IndentedStream::StreamBuf::StreamBuf(std::ostream& os, const std::string& indent)
+IndentedStream::StreamBuf::StreamBuf(std::ostream& os, std::string_view indent)
: m_output(os)
, m_indent(indent)
{
@@ -50,9 +50,9 @@
int
IndentedStream::StreamBuf::sync()
{
- typedef boost::iterator_range<std::string::const_iterator> StringView;
+ using StringView = boost::iterator_range<std::string::const_iterator>;
- const std::string& output = str();
+ std::string output = str();
std::vector<StringView> splitOutput;
boost::split(splitOutput, output, boost::is_any_of("\n"));
diff --git a/ndn-cxx/util/indented-stream.hpp b/ndn-cxx/util/indented-stream.hpp
index 7b05960..b2d5059 100644
--- a/ndn-cxx/util/indented-stream.hpp
+++ b/ndn-cxx/util/indented-stream.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2021 Regents of the University of California.
+ * Copyright (c) 2013-2023 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -54,7 +54,7 @@
class IndentedStream : public std::ostream
{
public:
- IndentedStream(std::ostream& os, const std::string& indent);
+ IndentedStream(std::ostream& os, std::string_view indent);
~IndentedStream() override;
@@ -63,7 +63,7 @@
class StreamBuf : public std::stringbuf
{
public:
- StreamBuf(std::ostream& os, const std::string& indent);
+ StreamBuf(std::ostream& os, std::string_view indent);
int
sync() override;
diff --git a/ndn-cxx/util/logger.cpp b/ndn-cxx/util/logger.cpp
index 4ebdf15..c9bea26 100644
--- a/ndn-cxx/util/logger.cpp
+++ b/ndn-cxx/util/logger.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2022 Regents of the University of California.
+ * Copyright (c) 2013-2023 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -22,8 +22,6 @@
#include "ndn-cxx/util/logger.hpp"
#include "ndn-cxx/util/logging.hpp"
-#include <cstring> // for std::strspn()
-
namespace ndn {
namespace util {
@@ -49,11 +47,11 @@
return os << "ALL";
}
- NDN_THROW(std::invalid_argument("unknown log level " + to_string(to_underlying(level))));
+ NDN_THROW(std::invalid_argument("unknown log level " + std::to_string(to_underlying(level))));
}
LogLevel
-parseLogLevel(const std::string& s)
+parseLogLevel(std::string_view s)
{
if (s == "FATAL")
return LogLevel::FATAL;
@@ -72,24 +70,19 @@
else if (s == "ALL")
return LogLevel::ALL;
- NDN_THROW(std::invalid_argument("unrecognized log level '" + s + "'"));
+ NDN_THROW(std::invalid_argument("unrecognized log level '" + std::string(s) + "'"));
}
-static bool
-isValidLoggerName(const std::string& name)
+static constexpr bool
+isValidLoggerName(std::string_view name)
{
+ if (name.empty() || name.front() == '.' || name.back() == '.' ||
+ name.find("..") != std::string_view::npos) {
+ return false;
+ }
// acceptable characters for Logger name
- const char* okChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~#%_<>.-";
- if (std::strspn(name.c_str(), okChars) != name.size()) {
- return false;
- }
- if (name.empty() || name.front() == '.' || name.back() == '.') {
- return false;
- }
- if (name.find("..") != std::string::npos) {
- return false;
- }
- return true;
+ constexpr std::string_view okChars{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~#%_<>.-"sv};
+ return name.find_first_not_of(okChars) == std::string_view::npos;
}
Logger::Logger(const char* name)
@@ -99,18 +92,17 @@
NDN_THROW(std::invalid_argument("Logger name '" + m_moduleName + "' is invalid"));
}
this->setLevel(LogLevel::NONE);
- this->add_attribute(log::module.get_name(), boost::log::attributes::constant<std::string>(m_moduleName));
+ this->add_attribute(log::module.get_name(), boost::log::attributes::constant(m_moduleName));
Logging::get().addLoggerImpl(*this);
}
void
Logger::registerModuleName(const char* name)
{
- std::string moduleName(name);
- if (!isValidLoggerName(moduleName)) {
- NDN_THROW(std::invalid_argument("Logger name '" + moduleName + "' is invalid"));
+ if (!isValidLoggerName(name)) {
+ NDN_THROW(std::invalid_argument("Logger name '"s + name + "' is invalid"));
}
- Logging::get().registerLoggerNameImpl(std::move(moduleName));
+ Logging::get().registerLoggerNameImpl(name);
}
} // namespace util
diff --git a/ndn-cxx/util/logger.hpp b/ndn-cxx/util/logger.hpp
index bdf1c07..3261b85 100644
--- a/ndn-cxx/util/logger.hpp
+++ b/ndn-cxx/util/logger.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2021 Regents of the University of California.
+ * Copyright (c) 2013-2023 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -37,7 +37,8 @@
namespace ndn {
namespace util {
-/** \brief Indicates the severity level of a log message.
+/**
+ * \brief Indicates the severity level of a log message.
*/
enum class LogLevel {
FATAL = -1, ///< fatal (will be logged unconditionally)
@@ -50,17 +51,19 @@
ALL = 255 ///< all messages
};
-/** \brief Output LogLevel as a string.
- * \throw std::invalid_argument unknown \p level
+/**
+ * \brief Output LogLevel as a string.
+ * \throw std::invalid_argument Unknown \p level
*/
std::ostream&
operator<<(std::ostream& os, LogLevel level);
-/** \brief Parse LogLevel from a string.
- * \throw std::invalid_argument unknown level name
+/**
+ * \brief Parse LogLevel from a string.
+ * \throw std::invalid_argument Unknown level name
*/
LogLevel
-parseLogLevel(const std::string& s);
+parseLogLevel(std::string_view s);
namespace log {
@@ -69,10 +72,11 @@
} // namespace log
-/** \brief Represents a log module in the logging facility.
+/**
+ * \brief Represents a log module in the logging facility.
*
- * \note Normally, loggers should be defined using #NDN_LOG_INIT, #NDN_LOG_MEMBER_INIT,
- * or #NDN_LOG_MEMBER_INIT_SPECIALIZED.
+ * \note Normally, loggers should be defined using #NDN_LOG_INIT, #NDN_LOG_MEMBER_INIT,
+ * or #NDN_LOG_MEMBER_INIT_SPECIALIZED.
*/
class Logger : public boost::log::sources::severity_logger_mt<LogLevel>
{
diff --git a/ndn-cxx/util/sha256.cpp b/ndn-cxx/util/sha256.cpp
index c789a7e..66355d6 100644
--- a/ndn-cxx/util/sha256.cpp
+++ b/ndn-cxx/util/sha256.cpp
@@ -92,7 +92,7 @@
}
Sha256&
-Sha256::operator<<(const std::string& str)
+Sha256::operator<<(std::string_view str)
{
update({reinterpret_cast<const uint8_t*>(str.data()), str.size()});
return *this;
diff --git a/ndn-cxx/util/sha256.hpp b/ndn-cxx/util/sha256.hpp
index 76a094e..2e7b09c 100644
--- a/ndn-cxx/util/sha256.hpp
+++ b/ndn-cxx/util/sha256.hpp
@@ -122,7 +122,7 @@
* @throw Error the digest has already been finalized
*/
Sha256&
- operator<<(const std::string& str);
+ operator<<(std::string_view str);
/**
* @brief Add a uint64_t value to the digest calculation.
diff --git a/ndn-cxx/util/string-helper.cpp b/ndn-cxx/util/string-helper.cpp
index 011f967..ef7f85b 100644
--- a/ndn-cxx/util/string-helper.cpp
+++ b/ndn-cxx/util/string-helper.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2021 Regents of the University of California.
+ * Copyright (c) 2013-2023 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -57,7 +57,7 @@
}
shared_ptr<Buffer>
-fromHex(const std::string& hexString)
+fromHex(std::string_view hexString)
{
namespace tr = security::transform;
@@ -73,18 +73,17 @@
}
std::string
-escape(const std::string& str)
+escape(std::string_view str)
{
std::ostringstream os;
- escape(os, str.data(), str.size());
+ escape(os, str);
return os.str();
}
void
-escape(std::ostream& os, const char* str, size_t len)
+escape(std::ostream& os, std::string_view str)
{
- for (size_t i = 0; i < len; ++i) {
- auto c = str[i];
+ for (auto c : str) {
// Unreserved characters don't need to be escaped.
if ((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
@@ -102,18 +101,18 @@
}
std::string
-unescape(const std::string& str)
+unescape(std::string_view str)
{
std::ostringstream os;
- unescape(os, str.data(), str.size());
+ unescape(os, str);
return os.str();
}
void
-unescape(std::ostream& os, const char* str, size_t len)
+unescape(std::ostream& os, std::string_view str)
{
- for (size_t i = 0; i < len; ++i) {
- if (str[i] == '%' && i + 2 < len) {
+ for (size_t i = 0; i < str.size(); ++i) {
+ if (str[i] == '%' && i + 2 < str.size()) {
int hi = fromHexChar(str[i + 1]);
int lo = fromHexChar(str[i + 2]);
diff --git a/ndn-cxx/util/string-helper.hpp b/ndn-cxx/util/string-helper.hpp
index 4447df4..9aa58c9 100644
--- a/ndn-cxx/util/string-helper.hpp
+++ b/ndn-cxx/util/string-helper.hpp
@@ -109,7 +109,7 @@
* @throw StringHelperError Input string is invalid
*/
shared_ptr<Buffer>
-fromHex(const std::string& hexString);
+fromHex(std::string_view hexString);
/**
* @brief Convert (the least significant nibble of) @p n to the corresponding hex character.
@@ -152,10 +152,17 @@
* @see RFC 3986 section 2
*/
[[nodiscard]] std::string
-escape(const std::string& str);
+escape(std::string_view str);
void
-escape(std::ostream& os, const char* str, size_t len);
+escape(std::ostream& os, std::string_view str);
+
+[[deprecated("use the string_view overload")]]
+inline void
+escape(std::ostream& os, const char* str, size_t len)
+{
+ escape(os, {str, len});
+}
/**
* @brief Decode a percent-encoded string.
@@ -172,10 +179,17 @@
* @see RFC 3986 section 2
*/
[[nodiscard]] std::string
-unescape(const std::string& str);
+unescape(std::string_view str);
void
-unescape(std::ostream& os, const char* str, size_t len);
+unescape(std::ostream& os, std::string_view str);
+
+[[deprecated("use the string_view overload")]]
+inline void
+unescape(std::ostream& os, const char* str, size_t len)
+{
+ unescape(os, {str, len});
+}
} // namespace ndn