From 79c5087ca57de629badb72580a296231b242d849 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 12 Jan 2020 15:27:11 +0100 Subject: [PATCH] Marked some functions noexcept. --- include/instance.hpp | 14 +++--- src/instance.cpp | 114 +++++++++++++++++++++++++++---------------- 2 files changed, 79 insertions(+), 49 deletions(-) diff --git a/include/instance.hpp b/include/instance.hpp index 0667a83..7f74c46 100644 --- a/include/instance.hpp +++ b/include/instance.hpp @@ -61,7 +61,7 @@ public: * @since 0.1.0 */ [[nodiscard]] - inline string_view get_hostname() const + inline string_view get_hostname() const noexcept { return _hostname; } @@ -74,7 +74,7 @@ public: * @since 0.1.0 */ [[nodiscard]] - inline string_view get_baseuri() const + inline string_view get_baseuri() const noexcept { return _baseuri; } @@ -85,7 +85,7 @@ public: * @since 0.1.0 */ [[nodiscard]] - inline string_view get_access_token() const + inline string_view get_access_token() const noexcept { return _access_token; } @@ -115,7 +115,7 @@ public: * @since 0.1.0 */ [[nodiscard]] - uint64_t get_max_chars(); + uint64_t get_max_chars() noexcept; /*! @copydoc CURLWrapper::set_proxy(string_view) * @@ -136,7 +136,7 @@ public: * @since 0.1.0 */ [[nodiscard]] - string_view get_proxy() const + string_view get_proxy() const noexcept { return _proxy; } @@ -164,7 +164,7 @@ public: * * @since 0.3.0 */ - vector get_post_formats(); + vector get_post_formats() noexcept; /*! * @brief Set path to Certificate Authority (CA) bundle. @@ -187,7 +187,7 @@ public: * * @since 0.2.1 */ - string_view get_cainfo() + string_view get_cainfo() const noexcept { return _cainfo; } diff --git a/src/instance.cpp b/src/instance.cpp index 172489c..cc8ffeb 100644 --- a/src/instance.cpp +++ b/src/instance.cpp @@ -35,46 +35,48 @@ Instance::Instance(const string_view hostname, const string_view access_token) , _max_chars{0} {} -uint64_t Instance::get_max_chars() +uint64_t Instance::get_max_chars() noexcept { constexpr uint64_t default_max_chars{500}; - if (_max_chars == 0) + if (_max_chars != 0) { - try - { - debuglog << "Querying " << _hostname << " for max_toot_chars…\n"; - const auto answer{make_request(http_method::GET, - _baseuri + "/api/v1/instance", {})}; - if (!answer) - { - debuglog << "Could not get instance info.\n"; - return default_max_chars; - } + return _max_chars; + } - _max_chars = [&answer] - { - auto &body{answer.body}; - size_t pos_start{body.find("max_toot_chars")}; - if (pos_start == string::npos) - { - debuglog << "max_toot_chars not found.\n"; - return default_max_chars; - } - pos_start = body.find(':', pos_start) + 1; - const size_t pos_end{body.find(',', pos_start)}; - - const auto max_toot_chars{body.substr(pos_start, - pos_end - pos_start)}; - return static_cast(stoull(max_toot_chars)); - }(); - debuglog << "Set _max_chars to: " << _max_chars << '\n'; - } - catch (const exception &e) + try + { + debuglog << "Querying " << _hostname << " for max_toot_chars…\n"; + const auto answer{make_request(http_method::GET, + _baseuri + "/api/v1/instance", {})}; + if (!answer) { - debuglog << "Unexpected exception: " << e.what() << '\n'; + debuglog << "Could not get instance info.\n"; return default_max_chars; } + + _max_chars = [&answer] + { + auto &body{answer.body}; + size_t pos_start{body.find("max_toot_chars")}; + if (pos_start == string::npos) + { + debuglog << "max_toot_chars not found.\n"; + return default_max_chars; + } + pos_start = body.find(':', pos_start) + 1; + const size_t pos_end{body.find(',', pos_start)}; + + const auto max_toot_chars{body.substr(pos_start, + pos_end - pos_start)}; + return static_cast(stoull(max_toot_chars)); + }(); + debuglog << "Set _max_chars to: " << _max_chars << '\n'; + } + catch (const exception &e) + { + debuglog << "Unexpected exception: " << e.what() << '\n'; + return default_max_chars; } return _max_chars; @@ -107,7 +109,7 @@ answer_type Instance::get_nodeinfo() return make_request(http_method::GET, hrefs.back(), {}); } -vector Instance::get_post_formats() +vector Instance::get_post_formats() noexcept { constexpr auto default_value{"text/plain"}; @@ -116,18 +118,46 @@ vector Instance::get_post_formats() return _post_formats; } - debuglog << "Querying " << _hostname << " for postFormats…\n"; - const auto answer{get_nodeinfo()}; - if (!answer) + try { - debuglog << "Couldn't get NodeInfo.\n"; - _post_formats = {default_value}; - return _post_formats; + debuglog << "Querying " << _hostname << " for postFormats…\n"; + const auto answer{get_nodeinfo()}; + if (!answer) + { + debuglog << "Couldn't get NodeInfo.\n"; + _post_formats = {default_value}; + return _post_formats; + } + + constexpr string_view searchstring{R"("postFormats":[)"}; + auto pos{answer.body.find(searchstring)}; + if (pos == string::npos) + { + debuglog << "Couldn't find metadata.postFormats.\n"; + _post_formats = {default_value}; + return _post_formats; + } + pos += searchstring.size(); + auto endpos{answer.body.find("],", pos)}; + string formats{answer.body.substr(pos, endpos - pos)}; + debuglog << "Extracted postFormats: " << formats << '\n'; + + while ((pos = formats.find('"', 1)) != string::npos) + { + _post_formats.push_back(formats.substr(1, pos - 1)); + formats.erase(0, pos + 2); // 2 is the length of: ", + debuglog << "Found postFormat: " << _post_formats.back() << '\n'; + } + } + catch (const std::exception &e) + { + debuglog << "Unexpected exception: " << e.what() << '\n'; + return {default_value}; } - constexpr string_view searchstring{R"("postFormats":[)"}; - auto pos{answer.body.find(searchstring)}; - if (pos == string::npos) + return _post_formats; +} + { debuglog << "Couldn't find metadata.postFormats.\n"; _post_formats = {default_value};