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



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