From 290d6da0ef2cea3e6bfc3aa36f3ec379ac5f8d9b Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Fri, 2 Jul 2021 22:36:08 -0700 Subject: [PATCH] :fire::art::racehorse: removed substr_view after learning string_view::substr is O(1) --- CMakeLists.txt | 1 - include/admin_filter.h | 1 - include/substr_view.h | 16 ---------------- src/admin_filter.cpp | 20 ++++++++++---------- src/substr_view.cpp | 31 ------------------------------- src/user_filter.cpp | 20 ++++++++++---------- 6 files changed, 20 insertions(+), 69 deletions(-) delete mode 100644 include/substr_view.h delete mode 100644 src/substr_view.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c35160e..cf5bd9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,6 @@ target_sources(${PROJECT_NAME} PRIVATE src/bank.cpp src/change_flag.cpp src/log.cpp - src/substr_view.cpp src/transaction.cpp src/user_filter.cpp src/user.cpp diff --git a/include/admin_filter.h b/include/admin_filter.h index ab51585..6cb4f2f 100644 --- a/include/admin_filter.h +++ b/include/admin_filter.h @@ -1,7 +1,6 @@ #pragma once #include #include -#include "substr_view.h" #include "bank.h" using namespace drogon; diff --git a/include/substr_view.h b/include/substr_view.h deleted file mode 100644 index af3e394..0000000 --- a/include/substr_view.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include -#include - -class substr_view -{ -private: - const char *begin; - size_t end; - -public: - substr_view(std::string_view str, size_t begin_init = 0, size_t end_init = 0) noexcept; - bool operator==(const std::string &str) const noexcept; - const char *data() const noexcept; - std::string_view str_view() const noexcept; -}; \ No newline at end of file diff --git a/src/admin_filter.cpp b/src/admin_filter.cpp index 18bdab0..4b7c4d3 100644 --- a/src/admin_filter.cpp +++ b/src/admin_filter.cpp @@ -6,24 +6,24 @@ void AdminFilter::doFilter(const HttpRequestPtr &req, FilterCallback &&fcb, FilterChainCallback &&fccb) { - const std::string &auth_header = req->getHeader("Authorization"); + const std::string_view &auth_header = req->getHeader("Authorization"); if (auth_header.size() > 6) { - if (substr_view(auth_header, 0, 6) == "Basic ") + if (auth_header.substr(0, 6) == "Basic ") { - //only one alloc for this entire thing! - char base64_result[((auth_header.size() - 6) * 3) / 4]; + std::string_view base64_input = auth_header.substr(6); + char base64_result[(base64_input.size() * 3) / 4]; size_t new_sz; - base64_decode(substr_view(auth_header, 6).data(), auth_header.size() - 6, base64_result, &new_sz, 0); + base64_decode(base64_input.data(), base64_input.size(), base64_result, &new_sz, 0); - std::size_t res = std::string_view(base64_result, new_sz).find(':'); - if (res != std::string::npos) + std::string_view results_view(base64_result, new_sz); + std::size_t middle = results_view.find(':'); + if (middle != std::string::npos) { - std::string_view username = substr_view(base64_result, 0, res).str_view(); - std::string_view password = substr_view(base64_result, res + 1, new_sz).str_view(); + std::string_view username = results_view.substr(0, middle); + std::string_view password = results_view.substr(middle + 1); if (bank.AdminVerifyAccount(username)) { - //another alloc if (bank.VerifyPassword(username, password)) { fccb(); diff --git a/src/substr_view.cpp b/src/substr_view.cpp deleted file mode 100644 index b39d508..0000000 --- a/src/substr_view.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "substr_view.h" - -substr_view::substr_view(std::string_view str, size_t begin_init, size_t end_init) noexcept -{ - begin = str.begin() + begin_init; - if (!end_init) - { - end = str.size() - begin_init; - } - else - { - end = end_init - begin_init; - } -} -bool substr_view::operator==(const std::string &str) const noexcept -{ - if (str.size() != end) - { - return false; - } - for (size_t i = 0; i < end; ++i) - { - if (*(begin + end) == str[i]) - { - return false; - } - } - return true; -} -const char *substr_view::data() const noexcept { return begin; } -std::string_view substr_view::str_view() const noexcept { return std::string_view(data(), end); } \ No newline at end of file diff --git a/src/user_filter.cpp b/src/user_filter.cpp index f87f8a9..71392bb 100644 --- a/src/user_filter.cpp +++ b/src/user_filter.cpp @@ -6,22 +6,22 @@ void UserFilter::doFilter(const HttpRequestPtr &req, FilterCallback &&fcb, FilterChainCallback &&fccb) { - const std::string &auth_header = req->getHeader("Authorization"); + std::string_view auth_header = req->getHeader("Authorization"); if (auth_header.size() > 6) { - if (substr_view(auth_header, 0, 6) == "Basic ") + if (auth_header.substr(0, 6) == "Basic ") { - //only one alloc for this entire thing! - char base64_result[((auth_header.size() - 6) * 3) / 4]; + std::string_view base64_input = auth_header.substr(6); + char base64_result[(base64_input.size() * 3) / 4]; //only alloc size_t new_sz; - base64_decode(substr_view(auth_header, 6).data(), auth_header.size() - 6, base64_result, &new_sz, 0); + base64_decode(base64_input.data(), base64_input.size(), base64_result, &new_sz, 0); - std::size_t res = std::string_view(base64_result, new_sz).find(':'); - if (res != std::string::npos) + std::string_view results_view(base64_result, new_sz); + std::size_t middle = results_view.find(':'); + if (middle != std::string::npos) { - std::string_view username = substr_view(base64_result, 0, res).str_view(); - std::string_view password = substr_view(base64_result, res + 1, new_sz).str_view(); - //another alloc + std::string_view username = results_view.substr(0, middle); + std::string_view password = results_view.substr(middle + 1); if (bank.VerifyPassword(username, password)) { fccb();