Skip to content

Commit 81d7b78

Browse files
committed
feat:1.12.17, 完善StringTo()函数,并使用StringTo()对已有模块进行优化
1 parent b58f1d7 commit 81d7b78

File tree

6 files changed

+52
-33
lines changed

6 files changed

+52
-33
lines changed

modules/main/log.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <tbox/terminal/helper.h>
2727
#include <tbox/util/fs.h>
2828
#include <tbox/util/json.h>
29+
#include <tbox/util/string_to.h>
2930

3031
namespace tbox {
3132
namespace main {
@@ -388,7 +389,7 @@ void Log::installShellForSink(log::Sink &sink, terminal::NodeToken parent_node,
388389
do {
389390
auto &module_id = args[1];
390391
int level = 0;
391-
if (CatchThrowQuietly([&] { level = std::stoi(args[2]); })) {
392+
if (!util::StringTo(args[2], level)) {
392393
oss << "level must be number\r\n";
393394
break;
394395
}

modules/terminal/helper.cpp

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "helper.h"
2222

2323
#include <sstream>
24-
#include <tbox/base/catch_throw.h>
24+
#include <tbox/util/string_to.h>
2525

2626
namespace tbox {
2727
namespace terminal {
@@ -64,16 +64,7 @@ NodeToken AddFuncNode(TerminalNodes &terminal, NodeToken parent_node,
6464

6565
} else if (a.size() == 2u) {
6666
auto &str = a[1];
67-
if (str == "true" || str == "True" || str == "TRUE" ||
68-
str == "on" || str == "On" || str == "ON") {
69-
value = true;
70-
is_ok = true;
71-
72-
} else if (str == "false" || str == "False" || str == "FALSE" ||
73-
str == "off" || str == "Off" || str == "OFF") {
74-
value = false;
75-
is_ok = true;
76-
}
67+
is_ok = util::StringTo(str, value);
7768
}
7869

7970
if (is_ok) {
@@ -136,7 +127,7 @@ NodeToken AddFuncNode(TerminalNodes &terminal, NodeToken parent_node,
136127
} else if (a.size() == 2u) {
137128
auto &str = a[1];
138129
int new_value = 0;
139-
if (!CatchThrowQuietly([&] { new_value = std::stoi(str); })) {
130+
if (util::StringTo(str, new_value)) {
140131
if (new_value >= min_value && new_value <= max_value) {
141132
value = new_value;
142133
is_ok = true;
@@ -177,7 +168,7 @@ NodeToken AddFuncNode(TerminalNodes &terminal, NodeToken parent_node,
177168
} else if (a.size() == 2u) {
178169
auto &str = a[1];
179170
double new_value = 0;
180-
if (!CatchThrowQuietly([&] { new_value = std::stod(str); })) {
171+
if (util::StringTo(str, new_value)) {
181172
if (new_value >= min_value && new_value <= max_value) {
182173
value = new_value;
183174
is_ok = true;
@@ -264,16 +255,7 @@ NodeToken AddFuncNode(TerminalNodes &terminal, NodeToken parent_node, const std:
264255
} else if (a.size() == 2u && profile.set_func) {
265256
auto &str = a[1];
266257
bool new_value = false;
267-
if (str == "true" || str == "True" || str == "TRUE" ||
268-
str == "on" || str == "On" || str == "ON") {
269-
new_value = true;
270-
is_ok = true;
271-
272-
} else if (str == "false" || str == "False" || str == "FALSE" ||
273-
str == "off" || str == "Off" || str == "OFF") {
274-
new_value = false;
275-
is_ok = true;
276-
}
258+
is_ok = util::StringTo(str, new_value);
277259

278260
if (is_ok) {
279261
if (profile.set_func(new_value)) {
@@ -322,7 +304,7 @@ NodeToken AddFuncNode(TerminalNodes &terminal, NodeToken parent_node, const std:
322304
} else if (a.size() == 2u && profile.set_func) {
323305
auto &str = a[1];
324306
int new_value = 0;
325-
CatchThrowQuietly([&] { new_value = std::stoi(str); is_ok = true; });
307+
is_ok = util::StringTo(str, new_value);
326308
if (is_ok) {
327309
if (new_value < profile.min_value || new_value > profile.max_value) {
328310
oss << "fail, out of range.\r\n";
@@ -387,7 +369,7 @@ NodeToken AddFuncNode(TerminalNodes &terminal, NodeToken parent_node, const std:
387369
} else if (a.size() == 2u && profile.set_func) {
388370
auto &str = a[1];
389371
double new_value = 0;
390-
CatchThrowQuietly([&] { new_value = std::stod(str); is_ok = true; });
372+
is_ok = util::StringTo(str, new_value);
391373
if (is_ok) {
392374
if (new_value < profile.min_value || new_value > profile.max_value) {
393375
oss << "fail, out of range.\r\n";

modules/util/string_to.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "string_to.h"
2222

2323
#include <tbox/base/defines.h>
24+
#include <tbox/base/log.h>
2425
#include "string.h"
2526

2627
namespace tbox {
@@ -29,12 +30,18 @@ namespace util {
2930
bool StringTo(const std::string &text, bool &value)
3031
{
3132
const char *text_tbl[] = {
33+
"TRUE", "FALSE",
3234
"YES", "NO",
33-
"YEP", "NOPE",
35+
"ON", "OFF",
36+
"1", "0",
37+
"ENABLE", "DISABLE",
3438
"Y", "N",
35-
"TRUE", "FALSE",
3639
"T", "F",
37-
"ON", "OFF",
40+
"YEP", "NOPE",
41+
"ACTIVE", "INACTIVE",
42+
"POSITIVE", "NEGATIVE",
43+
"POS", "NEG",
44+
"+", "-",
3845
};
3946

4047
auto upper_text = string::ToUpper(text);
@@ -45,6 +52,7 @@ bool StringTo(const std::string &text, bool &value)
4552
}
4653
}
4754

55+
LogNotice("can't convert '%s' to bool", text.c_str());
4856
return false;
4957
}
5058

@@ -56,7 +64,9 @@ bool StringTo(const std::string &text, bool &value)
5664
value = tmp; \
5765
return true; \
5866
} \
59-
} catch (...) { } \
67+
} catch (...) { \
68+
LogNotice("can't convert '%s' to number", text.c_str()); \
69+
} \
6070
return false
6171

6272
#define TO_NUMBER_WITH_BASE(func) \
@@ -67,7 +77,9 @@ bool StringTo(const std::string &text, bool &value)
6777
value = tmp; \
6878
return true; \
6979
} \
70-
} catch (...) { } \
80+
} catch (...) { \
81+
LogNotice("can't convert '%s' to integer with base %d", text.c_str(), base); \
82+
} \
7183
return false
7284

7385
bool StringTo(const std::string &text, int &value, int base)
@@ -110,5 +122,11 @@ bool StringTo(const std::string &text, double &value)
110122
TO_NUMBER(std::stod);
111123
}
112124

125+
bool StringTo(const std::string &text, std::string &value)
126+
{
127+
value = text;
128+
return true;
129+
}
130+
113131
}
114132
}

modules/util/string_to.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@
3131
namespace tbox {
3232
namespace util {
3333

34-
bool StringTo(const std::string &text, bool &value); //! 解析bool值
34+
/**
35+
* 解析字串为bool值
36+
*
37+
* 真值: true, yes, on, 1, enable, y, t, yep, active, positive, pos, +
38+
* 假值: false, no, off, 0, disable, n, f, nope, inactive, negative, neg, -
39+
*
40+
* 大小写不敏感
41+
*/
42+
bool StringTo(const std::string &text, bool &value);
3543

3644
bool StringTo(const std::string &text, int &value, int base = 10); //! 解析int值
3745
bool StringTo(const std::string &text, long &value, int base = 10); //! 解析long值
@@ -43,6 +51,8 @@ bool StringTo(const std::string &text, unsigned long long &value, int base = 10)
4351
bool StringTo(const std::string &text, float &value); //! 解析float值
4452
bool StringTo(const std::string &text, double &value); //! 解析double值
4553

54+
bool StringTo(const std::string &text, std::string &value);
55+
4656
}
4757
}
4858

modules/util/string_to_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ TEST(StringTo, Bool)
4141
EXPECT_TRUE(StringTo("On", value));
4242
EXPECT_TRUE(value);
4343

44+
value = false;
45+
EXPECT_TRUE(StringTo("1", value));
46+
EXPECT_TRUE(value);
47+
4448
value = true;
4549
EXPECT_TRUE(StringTo("no", value));
4650
EXPECT_FALSE(value);
@@ -53,6 +57,10 @@ TEST(StringTo, Bool)
5357
EXPECT_TRUE(StringTo("False", value));
5458
EXPECT_FALSE(value);
5559

60+
value = true;
61+
EXPECT_TRUE(StringTo("0", value));
62+
EXPECT_FALSE(value);
63+
5664
EXPECT_FALSE(StringTo("", value));
5765
EXPECT_FALSE(StringTo("A", value));
5866
EXPECT_FALSE(StringTo("343", value));

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 := 16
24+
TBOX_VERSION_REVISION := 17

0 commit comments

Comments
 (0)