]> git.mxchange.org Git - simgear.git/blob - simgear/misc/strutils.hxx
Merge remote branch 'origin/releases/2.2.0' into next
[simgear.git] / simgear / misc / strutils.hxx
1 /**
2  * \file strutils.hxx
3  * String utilities.
4  */
5
6 // Written by Bernie Bright, started 1998
7 //
8 // Copyright (C) 1998  Bernie Bright - bbright@bigpond.net.au
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 //
24 // $Id$
25
26
27 #ifndef STRUTILS_H
28 #define STRUTILS_H
29
30 #include <simgear/compiler.h>
31
32 #include <string>
33 #include <vector>
34 #include <cstdlib>
35
36
37 namespace simgear {
38   namespace strutils {
39
40 //      /** 
41 //       * atof() wrapper for "string" type
42 //       */
43 //      inline double
44 //      atof( const string& str )
45 //      {
46 //          return ::atof( str.c_str() );
47 //      }
48
49 //      /**
50 //       * atoi() wrapper for "string" type
51 //       */
52 //      inline int
53 //      atoi( const string& str )
54 //      {
55 //          return ::atoi( str.c_str() );
56 //      }
57
58         /**
59          * Strip leading and/or trailing whitespace from s.
60          * @param s String to strip.
61          * @return The stripped string.
62          */
63         std::string lstrip( const std::string& s );
64         std::string rstrip( const std::string& s );
65         std::string strip( const std::string& s );
66
67         /**
68          * Right-padding of a string to a given length
69          * @param s String to pad
70          * @param length The total length of the resulting string
71          * @param c The character to pad with
72          * @return The padded string
73          */
74         std::string rpad( const std::string & s, size_t length, char c );
75
76         /**
77          * Left-padding of a string to a given length
78          * @param s String to pad
79          * @param length The total length of the resulting string
80          * @param c The character to pad with
81          * @return The padded string
82          */
83         std::string lpad( const std::string & s, size_t length, char c );
84
85         /**
86          * Split a string into a words using 'sep' as the delimiter string.
87          * Produces a result similar to the perl and python functions of the
88          * same name.
89          * 
90          * @param s The string to split into words,
91          * @param sep Word delimiters.  If not specified then any whitespace is a separator,
92          * @param maxsplit If given, splits at no more than maxsplit places,
93          * resulting in at most maxsplit+1 words.
94          * @return Array of words.
95          */
96         std::vector<std::string>
97         split( const std::string& s,
98                const char* sep = 0,
99                int maxsplit = 0 );
100
101         /**
102          * Test if a string starts with a string 
103          *
104          * @param s The string to be tested
105          * @param substr The string to test
106          * @return True, if s starts with substr, False otherwise
107          */
108         bool starts_with( const std::string & s, const std::string & substr );
109
110         /**
111          * Test if a string ends with a string 
112          *
113          * @param s The string to be tested
114          * @param substr The string to test
115          * @return True, if s ends with substr, False otherwise
116          */
117         bool ends_with( const std::string & s, const std::string & substr );
118   
119   } // end namespace strutils
120 } // end namespace simgear
121
122 #endif // STRUTILS_H
123