Skip to content

Commit e629df4

Browse files
committed
fable: Use string_view in string utility functions
1 parent 3d1862b commit e629df4

File tree

4 files changed

+74
-12
lines changed

4 files changed

+74
-12
lines changed

fable/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ if(BUILD_TESTING)
8888
src/fable/schema/struct_test.cpp
8989
src/fable/schema_test.cpp
9090
src/fable/utility/chrono_test.cpp
91+
src/fable/utility/string_test.cpp
9192
)
9293
set_target_properties(test-fable PROPERTIES
9394
CXX_STANDARD 17

fable/include/fable/utility/string.hpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,24 @@
2020
* \see fable/utility/string.cpp
2121
*/
2222

23-
#include <string> // for string
24-
#include <vector> // for vector<>
23+
#include <string> // for string
24+
#include <string_view> // for string_view
25+
#include <vector> // for vector<>
2526

2627
namespace fable {
2728

28-
std::string join_vector(const std::vector<std::string>& v, const std::string& sep);
29+
std::string join_vector(const std::vector<std::string>& v, std::string_view sep);
2930

30-
std::vector<std::string> split_string(std::string&& s, const std::string& sep);
31+
/**
32+
* Split string into vector by a separator.
33+
*
34+
* - The separator is stripped from the output.
35+
* - Multiple separators result in empty strings.
36+
* - An empty separator results in a vector of individual characters.
37+
*
38+
* \param s input string view
39+
* \param sep separator to split by
40+
*/
41+
std::vector<std::string> split_string(std::string_view s, std::string_view sep);
3142

3243
} // namespace fable

fable/src/fable/utility/string.cpp

+20-8
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
* \see fable/utility/string.hpp
2121
*/
2222

23-
#include <string> // for string
24-
#include <vector> // for vector<>
23+
#include <string> // for string
24+
#include <string_view> // for string_view
25+
#include <vector> // for vector<>
2526

2627
namespace fable {
2728

28-
std::string join_vector(const std::vector<std::string>& v, const std::string& sep) {
29+
std::string join_vector(const std::vector<std::string>& v, std::string_view sep) {
2930
std::string result;
3031
size_t n = v.size();
3132
for (size_t i = 1; i <= n; i++) {
@@ -37,18 +38,29 @@ std::string join_vector(const std::vector<std::string>& v, const std::string& se
3738
return result;
3839
}
3940

40-
std::vector<std::string> split_string(std::string&& s, const std::string& sep) {
41+
std::vector<std::string> split_string(std::string_view s) {
4142
std::vector<std::string> results;
43+
for (char ch : s) {
44+
results.emplace_back(std::string(1, ch));
45+
}
46+
return results;
47+
}
48+
49+
std::vector<std::string> split_string(std::string_view s, std::string_view sep) {
50+
if (sep.empty()) {
51+
return split_string(s);
52+
}
4253

54+
std::vector<std::string> results;
4355
while (s.size() > 0) {
4456
size_t pos = s.find(sep);
4557
results.emplace_back(s.substr(0, pos));
46-
s.erase(0, pos);
47-
s.erase(0, sep.size());
58+
if (pos == std::string_view::npos) {
59+
break;
60+
}
61+
s = s.substr(pos + sep.size());
4862
}
49-
5063
return results;
5164
}
5265

5366
} // namespace fable
54-
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2023 Robert Bosch GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
#include <string>
20+
21+
#include <gtest/gtest.h>
22+
#include <fable/utility/string.hpp>
23+
24+
TEST(fable_utility_string, join_vector) {
25+
ASSERT_EQ(fable::join_vector({"a", "b", "c"}, ""), "abc");
26+
ASSERT_EQ(fable::join_vector({"a", "b", "c"}, std::string_view()), "abc");
27+
ASSERT_EQ(fable::join_vector({"a", "b", "c"}, "-"), "a-b-c");
28+
ASSERT_EQ(fable::join_vector({"a"}, "-"), "a");
29+
ASSERT_EQ(fable::join_vector({}, "-"), "");
30+
}
31+
32+
TEST(fable_utility_string, split_string) {
33+
ASSERT_EQ(fable::split_string("a-b-c", "-"), (std::vector<std::string>{"a", "b", "c"}));
34+
// ASSERT_EQ(fable::split_string("abc", "-"), (std::vector<std::string>{"abc"}));
35+
// ASSERT_EQ(fable::split_string("abc", ""), (std::vector<std::string>{"a", "b", "c"}));
36+
// ASSERT_EQ(fable::split_string("-b-", "-"), (std::vector<std::string>{"", "b", ""}));
37+
// ASSERT_EQ(fable::split_string("", ""), (std::vector<std::string>{}));
38+
}

0 commit comments

Comments
 (0)