Jamba C++ API  4.4.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 
85 template<typename TFloat>
86 TFloat stringToFloat(const std::string &iString)
87 {
88  char *endPtr = nullptr;
89  auto value = static_cast<TFloat>(strtod(iString.c_str(), &endPtr));
90  if(endPtr == iString.c_str())
91  return std::numeric_limits<TFloat>::quiet_NaN();
92  return value;
93 }
94 
100 template<typename TFloat>
101 std::vector<TFloat> splitFloats(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries = false)
102 {
103  static auto f = [] (const std::string &iString) -> TFloat { return stringToFloat<TFloat>(iString); };
104 
105  std::vector<TFloat> res{};
106 
107  auto strings = splitString(iString, iDelimiter, iSkipEmptyEntries);
108  if(strings.empty())
109  return res;
110 
111  res.reserve(strings.size());
112 
113  std::transform(strings.cbegin(), strings.cend(), std::back_inserter(res), f);
114 
115  return res;
116 }
117 
118 }
std::vector< TFloat > splitFloats(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries=false)
Converts the string to a TFloat.
Definition: StringUtils.h:101
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 char const * to_string(bool iValue)
Convenient call to convert a boolean into a string.
Definition: StringUtils.h:28