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 88f5050 commit 5a07c52
Showing 1 changed file with 30 additions and 64 deletions.
94 changes: 30 additions & 64 deletions poseidon/http/http_field_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,14 @@
namespace poseidon {
namespace {

struct ci_reader_str
constexpr ROCKET_ALWAYS_INLINE
uint8_t
do_lchar(char c) noexcept
{
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;
}
};
unsigned ch = static_cast<unsigned char>(c);
ch |= (('A' - ch - 1) & (ch - 'Z' - 1) & 0x100) >> 3;
return static_cast<uint8_t>(ch);
}

} // namespace

Expand All @@ -70,40 +27,49 @@ int
HTTP_Field_Name::
compare(const char* cmps) const noexcept
{
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;
for(size_t k = 0; k != this->m_str.size(); ++k)
if(cmps[k] == 0)
return 1;
else if(int diff = do_lchar(this->m_str[k]) - do_lchar(cmps[k]))
return diff;

if(*(cmps + this->m_str.size()) != 0)
return -1;

return 0;
}

int
HTTP_Field_Name::
compare(cow_stringR cmps) const noexcept
{
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;
for(size_t k = 0; k != this->m_str.size(); ++k)
if(k == cmps.size())
return 1;
else if(int diff = do_lchar(this->m_str[k]) - do_lchar(cmps[k]))
return diff;

if(this->m_str.size() != cmps.size())
return -1;

return 0;
}

int
HTTP_Field_Name::
compare(const HTTP_Field_Name& other) const noexcept
{
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;
return this->compare(other.m_str);
}

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;
while(rs1.get() != -1) hval = (hval ^ (uint32_t) rs1.ch) * 16777619;
for(size_t k = 0; k != this->m_str.size(); ++k)
hval = (hval ^ do_lchar(this->m_str[k])) * 16777619;
return hval;
}

Expand Down

0 comments on commit 5a07c52

Please sign in to comment.