From 2d572ca660a799f924b4ea13688cc7ea083a5159 Mon Sep 17 00:00:00 2001 From: LIU Hao Date: Tue, 11 Jun 2024 12:57:28 +0800 Subject: [PATCH] http_field_name: Cleanup --- poseidon/http/http_field_name.cpp | 97 ++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 27 deletions(-) diff --git a/poseidon/http/http_field_name.cpp b/poseidon/http/http_field_name.cpp index d49dd0e6..f66ecda4 100644 --- a/poseidon/http/http_field_name.cpp +++ b/poseidon/http/http_field_name.cpp @@ -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() @@ -15,41 +70,30 @@ 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(::rocket::ascii_to_lower(this->m_str[k])); - if(cmps[k] != 0) - c2 = static_cast(::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(::rocket::ascii_to_lower(this->m_str[k])); - if(k != cmps.size()) - c2 = static_cast(::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 @@ -57,10 +101,9 @@ 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(::rocket::ascii_to_lower(this->m_str[k]))) - * 16777619; + while(rs1.get() != -1) hval = (hval ^ (uint32_t) rs1.ch) * 16777619; return hval; } @@ -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: