Add string functions (trim, split)
authorSangwan Kwon <sangwan.kwon@samsung.com>
Tue, 12 May 2020 09:53:30 +0000 (18:53 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Mon, 18 May 2020 00:52:02 +0000 (09:52 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/common/tests/string.cpp [new file with mode: 0644]
src/vist/string.hpp [new file with mode: 0644]

diff --git a/src/vist/common/tests/string.cpp b/src/vist/common/tests/string.cpp
new file mode 100644 (file)
index 0000000..371d577
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ *  Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#include <gtest/gtest.h>
+
+#include <vist/string.hpp>
+
+using namespace vist;
+
+TEST(StringTests, ltrim)
+{
+       std::string str = " a b c ";
+       ltrim(str);
+       EXPECT_EQ(str, "a b c ");
+}
+
+TEST(StringTests, rtrim)
+{
+       std::string str = " a b c ";
+       rtrim(str);
+       EXPECT_EQ(str, " a b c");
+}
+
+TEST(StringTests, trim)
+{
+       std::string str = " a b c ";
+       trim(str);
+       EXPECT_EQ(str, "a b c");
+}
+
+TEST(StringTests, split)
+{
+       std::string origin = "a b c";
+       auto token = split(origin, std::regex("\\s"));
+       if (token.size() == 3) {
+               EXPECT_EQ(token[0], "a");
+               EXPECT_EQ(token[1], "b");
+               EXPECT_EQ(token[2], "c");
+       } else {
+               EXPECT_TRUE(false);
+       }
+
+       origin = "a,b,c";
+       token = split(origin, std::regex(","));
+       EXPECT_EQ(token.size(), 3);
+       if (token.size() == 3) {
+               EXPECT_EQ(token[0], "a");
+               EXPECT_EQ(token[1], "b");
+               EXPECT_EQ(token[2], "c");
+       } else {
+               EXPECT_TRUE(false);
+       }
+}
diff --git a/src/vist/string.hpp b/src/vist/string.hpp
new file mode 100644 (file)
index 0000000..42424ae
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ *  Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the language governing permissions and
+ *  limitations under the License
+ */
+
+#pragma once
+
+#include <algorithm>
+#include <cctype>
+#include <locale>
+#include <regex>
+
+namespace vist {
+
+inline void ltrim(std::string& str)
+{
+       str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](int ch) {
+               return !std::isspace(ch);
+       }));
+}
+
+inline void rtrim(std::string& str)
+{
+       str.erase(std::find_if(str.rbegin(), str.rend(), [](int ch) {
+               return !std::isspace(ch);
+       }).base(), str.end());
+}
+
+inline void trim(std::string& str)
+{
+       ltrim(str);
+       rtrim(str);
+}
+
+inline std::vector<std::string> split(const std::string origin, std::regex regex)
+{
+       auto begin = std::sregex_token_iterator(origin.begin(), origin.end(), regex, -1);
+       auto end = std::sregex_token_iterator();
+       return std::vector<std::string>(begin, end);
+}
+
+} // namespace vist