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
const_iterator
const Scalar * const_iterator
Definition: eigen_plugins.h:27
uint16_t
unsigned __int16 uint16_t
Definition: rptypes.h:44
mrpt::system::strStartsI
bool strStartsI(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case insensitive)
Definition: string_utils.cpp:321
mrpt::system::strCmp
bool strCmp(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case sensitive)
Definition: string_utils.cpp:299
mrpt::system::decodeUTF8
void decodeUTF8(const std::string &strUTF8, std::vector< uint16_t > &out_uniStr)
Decodes a UTF-8 string into an UNICODE string.
Definition: string_utils.cpp:98
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::system::encodeUTF8
void encodeUTF8(const std::vector< uint16_t > &input, std::string &output)
Encodes a 2-bytes UNICODE string into a UTF-8 string.
Definition: string_utils.cpp:64
uint8_t
unsigned char uint8_t
Definition: rptypes.h:41
mrpt::system::decodeBase64
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
mrpt::system::tokenize
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.
mrpt::system::lowerCase
std::string lowerCase(const std::string &str)
Returns an lower-case version of a string.
Definition: string_utils.cpp:26
val
int val
Definition: mrpt_jpeglib.h:955
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
mrpt::system::strtok
char * strtok(char *str, const char *strDelimit, char **context) noexcept
An OS-independent method for tokenizing a string.
Definition: string_utils.cpp:197
mrpt::system::unitsFormat
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->'p',...
Definition: string_utils.cpp:136
mrpt::system::str2num
T str2num(std::string const &value)
Original code snippet found in http://stackoverflow.com/a/30357710.
Definition: string_utils.h:146
mrpt::system::encodeBase64
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
mrpt::system::rightPad
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.
Definition: string_utils.cpp:290
value
GLsizei const GLfloat * value
Definition: glext.h:4117
mrpt::system::strStarts
bool strStarts(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case sensitive)
Definition: string_utils.cpp:312
mrpt::system::upperCase
std::string upperCase(const std::string &str)
Returns a upper-case version of a string.
Definition: string_utils.cpp:40
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::system::trim
std::string trim(const std::string &str)
Removes leading and trailing spaces.
Definition: string_utils.cpp:270
mrpt::system::stringListAsString
void stringListAsString(const std::vector< std::string > &lst, std::string &out, const std::string &newline="\r\n")
Convert a string list to one single string with new-lines.
Definition: string_utils.cpp:351
input
GLenum GLenum GLenum input
Definition: glext.h:6499
mrpt::system::strCmpI
bool strCmpI(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case insensitive)
Definition: string_utils.cpp:305
mrpt::system::sprintf_container
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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST