Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix http message #250

Merged
merged 6 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions common/estring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,33 @@ size_t estring_view::find_last_not_of(const charset& set) const

bool estring_view::to_uint64_check(uint64_t* v) const
{
v ? (*v = 0) : 0;
uint64_t val = 0;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplify the syntax

for (unsigned char c : *this) {
if (c > '9' || c < '0')
c -= '0';
if (c > 9)
return false;
v ? (*v = *v * 10 + (c - '0')) : 0;
val = val * 10 + c;
}
if (v) *v = val;
return true;
}

uint64_t estring_view::hex_to_uint64() const
{
uint64_t estring_view::hex_to_uint64() const {
uint64_t ret = 0;
for (unsigned char c : *this) {
if (c >= '0' && c <= '9') {
ret = ret * 16 + (c - '0');
} else if (c >= 'A' && c <= 'F') {
ret = ret * 16 + (c - 'A' + 10);
} else if (c >= 'a' && c <= 'f') {
ret = ret * 16 + (c - 'a' + 10);
unsigned char cc = c - '0';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reducing branches in machine code

if (cc < 10) {
ret = ret * 16 + cc;
} else {
return ret;
const unsigned char mask = 'a' - 'A';
static_assert(mask == 32, "..."); // single digit
c |= mask; // unified to 'a'..'f'
cc = c - 'a';
if (cc < 6) {
ret = ret * 16 + cc + 10;
} else {
break; // invalid char
}
}
}
return ret;
Expand Down
34 changes: 30 additions & 4 deletions common/estring.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
#include <cstdio>
#include <cassert>
#include <cstring>
#include <math.h>
#include <limits>
#include <string>
#include <bitset>
Expand Down Expand Up @@ -289,8 +290,33 @@ class estring_view : public std::string_view
uint64_t to_uint64(uint64_t default_val = 0) const
{
uint64_t val;
to_uint64_check(&val);
return val;
return to_uint64_check(&val) ? val : default_val;
}
bool to_int64_check(int64_t* v = nullptr) const
{
if (this->empty()) return false;
if (this->front() != '-') return to_uint64_check((uint64_t*)v);
bool ret = this->substr(1).to_uint64_check((uint64_t*)v);
if (ret) *v = -*v;
return ret;
}
int64_t to_int64(int64_t default_val = 0) const
{
int64_t val;
return to_int64_check(&val) ? val : default_val;
}
bool to_double_check(double* v = nullptr)
{
char buf[32];
auto len = std::max(this->size(), sizeof(buf) - 1 );
memcpy(buf, data(), len);
buf[len] = '0';
return sscanf(buf, "%lf", v) == 1;
}
double to_double(double default_val = NAN)
{
double val;
return to_double_check(&val) ? val : default_val;
}
// do not support 0x/0X prefix
uint64_t hex_to_uint64() const;
Expand All @@ -312,8 +338,8 @@ class rstring_view // relative string_view, that stores values relative
protected:
static_assert(std::is_integral<OffsetType>::value, "...");
static_assert(std::is_integral<LengthType>::value, "...");
OffsetType _offset;
LengthType _length;
OffsetType _offset = 0;
LengthType _length = 0;

estring_view to_abs(const char* s) const
{
Expand Down
9 changes: 9 additions & 0 deletions common/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,15 @@ TEST(estring, test)
estring as = " \tasdf \t\r\n";
auto trimmed = as.trim();
EXPECT_EQ(trimmed, "asdf");

EXPECT_EQ(estring_view("234423").to_uint64(), 234423);
EXPECT_EQ(estring_view("-234423").to_int64(), -234423);
EXPECT_EQ(estring_view("asfdsf").to_uint64(32), 32);
EXPECT_EQ(estring_view("-3.14").to_double(), -3.14);
EXPECT_EQ(estring_view("1e10").to_double(), 1e10);

EXPECT_EQ(estring_view("1").hex_to_uint64(), 0x1);
EXPECT_EQ(estring_view("1a2b3d4e5f").hex_to_uint64(), 0x1a2b3d4e5f);
}

TEST(generator, example)
Expand Down
2 changes: 1 addition & 1 deletion net/http/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ int Request::redirect(Verb v, estring_view location, bool enable_proxy) {
}
StoredURL u(location);
auto new_request_line_size = verbstr[v].size() + sizeof(" HTTP/1.1\r\n") +
enable_proxy ? full_url_size(u) : u.target().size();
(enable_proxy ? full_url_size(u) : u.target().size());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops...


auto delta = new_request_line_size - m_buf_size;
LOG_DEBUG(VALUE(delta));
Expand Down
Loading