Blue Brain BioExplorer
StringUtils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, EPFL/Blue Brain Project
3  *
4  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
5  *
6  * This library is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License version 3.0 as published
8  * by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include "StringUtils.h"
21 
22 #include <algorithm>
23 #include <cctype>
24 #include <cmath>
25 #include <locale>
26 #include <set>
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 
31 namespace core
32 {
33 namespace string_utils
34 {
35 const std::string ELLIPSIS("...");
36 
37 std::string shortenString(const std::string& string, const size_t maxLength)
38 {
39  if (string.length() <= maxLength)
40  return string;
41 
42  const float spacePerPart = (maxLength - ELLIPSIS.length()) / 2.f;
43  const auto beforeEllipsis = string.substr(0, std::ceil(spacePerPart));
44  const auto afterEllipsis = string.substr(string.size() - std::floor(spacePerPart));
45 
46  return beforeEllipsis + ELLIPSIS + afterEllipsis;
47 }
48 
49 std::string replaceFirstOccurrence(std::string input, const std::string& toReplace, const std::string& replaceWith)
50 {
51  std::size_t pos = input.find(toReplace);
52  if (pos == std::string::npos)
53  return input;
54  return input.replace(pos, toReplace.length(), replaceWith);
55 }
56 
57 std::string camelCaseToSeparated(const std::string& camelCase, const char separator)
58 {
59  if (camelCase.empty())
60  return camelCase;
61 
62  std::string str(1, ::tolower(camelCase[0]));
63  for (auto it = camelCase.begin() + 1; it != camelCase.end(); ++it)
64  {
65  if (isupper(*it) && *(it - 1) != '-' && islower(*(it - 1)))
66  str += separator;
67  str += *it;
68  }
69 
70  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
71  return str;
72 }
73 
74 std::string separatedToCamelCase(const std::string& separated, const char separator)
75 {
76  std::string camel = separated;
77 
78  for (size_t x = 0; x < camel.length(); x++)
79  {
80  if (camel[x] == separator)
81  {
82  std::string tempString = camel.substr(x + 1, 1);
83 
84  std::transform(tempString.begin(), tempString.end(), tempString.begin(), ::toupper);
85 
86  camel.erase(x, 2);
87  camel.insert(x, tempString);
88  }
89  }
90  return camel;
91 }
92 
93 std::string join(const std::vector<std::string>& strings, const std::string& joinWith)
94 {
95  const size_t numStrings = strings.size();
96  if (numStrings == 0)
97  return "";
98 
99  std::stringstream ss;
100  ss << strings[0];
101  for (size_t i = 1; i < numStrings; i++)
102  ss << joinWith << strings[i];
103  return ss.str();
104 }
105 
106 std::string toLowercase(std::string input)
107 {
108  std::transform(input.begin(), input.end(), input.begin(), ::tolower);
109  return input;
110 }
111 
112 void ltrim(std::string& s)
113 {
114  s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
115 }
116 
117 void rtrim(std::string& s)
118 {
119  s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end());
120 }
121 
122 void trim(std::string& s)
123 {
124  ltrim(s);
125  rtrim(s);
126 }
127 
128 std::vector<std::string> split(const std::string& s, char delim)
129 {
130  std::stringstream ss(s);
131  std::string item;
132  std::vector<std::string> elems;
133  while (std::getline(ss, item, delim))
134  elems.push_back(std::move(item));
135  return elems;
136 }
137 } // namespace string_utils
138 } // namespace core
std::string join(const std::vector< std::string > &strings, const std::string &joinWith)
Definition: StringUtils.cpp:93
std::string separatedToCamelCase(const std::string &separated, const char separator)
Definition: StringUtils.cpp:74
void rtrim(std::string &s)
void ltrim(std::string &s)
void trim(std::string &s)
std::string shortenString(const std::string &string, const size_t maxLength)
Definition: StringUtils.cpp:37
std::string replaceFirstOccurrence(std::string input, const std::string &toReplace, const std::string &replaceWith)
Definition: StringUtils.cpp:49
const std::string ELLIPSIS("...")
std::string camelCaseToSeparated(const std::string &camelCase, const char separator)
Definition: StringUtils.cpp:57
std::vector< std::string > split(const std::string &s, char delim)
std::string toLowercase(std::string input)
std::vector< std::string > strings
Definition: Types.h:44