Skip to content

Commit

Permalink
fable: Add fable/utility/string.hpp utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cassava committed Apr 22, 2024
1 parent 6416931 commit 3bc7a96
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions fable/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_library(fable
src/fable/schema/path.cpp
src/fable/environment.cpp
src/fable/utility.cpp
src/fable/utility/string.cpp

# For IDE integration
${fable_PUBLIC_HEADERS}
Expand Down
32 changes: 32 additions & 0 deletions fable/include/fable/utility/string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2023 Robert Bosch GmbH
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* \file fable/utility/string.hpp
* \see fable/utility/string.cpp
*/

#include <string> // for string
#include <vector> // for vector<>

namespace fable {

std::string join_vector(const std::vector<std::string>& v, const std::string& sep);

std::vector<std::string> split_string(std::string&& s, const std::string& sep);

} // namespace fable
54 changes: 54 additions & 0 deletions fable/src/fable/utility/string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2023 Robert Bosch GmbH
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* \file fable/utility/string.cpp
* \see fable/utility/string.hpp
*/

#include <string> // for string
#include <vector> // for vector<>

namespace fable {

std::string join_vector(const std::vector<std::string>& v, const std::string& sep) {
std::string result;
size_t n = v.size();
for (size_t i = 1; i <= n; i++) {
result += v[i - 1];
if (i < n) {
result += sep;
}
}
return result;
}

std::vector<std::string> split_string(std::string&& s, const std::string& sep) {
std::vector<std::string> results;

while (s.size() > 0) {
size_t pos = s.find(sep);
results.emplace_back(s.substr(0, pos));
s.erase(0, pos);
s.erase(0, sep.size());
}

return results;
}

} // namespace fable

0 comments on commit 3bc7a96

Please sign in to comment.