Main MRPT website > C++ reference
MRPT logo
string_utils.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef MRPT_STRING_UTILS_H
29 #define MRPT_STRING_UTILS_H
30 
31 #include <mrpt/utils/utils_defs.h>
32 
33 namespace mrpt
34 {
35  namespace system
36  {
37  /** \addtogroup string_manage String management and utilities (in #include <mrpt/system/string_utils.h>)
38  * \ingroup mrpt_base_grp
39  * @{ */
40 
41  /** An OS-independent method for tokenizing a string.
42  * 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.
43  * \sa system::tokenize
44  */
45  char BASE_IMPEXP *strtok( char *str, const char *strDelimit, char **context ) MRPT_NO_THROWS;
46 
47  /** Tokenizes a string according to a set of delimiting characters.
48  * Example:
49  * \code
50  std::vector<std::string> tokens;
51  tokenize( " - Pepe-Er Muo"," -",tokens);
52  * \endcode
53  *
54  * Will generate 3 tokens:
55  * - "Pepe"
56  * - "Er"
57  * - "Muo"
58  */
59  void BASE_IMPEXP tokenize(
60  const std::string &inString,
61  const std::string &inDelimiters,
62  std::deque<std::string> &outTokens ) MRPT_NO_THROWS;
63 
64  /** Tokenizes a string according to a set of delimiting characters.
65  * Example:
66  * \code
67  std::vector<std::string> tokens;
68  tokenize( " - Pepe-Er Muo"," -",tokens);
69  * \endcode
70  *
71  * Will generate 3 tokens:
72  * - "Pepe"
73  * - "Er"
74  * - "Muo"
75  */
76  void BASE_IMPEXP tokenize(
77  const std::string &inString,
78  const std::string &inDelimiters,
79  std::vector<std::string> &outTokens ) MRPT_NO_THROWS;
80 
81  /** Removes leading and trailing spaces */
82  std::string BASE_IMPEXP trim(const std::string &str);
83 
84  /** Returns a lower-case version of a string.
85  * \sa lowerCase */
86  std::string BASE_IMPEXP upperCase(const std::string& str);
87 
88  /** Returns an upper-case version of a string.
89  * \sa upperCase */
90  std::string BASE_IMPEXP lowerCase(const std::string& str);
91 
92  /** Decodes a UTF-8 string into an UNICODE string.
93  * See http://en.wikipedia.org/wiki/UTF-8 and http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
94  */
95  void BASE_IMPEXP decodeUTF8( const std::string &strUTF8, vector_word &out_uniStr );
96 
97  /** Encodes a 2-bytes UNICODE string into a UTF-8 string.
98  * See http://en.wikipedia.org/wiki/UTF-8 and http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
99  */
100  void BASE_IMPEXP encodeUTF8( const vector_word &input, std::string &output );
101 
102  /** Encode a sequence of bytes as a string in base-64.
103  * \sa decodeBase64 */
104  void BASE_IMPEXP encodeBase64( const vector_byte &inputData, std::string &outString );
105 
106  /** Decode a base-64 string into the original sequence of bytes.
107  * \sa encodeBase64
108  * \return false on invalid base-64 string passed as input, true on success.
109  */
110  bool BASE_IMPEXP decodeBase64( const std::string &inString, vector_byte &outData );
111 
112  /** 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'
113  * \sa intervalFormat */
114  std::string BASE_IMPEXP unitsFormat(const double val,int nDecimalDigits=2, bool middle_space=true);
115 
116  /** Enlarge the string with spaces up to the given length. */
117  std::string BASE_IMPEXP rightPad(const std::string &str, const size_t total_len, bool truncate_if_larger = false);
118 
119  /** Return true if the two strings are equal (case sensitive) \sa strCmpI */
120  bool BASE_IMPEXP strCmp(const std::string &s1, const std::string &s2);
121 
122  /** Return true if the two strings are equal (case insensitive) \sa strCmp */
123  bool BASE_IMPEXP strCmpI(const std::string &s1, const std::string &s2);
124 
125  /** Return true if "str" starts with "subStr" (case sensitive) \sa strStartsI */
126  bool BASE_IMPEXP strStarts(const std::string &str, const std::string &subStr);
127 
128  /** Return true if "str" starts with "subStr" (case insensitive) \sa strStarts */
129  bool BASE_IMPEXP strStartsI(const std::string &str, const std::string &subStr);
130 
131  /** @} */
132  } // End of namespace
133 } // End of namespace
134 
135 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013