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 
28 
29 std::vector<std::string> armarx::Split(const std::string& source, const std::string& splitBy, bool trimElements, bool removeEmptyElements)
30 {
31  return simox::alg::split(source, splitBy, trimElements, removeEmptyElements);
32 }
33 
34 
35 std::vector<std::string>
36 armarx::split(const std::string& source, const std::string& splitBy, bool trimElements, bool removeEmptyElements)
37 {
38  return armarx::Split(source, splitBy, trimElements, removeEmptyElements);
39 }
40 
41 
42 bool
43 armarx::starts_with(const std::string& haystack, const std::string& needle)
44 {
45  return simox::alg::starts_with(haystack, needle);
46 }
47 
48 
49 bool
50 armarx::ends_with(const std::string& haystack, const std::string& needle)
51 {
52  return simox::alg::ends_with(haystack, needle);
53 }
54 
55 
56 bool armarx::Contains(const std::string& haystack, const std::string& needle, bool caseInsensitive)
57 {
58  std::string haystackToSearch = (caseInsensitive) ? simox::alg::to_lower(haystack) : haystack;
59  std::string needleToSearchFor = (caseInsensitive) ? simox::alg::to_lower(needle) : needle;
60 
61  return haystackToSearch.find(needleToSearchFor) != std::string::npos;
62 }
63 
64 
65 std::string armarx::Encode(const std::string& data)
66 {
67  std::string buffer;
68  buffer.reserve(data.size());
69  for (size_t pos = 0; pos != data.size(); ++pos)
70  {
71  switch (data[pos])
72  {
73  case '&':
74  buffer.append("&amp;");
75  break;
76  case '\"':
77  buffer.append("&quot;");
78  break;
79  case '\'':
80  buffer.append("&apos;");
81  break;
82  case '<':
83  buffer.append("&lt;");
84  break;
85  case '>':
86  buffer.append("&gt;");
87  break;
88  default:
89  buffer.append(&data[pos], 1);
90  break;
91  }
92  }
93  return buffer;
94 }
95 
96 
97 float armarx::toFloat(const std::string& input)
98 {
99  std::stringstream ss;
100  float result;
101  ss.imbue(std::locale::classic());
102  ss << input;
103  ss >> result;
104  return result;
105 }
106 
107 
108 int armarx::toInt(const std::string& input)
109 {
110  std::stringstream ss;
111  int result;
112  ss.imbue(std::locale::classic());
113  ss << input;
114  ss >> result;
115  return result;
116 }
117 
118 unsigned int armarx::toUInt(const std::string &input)
119 {
120  long long l = std::stoll(input, nullptr, 0);
121  if (l < 0) {
122  std::stringstream ss;
123  ss << "Could not parse " << input << " to unsigned int";
124  throw std::out_of_range(ss.str());
125  }
126  return static_cast<unsigned int>(l);
127 }
armarx::toUInt
unsigned int toUInt(const std::string &input)
Definition: StringHelpers.cpp:118
armarx::toInt
int toInt(const std::string &input)
Definition: StringHelpers.cpp:108
armarx::Contains
bool Contains(const ContainerType &container, const ElementType &searchElement)
Definition: algorithm.h:295
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:35
armarx::toFloat
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.
Definition: StringHelpers.cpp:97
armarx::Encode
std::string Encode(const std::string &data)
Definition: StringHelpers.cpp:65
armarx::starts_with
bool starts_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:43
StringHelpers.h
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
armarx::ends_with
bool ends_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:50
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36