Main MRPT website > C++ reference
MRPT logo

string_utils.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  MRPT_STRING_UTILS_H
00029 #define  MRPT_STRING_UTILS_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 
00033 namespace mrpt
00034 {
00035         namespace system
00036         {
00037                 /** @name String management and utilities
00038                 @{ */
00039 
00040                 /** An OS-independent method for tokenizing a string.
00041                   * 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.
00042                   * \sa system::tokenize
00043                   */
00044                 char BASE_IMPEXP  *strtok( char *str, const char *strDelimit, char **context ) MRPT_NO_THROWS;
00045 
00046                 /** Tokenizes a string according to a set of delimiting characters.
00047                   * Example:
00048                   * \code
00049                   std::vector<std::string>      tokens;
00050                   tokenize( " - Pepe-Er  Muo"," -",tokens);
00051                   * \endcode
00052                   *
00053                   *  Will generate 3 tokens:
00054                   *             - "Pepe"
00055                   *             - "Er"
00056                   *             - "Muo"
00057                   */
00058                 void  BASE_IMPEXP tokenize(
00059                         const std::string                       &inString,
00060                         const std::string                       &inDelimiters,
00061                         std::deque<std::string>         &outTokens ) MRPT_NO_THROWS;
00062 
00063                 /** Tokenizes a string according to a set of delimiting characters.
00064                   * Example:
00065                   * \code
00066                   std::vector<std::string>      tokens;
00067                   tokenize( " - Pepe-Er  Muo"," -",tokens);
00068                   * \endcode
00069                   *
00070                   *  Will generate 3 tokens:
00071                   *             - "Pepe"
00072                   *             - "Er"
00073                   *             - "Muo"
00074                   */
00075                 void BASE_IMPEXP  tokenize(
00076                         const std::string                       &inString,
00077                         const std::string                       &inDelimiters,
00078                         std::vector<std::string>                &outTokens ) MRPT_NO_THROWS;
00079 
00080                 /**  Removes leading and trailing spaces */
00081                 std::string BASE_IMPEXP trim(const std::string &str);
00082 
00083                 /** Returns a lower-case version of a string.
00084                   * \sa lowerCase  */
00085                 std::string  BASE_IMPEXP upperCase(const std::string& str);
00086 
00087                 /** Returns an upper-case version of a string.
00088                   * \sa upperCase  */
00089                 std::string  BASE_IMPEXP lowerCase(const std::string& str);
00090 
00091                 /** Decodes a UTF-8 string into an UNICODE string.
00092                   *  See http://en.wikipedia.org/wiki/UTF-8  and http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
00093                   */
00094                 void BASE_IMPEXP decodeUTF8( const std::string &strUTF8, vector_word &out_uniStr );
00095 
00096                 /** Encodes a 2-bytes UNICODE string into a UTF-8 string.
00097                   *  See http://en.wikipedia.org/wiki/UTF-8 and http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
00098                   */
00099         void BASE_IMPEXP encodeUTF8( const vector_word &input, std::string &output );
00100 
00101                 /** Encode a sequence of bytes as a string in base-64.
00102                   * \sa decodeBase64  */
00103                 void BASE_IMPEXP encodeBase64( const vector_byte &inputData,  std::string &outString );
00104 
00105                 /** Decode a base-64 string into the original sequence of bytes.
00106                   * \sa encodeBase64
00107                   * \return false on invalid base-64 string passed as input, true on success.
00108                   */
00109                 bool BASE_IMPEXP decodeBase64( const std::string &inString, vector_byte &outData );
00110 
00111                 /** 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'
00112                  * \sa intervalFormat */
00113                 std::string BASE_IMPEXP unitsFormat(const double val,int nDecimalDigits=2, bool middle_space=true);
00114 
00115                 /** Enlarge the string with spaces up to the given length. */
00116                 std::string BASE_IMPEXP rightPad(const std::string &str, const size_t total_len, bool truncate_if_larger = false);
00117 
00118                 /** Return true if the two strings are equal (case sensitive)  \sa strCmpI  */
00119                 bool BASE_IMPEXP strCmp(const std::string &s1, const std::string &s2);
00120 
00121                 /** Return true if the two strings are equal (case insensitive)  \sa strCmp */
00122                 bool BASE_IMPEXP strCmpI(const std::string &s1, const std::string &s2);
00123 
00124                 /** Return true if "str" starts with "subStr" (case sensitive)  \sa strStartsI  */
00125                 bool BASE_IMPEXP strStarts(const std::string &str, const std::string &subStr);
00126 
00127                 /** Return true if "str" starts with "subStr" (case insensitive)  \sa strStarts */
00128                 bool BASE_IMPEXP strStartsI(const std::string &str, const std::string &subStr);
00129 
00130                 /** @} */
00131         } // End of namespace
00132 } // End of namespace
00133 
00134 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011