Skip to content

Commit 0cfc600

Browse files
committed
feat(util.string):1.10.14, 添加Join()字串拼接函数
1 parent e6244ce commit 0cfc600

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

modules/util/string.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ size_t Split(const std::string &src_str, const std::string sep, std::vector<std:
3131
size_t start_pos = 0;
3232
size_t end_pos = 0;
3333
str_vec.clear();
34+
3435
while (true) {
3536
end_pos = src_str.find(sep, start_pos);
3637
const std::string &str_chip = src_str.substr(start_pos, end_pos - start_pos);
@@ -39,6 +40,7 @@ size_t Split(const std::string &src_str, const std::string sep, std::vector<std:
3940
break;
4041
start_pos = end_pos + sep.size();
4142
}
43+
4244
return str_vec.size();
4345
}
4446

@@ -47,6 +49,7 @@ size_t SplitBySpace(const std::string &src_str, std::vector<std::string> &str_ve
4749
size_t start_pos = 0;
4850
size_t end_pos = 0;
4951
str_vec.clear();
52+
5053
while (true) {
5154
start_pos = src_str.find_first_not_of(" \t", end_pos);
5255
if (start_pos == std::string::npos)
@@ -57,14 +60,41 @@ size_t SplitBySpace(const std::string &src_str, std::vector<std::string> &str_ve
5760
if (end_pos == std::string::npos)
5861
break;
5962
}
63+
6064
return str_vec.size();
6165
}
6266

67+
std::string Join(const std::vector<std::string> &str_vec, const std::string &delimiter)
68+
{
69+
std::string tmp;
70+
71+
if (!str_vec.empty()) {
72+
//! 计算预留空间
73+
size_t reserve_length = delimiter.length() * (str_vec.size() - 1);
74+
for (const auto &str : str_vec)
75+
reserve_length += str.length();
76+
77+
tmp.reserve(reserve_length); //! 预留空间
78+
79+
bool is_need_insert_delimiter = false;
80+
for (const auto &str : str_vec) {
81+
if (is_need_insert_delimiter)
82+
tmp += delimiter;
83+
84+
tmp += str;
85+
is_need_insert_delimiter = true;
86+
}
87+
}
88+
89+
return tmp;
90+
}
91+
6392
std::string StripLeft(const std::string &orig_str)
6493
{
6594
size_t start_pos = orig_str.find_first_not_of(' ', 0);
6695
if (start_pos == std::string::npos)
6796
return std::string();
97+
6898
return orig_str.substr(start_pos);
6999
}
70100

@@ -73,6 +103,7 @@ std::string StripRight(const std::string &orig_str)
73103
size_t end_pos = orig_str.find_last_not_of(' ', orig_str.size() - 1);
74104
if (end_pos == std::string::npos)
75105
return std::string();
106+
76107
return orig_str.substr(0, end_pos + 1);
77108
}
78109

@@ -93,6 +124,7 @@ std::string StripQuot(const std::string &orig_str)
93124
{
94125
auto first_char = orig_str.front();
95126
auto last_char = orig_str.back();
127+
96128
if (first_char == last_char && (first_char == '\'' || first_char == '\"')) {
97129
return orig_str.substr(1, orig_str.length() - 2);
98130
} else {
@@ -117,6 +149,7 @@ std::string RawDataToHexStr(const void *data_ptr, uint16_t data_len, bool upperc
117149
if (i < (data_len - 1))
118150
oss << delimiter;
119151
}
152+
120153
return oss.str();
121154
}
122155

@@ -148,6 +181,7 @@ size_t HexStrToRawData(const std::string &hex_str, void *out_ptr, uint16_t out_l
148181
p_data[i] = (hexCharToValue(h_char) << 4) | (hexCharToValue(l_char) & 0x0f);
149182
++data_len;
150183
}
184+
151185
return data_len;
152186
}
153187

modules/util/string.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ size_t Split(const std::string &src_str, const std::string sep, std::vector<std:
4242
*/
4343
size_t SplitBySpace(const std::string &src_str, std::vector<std::string> &str_vec);
4444

45+
/**
46+
* \brief 合并字符串
47+
* \param str_vec 字串数组
48+
* \param delimiter 分隔符
49+
*/
50+
std::string Join(const std::vector<std::string> &str_vec, const std::string &delimiter = "");
51+
4552
/**
4653
* \brief 消除字串左边的空格符
4754
* \param orig_str 原始字串

modules/util/string_test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ TEST(string, SplitBySpace) {
5959

6060
}
6161

62+
TEST(string, JoinStrVecNormal) {
63+
std::vector<std::string> str_vec = {"aa", "bb", "cc"};
64+
EXPECT_EQ(Join(str_vec, ":"), "aa:bb:cc");
65+
}
66+
67+
TEST(string, JoinStrVecNoDelimeter) {
68+
std::vector<std::string> str_vec = {"aa", "bb"};
69+
EXPECT_EQ(Join(str_vec), "aabb");
70+
}
71+
72+
TEST(string, JoinEmptyStrVec_1) {
73+
std::vector<std::string> str_vec;
74+
EXPECT_EQ(Join(str_vec, ":"), "");
75+
}
76+
77+
TEST(string, JoinEmptyStrVec_2) {
78+
std::vector<std::string> str_vec = {"", "", ""};
79+
EXPECT_EQ(Join(str_vec, ":"), "::");
80+
}
81+
6282
TEST(string, StripLeft) {
6383
EXPECT_EQ(StripLeft(" A "), "A ");
6484
}

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 := 10
24-
TBOX_VERSION_REVISION := 13
24+
TBOX_VERSION_REVISION := 14

0 commit comments

Comments
 (0)