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 )
137 string::size_type len = s.length();
138 string::size_type i = 0;
139 if (striptype != RIGHTSTRIP)
141 while (i < len && isspace(s[i]))
147 string::size_type j = len;
148 if (striptype != LEFTSTRIP)
154 while (j >= 1 && isspace(s[j]));
158 if (i == 0 && j == len)
164 return s.substr( i, j - i );
169 lstrip( const string& s )
171 return do_strip( s, LEFTSTRIP );
175 rstrip( const string& s )
177 return do_strip( s, RIGHTSTRIP );
181 strip( const string& s )
183 return do_strip( s, BOTHSTRIP );
186 } // end namespace strutils
187 } // end namespace simgear