string_utilities.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // © Copyright 2020, Septentrio NV/SA.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // 3. Neither the name of the copyright holder nor the names of its
14 // contributors may be used to endorse or promote products derived
15 // from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // *****************************************************************************
30 
31 // ROSaic includes
33 // C++ library includes
34 #include <cerrno>
35 #include <cmath>
36 #include <cstdlib>
37 #include <iomanip>
38 #include <limits>
39 #include <sstream>
40 
47 namespace string_utilities {
53  [[nodiscard]] bool toDouble(const std::string& string, double& value)
54  {
55  if (string.empty())
56  {
57  return false;
58  }
59 
60  char* end;
61  errno = 0;
62 
63  double value_new = std::strtod(string.c_str(), &end);
64 
65  if (errno != 0 || end != string.c_str() + string.length())
66  {
67  return false;
68  }
69 
70  value = value_new;
71  return true;
72  }
73 
79  [[nodiscard]] bool toFloat(const std::string& string, float& value)
80  {
81  if (string.empty())
82  {
83  return false;
84  }
85 
86  char* end;
87  errno = 0;
88  float value_new = std::strtof(string.c_str(), &end);
89 
90  if (errno != 0 || end != string.c_str() + string.length())
91  {
92  return false;
93  }
94 
95  value = value_new;
96  return true;
97  }
98 
104  [[nodiscard]] bool toInt32(const std::string& string, int32_t& value,
105  int32_t base)
106  {
107  if (string.empty())
108  {
109  return false;
110  }
111 
112  char* end;
113  errno = 0;
114  int64_t value_new = std::strtol(string.c_str(), &end, base);
115 
116  if (errno != 0 || end != string.c_str() + string.length())
117  {
118  return false;
119  }
120 
121  if (value_new > std::numeric_limits<int32_t>::max() ||
122  value_new < std::numeric_limits<int32_t>::min())
123  {
124  return false;
125  }
126 
127  value = (int32_t)value_new;
128  return true;
129  }
130 
136  [[nodiscard]] bool toUInt32(const std::string& string, uint32_t& value,
137  int32_t base)
138  {
139  if (string.empty())
140  {
141  return false;
142  }
143 
144  char* end;
145  errno = 0;
146  int64_t value_new = std::strtol(string.c_str(), &end, base);
147 
148  if (errno != 0 || end != string.c_str() + string.length())
149  {
150  return false;
151  }
152 
153  if (value_new > std::numeric_limits<uint32_t>::max() || value_new < 0)
154  {
155  return false;
156  }
157 
158  value = (uint32_t)value_new;
159  return true;
160  }
161 
165  [[nodiscard]] int8_t toInt8(const std::string& string, int8_t& value,
166  int32_t base)
167  {
168  char* end;
169  errno = 0;
170  int64_t value_new = std::strtol(string.c_str(), &end, base);
171 
172  value = (int8_t)value_new;
173  return value;
174  }
175 
179  [[nodiscard]] uint8_t toUInt8(const std::string& string, uint8_t& value,
180  int32_t base)
181  {
182  char* end;
183  errno = 0;
184  int64_t value_new = std::strtol(string.c_str(), &end, base);
185 
186  value = (uint8_t)value_new;
187  return true;
188  }
189 
190  [[nodiscard]] std::string trimDecimalPlaces(double num)
191  {
192  num = std::round(num * 1000);
193  num = num / 1000;
194  std::stringstream ss;
195  ss << std::fixed;
196  ss << std::setprecision(3);
197  ss << num;
198  return ss.str();
199  }
200 
201  [[nodiscard]] bool containsSpace(const std::string str)
202  {
203  for (size_t i = 0; i < str.size(); ++i)
204  {
205  if (std::isspace(str[i]))
206  return true;
207  }
208  return false;
209  }
210 } // namespace string_utilities
string_utilities::toInt32
bool toInt32(const std::string &string, int32_t &value, int32_t base=10)
Interprets the contents of "string" as a floating point number of whatever integer type your system h...
Definition: string_utilities.cpp:104
string_utilities::toDouble
bool toDouble(const std::string &string, double &value)
Interprets the contents of "string" as a floating point number of type double It stores the "string"'...
Definition: string_utilities.cpp:53
string_utilities
string_utilities::trimDecimalPlaces
std::string trimDecimalPlaces(double num)
Trims decimal places to three.
Definition: string_utilities.cpp:190
string_utilities::toUInt8
uint8_t toUInt8(const std::string &string, uint8_t &value, int32_t base=10)
Interprets the contents of "string" as a floating point number of whatever unsigned integer type your...
Definition: string_utilities.cpp:179
string_utilities::toUInt32
bool toUInt32(const std::string &string, uint32_t &value, int32_t base=10)
Interprets the contents of "string" as a floating point number of whatever unsigned integer type your...
Definition: string_utilities.cpp:136
string_utilities::containsSpace
bool containsSpace(const std::string str)
Checks if a string contains spaces.
Definition: string_utilities.cpp:201
string_utilities.hpp
Declares lower-level string utility functions used when parsing messages.
string_utilities::toInt8
int8_t toInt8(const std::string &string, int8_t &value, int32_t base=10)
Interprets the contents of "string" as a floating point number of whatever integer type your system h...
Definition: string_utilities.cpp:165
length
TF2SIMD_FORCE_INLINE tf2Scalar length(const Quaternion &q)
string_utilities::toFloat
bool toFloat(const std::string &string, float &value)
Interprets the contents of "string" as a floating point number of type float.
Definition: string_utilities.cpp:79


septentrio_gnss_driver
Author(s): Tibor Dome
autogenerated on Wed Nov 22 2023 04:04:27