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::system
18 {
19 /** \addtogroup string_manage String management and utilities
20  * Header: `#include <mrpt/system/string_utils.h>`.
21  * Library: \ref mrpt_system_grp
22  * \ingroup mrpt_system_grp
23  * @{ */
24 
25 /** An OS-independent method for tokenizing a string.
26  * The extra parameter "context" must be a pointer to a "char*" variable, which
27  * needs no initialization and is used to save information between calls to
28  * strtok.
29  * \sa system::tokenize
30  */
31 char* strtok(char* str, const char* strDelimit, char** context) noexcept;
32 
33 /** Tokenizes a string according to a set of delimiting characters.
34  * Example:
35  * \code
36  std::vector<std::string> tokens;
37  tokenize(" - Pepe-Er Muo"," -",tokens);
38  * \endcode
39  *
40  * Will generate 3 tokens:
41  * - "Pepe"
42  * - "Er"
43  * - "Muo"
44  * \param[in] skipBlankTokens If `true`, consecutive "delimiters" will be
45  considered one single delimiters. If `false`, a blank token will be returned
46  between each pair of delimiters.
47  * \tparam OUT_CONTAINER Can be a std::vector or std::deque of std::string's.
48  */
49 template <class OUT_CONTAINER>
50 void tokenize(
51  const std::string& inString, const std::string& inDelimiters,
52  OUT_CONTAINER& outTokens, bool skipBlankTokens = true) noexcept;
53 
54 /** Removes leading and trailing spaces */
55 std::string trim(const std::string& str);
56 
57 /** Returns a upper-case version of a string.
58  * \sa lowerCase */
59 std::string upperCase(const std::string& str);
60 
61 /** Returns an lower-case version of a string.
62  * \sa upperCase */
63 std::string 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
67  * http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
68  */
69 void decodeUTF8(const std::string& strUTF8, std::vector<uint16_t>& out_uniStr);
70 
71 /** Encodes a 2-bytes UNICODE string into a UTF-8 string.
72  * See http://en.wikipedia.org/wiki/UTF-8 and
73  * http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
74  */
75 void encodeUTF8(const std::vector<uint16_t>& input, std::string& output);
76 
77 /** Encode a sequence of bytes as a string in base-64.
78  * \sa decodeBase64 */
79 void encodeBase64(
80  const std::vector<uint8_t>& 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, std::vector<uint8_t>& 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 /** @brief Convert a string list to one single string with new-lines. */
132 void stringListAsString(
133  const std::vector<std::string>& lst, std::string& out,
134  const std::string& newline = "\r\n");
135 /** \overload */
136 void stringListAsString(
137  const std::deque<std::string>& lst, std::string& out,
138  const std::string& newline = "\r\n");
139 
140 /** Original code snippet found in http://stackoverflow.com/a/30357710 */
141 /**\{*/
142 
143 /** @brief Convert string instance to number */
144 template <typename T>
146 {
147  std::stringstream sin;
148  sin << value;
149  T output;
150  sin >> output;
151  return output;
152 }
153 
154 /**\}*/
155 
156 /** @} */
157 } // namespace mrpt::system
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
GLsizei const GLchar ** string
Definition: glext.h:4101
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
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
T str2num(std::string const &value)
Original code snippet found in http://stackoverflow.com/a/30357710.
Definition: string_utils.h:145
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:117
const Scalar * const_iterator
Definition: eigen_plugins.h:27



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020