Skip to content

Commit

Permalink
http_field_name: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lhmouse committed Jun 11, 2024
1 parent 229d8ec commit 2d572ca
Showing 1 changed file with 70 additions and 27 deletions.
97 changes: 70 additions & 27 deletions poseidon/http/http_field_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,61 @@
#include "http_field_name.hpp"
#include "../utils.hpp"
namespace poseidon {
namespace {

struct ci_reader_str
{
const char* cur;
const char* end;
int ch;

explicit
ci_reader_str(const cow_string& str) noexcept
{
this->cur = str.data();
this->end = str.data() + str.size();
}

int
get() noexcept
{
if(this->cur == this->end)
this->ch = -1;
else {
this->ch = (unsigned char) *(this->cur);
this->ch |= (('A' - this->ch - 1) & (this->ch - 'Z' - 1) & 0x100) >> 3;
this->cur ++;
}
return this->ch;
}
};

struct ci_reader_sz
{
const char* cur;
int ch;

explicit
ci_reader_sz(const char* str) noexcept
{
this->cur = str;
}

int
get() noexcept
{
this->ch = (unsigned char) *(this->cur);
if(this->ch == 0)
this->ch = -1;
else {
this->ch |= (('A' - this->ch - 1) & (this->ch - 'Z' - 1) & 0x100) >> 3;
this->cur ++;
}
return this->ch;
}
};

} // namespace

HTTP_Field_Name::
~HTTP_Field_Name()
Expand All @@ -15,52 +70,40 @@ int
HTTP_Field_Name::
compare(const char* cmps) const noexcept
{
for(size_t k = 0;; ++k) {
int c1 = -1, c2 = -1;
if(k != this->m_str.size())
c1 = static_cast<uint8_t>(::rocket::ascii_to_lower(this->m_str[k]));
if(cmps[k] != 0)
c2 = static_cast<uint8_t>(::rocket::ascii_to_lower(cmps[k]));
if(c1 != c2)
return c1 - c2;
if(c1 == -1)
return 0;
}
ci_reader_str rs1(this->m_str);
ci_reader_sz rs2(cmps);
while((rs1.get() == rs2.get()) && (rs1.ch != -1));
return rs1.ch - rs2.ch;
}

int
HTTP_Field_Name::
compare(cow_stringR cmps) const noexcept
{
for(size_t k = 0;; ++k) {
int c1 = -1, c2 = -1;
if(k != this->m_str.size())
c1 = static_cast<uint8_t>(::rocket::ascii_to_lower(this->m_str[k]));
if(k != cmps.size())
c2 = static_cast<uint8_t>(::rocket::ascii_to_lower(cmps[k]));
if(c1 != c2)
return c1 - c2;
if(c1 == -1)
return 0;
}
ci_reader_str rs1(this->m_str);
ci_reader_str rs2(cmps);
while((rs1.get() == rs2.get()) && (rs1.ch != -1));
return rs1.ch - rs2.ch;
}

int
HTTP_Field_Name::
compare(const HTTP_Field_Name& other) const noexcept
{
return this->compare(other.m_str);
ci_reader_str rs1(this->m_str);
ci_reader_str rs2(other.m_str);
while((rs1.get() == rs2.get()) && (rs1.ch != -1));
return rs1.ch - rs2.ch;
}

size_t
HTTP_Field_Name::
rdhash() const noexcept
{
// https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function#FNV-1a_hash
ci_reader_str rs1(this->m_str);
uint32_t hval = 2166136261;
for(size_t k = 0; k != this->m_str.size(); ++k)
hval = (hval ^ static_cast<uint8_t>(::rocket::ascii_to_lower(this->m_str[k])))
* 16777619;
while(rs1.get() != -1) hval = (hval ^ (uint32_t) rs1.ch) * 16777619;
return hval;
}

Expand All @@ -79,7 +122,7 @@ canonicalize()

case 'A' ... 'Z':
// Convert this letter to lowercase.
this->m_str.mut(k) = ::rocket::ascii_to_lower(this->m_str[k]);
this->m_str.mut(k) = (char) (this->m_str[k] | 0x20);
break;

default:
Expand Down

0 comments on commit 2d572ca

Please sign in to comment.