Main MRPT website > C++ reference for MRPT 1.5.7
string_utils.h
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 #ifndef MRPT_STRING_UTILS_H
10 #define MRPT_STRING_UTILS_H
11 
12 #include <mrpt/utils/utils_defs.h>
13 #include <deque>
14 
15 namespace mrpt
16 {
17  namespace system
18  {
19  /** \addtogroup string_manage String management and utilities (in #include <mrpt/system/string_utils.h>)
20  * \ingroup mrpt_base_grp
21  * @{ */
22 
23  /** An OS-independent method for tokenizing a string.
24  * The extra parameter "context" must be a pointer to a "char*" variable, which needs no initialization and is used to save information between calls to strtok.
25  * \sa system::tokenize
26  */
27  char BASE_IMPEXP *strtok(char *str, const char *strDelimit, char **context) MRPT_NO_THROWS;
28 
29  /** Tokenizes a string according to a set of delimiting characters.
30  * Example:
31  * \code
32  std::vector<std::string> tokens;
33  tokenize(" - Pepe-Er Muo"," -",tokens);
34  * \endcode
35  *
36  * Will generate 3 tokens:
37  * - "Pepe"
38  * - "Er"
39  * - "Muo"
40  * \param[in] skipBlankTokens If `true`, consecutive "delimiters" will be considered one single delimiters. If `false`, a blank token will be returned between each pair of delimiters.
41  */
42  void BASE_IMPEXP tokenize(
43  const std::string & inString,
44  const std::string & inDelimiters,
45  std::deque<std::string> & outTokens,
46  bool skipBlankTokens = true) MRPT_NO_THROWS;
47  /** \overload */
48  void BASE_IMPEXP tokenize(
49  const std::string & inString,
50  const std::string & inDelimiters,
51  std::vector<std::string> & outTokens,
52  bool skipBlankTokens = true) MRPT_NO_THROWS;
53 
54  /** Removes leading and trailing spaces */
55  std::string BASE_IMPEXP trim(const std::string &str);
56 
57  /** Returns a upper-case version of a string.
58  * \sa lowerCase */
59  std::string BASE_IMPEXP upperCase(const std::string& str);
60 
61  /** Returns an lower-case version of a string.
62  * \sa upperCase */
63  std::string BASE_IMPEXP lowerCase(const std::string& str);
64 
65  /** Decodes a UTF-8 string into an UNICODE string.
66  * See http://en.wikipedia.org/wiki/UTF-8 and http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
67  */
68  void BASE_IMPEXP decodeUTF8(const std::string &strUTF8, vector_word &out_uniStr);
69 
70  /** Encodes a 2-bytes UNICODE string into a UTF-8 string.
71  * See http://en.wikipedia.org/wiki/UTF-8 and http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
72  */
73  void BASE_IMPEXP encodeUTF8(const vector_word &input, std::string &output);
74 
75  /** Encode a sequence of bytes as a string in base-64.
76  * \sa decodeBase64 */
77  void BASE_IMPEXP encodeBase64(const vector_byte &inputData, std::string &outString);
78 
79  /** Decode a base-64 string into the original sequence of bytes.
80  * \sa encodeBase64
81  * \return false on invalid base-64 string passed as input, true on success.
82  */
83  bool BASE_IMPEXP decodeBase64(const std::string &inString, vector_byte &outData);
84 
85  /** This function implements formatting with the appropriate SI metric unit
86  * prefix: 1e-12->'p', 1e-9->'n', 1e-6->'u', 1e-3->'m', 1->'', 1e3->'K',
87  * 1e6->'M', 1e9->'G', 1e12->'T' \sa intervalFormat */
88  std::string BASE_IMPEXP unitsFormat(const double val,int nDecimalDigits=2, bool middle_space=true);
89 
90  /** Enlarge the string with spaces up to the given length. */
91  std::string BASE_IMPEXP rightPad(const std::string &str, const size_t total_len, bool truncate_if_larger = false);
92 
93  /** Return true if the two strings are equal (case sensitive) \sa strCmpI */
94  bool BASE_IMPEXP strCmp(const std::string &s1, const std::string &s2);
95 
96  /** Return true if the two strings are equal (case insensitive) \sa strCmp */
97  bool BASE_IMPEXP strCmpI(const std::string &s1, const std::string &s2);
98 
99  /** Return true if "str" starts with "subStr" (case sensitive) \sa strStartsI */
100  bool BASE_IMPEXP strStarts(const std::string &str, const std::string &subStr);
101 
102  /** Return true if "str" starts with "subStr" (case insensitive) \sa strStarts */
103  bool BASE_IMPEXP strStartsI(const std::string &str, const std::string &subStr);
104 
105  /** Generates a string for a container in the format [A,B,C,...], and the
106  * fmt string for <b>each</b> vector element.
107  */
108  template <typename T>
109  std::string sprintf_container(const char *fmt, const T &V)
110  {
111  std::string ret = "[";
112  typename T::const_iterator it=V.begin();
113  for (;it!=V.end();)
114  {
115  ret+= format(fmt,*it);
116  ++it;
117  if (it!=V.end())
118  ret+= ",";
119  }
120  ret+="]";
121  return ret;
122  }
123 
124  /** Original code snippet found in http://stackoverflow.com/a/30357710 */
125  /**\{*/
126 
127  /** @brief Convert string instance to number */
128  template <typename T>
130  std::stringstream sin;
131  sin << value;
132  T output;
133  sin >> output;
134  return output;
135 
136  }
137 
138  /** @brief Convert number instance to string */
139  template <typename T>
141  std::stringstream sin;
142  sin << value;
143  return sin.str();
144  }
145 
146  /**\}*/
147 
148 
149  /** @} */
150  } // End of namespace
151 } // End of namespace
152 
153 #endif
std::vector< uint8_t > vector_byte
Definition: types_simple.h:26
bool BASE_IMPEXP strStartsI(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case insensitive)
void BASE_IMPEXP encodeBase64(const vector_byte &inputData, std::string &outString)
Encode a sequence of bytes as a string in base-64.
Definition: base64.cpp:27
#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.
const Scalar * const_iterator
Definition: eigen_plugins.h:24
void BASE_IMPEXP encodeUTF8(const vector_word &input, std::string &output)
Encodes a 2-bytes UNICODE string into a UTF-8 string.
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.
bool BASE_IMPEXP decodeBase64(const std::string &inString, vector_byte &outData)
Decode a base-64 string into the original sequence of bytes.
Definition: base64.cpp:87
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
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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
T str2num(std::string const &value)
Original code snippet found in http://stackoverflow.com/a/30357710.
Definition: string_utils.h:129
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 num2str(T const &value)
Convert number instance to string.
Definition: string_utils.h:140
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.
GLsizei const GLfloat * value
Definition: glext.h:3929
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.
std::string sprintf_container(const char *fmt, const T &V)
Generates a string for a container in the format [A,B,C,...], and the fmt string for each vector elem...
Definition: string_utils.h:109
char BASE_IMPEXP * strtok(char *str, const char *strDelimit, char **context) MRPT_NO_THROWS
An OS-independent method for tokenizing a string.



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