Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
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 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19#pragma once
20
21#include <string>
22#include <sstream>
23#include <vector>
24#include <iterator>
25
26namespace pongasoft::Utils {
27
29constexpr char const *to_string(bool iValue) { return iValue ? "true" : "false"; }
30
39template<typename Out>
40void splitString(const std::string &iString, char iDelimiter, Out oResult, bool iSkipEmptyEntries = false)
41{
42 std::stringstream ss(iString);
43 std::string item;
44 while(std::getline(ss, item, iDelimiter))
45 {
46 if(!item.empty() || !iSkipEmptyEntries)
47 *(oResult++) = item;
48 }
49 if(!iSkipEmptyEntries)
50 {
51 if(!iString.empty() && iString[iString.size() - 1] == iDelimiter)
52 *oResult = "";
53 }
54}
55
63std::vector<std::string> splitString(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries = false);
64
70template<typename TFloat>
71bool stringToFloat(const std::string &iString, TFloat &oValue)
72{
73 char *endPtr = nullptr;
74 auto value = static_cast<TFloat>(strtod(iString.c_str(), &endPtr));
75 if(endPtr == iString.c_str())
76 return false;
77 oValue = value;
78 return true;
79}
80
90template<typename TFloat>
91TFloat stringToFloat(const std::string &iString)
92{
93 char *endPtr = nullptr;
94 auto value = static_cast<TFloat>(strtod(iString.c_str(), &endPtr));
95 if(endPtr == iString.c_str())
96 return 0;
97 return value;
98}
99
103template<typename TFloat>
104std::vector<TFloat> splitFloats(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries = false)
105{
106 static auto f = [] (const std::string &iString) -> TFloat { return stringToFloat<TFloat>(iString); };
107
108 std::vector<TFloat> res{};
109
110 auto strings = splitString(iString, iDelimiter, iSkipEmptyEntries);
111 if(strings.empty())
112 return res;
113
114 res.reserve(strings.size());
115
116 std::transform(strings.cbegin(), strings.cend(), std::back_inserter(res), f);
117
118 return res;
119}
120
121}
Definition CircularBuffer.h:26
constexpr char const * to_string(bool iValue)
Convenient call to convert a boolean into a string.
Definition StringUtils.h:29
bool stringToFloat(const std::string &iString, TFloat &oValue)
Converts the string to a TFloat (float or double).
Definition StringUtils.h:71
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:31
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:104