Skip to content

Commit 3bc7a96

Browse files
committed
fable: Add fable/utility/string.hpp utility functions
1 parent 6416931 commit 3bc7a96

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

fable/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_library(fable
2929
src/fable/schema/path.cpp
3030
src/fable/environment.cpp
3131
src/fable/utility.cpp
32+
src/fable/utility/string.cpp
3233

3334
# For IDE integration
3435
${fable_PUBLIC_HEADERS}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
* \file fable/utility/string.hpp
20+
* \see fable/utility/string.cpp
21+
*/
22+
23+
#include <string> // for string
24+
#include <vector> // for vector<>
25+
26+
namespace fable {
27+
28+
std::string join_vector(const std::vector<std::string>& v, const std::string& sep);
29+
30+
std::vector<std::string> split_string(std::string&& s, const std::string& sep);
31+
32+
} // namespace fable

fable/src/fable/utility/string.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
* \file fable/utility/string.cpp
20+
* \see fable/utility/string.hpp
21+
*/
22+
23+
#include <string> // for string
24+
#include <vector> // for vector<>
25+
26+
namespace fable {
27+
28+
std::string join_vector(const std::vector<std::string>& v, const std::string& sep) {
29+
std::string result;
30+
size_t n = v.size();
31+
for (size_t i = 1; i <= n; i++) {
32+
result += v[i - 1];
33+
if (i < n) {
34+
result += sep;
35+
}
36+
}
37+
return result;
38+
}
39+
40+
std::vector<std::string> split_string(std::string&& s, const std::string& sep) {
41+
std::vector<std::string> results;
42+
43+
while (s.size() > 0) {
44+
size_t pos = s.find(sep);
45+
results.emplace_back(s.substr(0, pos));
46+
s.erase(0, pos);
47+
s.erase(0, sep.size());
48+
}
49+
50+
return results;
51+
}
52+
53+
} // namespace fable
54+

0 commit comments

Comments
 (0)