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.
24 #include "strutils.hxx"
33 split_whitespace( const string& str, int maxsplit )
35 vector<string> result;
36 string::size_type len = str.length();
37 string::size_type i = 0;
43 while (i < len && isspace((unsigned char)str[i]))
50 while (i < len && !isspace((unsigned char)str[i]))
57 result.push_back( str.substr(j, i-j) );
59 while (i < len && isspace((unsigned char)str[i]))
64 if (maxsplit && (countsplit >= maxsplit) && i < len)
66 result.push_back( str.substr( i, len-i ) );
79 split( const string& str, const char* sep, int maxsplit )
82 return split_whitespace( str, maxsplit );
84 vector<string> result;
85 int n = strlen( sep );
88 // Error: empty separator string
91 const char* s = str.c_str();
92 string::size_type len = str.length();
93 string::size_type i = 0;
94 string::size_type j = 0;
99 if (s[i] == sep[0] && (n == 1 || memcmp(s+i, sep, n) == 0))
101 result.push_back( str.substr(j,i-j) );
104 if (maxsplit && (splitcount >= maxsplit))
113 result.push_back( str.substr(j,len-j) );
118 * The lstrip(), rstrip() and strip() functions are implemented
119 * in do_strip() which uses an additional parameter to indicate what
120 * type of strip should occur.
122 const int LEFTSTRIP = 0;
123 const int RIGHTSTRIP = 1;
124 const int BOTHSTRIP = 2;
127 do_strip( const string& s, int striptype )
132 string::size_type len = s.length();
133 string::size_type i = 0;
134 if (striptype != RIGHTSTRIP)
136 while (i < len && isspace(s[i]))
142 string::size_type j = len;
143 if (striptype != LEFTSTRIP)
149 while (j >= 1 && isspace(s[j]));
153 if (i == 0 && j == len)
159 return s.substr( i, j - i );
164 lstrip( const string& s )
166 return do_strip( s, LEFTSTRIP );
170 rstrip( const string& s )
172 return do_strip( s, RIGHTSTRIP );
176 strip( const string& s )
178 return do_strip( s, BOTHSTRIP );
181 } // end namespace strutils
182 } // end namespace simgear