AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
string.cpp
Go to the documentation of this file.
1// Copyright 2024 Robin Müller
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
17#include <algorithm>
18#include <sstream>
19#include <iterator>
20
22{
23
24std::vector<std::string> splitString(const std::string& str, const std::string& delimiter, bool preserve_empty)
25{
26 std::vector<std::string> parts;
27 size_t start = 0;
28 size_t end = str.find(delimiter);
29 while (end != std::string::npos)
30 {
31 parts.push_back(str.substr(start, end - start)); // Add the part before the delimiter
32 start = end + delimiter.length(); // Move start to after the delimiter
33 end = str.find(delimiter, start); // Find the next occurrence of the delimiter
34 }
35 // Add the last part after the last delimiter (or the entire string if no delimiter was found)
36 parts.push_back(str.substr(start));
37
38 // Remove empty strings if desired
39 if (!preserve_empty)
40 parts.erase(std::remove(parts.begin(), parts.end(), ""), parts.end());
41
42 return parts;
43}
44
45std::string makeColoredText(const std::string& text, TextColor color)
46{
47 switch (color)
48 {
50 return "\x1b[32m" + text + "\x1b[0m";
51 case TextColor::RED:
52 return "\x1b[31m" + text + "\x1b[0m";
54 return "\x1b[33m" + text + "\x1b[0m";
55 case TextColor::BLUE:
56 return "\x1b[34m" + text + "\x1b[0m";
58 return "\x1b[35m" + text + "\x1b[0m";
59 case TextColor::CYAN:
60 return "\x1b[36m" + text + "\x1b[0m";
61 }
62 return text;
63}
64
65} // namespace auto_apms_core::util
std::string makeColoredText(const std::string &text, TextColor color)
Add ANSI color escape sequences to display the text in color when printed to console.
Definition string.cpp:45
std::vector< std::string > splitString(const std::string &str, const std::string &delimiter, bool preserve_empty=true)
Split a string into multiple tokens using a specific delimiter string (Delimiter may consist of multi...
Definition string.cpp:24