Skip to content

Commit

Permalink
Improve string to int stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Dec 4, 2024
1 parent f34c45d commit a96fee8
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 231 deletions.
16 changes: 8 additions & 8 deletions CLI/cli_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1736,20 +1736,20 @@ static void unit_util_string()
{
assert(string::toInt<int>("1337", 0) == 1337);
assert(string::toInt<int>("1337.", 0) == 1337);
assert(string::toInt<int, string::TI_FULL>("1337", 0) == 1337);
assert(string::toInt<int, string::TI_FULL>("1337.", 0) == 0);
assert(string::toInt<int>("1337", 0, string::TI_FULL) == 1337);
assert(string::toInt<int>("1337.", 0, string::TI_FULL) == 0);
assert(string::hexToInt<int>("1337", 0) == 0x1337);
assert(string::hexToInt<int>("1337.", 0) == 0x1337);
assert(string::hexToInt<int, string::TI_FULL>("1337", 0) == 0x1337);
assert(string::hexToInt<int, string::TI_FULL>("1337.", 0) == 0);
assert(string::hexToInt<int>("1337", 0, string::TI_FULL) == 0x1337);
assert(string::hexToInt<int>("1337.", 0, string::TI_FULL) == 0);
assert(string::toInt<int>(L"1337", 0) == 1337);
assert(string::toInt<int>(L"1337.", 0) == 1337);
assert(string::toInt<int, string::TI_FULL>(L"1337", 0) == 1337);
assert(string::toInt<int, string::TI_FULL>(L"1337.", 0) == 0);
assert(string::toInt<int>(L"1337", 0, string::TI_FULL) == 1337);
assert(string::toInt<int>(L"1337.", 0, string::TI_FULL) == 0);
assert(string::hexToInt<int>(L"1337", 0) == 0x1337);
assert(string::hexToInt<int>(L"1337.", 0) == 0x1337);
assert(string::hexToInt<int, string::TI_FULL>(L"1337", 0) == 0x1337);
assert(string::hexToInt<int, string::TI_FULL>(L"1337.", 0) == 0);
assert(string::hexToInt<int>(L"1337", 0, string::TI_FULL) == 0x1337);
assert(string::hexToInt<int>(L"1337.", 0, string::TI_FULL) == 0);
});
test("truncateWithEllipsis", []
{
Expand Down
2 changes: 1 addition & 1 deletion soup/CidrSubnetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ NAMESPACE_SOUP
auto sep = str.find('/');
IpAddr addr;
SOUP_ASSERT(addr.fromString(str.substr(0, sep)));
auto size = string::toInt<uint8_t, string::TI_FULL>(str.substr(sep + 1));
auto size = string::toIntOpt<uint8_t>(str.substr(sep + 1), string::TI_FULL);
return construct(addr, size.value());
}

Expand Down
4 changes: 2 additions & 2 deletions soup/HttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ NAMESPACE_SOUP
if (auto len = self.resp.header_fields.find(ObfusString("Content-Length")); len != self.resp.header_fields.end())
{
self.status = BODY_LEN;
if (auto opt = string::toInt<uint64_t, string::TI_FULL>(len->second); opt.has_value())
if (auto opt = string::toIntOpt<uint64_t>(len->second, string::TI_FULL); opt.has_value())
{
self.bytes_remain = opt.value();
}
Expand Down Expand Up @@ -394,7 +394,7 @@ NAMESPACE_SOUP
{
break;
}
if (auto opt = string::hexToInt<uint64_t>(self.buf.substr(0, i)); opt.has_value())
if (auto opt = string::hexToIntOpt<uint64_t>(self.buf.substr(0, i)); opt.has_value())
{
self.bytes_remain = opt.value();
}
Expand Down
4 changes: 2 additions & 2 deletions soup/JsonString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ NAMESPACE_SOUP
++c;
if (c[0] && c[1] && c[2] && c[3])
{
if (char32_t w1; string::hexToInt<char32_t>(std::string(c, 4)).consume(w1))
if (char32_t w1; string::hexToIntOpt<char32_t>(std::string(c, 4)).consume(w1))
{
c += 4;
if ((w1 >> 10) == 0x36) // Surrogate pair?
{
if (c[0] == '\\' && c[1] == 'u' && c[2] && c[3] && c[4] && c[5])
{
c += 2;
if (char32_t w2; string::hexToInt<char32_t>(std::string(c, 4)).consume(w2))
if (char32_t w2; string::hexToIntOpt<char32_t>(std::string(c, 4)).consume(w2))
{
c += 4;
value.append(unicode::utf32_to_utf8(unicode::utf16_to_utf32(w1, w2)));
Expand Down
2 changes: 1 addition & 1 deletion soup/LangDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ NAMESPACE_SOUP
{
lexemes.emplace_back(Lexeme{ tk->keyword });
}
else if (c = lb.c_str(), opt = string::toIntOpt<int64_t>(c); opt.has_value() && *c == 0)
else if (c = lb.c_str(), opt = string::toIntEx<int64_t>(c, 0, &c); opt.has_value() && *c == 0)
{
lexemes.emplace_back(Lexeme{ Lexeme::VAL, opt.value() });
}
Expand Down
2 changes: 1 addition & 1 deletion soup/SocketAddr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ NAMESPACE_SOUP
{
ip.reset();
}
const auto opt = string::toInt<uint16_t, string::TI_FULL>(str.substr(sep + 1));
const auto opt = string::toIntOpt<uint16_t>(str.substr(sep + 1), string::TI_FULL);
SOUP_RETHROW_FALSE(opt.has_value());
port = Endianness::toNetwork(*opt);
return true;
Expand Down
2 changes: 1 addition & 1 deletion soup/Uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ NAMESPACE_SOUP
host = uri.substr(0, port_sep);
const char* pPort = &uri.at(port_sep + 1);
const char* i = pPort;
port = string::toIntImpl<uint16_t>(i);
port = string::toIntEx<uint16_t>(i, 0, &i).value_or(0);
++i;

uri.erase(0, port_sep + (i - pPort));
Expand Down
2 changes: 1 addition & 1 deletion soup/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ NAMESPACE_SOUP
{
if (is_int)
{
auto opt = string::toInt<int64_t>(buf);
auto opt = string::toIntOpt<int64_t>(buf);
if (opt.has_value())
{
return soup::make_unique<JsonInt>(opt.value());
Expand Down
14 changes: 7 additions & 7 deletions soup/netIntel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ NAMESPACE_SOUP
continue;
}
netAs as;
as.number = string::toInt<uint32_t>(line.substr(0, asn_sep)).value();
as.number = string::toIntOpt<uint32_t>(line.substr(0, asn_sep)).value();
++asn_sep;
auto handle_sep = line.find(',', asn_sep);
as.handle = as_pool.emplace(line.substr(asn_sep, handle_sep - asn_sep));
Expand Down Expand Up @@ -149,13 +149,13 @@ NAMESPACE_SOUP
{
continue;
}
uint32_t asn = string::toInt<uint32_t>(arr.at(2)).value();
uint32_t asn = string::toIntOpt<uint32_t>(arr.at(2)).value();
if (asn == 0)
{
continue;
}
auto begin = string::toInt<uint32_t>(arr.at(0)).value();
auto end = string::toInt<uint32_t>(arr.at(1)).value();
auto begin = string::toIntOpt<uint32_t>(arr.at(0)).value();
auto end = string::toIntOpt<uint32_t>(arr.at(1)).value();
const netAs* as = getAsByNumber(asn);
if (as == nullptr)
{
Expand All @@ -180,7 +180,7 @@ NAMESPACE_SOUP
{
continue;
}
uint32_t asn = string::toInt<uint32_t>(arr.at(2)).value();
uint32_t asn = string::toIntOpt<uint32_t>(arr.at(2)).value();
if (asn == 0)
{
continue;
Expand Down Expand Up @@ -214,8 +214,8 @@ NAMESPACE_SOUP
continue;
}
ipv4tolocation.emplace(
string::toInt<uint32_t>(arr.at(0)).value(),
string::toInt<uint32_t>(arr.at(1)).value(),
string::toIntOpt<uint32_t>(arr.at(0)).value(),
string::toIntOpt<uint32_t>(arr.at(1)).value(),
netIntelLocationData{
std::move(arr.at(2)),
location_pool.emplace(std::move(arr.at(3))),
Expand Down
Loading

0 comments on commit a96fee8

Please sign in to comment.