Main MRPT website > C++ reference for MRPT 1.9.9
string_utils.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
13 #include <mrpt/system/os.h>
14 #include <cstring>
15 
16 #ifndef HAVE_STRTOK_R
17 #endif
18 
19 using namespace mrpt;
20 using namespace mrpt::utils;
21 using namespace mrpt::system;
22 using namespace std;
23 
24 /*---------------------------------------------------------------
25  lowerCase
26  ---------------------------------------------------------------*/
27 string mrpt::system::lowerCase(const string& str)
28 {
29  string outStr(str);
30 
31  transform(
32  outStr.begin(), outStr.end(), // In
33  outStr.begin(), // Out
34  (int (*)(int))tolower);
35  return outStr;
36 }
37 
38 /*---------------------------------------------------------------
39  upperCase
40  ---------------------------------------------------------------*/
41 string mrpt::system::upperCase(const string& str)
42 {
43  string outStr(str);
44  transform(
45  outStr.begin(), outStr.end(), // In
46  outStr.begin(), // Out
47  (int (*)(int))toupper);
48  return outStr;
49 }
50 
51 /*---------------------------------------------------------------
52  encodeUTF8
53 
54  Author: Marius Bancila
55  http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/
56 ---------------------------------------------------------------*/
57 #define MASKBITS 0x3F
58 #define MASKBYTE 0x80
59 #define MASK2BYTES 0xC0
60 #define MASK3BYTES 0xE0
61 #define MASK4BYTES 0xF0
62 #define MASK5BYTES 0xF8
63 #define MASK6BYTES 0xFC
64 
66 {
67  output = ""; // output.clear(); VC6...
68  output.reserve(input.size());
69  for (size_t i = 0; i < input.size(); i++)
70  {
71  // 0xxxxxxx
72  if (input[i] < 0x80)
73  {
74  output += (char)input[i];
75  }
76  // 110xxxxx 10xxxxxx
77  else if (input[i] < 0x800)
78  {
79  output += (char)(MASK2BYTES | input[i] >> 6);
80  output += (char)(MASKBYTE | (input[i] & MASKBITS));
81  }
82  // 1110xxxx 10xxxxxx 10xxxxxx
83  /*else if(input[i] < 0x10000)
84  {
85  output.push_back((char)(MASK3BYTES | input[i] >> 12));
86  output.push_back((char)(MASKBYTE | input[i] >> 6 & MASKBITS));
87  output.push_back((char)(MASKBYTE | input[i] & MASKBITS));
88  }*/
89  }
90 }
91 
92 /*---------------------------------------------------------------
93  decodeUTF8
94 
95  Author: Marius Bancila
96  http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/
97 ---------------------------------------------------------------*/
99 {
100  output.clear();
101  output.reserve(input.size());
102  for (size_t i = 0; i < input.size();)
103  {
104  uint16_t ch = 0;
105 
106  // 1110xxxx 10xxxxxx 10xxxxxx
107  if ((input[i] & MASK3BYTES) == MASK3BYTES)
108  {
109  ch = ((input[i] & 0x0F) << 12) | ((input[i + 1] & MASKBITS) << 6) |
110  (input[i + 2] & MASKBITS);
111  i += 3;
112  }
113  // 110xxxxx 10xxxxxx
114  else if ((input[i] & MASK2BYTES) == MASK2BYTES)
115  {
116  ch = ((input[i] & 0x1F) << 6) | (input[i + 1] & MASKBITS);
117  i += 2;
118  }
119  // 0xxxxxxx
120  else if (uint8_t(input[i]) < MASKBYTE)
121  {
122  ch = input[i];
123  i += 1;
124  }
125  output.push_back(ch);
126  }
127 }
128 
129 /** This function implements formatting with the appropriate SI metric unit
130  prefix:
131  1e-12->'p', 1e-9->'n', 1e-6->'u', 1e-3->'m',
132  1->'',
133  1e3->'K', 1e6->'M', 1e9->'G', 1e12->'T'
134  */
136  const double val, int nDecimalDigits, bool middle_space)
137 {
138  char prefix;
139  double mult;
140  const double aVal = std::abs(val);
141 
142  if (aVal >= 1e12)
143  {
144  mult = 1e-12;
145  prefix = 'T';
146  }
147  else if (aVal >= 1e9)
148  {
149  mult = 1e-9;
150  prefix = 'G';
151  }
152  else if (aVal >= 1e6)
153  {
154  mult = 1e-6;
155  prefix = 'M';
156  }
157  else if (aVal >= 1e3)
158  {
159  mult = 1e-3;
160  prefix = 'K';
161  }
162  else if (aVal >= 1)
163  {
164  mult = 1;
165  prefix = ' ';
166  }
167  else if (aVal >= 1e-3)
168  {
169  mult = 1e+3;
170  prefix = 'm';
171  }
172  else if (aVal >= 1e-6)
173  {
174  mult = 1e+6;
175  prefix = 'u';
176  }
177  else if (aVal >= 1e-9)
178  {
179  mult = 1e+9;
180  prefix = 'n';
181  }
182  else
183  {
184  mult = 1e+12;
185  prefix = 'p';
186  }
187 
188  return format(
189  middle_space ? "%.*f %c" : "%.*f%c", nDecimalDigits, val * mult,
190  prefix);
191 }
192 
193 /*---------------------------------------------------------------
194  strtok
195 ---------------------------------------------------------------*/
197  char* str, const char* strDelimit, char** context) noexcept
198 {
199 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
200  // Use a secure version in Visual Studio 2005:
201  return ::strtok_s(str, strDelimit, context);
202 #else
203 #ifdef HAVE_STRTOK_R
204  // POSIX safe version:
205  return ::strtok_r(str, strDelimit, context);
206 #else
207  // Use standard version:
208  return ::strtok(str, strDelimit);
209 #endif
210 #endif
211 }
212 
213 template <class CONTAINER>
215  const std::string& inString, const std::string& inDelimiters,
216  CONTAINER& outTokens, bool skipBlankTokens) noexcept
217 {
218  outTokens.clear();
219 
220  const size_t len = inString.size();
221  bool prev_was_delim = true;
222  std::string cur_token;
223  for (size_t pos = 0; pos <= len; pos++) // the "<=" is intentional!!
224  {
225  char c = '\0';
226  bool cur_is_delim;
227  if (pos == len)
228  {
229  // end of string.
230  cur_is_delim = true;
231  }
232  else
233  {
234  // Regular string char:
235  c = inString[pos];
236  cur_is_delim = (inDelimiters.find(c) != string::npos);
237  }
238  if (cur_is_delim)
239  {
240  if (prev_was_delim)
241  {
242  if (!skipBlankTokens) outTokens.push_back(std::string());
243  }
244  else
245  {
246  outTokens.push_back(cur_token);
247  }
248  cur_token.clear();
249  }
250  else
251  {
252  cur_token.push_back(c);
253  }
254  prev_was_delim = cur_is_delim;
255  }
256 }
257 
259  const std::string& inString, const std::string& inDelimiters,
260  std::deque<std::string>& outTokens, bool skipBlankTokens) noexcept
261 {
262  my_tokenize(inString, inDelimiters, outTokens, skipBlankTokens);
263 }
264 
266  const std::string& inString, const std::string& inDelimiters,
267  std::vector<std::string>& outTokens, bool skipBlankTokens) noexcept
268 {
269  my_tokenize(inString, inDelimiters, outTokens, skipBlankTokens);
270 }
271 
272 /*---------------------------------------------------------------
273  trim
274 ---------------------------------------------------------------*/
276 {
277  if (str.empty())
278  {
279  return std::string();
280  }
281  else
282  {
283  size_t s = str.find_first_not_of(" \t");
284  size_t e = str.find_last_not_of(" \t");
285  if (s == std::string::npos || e == std::string::npos)
286  return std::string();
287  else
288  return str.substr(s, e - s + 1);
289  }
290 }
291 
292 /*---------------------------------------------------------------
293  rightPad
294 ---------------------------------------------------------------*/
296  const std::string& str, const size_t total_len, bool truncate_if_larger)
297 {
298  std::string r = str;
299  if (r.size() < total_len || truncate_if_larger) r.resize(total_len, ' ');
300  return r;
301 }
302 
303 /** Return true if the two strings are equal (case sensitive) \sa StrCmpI */
304 bool mrpt::system::strCmp(const std::string& s1, const std::string& s2)
305 {
306  return !mrpt::system::os::_strcmp(s1.c_str(), s2.c_str());
307 }
308 
309 /** Return true if the two strings are equal (case insensitive) \sa StrCmp */
311 {
312  return !mrpt::system::os::_strcmpi(s1.c_str(), s2.c_str());
313 }
314 
315 /** Return true if "str" starts with "subStr" (case sensitive) \sa strStartsI
316  */
318 {
320  s1.c_str(), s2.c_str(),
321  s2.size()); // if s1 is shorter it's not a problem
322 }
323 
324 /** Return true if "str" starts with "subStr" (case insensitive) \sa strStarts
325  */
327 {
329  s1.c_str(), s2.c_str(),
330  s2.size()); // if s1 is shorter it's not a problem
331 }
#define MASKBITS
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
unsigned __int16 uint16_t
Definition: rptypes.h:44
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
int _strncmp(const char *str, const char *subStr, size_t count) noexcept
An OS-independent version of strncmp.
Definition: os.cpp:335
bool strStartsI(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case insensitive)
int _strnicmp(const char *str, const char *subStr, size_t count) noexcept
An OS-independent version of strnicmp.
Definition: os.cpp:343
void my_tokenize(const std::string &inString, const std::string &inDelimiters, CONTAINER &outTokens, bool skipBlankTokens) noexcept
bool strCmp(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case sensitive)
STL namespace.
void encodeUTF8(const vector_word &input, std::string &output)
Encodes a 2-bytes UNICODE string into a UTF-8 string.
GLdouble s
Definition: glext.h:3676
GLenum GLsizei len
Definition: glext.h:4712
unsigned char uint8_t
Definition: rptypes.h:41
void decodeUTF8(const std::string &strUTF8, vector_word &out_uniStr)
Decodes a UTF-8 string into an UNICODE string.
std::string lowerCase(const std::string &str)
Returns an lower-case version of a string.
const GLubyte * c
Definition: glext.h:6313
char * strtok(char *str, const char *strDelimit, char **context) noexcept
An OS-independent method for tokenizing a string.
#define MASK3BYTES
int val
Definition: mrpt_jpeglib.h:955
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
GLsizei const GLchar ** string
Definition: glext.h:4101
#define MASKBYTE
void tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) noexcept
Tokenizes a string according to a set of delimiting characters.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
std::string unitsFormat(const double val, int nDecimalDigits=2, bool middle_space=true)
This function implements formatting with the appropriate SI metric unit prefix: 1e-12->&#39;p&#39;, 1e-9->&#39;n&#39;, 1e-6->&#39;u&#39;, 1e-3->&#39;m&#39;, 1->&#39;&#39;, 1e3->&#39;K&#39;, 1e6->&#39;M&#39;, 1e9->&#39;G&#39;, 1e12->&#39;T&#39;.
std::vector< uint16_t > vector_word
Definition: types_simple.h:28
std::string upperCase(const std::string &str)
Returns a upper-case version of a string.
GLenum GLenum GLenum input
Definition: glext.h:6499
bool strStarts(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case sensitive)
std::string trim(const std::string &str)
Removes leading and trailing spaces.
std::string rightPad(const std::string &str, const size_t total_len, bool truncate_if_larger=false)
Enlarge the string with spaces up to the given length.
GLuint GLenum GLenum transform
Definition: glext.h:6975
bool strCmpI(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case insensitive)
#define MASK2BYTES
int _strcmp(const char *str1, const char *str2) noexcept
An OS-independent version of strcmp.
Definition: os.cpp:311
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.
Definition: os.cpp:319



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019