AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
string.cpp
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
15#include "auto_apms_util/string.hpp"
16
17#include <algorithm>
18#include <iterator>
19#include <sstream>
20
21namespace auto_apms_util
22{
23
24std::vector<std::string> splitString(const std::string & str, const std::string & delimiter, bool remove_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 parts.push_back(str.substr(start, end - start)); // Add the part before the delimiter
31 start = end + delimiter.length(); // Move start to after the delimiter
32 end = str.find(delimiter, start); // Find the next occurrence of the delimiter
33 }
34 // Add the last part after the last delimiter (or the entire string if no delimiter was found)
35 parts.push_back(str.substr(start));
36
37 // Remove empty strings if desired
38 if (remove_empty) {
39 auto it = parts.begin();
40 while (it != parts.end()) {
41 if (*it == "")
42 it = parts.erase(it);
43 else
44 ++it;
45 }
46 }
47 return parts;
48}
49
50std::string printMap(
51 const std::map<std::string, std::string> & map, const std::string & key_val_sep, const std::string & entry_sep)
52{
53 std::ostringstream oss;
54 bool first = true;
55 for (const auto & [key, val] : map) {
56 if (!first) {
57 oss << entry_sep; // Separator between entries
58 }
59 oss << key << key_val_sep << val; // Separator between key and value
60 first = false;
61 }
62 return oss.str();
63}
64
65std::string makeColoredText(const std::string & text, TextColor color)
66{
67 switch (color) {
68 case TextColor::GREEN:
69 return "\x1b[32m" + text + "\x1b[0m";
70 case TextColor::RED:
71 return "\x1b[31m" + text + "\x1b[0m";
72 case TextColor::YELLOW:
73 return "\x1b[33m" + text + "\x1b[0m";
74 case TextColor::BLUE:
75 return "\x1b[34m" + text + "\x1b[0m";
76 case TextColor::MAGENTA:
77 return "\x1b[35m" + text + "\x1b[0m";
78 case TextColor::CYAN:
79 return "\x1b[36m" + text + "\x1b[0m";
80 }
81 return text;
82}
83
84std::string trimWhitespaces(const std::string & str)
85{
86 std::string::const_iterator start = str.begin();
87 while (start != str.end() && std::isspace(*start)) {
88 ++start;
89 }
90 std::string::const_iterator end = str.end();
91 do {
92 --end;
93 } while (std::distance(start, end) > 0 && std::isspace(*end));
94 return std::string(start, end + 1);
95}
96
97std::string toCamelCase(const std::string & str)
98{
99 std::string new_str;
100 bool capitalizeNext = true;
101 for (char ch : str) {
102 if (ch == '_') {
103 capitalizeNext = true; // Flag to capitalize the next character
104 } else {
105 if (capitalizeNext) {
106 new_str += std::toupper(ch); // Capitalize the character
107 capitalizeNext = false; // Reset the flag
108 } else {
109 new_str += std::tolower(ch); // Add lowercase character
110 }
111 }
112 }
113 return new_str;
114}
115
116std::string toSnakeCase(const std::string & str)
117{
118 std::string new_str;
119 for (size_t i = 0; i < str.size(); ++i) {
120 char ch = str[i];
121 if (std::isupper(ch)) {
122 // If it's an uppercase letter, check if it's part of an acronym
123 if (i > 0 && std::islower(str[i - 1])) {
124 // If the previous character is lowercase, insert an underscore
125 new_str += '_';
126 }
127
128 // Append the lowercase version of the current character
129 new_str += std::tolower(ch);
130
131 // Check if we are inside an acronym (two or more consecutive uppercase letters)
132 if (i + 1 < str.size() && std::isupper(str[i + 1])) {
133 // Continue appending subsequent uppercase letters as lowercase without underscores
134 continue;
135 }
136 } else {
137 new_str += ch; // Append non-uppercase characters directly
138 }
139 }
140 return new_str;
141}
142
143} // namespace auto_apms_util
std::string trimWhitespaces(const std::string &str)
Trim whitespaces from both ends of a string.
Definition string.cpp:84
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:65
std::vector< std::string > splitString(const std::string &str, const std::string &delimiter, bool remove_empty=true)
Split a string into multiple tokens using a specific delimiter string (Delimiter may consist of multi...
Definition string.cpp:24
std::string printMap(const std::map< std::string, std::string > &map, const std::string &key_val_sep="=", const std::string &entry_sep=", ")
Converts a map to a string representation that is suited for printing to console.
Definition string.cpp:50
std::string toCamelCase(const std::string &str)
Transform a string to camelCase.
Definition string.cpp:97
std::string toSnakeCase(const std::string &str)
Transform a string to snake_case.
Definition string.cpp:116
Fundamental helper classes and utility functions.