Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lihuiba committed Nov 16, 2023
1 parent c8ab60d commit fb53a2f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
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;
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';
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());

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

0 comments on commit fb53a2f

Please sign in to comment.