Skip to content

Commit fa1fc51

Browse files
committed
feat(util.string):1.10.21, 添加util::string::ToUpper()与ToLower()
1 parent 395f91a commit fa1fc51

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

modules/util/string.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <iomanip>
2323
#include <sstream>
24+
#include <algorithm>
2425

2526
namespace tbox {
2627
namespace util {
@@ -251,6 +252,24 @@ void Replace(std::string &target_str, const std::string &pattern_str, const std:
251252
}
252253
}
253254

255+
std::string ToUpper(const std::string &origin_str)
256+
{
257+
std::string target_str;
258+
target_str.reserve(origin_str.size());
259+
std::back_insert_iterator<std::string> back_insert_iter(target_str);
260+
std::transform(origin_str.begin(), origin_str.end(), back_insert_iter, ::toupper);
261+
return target_str;
262+
}
263+
264+
std::string ToLower(const std::string &origin_str)
265+
{
266+
std::string target_str;
267+
target_str.reserve(origin_str.size());
268+
std::back_insert_iterator<std::string> back_insert_iter(target_str);
269+
std::transform(origin_str.begin(), origin_str.end(), back_insert_iter, ::tolower);
270+
return target_str;
271+
}
272+
254273
}
255274
}
256275
}

modules/util/string.h

+18
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,24 @@ size_t HexStrToRawData(const std::string &hex_str, std::vector<uint8_t> &out, co
138138
void Replace(std::string &target_str, const std::string &pattern_str, const std::string &replace_str,
139139
std::string::size_type start = 0, std::string::size_type count = 0);
140140

141+
/**
142+
* \brief 将字串中所有小写字母替换成大写
143+
*
144+
* \param origin_str 原始字串
145+
*
146+
* \return std::string 修改后的字串
147+
*/
148+
std::string ToUpper(const std::string &origin_str);
149+
150+
/**
151+
* \brief 将字串中所有大写字母替换成小写
152+
*
153+
* \param origin_str 原始字串
154+
*
155+
* \return std::string 修改后的字串
156+
*/
157+
std::string ToLower(const std::string &origin_str);
158+
141159
}
142160
}
143161
}

modules/util/string_test.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,14 @@ TEST(string, Replace) {
273273
EXPECT_STREQ(t.c_str(), "aa bbaa aab aaaa aab");
274274
}
275275
}
276+
277+
TEST(string, ToUpper) {
278+
EXPECT_EQ(ToUpper("Abc:?x#Y$z "), "ABC:?X#Y$Z ");
279+
EXPECT_EQ(ToUpper(""), "");
280+
}
281+
282+
TEST(string, ToLower) {
283+
EXPECT_EQ(ToLower("Abc:?x#Y$z "), "abc:?x#y$z ");
284+
EXPECT_EQ(ToLower(""), "");
285+
}
286+

version.mk

+1-1
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 := 20
24+
TBOX_VERSION_REVISION := 21

0 commit comments

Comments
 (0)