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



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019