StringHelpers.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX
19 * @author Mirko Waechter( mirko.waechter at kit dot edu)
20 * @date 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#include "StringHelpers.h"
25
26#include <SimoxUtility/algorithm/string/string_tools.h>
27
28std::vector<std::string>
29armarx::Split(const std::string& source,
30 const std::string& splitBy,
31 bool trimElements,
32 bool removeEmptyElements)
33{
34 return simox::alg::split(source, splitBy, trimElements, removeEmptyElements);
35}
36
37std::vector<std::string>
38armarx::split(const std::string& source,
39 const std::string& splitBy,
40 bool trimElements,
41 bool removeEmptyElements)
42{
43 return armarx::Split(source, splitBy, trimElements, removeEmptyElements);
44}
45
46bool
47armarx::starts_with(const std::string& haystack, const std::string& needle)
48{
49 return simox::alg::starts_with(haystack, needle);
50}
51
52bool
53armarx::ends_with(const std::string& haystack, const std::string& needle)
54{
55 return simox::alg::ends_with(haystack, needle);
56}
57
58bool
59armarx::Contains(const std::string& haystack, const std::string& needle, bool caseInsensitive)
60{
61 std::string haystackToSearch = (caseInsensitive) ? simox::alg::to_lower(haystack) : haystack;
62 std::string needleToSearchFor = (caseInsensitive) ? simox::alg::to_lower(needle) : needle;
63
64 return haystackToSearch.find(needleToSearchFor) != std::string::npos;
65}
66
67std::string
68armarx::Encode(const std::string& data)
69{
70 std::string buffer;
71 buffer.reserve(data.size());
72 for (size_t pos = 0; pos != data.size(); ++pos)
73 {
74 switch (data[pos])
75 {
76 case '&':
77 buffer.append("&amp;");
78 break;
79 case '\"':
80 buffer.append("&quot;");
81 break;
82 case '\'':
83 buffer.append("&apos;");
84 break;
85 case '<':
86 buffer.append("&lt;");
87 break;
88 case '>':
89 buffer.append("&gt;");
90 break;
91 default:
92 buffer.append(&data[pos], 1);
93 break;
94 }
95 }
96 return buffer;
97}
98
99float
100armarx::toFloat(const std::string& input)
101{
102 std::stringstream ss;
103 float result;
104 ss.imbue(std::locale::classic());
105 ss << input;
106 ss >> result;
107 return result;
108}
109
110int
111armarx::toInt(const std::string& input)
112{
113 std::stringstream ss;
114 int result;
115 ss.imbue(std::locale::classic());
116 ss << input;
117 ss >> result;
118 return result;
119}
120
121unsigned int
122armarx::toUInt(const std::string& input)
123{
124 long long l = std::stoll(input, nullptr, 0);
125 if (l < 0)
126 {
127 std::stringstream ss;
128 ss << "Could not parse " << input << " to unsigned int";
129 throw std::out_of_range(ss.str());
130 }
131 return static_cast<unsigned int>(l);
132}
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
bool ends_with(const std::string &haystack, const std::string &needle)
unsigned int toUInt(const std::string &input)
std::string Encode(const std::string &data)
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
bool Contains(const ContainerType &container, const ElementType &searchElement)
Definition algorithm.h:330
int toInt(const std::string &input)
bool starts_with(const std::string &haystack, const std::string &needle)
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.