From d69310343cf9e1bffc5834efa0cac8ca4fa88a66 Mon Sep 17 00:00:00 2001 From: eidheim Date: Sat, 25 Nov 2017 12:12:26 +0100 Subject: [PATCH] Updated status_code.hpp --- status_code.hpp | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/status_code.hpp b/status_code.hpp index 51c8ddeb..07927274 100644 --- a/status_code.hpp +++ b/status_code.hpp @@ -72,8 +72,8 @@ namespace SimpleWeb { server_error_network_authentication_required }; - const inline std::vector> &status_codes() noexcept { - const static std::vector> status_codes = { + inline const std::map &status_code_strings() { + static const std::map status_code_strings = { {StatusCode::unknown, ""}, {StatusCode::information_continue, "100 Continue"}, {StatusCode::information_switching_protocols, "101 Switching Protocols"}, @@ -136,36 +136,30 @@ namespace SimpleWeb { {StatusCode::server_error_loop_detected, "508 Loop Detected"}, {StatusCode::server_error_not_extended, "510 Not Extended"}, {StatusCode::server_error_network_authentication_required, "511 Network Authentication Required"}}; - return status_codes; + return status_code_strings; } - inline StatusCode status_code(const std::string &status_code_str) noexcept { + inline StatusCode status_code(const std::string &status_code_string) noexcept { class StringToStatusCode : public std::unordered_map { public: StringToStatusCode() { - for(auto &status_code : SimpleWeb::status_codes()) + for(auto &status_code : status_code_strings()) emplace(status_code.second, status_code.first); } }; static StringToStatusCode string_to_status_code; - auto pos = string_to_status_code.find(status_code_str); + auto pos = string_to_status_code.find(status_code_string); if(pos == string_to_status_code.end()) return StatusCode::unknown; return pos->second; } - const inline std::string &status_code(StatusCode status_code_enum) noexcept { - class StatusCodeToString : public std::map { - public: - StatusCodeToString() { - for(auto &status_code : SimpleWeb::status_codes()) - emplace(status_code.first, status_code.second); - } - }; - static StatusCodeToString status_code_to_string; - auto pos = status_code_to_string.find(status_code_enum); - if(pos == status_code_to_string.end()) - return status_codes()[0].second; + inline const std::string &status_code(StatusCode status_code_enum) noexcept { + auto pos = status_code_strings().find(status_code_enum); + if(pos == status_code_strings().end()) { + static std::string empty_string; + return empty_string; + } return pos->second; } } // namespace SimpleWeb