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-2018, 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 #pragma once
10 
11 #include <string>
12 #include <vector>
13 #include <deque>
14 #include <cstdint>
15 #include <sstream>
16 
17 namespace mrpt
18 {
19 namespace system
20 {
21 /** \addtogroup string_manage String management and utilities (in #include
22  * <mrpt/system/string_utils.h>)
23  * \ingroup mrpt_system_grp
24  * @{ */
25 
26 /** An OS-independent method for tokenizing a string.
27  * The extra parameter "context" must be a pointer to a "char*" variable, which
28  * needs no initialization and is used to save information between calls to
29  * strtok.
30  * \sa system::tokenize
31  */
32 char* strtok(char* str, const char* strDelimit, char** context) noexcept;
33 
34 /** Tokenizes a string according to a set of delimiting characters.
35  * Example:
36  * \code
37  std::vector<std::string> tokens;
38  tokenize(" - Pepe-Er Muo"," -",tokens);
39  * \endcode
40  *
41  * Will generate 3 tokens:
42  * - "Pepe"
43  * - "Er"
44  * - "Muo"
45  * \param[in] skipBlankTokens If `true`, consecutive "delimiters" will be
46  considered one single delimiters. If `false`, a blank token will be returned
47  between each pair of delimiters.
48  * \tparam OUT_CONTAINER Can be a std::vector or std::deque of std::string's.
49  */
50 template <class OUT_CONTAINER>
51 void tokenize(
52  const std::string& inString, const std::string& inDelimiters,
53  OUT_CONTAINER& 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, std::vector<uint16_t>& 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 std::vector<uint16_t>& input, std::string& output);
77 
78 /** Encode a sequence of bytes as a string in base-64.
79  * \sa decodeBase64 */
80 void encodeBase64(
81  const std::vector<uint8_t>& inputData, std::string& outString);
82 
83 /** Decode a base-64 string into the original sequence of bytes.
84  * \sa encodeBase64
85  * \return false on invalid base-64 string passed as input, true on success.
86  */
87 bool decodeBase64(const std::string& inString, std::vector<uint8_t>& outData);
88 
89 /** This function implements formatting with the appropriate SI metric unit
90  * prefix: 1e-12->'p', 1e-9->'n', 1e-6->'u', 1e-3->'m', 1->'', 1e3->'K',
91  * 1e6->'M', 1e9->'G', 1e12->'T' \sa intervalFormat */
92 std::string unitsFormat(
93  const double val, int nDecimalDigits = 2, bool middle_space = true);
94 
95 /** Enlarge the string with spaces up to the given length. */
96 std::string rightPad(
97  const std::string& str, const size_t total_len,
98  bool truncate_if_larger = false);
99 
100 /** Return true if the two strings are equal (case sensitive) \sa strCmpI */
101 bool strCmp(const std::string& s1, const std::string& s2);
102 
103 /** Return true if the two strings are equal (case insensitive) \sa strCmp */
104 bool strCmpI(const std::string& s1, const std::string& s2);
105 
106 /** Return true if "str" starts with "subStr" (case sensitive) \sa strStartsI
107  */
108 bool strStarts(const std::string& str, const std::string& subStr);
109 
110 /** Return true if "str" starts with "subStr" (case insensitive) \sa strStarts
111  */
112 bool strStartsI(const std::string& str, const std::string& subStr);
113 
114 /** Generates a string for a container in the format [A,B,C,...], and the
115  * fmt string for <b>each</b> vector element.
116  */
117 template <typename T>
118 std::string sprintf_container(const char* fmt, const T& V)
119 {
120  std::string ret = "[";
121  typename T::const_iterator it = V.begin();
122  for (; it != V.end();)
123  {
124  ret += format(fmt, *it);
125  ++it;
126  if (it != V.end()) ret += ",";
127  }
128  ret += "]";
129  return ret;
130 }
131 
132 /** @brief Convert a string list to one single string with new-lines. */
133 void stringListAsString(
134  const std::vector<std::string>& lst, std::string& out,
135  const std::string& newline = "\r\n");
136 /** \overload */
137 void stringListAsString(
138  const std::deque<std::string>& lst, std::string& out,
139  const std::string& newline = "\r\n");
140 
141 /** Original code snippet found in http://stackoverflow.com/a/30357710 */
142 /**\{*/
143 
144 /** @brief Convert string instance to number */
145 template <typename T>
147 {
148  std::stringstream sin;
149  sin << value;
150  T output;
151  sin >> output;
152  return output;
153 }
154 
155 /**\}*/
156 
157 /** @} */
158 } // namespace system
159 } // namespace mrpt
unsigned __int16 uint16_t
Definition: rptypes.h:44
bool strStartsI(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case insensitive)
void decodeUTF8(const std::string &strUTF8, std::vector< uint16_t > &out_uniStr)
Decodes a UTF-8 string into an UNICODE string.
bool strCmp(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case sensitive)
STL namespace.
void stringListAsString(const std::vector< std::string > &lst, std::string &out, const std::string &newline="\")
Convert a string list to one single string with new-lines.
void encodeUTF8(const std::vector< uint16_t > &input, std::string &output)
Encodes a 2-bytes UNICODE string into a UTF-8 string.
void tokenize(const std::string &inString, const std::string &inDelimiters, OUT_CONTAINER &outTokens, bool skipBlankTokens=true) noexcept
Tokenizes a string according to a set of delimiting characters.
unsigned char uint8_t
Definition: rptypes.h:41
bool decodeBase64(const std::string &inString, std::vector< uint8_t > &outData)
Decode a base-64 string into the original sequence of bytes.
Definition: base64.cpp:89
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.
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:16
GLsizei const GLchar ** string
Definition: glext.h:4101
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;.
void encodeBase64(const std::vector< uint8_t > &inputData, std::string &outString)
Encode a sequence of bytes as a string in base-64.
Definition: base64.cpp:29
T str2num(std::string const &value)
Original code snippet found in http://stackoverflow.com/a/30357710.
Definition: string_utils.h:146
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.
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:118
const Scalar * const_iterator
Definition: eigen_plugins.h:27



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019