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 std::vector<std::string>
29 armarx::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 
37 std::vector<std::string>
38 armarx::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 
46 bool
47 armarx::starts_with(const std::string& haystack, const std::string& needle)
48 {
49  return simox::alg::starts_with(haystack, needle);
50 }
51 
52 bool
53 armarx::ends_with(const std::string& haystack, const std::string& needle)
54 {
55  return simox::alg::ends_with(haystack, needle);
56 }
57 
58 bool
59 armarx::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 
67 std::string
68 armarx::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 
99 float
100 armarx::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 
110 int
111 armarx::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 
121 unsigned int
122 armarx::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 }
armarx::toUInt
unsigned int toUInt(const std::string &input)
Definition: StringHelpers.cpp:122
armarx::toInt
int toInt(const std::string &input)
Definition: StringHelpers.cpp:111
armarx::Contains
bool Contains(const ContainerType &container, const ElementType &searchElement)
Definition: algorithm.h:330
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:36
armarx::toFloat
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.
Definition: StringHelpers.cpp:100
armarx::Encode
std::string Encode(const std::string &data)
Definition: StringHelpers.cpp:68
armarx::starts_with
bool starts_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:47
StringHelpers.h
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:12
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:661
armarx::ends_with
bool ends_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:53
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:38