Jamba C++ API  6.3.0
StringUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 pongasoft
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #pragma once
19 
20 #include <string>
21 #include <sstream>
22 #include <vector>
23 #include <iterator>
24 
25 namespace pongasoft::Utils {
26 
28 constexpr char const *to_string(bool iValue) { return iValue ? "true" : "false"; }
29 
38 template<typename Out>
39 void splitString(const std::string &iString, char iDelimiter, Out oResult, bool iSkipEmptyEntries = false)
40 {
41  std::stringstream ss(iString);
42  std::string item;
43  while(std::getline(ss, item, iDelimiter))
44  {
45  if(!item.empty() || !iSkipEmptyEntries)
46  *(oResult++) = item;
47  }
48  if(!iSkipEmptyEntries)
49  {
50  if(!iString.empty() && iString[iString.size() - 1] == iDelimiter)
51  *oResult = "";
52  }
53 }
54 
62 std::vector<std::string> splitString(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries = false);
63 
69 template<typename TFloat>
70 bool stringToFloat(const std::string &iString, TFloat &oValue)
71 {
72  char *endPtr = nullptr;
73  auto value = static_cast<TFloat>(strtod(iString.c_str(), &endPtr));
74  if(endPtr == iString.c_str())
75  return false;
76  oValue = value;
77  return true;
78 }
79 
89 template<typename TFloat>
90 TFloat stringToFloat(const std::string &iString)
91 {
92  char *endPtr = nullptr;
93  auto value = static_cast<TFloat>(strtod(iString.c_str(), &endPtr));
94  if(endPtr == iString.c_str())
95  return 0;
96  return value;
97 }
98 
102 template<typename TFloat>
103 std::vector<TFloat> splitFloats(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries = false)
104 {
105  static auto f = [] (const std::string &iString) -> TFloat { return stringToFloat<TFloat>(iString); };
106 
107  std::vector<TFloat> res{};
108 
109  auto strings = splitString(iString, iDelimiter, iSkipEmptyEntries);
110  if(strings.empty())
111  return res;
112 
113  res.reserve(strings.size());
114 
115  std::transform(strings.cbegin(), strings.cend(), std::back_inserter(res), f);
116 
117  return res;
118 }
119 
120 }
std::vector< TFloat > splitFloats(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries=false)
Converts the string to a an array of floats.
Definition: StringUtils.h:103
Definition: CircularBuffer.h:25
std::vector< std::string > splitString(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries)
Split a string according to a delimiter and returns a vector.
Definition: StringUtils.cpp:30
bool stringToFloat(const std::string &iString, TFloat &oValue)
Converts the string to a TFloat (float or double)
Definition: StringUtils.h:70
constexpr std::array< U, N > transform(std::array< T, N > const &a, F &&f)
Transforms an array containing elements of type T into an array containing elements of type U by appl...
Definition: stl.h:40
constexpr char const * to_string(bool iValue)
Convenient call to convert a boolean into a string.
Definition: StringUtils.h:28