Jamba C++ API 8.0.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 <concepts>
23#include <sstream>
24#include <vector>
25#include <iterator>
26#include <algorithm>
27
28namespace pongasoft::Utils {
29
31constexpr char const *to_string(bool iValue) { return iValue ? "true" : "false"; }
32
34template<typename T>
35concept StringOutputIterator = std::output_iterator<T, std::string>;
36
45template<StringOutputIterator Out>
46void splitString(const std::string &iString, char iDelimiter, Out oResult, bool iSkipEmptyEntries = false)
47{
48 std::stringstream ss(iString);
49 std::string item;
50 while(std::getline(ss, item, iDelimiter))
51 {
52 if(!item.empty() || !iSkipEmptyEntries)
53 *(oResult++) = item;
54 }
55 if(!iSkipEmptyEntries)
56 {
57 if(!iString.empty() && iString[iString.size() - 1] == iDelimiter)
58 *oResult = "";
59 }
60}
61
69std::vector<std::string> splitString(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries = false);
70
76template<std::floating_point T>
77bool stringToFloat(const std::string &iString, T &oValue)
78{
79 char *endPtr = nullptr;
80 auto value = static_cast<T>(strtod(iString.c_str(), &endPtr));
81 if(endPtr == iString.c_str())
82 return false;
83 oValue = value;
84 return true;
85}
86
96template<std::floating_point T>
97T stringToFloat(const std::string &iString)
98{
99 T value{};
100 if(stringToFloat<T>(iString, value))
101 return value;
102 return 0;
103}
104
108template<std::floating_point T>
109std::vector<T> splitFloats(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries = false)
110{
111 static auto f = [] (const std::string &iString) -> T { return stringToFloat<T>(iString); };
112
113 std::vector<T> res{};
114
115 auto strings = splitString(iString, iDelimiter, iSkipEmptyEntries);
116 if(strings.empty())
117 return res;
118
119 res.reserve(strings.size());
120
121 std::transform(strings.cbegin(), strings.cend(), std::back_inserter(res), f);
122
123 return res;
124}
125
126}
A concept to check if T is an output iterator that accepts std::string.
Definition StringUtils.h:35
Definition CircularBuffer.h:27
constexpr char const * to_string(bool iValue)
Convenient call to convert a boolean into a string.
Definition StringUtils.h:31
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< T > splitFloats(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries=false)
Converts the string to an array of floating points (floats or doubles).
Definition StringUtils.h:109
bool stringToFloat(const std::string &iString, T &oValue)
Converts the string to a floating point (float or double).
Definition StringUtils.h:77