From 129ecb49df5bb6f9f7f8779dc4087e2ac6d1d1bd Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Tue, 12 May 2020 18:53:30 +0900 Subject: [PATCH] Add string functions (trim, split) Signed-off-by: Sangwan Kwon --- src/vist/common/tests/string.cpp | 66 ++++++++++++++++++++++++++++++++ src/vist/string.hpp | 53 +++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/vist/common/tests/string.cpp create mode 100644 src/vist/string.hpp diff --git a/src/vist/common/tests/string.cpp b/src/vist/common/tests/string.cpp new file mode 100644 index 0000000..371d577 --- /dev/null +++ b/src/vist/common/tests/string.cpp @@ -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 + +#include + +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 index 0000000..42424ae --- /dev/null +++ b/src/vist/string.hpp @@ -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 +#include +#include +#include + +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 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(begin, end); +} + +} // namespace vist -- 2.34.1