Jamba C++ API  4.1.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 {
26 namespace Utils {
27 
36 template<typename Out>
37 void splitString(const std::string &iString, char iDelimiter, Out oResult, bool iSkipEmptyEntries = false)
38 {
39  std::stringstream ss(iString);
40  std::string item;
41  while(std::getline(ss, item, iDelimiter))
42  {
43  if(!item.empty() || !iSkipEmptyEntries)
44  *(oResult++) = item;
45  }
46  if(!iSkipEmptyEntries)
47  {
48  if(!iString.empty() && iString[iString.size() - 1] == iDelimiter)
49  *oResult = "";
50  }
51 }
52 
60 std::vector<std::string> splitString(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries = false);
61 
67 template<typename TFloat>
68 bool stringToFloat(const std::string &iString, TFloat &oValue)
69 {
70  char *endPtr = nullptr;
71  auto value = static_cast<TFloat>(strtod(iString.c_str(), &endPtr));
72  if(endPtr == iString.c_str())
73  return false;
74  oValue = value;
75  return true;
76 }
77 
83 template<typename TFloat>
84 TFloat stringToFloat(const std::string &iString)
85 {
86  char *endPtr = nullptr;
87  auto value = static_cast<TFloat>(strtod(iString.c_str(), &endPtr));
88  if(endPtr == iString.c_str())
89  return std::numeric_limits<TFloat>::quiet_NaN();
90  return value;
91 }
92 
98 template<typename TFloat>
99 std::vector<TFloat> splitFloats(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries = false)
100 {
101  static auto f = [] (const std::string &iString) -> TFloat { return stringToFloat<TFloat>(iString); };
102 
103  std::vector<TFloat> res{};
104 
105  auto strings = splitString(iString, iDelimiter, iSkipEmptyEntries);
106  if(strings.empty())
107  return res;
108 
109  res.reserve(strings.size());
110 
111  std::transform(strings.cbegin(), strings.cend(), std::back_inserter(res), f);
112 
113  return res;
114 }
115 
116 }
117 }
Definition: Clock.h:22
std::vector< TFloat > splitFloats(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries=false)
Converts the string to a TFloat.
Definition: StringUtils.h:99
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:68