3 // Written by Bernie Bright, started 1998
5 // Copyright (C) 1998 Bernie Bright - bbright@bigpond.net.au
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include "strutils.hxx"
38 split_whitespace( const string& str, int maxsplit )
40 vector<string> result;
41 string::size_type len = str.length();
42 string::size_type i = 0;
48 while (i < len && isspace((unsigned char)str[i]))
55 while (i < len && !isspace((unsigned char)str[i]))
62 result.push_back( str.substr(j, i-j) );
64 while (i < len && isspace((unsigned char)str[i]))
69 if (maxsplit && (countsplit >= maxsplit) && i < len)
71 result.push_back( str.substr( i, len-i ) );
84 split( const string& str, const char* sep, int maxsplit )
87 return split_whitespace( str, maxsplit );
89 vector<string> result;
90 int n = std::strlen( sep );
93 // Error: empty separator string
96 const char* s = str.c_str();
97 string::size_type len = str.length();
98 string::size_type i = 0;
99 string::size_type j = 0;
104 if (s[i] == sep[0] && (n == 1 || std::memcmp(s+i, sep, n) == 0))
106 result.push_back( str.substr(j,i-j) );
109 if (maxsplit && (splitcount >= maxsplit))
118 result.push_back( str.substr(j,len-j) );
123 * The lstrip(), rstrip() and strip() functions are implemented
124 * in do_strip() which uses an additional parameter to indicate what
125 * type of strip should occur.
127 const int LEFTSTRIP = 0;
128 const int RIGHTSTRIP = 1;
129 const int BOTHSTRIP = 2;
132 do_strip( const string& s, int striptype )
134 string::size_type len = s.length();
135 if( len == 0 ) // empty string is trivial
137 string::size_type i = 0;
138 if (striptype != RIGHTSTRIP)
140 while (i < len && isspace(s[i]))
146 string::size_type j = len;
147 if (striptype != LEFTSTRIP)
153 while (j >= 1 && isspace(s[j]));
157 if (i == 0 && j == len)
163 return s.substr( i, j - i );
168 lstrip( const string& s )
170 return do_strip( s, LEFTSTRIP );
174 rstrip( const string& s )
176 return do_strip( s, RIGHTSTRIP );
180 strip( const string& s )
182 return do_strip( s, BOTHSTRIP );
186 rpad( const string & s, string::size_type length, char c )
188 string::size_type l = s.length();
189 if( l >= length ) return s;
191 return reply.append( length-l, c );
195 lpad( const string & s, size_t length, char c )
197 string::size_type l = s.length();
198 if( l >= length ) return s;
200 return reply.insert( 0, length-l, c );
204 starts_with( const string & s, const string & substr )
206 return s.find( substr ) == 0;
210 ends_with( const string & s, const string & substr )
212 size_t n = s.rfind( substr );
213 return (n != string::npos) && (n == s.length() - substr.length());
216 string simplify(const string& s)
218 string result; // reserve size of 's'?
219 string::const_iterator it = s.begin(),
222 // advance to first non-space char - simplifes logic in main loop,
223 // since we can always prepend a single space when we see a
224 // space -> non-space transition
225 for (; (it != end) && isspace(*it); ++it) { /* nothing */ }
227 bool lastWasSpace = false;
228 for (; it != end; ++it) {
236 result.push_back(' ');
239 lastWasSpace = false;
246 int to_int(const std::string& s)
248 return atoi(s.c_str());
251 } // end namespace strutils
253 } // end namespace simgear