From adafd6efba14317a2297c399ccff68ad9352a770 Mon Sep 17 00:00:00 2001 From: fintarin Date: Wed, 27 Mar 2024 23:53:27 +0300 Subject: [PATCH] Pass string by value to Parser --- include/fintamath/core/MathObjectBody.hpp | 9 ++++---- include/fintamath/core/Parser.hpp | 25 ++++++++--------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/include/fintamath/core/MathObjectBody.hpp b/include/fintamath/core/MathObjectBody.hpp index 80b4ad729..8918b4b83 100644 --- a/include/fintamath/core/MathObjectBody.hpp +++ b/include/fintamath/core/MathObjectBody.hpp @@ -22,13 +22,12 @@ private: \ static Class##Parser &getParser(); \ \ public: \ - template String> \ - static auto parse(String &&str) { \ - return getParser().parse(std::forward(str)); \ + static auto parse(std::string str) { \ + return getParser().parse(std::move(str)); \ } \ \ - static auto parseFirst(const std::string &str) { \ - return getParser().parseFirst(str); \ + static auto parseFirst(std::string str) { \ + return getParser().parseFirst(std::move(str)); \ } \ \ template T> \ diff --git a/include/fintamath/core/Parser.hpp b/include/fintamath/core/Parser.hpp index 5f31a135e..f1fc3d6e9 100644 --- a/include/fintamath/core/Parser.hpp +++ b/include/fintamath/core/Parser.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -37,12 +38,12 @@ class Parser final { using StringToConstructorsMap = std::unordered_map>; - using GeneratorConstructor = std::function; + using GeneratorConstructor = std::function; using GeneratorConstructorVector = std::vector; public: - Generator parse(const std::string &str) const { + Generator parse(std::string str) const { if (const auto stringToConstructors = stringToConstructorsMap.find(str); stringToConstructors != stringToConstructorsMap.end()) { for (const auto &constructor : stringToConstructors->second) { co_yield constructor(); @@ -56,16 +57,8 @@ class Parser final { } } - Generator parse(std::string &&str) const { - return [](const Parser &parser, const std::string localStr) -> Generator { - for (auto &value : parser.parse(localStr)) { - co_yield std::move(value); - } - }(*this, std::move(str)); - } - - std::optional parseFirst(const std::string &str) const { - auto gener = parse(str); + std::optional parseFirst(std::string str) const { + auto gener = parse(std::move(str)); if (auto iter = gener.begin(); iter != gener.end()) { return std::move(*iter); @@ -89,9 +82,9 @@ class Parser final { template requires(!std::is_abstract_v && StringConstructable) void registerType() { - generatorConstructors.emplace_back([](const std::string &str) -> Generator { + generatorConstructors.emplace_back([](std::string str) -> Generator { try { - co_yield std::make_unique(str); + co_yield std::make_unique(std::move(str)); } catch (const InvalidInputException &) { // Go to the next constructor @@ -102,8 +95,8 @@ class Parser final { template requires(std::is_abstract_v) void registerType() { - generatorConstructors.emplace_back([](const std::string &str) -> Generator { - for (auto &value : Type::parse(str)) { + generatorConstructors.emplace_back([](std::string str) -> Generator { + for (auto &value : Type::parse(std::move(str))) { co_yield std::move(value); } });