Skip to content

Commit

Permalink
misc: reduce object copying.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashigeru committed Jul 17, 2024
1 parent 05f6009 commit 75e2bf2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions include/takatori/datetime/conversion.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <optional>
#include <string>
#include <string_view>

#include <takatori/util/either.h>

Expand All @@ -23,7 +23,7 @@ using conversion_result = util::either<std::string, T>;
* @return the parsed date information
* @return an error message if the conversion was failed
*/
[[nodiscard]] conversion_result<date_info> parse_date(std::string const& contents);
[[nodiscard]] conversion_result<date_info> parse_date(std::string_view contents);

/**
* @brief parses the given contents as a time of day.
Expand All @@ -32,7 +32,7 @@ using conversion_result = util::either<std::string, T>;
* @return the parsed time of day information
* @return an error message if the conversion was failed
*/
[[nodiscard]] conversion_result<time_info> parse_time(std::string const& contents);
[[nodiscard]] conversion_result<time_info> parse_time(std::string_view contents);

/**
* @brief parses the given contents as a datetime, with or without zone offset.
Expand All @@ -41,7 +41,7 @@ using conversion_result = util::either<std::string, T>;
* @return the parsed datetime information with or without zone offset
* @return an error message if the conversion was failed
*/
[[nodiscard]] conversion_result<datetime_info> parse_datetime(std::string const& contents);
[[nodiscard]] conversion_result<datetime_info> parse_datetime(std::string_view contents);

/**
* @brief parses the given contents as a zone offset.
Expand All @@ -50,6 +50,6 @@ using conversion_result = util::either<std::string, T>;
* @return the parsed zone offset
* @return an error message if the conversion was failed
*/
[[nodiscard]] conversion_result<zone_offset_info> parse_zone_offset(std::string const& contents);
[[nodiscard]] conversion_result<zone_offset_info> parse_zone_offset(std::string_view contents);

} // namespace takatori::datetime
8 changes: 4 additions & 4 deletions src/takatori/datetime/conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ std::optional<std::string> require_offset(std::optional<zone_offset_info> const&

} // namespace

conversion_result<date_info> parse_date(std::string const& contents) {
conversion_result<date_info> parse_date(std::string_view contents) {
parser::parser p {};
auto result = p(contents);
if (!result) {
Expand All @@ -91,7 +91,7 @@ conversion_result<date_info> parse_date(std::string const& contents) {
return *info.date;
}

conversion_result<time_info> parse_time(std::string const& contents) {
conversion_result<time_info> parse_time(std::string_view contents) {
parser::parser p {};
auto result = p(contents);
if (!result) {
Expand All @@ -110,7 +110,7 @@ conversion_result<time_info> parse_time(std::string const& contents) {
return *info.time;
}

conversion_result<datetime_info> parse_datetime(std::string const& contents) {
conversion_result<datetime_info> parse_datetime(std::string_view contents) {
parser::parser p {};
auto result = p(contents);
if (!result) {
Expand All @@ -136,7 +136,7 @@ conversion_result<datetime_info> parse_datetime(std::string const& contents) {
return datetime_info { *info.date, *info.time, info.offset };
}

conversion_result<zone_offset_info> parse_zone_offset(std::string const& contents) {
conversion_result<zone_offset_info> parse_zone_offset(std::string_view contents) {
parser::parser p {};
auto result = p(contents);
if (!result) {
Expand Down
6 changes: 3 additions & 3 deletions src/takatori/datetime/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ parser &parser::set_debug(int level) noexcept {
return *this;
}

parser::result_type parser::operator()(std::string const& contents) const {
parser::result_type parser::operator()(std::string_view contents) const {
std::istringstream input {
contents,
std::string { contents },
};
driver driver {};
scanner scanner { input };
Expand All @@ -30,7 +30,7 @@ parser::result_type parser::operator()(std::string const& contents) const {
return std::move(driver.result());
}

parser::result_type parse(std::string const& contents, parser const& engine) {
parser::result_type parse(std::string_view contents, parser const& engine) {
return engine(contents);
}

Expand Down
6 changes: 3 additions & 3 deletions src/takatori/datetime/parser/parser.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <optional>
#include <string>
#include <string_view>
#include <variant>

#include <takatori/util/either.h>
Expand All @@ -19,12 +19,12 @@ class parser {

parser& set_debug(int level = 1) noexcept;

[[nodiscard]] result_type operator()(std::string const& contents) const;
[[nodiscard]] result_type operator()(std::string_view contents) const;

private:
int debug_ {};
};

parser::result_type parse(std::string const& contents, parser const& engine = {});
parser::result_type parse(std::string_view contents, parser const& engine = {});

} // namespace takatori::datetime::parser

0 comments on commit 75e2bf2

Please sign in to comment.