MRPT  2.0.4
string_utils.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <cstdint>
12 #include <deque>
13 #include <sstream>
14 #include <string>
15 #include <vector>
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 // explicit instantiations declarations (to silent gcc warnings):
55 extern template void tokenize<std::deque<std::string>>(
56  const std::string& inString, const std::string& inDelimiters,
57  std::deque<std::string>& outTokens, bool skipBlankTokens) noexcept;
58 extern template void tokenize<std::vector<std::string>>(
59  const std::string& inString, const std::string& inDelimiters,
60  std::vector<std::string>& outTokens, bool skipBlankTokens) noexcept;
61 
62 /** Removes leading and trailing spaces */
63 std::string trim(const std::string& str);
64 
65 /** Returns a upper-case version of a string.
66  * \sa lowerCase */
67 std::string upperCase(const std::string& str);
68 
69 /** Returns an lower-case version of a string.
70  * \sa upperCase */
71 std::string lowerCase(const std::string& str);
72 
73 /** Decodes a UTF-8 string into an UNICODE string.
74  * See http://en.wikipedia.org/wiki/UTF-8 and
75  * http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
76  */
77 void decodeUTF8(const std::string& strUTF8, std::vector<uint16_t>& out_uniStr);
78 
79 /** Encodes a 2-bytes UNICODE string into a UTF-8 string.
80  * See http://en.wikipedia.org/wiki/UTF-8 and
81  * http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
82  */
83 void encodeUTF8(const std::vector<uint16_t>& input, std::string& output);
84 
85 /** Encode a sequence of bytes as a string in base-64.
86  * \sa decodeBase64 */
87 void encodeBase64(
88  const std::vector<uint8_t>& inputData, std::string& outString);
89 
90 /** Decode a base-64 string into the original sequence of bytes.
91  * \sa encodeBase64
92  * \return false on invalid base-64 string passed as input, true on success.
93  */
94 bool decodeBase64(const std::string& inString, std::vector<uint8_t>& outData);
95 
96 /** This function implements formatting with the appropriate SI metric unit
97  * prefix: 1e-12->'p', 1e-9->'n', 1e-6->'u', 1e-3->'m', 1->'', 1e3->'K',
98  * 1e6->'M', 1e9->'G', 1e12->'T' \sa intervalFormat */
99 std::string unitsFormat(
100  const double val, int nDecimalDigits = 2, bool middle_space = true);
101 
102 /** Enlarge the string with spaces up to the given length. */
103 std::string rightPad(
104  const std::string& str, const size_t total_len,
105  bool truncate_if_larger = false);
106 
107 /** Return true if the two strings are equal (case sensitive) \sa strCmpI */
108 bool strCmp(const std::string& s1, const std::string& s2);
109 
110 /** Return true if the two strings are equal (case insensitive) \sa strCmp */
111 bool strCmpI(const std::string& s1, const std::string& s2);
112 
113 /** Return true if "str" starts with "subStr" (case sensitive) \sa strStartsI
114  */
115 bool strStarts(const std::string& str, const std::string& subStr);
116 
117 /** Return true if "str" starts with "subStr" (case insensitive) \sa strStarts
118  */
119 bool strStartsI(const std::string& str, const std::string& subStr);
120 
121 /** Generates a string for a container in the format [A,B,C,...], and the
122  * fmt string for <b>each</b> vector element.
123  */
124 template <typename T>
125 std::string sprintf_container(const char* fmt, const T& V)
126 {
127  std::string ret = "[";
128  auto it = V.begin();
129  for (; it != V.end();)
130  {
131  ret += format(fmt, *it);
132  ++it;
133  if (it != V.end()) ret += ",";
134  }
135  ret += "]";
136  return ret;
137 }
138 
139 /** @brief Convert a string list to one single string with new-lines. */
140 void stringListAsString(
141  const std::vector<std::string>& lst, std::string& out,
142  const std::string& newline = "\r\n");
143 /** \overload */
144 void stringListAsString(
145  const std::deque<std::string>& lst, std::string& out,
146  const std::string& newline = "\r\n");
147 
148 /** Original code snippet found in http://stackoverflow.com/a/30357710 */
149 /**\{*/
150 
151 /** @brief Convert string instance to number */
152 template <typename T>
153 T str2num(std::string const& value)
154 {
155  std::stringstream sin;
156  sin << value;
157  T output;
158  sin >> output;
159  return output;
160 }
161 
162 /**\}*/
163 
164 /** @} */
165 } // namespace mrpt::system
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
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.
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:88
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:957
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
mrpt::vision::TStereoCalibResults out
T str2num(std::string const &value)
Original code snippet found in http://stackoverflow.com/a/30357710.
Definition: string_utils.h:153
std::string upperCase(const std::string &str)
Returns a upper-case version of a string.
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.
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:125



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020