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"
35 split_whitespace( const string& str, int maxsplit )
37 vector<string> result;
38 string::size_type len = str.length();
39 string::size_type i = 0;
45 while (i < len && isspace((unsigned char)str[i]))
52 while (i < len && !isspace((unsigned char)str[i]))
59 result.push_back( str.substr(j, i-j) );
61 while (i < len && isspace((unsigned char)str[i]))
66 if (maxsplit && (countsplit >= maxsplit) && i < len)
68 result.push_back( str.substr( i, len-i ) );
81 split( const string& str, const char* sep, int maxsplit )
84 return split_whitespace( str, maxsplit );
86 vector<string> result;
87 int n = std::strlen( sep );
90 // Error: empty separator string
93 const char* s = str.c_str();
94 string::size_type len = str.length();
95 string::size_type i = 0;
96 string::size_type j = 0;
101 if (s[i] == sep[0] && (n == 1 || std::memcmp(s+i, sep, n) == 0))
103 result.push_back( str.substr(j,i-j) );
106 if (maxsplit && (splitcount >= maxsplit))
115 result.push_back( str.substr(j,len-j) );
120 * The lstrip(), rstrip() and strip() functions are implemented
121 * in do_strip() which uses an additional parameter to indicate what
122 * type of strip should occur.
124 const int LEFTSTRIP = 0;
125 const int RIGHTSTRIP = 1;
126 const int BOTHSTRIP = 2;
129 do_strip( const string& s, int striptype )
134 string::size_type len = s.length();
135 string::size_type i = 0;
136 if (striptype != RIGHTSTRIP)
138 while (i < len && isspace(s[i]))
144 string::size_type j = len;
145 if (striptype != LEFTSTRIP)
151 while (j >= 1 && isspace(s[j]));
155 if (i == 0 && j == len)
161 return s.substr( i, j - i );
166 lstrip( const string& s )
168 return do_strip( s, LEFTSTRIP );
172 rstrip( const string& s )
174 return do_strip( s, RIGHTSTRIP );
178 strip( const string& s )
180 return do_strip( s, BOTHSTRIP );
183 } // end namespace strutils
184 } // end namespace simgear