Skip to content

Commit 6dad077

Browse files
committed
feat:1.12.19, 使用StringTo()优化工程中已有代码
1 parent 98bd962 commit 6dad077

File tree

8 files changed

+107
-10
lines changed

8 files changed

+107
-10
lines changed

modules/http/server/request_parser.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <tbox/base/defines.h>
2323
#include <tbox/base/log.h>
2424
#include <tbox/util/string.h>
25+
#include <tbox/util/string_to.h>
2526

2627
namespace tbox {
2728
namespace http {
@@ -139,8 +140,13 @@ size_t RequestParser::parse(const void *data_ptr, size_t data_size)
139140
auto head_value = util::string::Strip(str.substr(head_value_start_pos, head_value_end_pos - head_value_start_pos));
140141
sp_request_->headers[head_key] = head_value;
141142

142-
if (head_key == "Content-Length")
143-
content_length_ = std::stoi(head_value);
143+
if (head_key == "Content-Length") {
144+
if (!util::StringTo(head_value, content_length_)) {
145+
LogNotice("Content-Length should be number");
146+
state_ = State::kFail;
147+
return pos;
148+
}
149+
}
144150

145151
} else {
146152
sp_request_->headers[head_key] = "";

modules/network/sockaddr.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <arpa/inet.h>
2323

2424
#include <tbox/base/log.h>
25+
#include <tbox/util/string_to.h>
26+
2527
#include <sstream>
2628

2729
#include "sockaddr.h"
@@ -99,10 +101,10 @@ SockAddr SockAddr::FromString(const string &addr_str)
99101
auto ipv4_str = addr_str.substr(0, colon_pos) ;
100102
auto port_str = addr_str.substr(colon_pos + 1) ;
101103

102-
try {
103-
uint16_t port = stoi(port_str);
104+
uint16_t port = 0;
105+
if (util::StringTo(port_str, port)) {
104106
return SockAddr(IPAddress::FromString(ipv4_str), port);
105-
} catch (const std::exception &e) {
107+
} else {
106108
return SockAddr();
107109
}
108110
} else {

modules/network/uart.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <fcntl.h>
2424

2525
#include <tbox/base/log.h>
26+
#include <tbox/util/string_to.h>
2627

2728
namespace tbox {
2829
namespace network {
@@ -233,7 +234,10 @@ bool Uart::setMode(const std::string &mode_str)
233234
Mode mode;
234235

235236
//! 波特率
236-
mode.baudrate = stoi(baudrate_str);
237+
if (!util::StringTo(baudrate_str, mode.baudrate)) {
238+
LogErr("baudrate must be number");
239+
return false;
240+
}
237241

238242
//! 数据位
239243
if (data_bits_ch == '8')

modules/terminal/impl/terminal_commands.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <tbox/base/log.h>
2626
#include <tbox/util/split_cmdline.h>
2727
#include <tbox/util/string.h>
28+
#include <tbox/util/string_to.h>
2829
#include <tbox/event/loop.h>
2930

3031
#include "session_context.h"
@@ -336,8 +337,9 @@ bool Terminal::Impl::executeRunHistoryCmd(SessionContext *s, const Args &args)
336337
return execute(s);
337338
}
338339

339-
try {
340-
auto index = std::stoi(sub_cmd);
340+
size_t index = 0;
341+
342+
if (util::StringTo(sub_cmd, index)) {
341343
bool is_index_valid = false;
342344
if (index >= 0) {
343345
if (static_cast<size_t>(index) < s->history.size()) {
@@ -356,7 +358,8 @@ bool Terminal::Impl::executeRunHistoryCmd(SessionContext *s, const Args &args)
356358
return execute(s);
357359
} else
358360
s->wp_conn->send(s->token, "Error: index out of range.\r\n");
359-
} catch (const invalid_argument &e) {
361+
362+
} else {
360363
s->wp_conn->send(s->token, "Error: parse index fail.\r\n");
361364
}
362365

modules/util/string_to.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,56 @@ bool StringTo(const std::string &text, unsigned long long &value, int base)
114114
TO_NUMBER_WITH_BASE(std::stoull);
115115
}
116116

117+
#if __SIZEOF_INT__ > 4
118+
bool StringTo(const std::string &text, uint32_t &value, int base)
119+
{
120+
uint64_t tmp;
121+
122+
if (StringTo(text, tmp, base)) {
123+
if (tmp < 0x100000000lu) {
124+
value = static_cast<uint32_t>(tmp);
125+
return true;
126+
} else {
127+
LogWarn("number overflow, %u > 0xffffffff", tmp);
128+
}
129+
}
130+
131+
return false;
132+
}
133+
#endif
134+
135+
bool StringTo(const std::string &text, uint16_t &value, int base)
136+
{
137+
uint32_t tmp;
138+
139+
if (StringTo(text, tmp, base)) {
140+
if (tmp < 0x10000u) {
141+
value = static_cast<uint16_t>(tmp);
142+
return true;
143+
} else {
144+
LogWarn("number overflow, %u > 0xffff", tmp);
145+
}
146+
}
147+
148+
return false;
149+
}
150+
151+
bool StringTo(const std::string &text, uint8_t &value, int base)
152+
{
153+
uint16_t tmp;
154+
155+
if (StringTo(text, tmp, base)) {
156+
if (tmp < 0x100u) {
157+
value = static_cast<uint8_t>(tmp);
158+
return true;
159+
} else {
160+
LogWarn("number overflow, %u > 0xff", tmp);
161+
}
162+
}
163+
164+
return false;
165+
}
166+
117167
bool StringTo(const std::string &text, float &value)
118168
{
119169
TO_NUMBER(std::stof);

modules/util/string_to.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ bool StringTo(const std::string &text, unsigned int &value, int base = 10); //!
5050
bool StringTo(const std::string &text, unsigned long &value, int base = 10); //! 解析unsigned long值
5151
bool StringTo(const std::string &text, unsigned long long &value, int base = 10); //! 解析unsigned long long值
5252

53+
#if __SIZEOF_INT__ > 4
54+
bool StringTo(const std::string &text, uint32_t &value, int base = 10); //! 解析uint32值
55+
#endif
56+
bool StringTo(const std::string &text, uint16_t &value, int base = 10); //! 解析uint16值
57+
bool StringTo(const std::string &text, uint8_t &value, int base = 10); //! 解析uint8值
58+
5359
bool StringTo(const std::string &text, float &value); //! 解析float值
5460
bool StringTo(const std::string &text, double &value); //! 解析double值
5561

modules/util/string_to_test.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,32 @@ TEST(StringTo, long)
109109
EXPECT_FALSE(StringTo("", value));
110110
}
111111

112+
TEST(StringTo, uint16_t)
113+
{
114+
uint16_t value = 0;
115+
EXPECT_TRUE(StringTo("12345", value));
116+
EXPECT_EQ(value, 12345);
117+
118+
EXPECT_TRUE(StringTo("0xFFFF", value, 16));
119+
EXPECT_EQ(value, 0xFFFF);
120+
121+
EXPECT_FALSE(StringTo("0x10000", value, 16));
122+
EXPECT_FALSE(StringTo("65536", value));
123+
}
124+
125+
TEST(StringTo, uint8_t)
126+
{
127+
uint8_t value = 0;
128+
EXPECT_TRUE(StringTo("123", value));
129+
EXPECT_EQ(value, 123);
130+
131+
EXPECT_TRUE(StringTo("0xFF", value, 16));
132+
EXPECT_EQ(value, 0xFF);
133+
134+
EXPECT_FALSE(StringTo("0x100", value, 16));
135+
EXPECT_FALSE(StringTo("256", value));
136+
}
137+
112138
TEST(StringTo, Double)
113139
{
114140
double value = -1;

version.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
# TBOX版本号
2222
TBOX_VERSION_MAJOR := 1
2323
TBOX_VERSION_MINOR := 12
24-
TBOX_VERSION_REVISION := 18
24+
TBOX_VERSION_REVISION := 19

0 commit comments

Comments
 (0)