iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StringUtil.H
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_UTIL_STRINGUTIL_H
37 #define INCLUDE_NRT_CORE_UTIL_STRINGUTIL_H
38 
39 #include <stdarg.h>
40 #include <string>
41 #include <vector>
42 
43 namespace nrt
44 {
45  /*! @defgroup stringutils String Utilities
46  Various helpful methods for dealing with std::strings
47  \ingroup util */
48 
49 
50  //! Replace all environment variables in the given string with the values found in the environment
51  /*! \note Use the helpers provided in Environment.H rather than replaceEnvVars() to access NRT-related environment
52  variables in a safer, strongly-typed manner.
53 
54  \ingroup stringutils */
55  std::string replaceEnvVars(std::string const & str);
56 
57  //! Split the string by the given delimiter
58  /*! \ingroup stringutils */
59  std::vector<std::string> splitString(std::string const & string, char const delimiter);
60 
61  //! Join a vector of strings with the given delimiter
62  /*! \ingroup stringutils */
63  std::string joinStrings(std::vector<std::string> const & strings, std::string const & delimiter);
64 
65  //! Return true is str starts with prefix (including if both strings are equal)
66  bool stringStartsWith(std::string const & str, std::string const & prefix);
67 
68  //! Trim whitespace from both ends of a string
69  /*! \ingroup stringutils */
70  std::string trimString(std::string const & s);
71 
72  //! Trim whitespace from the left side of a string
73  /*! \ingroup stringutils */
74  std::string lTrimString(std::string const & s);
75 
76  //! Trim whitespace from the right side of a string
77  /*! \ingroup stringutils */
78  std::string rTrimString(std::string const & s);
79 
80  //! Create a string using printf style arguments
81  /*! Example:
82  @code
83  std::string s = nrt::sformat("MyString_%f_%d", 1.0, 2);
84  @endcode
85  \ingroup stringutils */
86  std::string sformat(const char* fmt, ...)
87  // NOTE: this __attribute__ tells gcc that it should issue
88  // printf-style warnings when compiling calls to sformat(),
89  // treating the 1st argument (fmt) as the format string, and the
90  // 2nd and subsequent arguments as the printf-style parameters
91  __attribute__((format(__printf__, 1, 2)));
92 
93  //! Decode url-encoded strings
94  /*! \ingroup stringutils */
95  std::string urlDecode(std::string const & str);
96 }
97 #endif // INCLUDE_NRT_CORE_UTIL_STRINGUTIL_H
98 
99