Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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
13#include <deque>
14
15namespace mrpt
16{
17 namespace system
18 {
19 /** \addtogroup string_manage String management and utilities (in #include <mrpt/system/string_utils.h>)
20 * \ingroup mrpt_base_grp
21 * @{ */
22
23 /** An OS-independent method for tokenizing a string.
24 * The extra parameter "context" must be a pointer to a "char*" variable, which needs no initialization and is used to save information between calls to strtok.
25 * \sa system::tokenize
26 */
27 char BASE_IMPEXP *strtok( char *str, const char *strDelimit, char **context ) MRPT_NO_THROWS;
28
29 /** Tokenizes a string according to a set of delimiting characters.
30 * Example:
31 * \code
32 std::vector<std::string> tokens;
33 tokenize( " - Pepe-Er Muo"," -",tokens);
34 * \endcode
35 *
36 * Will generate 3 tokens:
37 * - "Pepe"
38 * - "Er"
39 * - "Muo"
40 * \param[in] skipBlankTokens If `true`, consecutive "delimiters" will be considered one single delimiters. If `false`, a blank token will be returned between each pair of delimiters.
41 */
43 const std::string & inString,
44 const std::string & inDelimiters,
45 std::deque<std::string> & outTokens,
46 bool skipBlankTokens = true) MRPT_NO_THROWS;
47 /** \overload */
49 const std::string & inString,
50 const std::string & inDelimiters,
51 std::vector<std::string> & outTokens,
52 bool skipBlankTokens = true) MRPT_NO_THROWS;
53
54 /** Removes leading and trailing spaces */
55 std::string BASE_IMPEXP trim(const std::string &str);
56
57 /** Returns a lower-case version of a string.
58 * \sa lowerCase */
59 std::string BASE_IMPEXP upperCase(const std::string& str);
60
61 /** Returns an upper-case version of a string.
62 * \sa upperCase */
63 std::string BASE_IMPEXP 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 http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
67 */
68 void BASE_IMPEXP decodeUTF8( const std::string &strUTF8, vector_word &out_uniStr );
69
70 /** Encodes a 2-bytes UNICODE string into a UTF-8 string.
71 * See http://en.wikipedia.org/wiki/UTF-8 and http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
72 */
73 void BASE_IMPEXP encodeUTF8( const vector_word &input, std::string &output );
74
75 /** Encode a sequence of bytes as a string in base-64.
76 * \sa decodeBase64 */
77 void BASE_IMPEXP encodeBase64( const vector_byte &inputData, std::string &outString );
78
79 /** Decode a base-64 string into the original sequence of bytes.
80 * \sa encodeBase64
81 * \return false on invalid base-64 string passed as input, true on success.
82 */
83 bool BASE_IMPEXP decodeBase64( const std::string &inString, vector_byte &outData );
84
85 /** This function implements formatting with the appropriate SI metric unit prefix: 1e-12->'p', 1e-9->'n', 1e-6->'u', 1e-3->'m', 1->'', 1e3->'K', 1e6->'M', 1e9->'G', 1e12->'T'
86 * \sa intervalFormat */
87 std::string BASE_IMPEXP unitsFormat(const double val,int nDecimalDigits=2, bool middle_space=true);
88
89 /** Enlarge the string with spaces up to the given length. */
90 std::string BASE_IMPEXP rightPad(const std::string &str, const size_t total_len, bool truncate_if_larger = false);
91
92 /** Return true if the two strings are equal (case sensitive) \sa strCmpI */
93 bool BASE_IMPEXP strCmp(const std::string &s1, const std::string &s2);
94
95 /** Return true if the two strings are equal (case insensitive) \sa strCmp */
96 bool BASE_IMPEXP strCmpI(const std::string &s1, const std::string &s2);
97
98 /** Return true if "str" starts with "subStr" (case sensitive) \sa strStartsI */
99 bool BASE_IMPEXP strStarts(const std::string &str, const std::string &subStr);
100
101 /** Return true if "str" starts with "subStr" (case insensitive) \sa strStarts */
102 bool BASE_IMPEXP strStartsI(const std::string &str, const std::string &subStr);
103
104 /** Generates a string for a container in the format [A,B,C,...], and the fmt string for <b>each</b> vector element. */
105 template <typename T>
106 std::string sprintf_container(const char *fmt, const T &V )
107 {
108 std::string ret = "[";
109 typename T::const_iterator it=V.begin();
110 for (;it!=V.end();)
111 {
112 ret+= format(fmt,*it);
113 ++it;
114 if (it!=V.end())
115 ret+= ",";
116 }
117 ret+="]";
118 return ret;
119 }
120
121 /** @} */
122 } // End of namespace
123} // End of namespace
124
125#endif
std::vector< uint8_t > vector_byte
Definition: types_simple.h:26
std::vector< uint16_t > vector_word
Definition: types_simple.h:27
void BASE_IMPEXP encodeUTF8(const vector_word &input, std::string &output)
Encodes a 2-bytes UNICODE string into a UTF-8 string.
std::string BASE_IMPEXP trim(const std::string &str)
Removes leading and trailing spaces.
void BASE_IMPEXP decodeUTF8(const std::string &strUTF8, vector_word &out_uniStr)
Decodes a UTF-8 string into an UNICODE string.
bool BASE_IMPEXP strCmpI(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case insensitive)
void BASE_IMPEXP encodeBase64(const vector_byte &inputData, std::string &outString)
Encode a sequence of bytes as a string in base-64.
std::string BASE_IMPEXP upperCase(const std::string &str)
Returns a lower-case version of a string.
std::string BASE_IMPEXP 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',...
bool BASE_IMPEXP strStarts(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case sensitive)
char BASE_IMPEXP * strtok(char *str, const char *strDelimit, char **context) MRPT_NO_THROWS
An OS-independent method for tokenizing a string.
bool BASE_IMPEXP strCmp(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case sensitive)
bool BASE_IMPEXP strStartsI(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case insensitive)
void BASE_IMPEXP tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) MRPT_NO_THROWS
Tokenizes a string according to a set of delimiting characters.
bool BASE_IMPEXP decodeBase64(const std::string &inString, vector_byte &outData)
Decode a base-64 string into the original sequence of bytes.
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:106
std::string BASE_IMPEXP 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.
std::string BASE_IMPEXP lowerCase(const std::string &str)
Returns an upper-case version of a string.
#define MRPT_NO_THROWS
Used after member declarations.
Definition: mrpt_macros.h:391
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
STL namespace.



Page generated by Doxygen 1.9.5 for MRPT 1.4.0 SVN: at Tue Dec 27 00:54:45 UTC 2022