Replace string_view with string

In datatypes.cpp function to_timestamp() the
istringstream needs a string, so passing
a string_view around is pointless.
This commit is contained in:
King_DuckZ 2020-09-04 00:05:19 +01:00
parent 8ca23eb221
commit 1cb40fd5ba
6 changed files with 17 additions and 23 deletions

View file

@ -30,7 +30,7 @@ Header to_header (const nap::HttpResponse& resp) {
Header ret; Header ret;
for (const auto& entry : resp.header) { for (const auto& entry : resp.header) {
if (entry.first == "Date") if (entry.first == "Date")
ret.date = from_header_timestamp(entry.second); ret.date = from_header_timestamp(std::string(entry.second));
else if (entry.first == "X-RateLimit-Limit") else if (entry.first == "X-RateLimit-Limit")
ret.rate_limit = int_conv<decltype(ret.rate_limit)>(entry.second); ret.rate_limit = int_conv<decltype(ret.rate_limit)>(entry.second);
else if (entry.first == "X-RateLimit-Remaining") else if (entry.first == "X-RateLimit-Remaining")

View file

@ -22,8 +22,8 @@
#include "date/date.h" #include "date/date.h"
namespace oro { namespace oro {
Timestamp& Timestamp::operator= (const std::string& str) { Timestamp& Timestamp::operator= (std::string&& str) {
ts = from_json_timestamp(str); ts = from_json_timestamp(std::move(str));
return *this; return *this;
} }

View file

@ -25,7 +25,7 @@ namespace oro {
typedef std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds> timestamp_t; typedef std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds> timestamp_t;
struct Timestamp { struct Timestamp {
Timestamp& operator= (const std::string& str); Timestamp& operator= (std::string&& str);
operator timestamp_t() const { return ts; } operator timestamp_t() const { return ts; }
timestamp_t ts; timestamp_t ts;

View file

@ -32,8 +32,8 @@ namespace {
} }
}; };
Timestamp to_timestamp (const char* fmt, std::string_view str) { Timestamp to_timestamp (const char* fmt, std::string&& str) {
std::istringstream iss{std::string(str)}; std::istringstream iss{std::move(str)};
Timestamp ts; Timestamp ts;
date::from_stream(iss, fmt, ts.ts); date::from_stream(iss, fmt, ts.ts);
return ts; return ts;
@ -45,29 +45,25 @@ namespace {
//For formatting modifiers see: //For formatting modifiers see:
//https://howardhinnant.github.io/date/date.html#from_stream_formatting //https://howardhinnant.github.io/date/date.html#from_stream_formatting
Timestamp from_json_timestamp (const std::string& str) { Timestamp from_json_timestamp (std::string&& str) {
if (not str.empty() and str.back() == 'Z') { if (not str.empty() and str.back() == 'Z') {
//date has this format: 2020-06-19T22:40:51Z //date has this format: 2020-06-19T22:40:51Z
return to_timestamp("%FT%TZ", str); return to_timestamp("%FT%TZ", std::move(str));
} }
else { else {
//date has this format: 2020-06-19T22:33:36.855672+00:00 //date has this format: 2020-06-19T22:33:36.855672+00:00
return to_timestamp("%FT%T%Ez", str); return to_timestamp("%FT%T%Ez", std::move(str));
} }
} }
Timestamp from_header_timestamp (const std::string& str) { Timestamp from_header_timestamp (std::string&& str) {
return from_header_timestamp(std::string_view(str));
}
Timestamp from_header_timestamp (std::string_view str) {
UniqueLocale loc(std::setlocale(LC_TIME, "POSIX")); UniqueLocale loc(std::setlocale(LC_TIME, "POSIX"));
//date has this format: Fri, 19 Jun 2020 22:33:43 GMT //date has this format: Fri, 19 Jun 2020 22:33:43 GMT
return to_timestamp("%a, %d %b %Y %T %Z", str); return to_timestamp("%a, %d %b %Y %T %Z", std::move(str));
} }
Timestamp from_sqlite_timestamp (const std::string& str) { Timestamp from_sqlite_timestamp (std::string&& str) {
return to_timestamp("%F %T", str); return to_timestamp("%F %T", std::move(str));
} }
std::string to_sqlite_string (const oro::Timestamp& ts) { std::string to_sqlite_string (const oro::Timestamp& ts) {

View file

@ -19,13 +19,11 @@
#include "oro/datatypes.hpp" #include "oro/datatypes.hpp"
#include <string> #include <string>
#include <string_view>
namespace oro { namespace oro {
Timestamp from_json_timestamp (const std::string& str); Timestamp from_json_timestamp (std::string&& str);
Timestamp from_header_timestamp (std::string_view str); Timestamp from_header_timestamp (std::string&& str);
Timestamp from_header_timestamp (const std::string& str); Timestamp from_sqlite_timestamp (std::string&& str);
Timestamp from_sqlite_timestamp (const std::string& str);
std::string to_sqlite_string (const Timestamp& ts); std::string to_sqlite_string (const Timestamp& ts);
std::string to_iso8601_string (const Timestamp& ts); std::string to_iso8601_string (const Timestamp& ts);
} //namespace oro } //namespace oro

View file

@ -556,7 +556,7 @@ Timestamp OriginsDBSQLite::next_access_time (DBOperation op) const {
if (not info) if (not info)
throw std::runtime_error("Query returned no value for access.operation " + to_string(static_cast<int>(op))); throw std::runtime_error("Query returned no value for access.operation " + to_string(static_cast<int>(op)));
return from_sqlite_timestamp(*info); return from_sqlite_timestamp(std::move(*info));
} }
} //namespace oro } //namespace oro