mirror of
https://github.com/Expand-sys/CCash
synced 2026-03-22 20:47:10 +11:00
🔥🎨🐎 removed substr_view after learning string_view::substr is O(1)
This commit is contained in:
parent
fc762871ff
commit
290d6da0ef
6 changed files with 20 additions and 69 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
#pragma once
|
||||
#include <drogon/HttpFilter.h>
|
||||
#include <libbase64.h>
|
||||
#include "substr_view.h"
|
||||
#include "bank.h"
|
||||
|
||||
using namespace drogon;
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
#pragma once
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
|
||||
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;
|
||||
};
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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); }
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue