]> git.mxchange.org Git - simgear.git/blob - simgear/misc/strutils.cxx
- new FSF addresses
[simgear.git] / simgear / misc / strutils.cxx
1 // String utilities.
2 //
3 // Written by Bernie Bright, started 1998
4 //
5 // Copyright (C) 1998  Bernie Bright - bbright@bigpond.net.au
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #include <ctype.h>
24 #include "strutils.hxx"
25
26 namespace simgear {
27     namespace strutils {
28
29         /**
30          * 
31          */
32         static vector<string>
33         split_whitespace( const string& str, int maxsplit )
34         {
35             vector<string> result;
36             string::size_type len = str.length();
37             string::size_type i = 0;
38             string::size_type j;
39             int countsplit = 0;
40
41             while (i < len)
42             {
43                 while (i < len && isspace((unsigned char)str[i]))
44                 {
45                     ++i;
46                 }
47
48                 j = i;
49
50                 while (i < len && !isspace((unsigned char)str[i]))
51                 {
52                     ++i;
53                 }
54
55                 if (j < i)
56                 {
57                     result.push_back( str.substr(j, i-j) );
58                     ++countsplit;
59                     while (i < len && isspace((unsigned char)str[i]))
60                     {
61                         ++i;
62                     }
63
64                     if (maxsplit && (countsplit >= maxsplit) && i < len)
65                     {
66                         result.push_back( str.substr( i, len-i ) );
67                         i = len;
68                     }
69                 }
70             }
71
72             return result;
73         }
74
75         /**
76          * 
77          */
78         vector<string>
79         split( const string& str, const char* sep, int maxsplit )
80         {
81             if (sep == 0)
82                 return split_whitespace( str, maxsplit );
83
84             vector<string> result;
85             int n = strlen( sep );
86             if (n == 0)
87             {
88                 // Error: empty separator string
89                 return result;
90             }
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;
95             int splitcount = 0;
96
97             while (i+n <= len)
98             {
99                 if (s[i] == sep[0] && (n == 1 || memcmp(s+i, sep, n) == 0))
100                 {
101                     result.push_back( str.substr(j,i-j) );
102                     i = j = i + n;
103                     ++splitcount;
104                     if (maxsplit && (splitcount >= maxsplit))
105                         break;
106                 }
107                 else
108                 {
109                     ++i;
110                 }
111             }
112
113             result.push_back( str.substr(j,len-j) );
114             return result;
115         }
116
117         /**
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.
121          */
122         const int LEFTSTRIP = 0;
123         const int RIGHTSTRIP = 1;
124         const int BOTHSTRIP = 2;
125
126         static string
127         do_strip( const string& s, int striptype )
128         {
129             //     if (s.empty())
130             //      return s;
131
132             string::size_type len = s.length();
133             string::size_type i = 0;
134             if (striptype != RIGHTSTRIP)
135             {
136                 while (i < len && isspace(s[i]))
137                 {
138                     ++i;
139                 }
140             }
141
142             string::size_type j = len;
143             if (striptype != LEFTSTRIP)
144             {
145                 do
146                 {
147                     --j;
148                 }
149                 while (j >= 1 && isspace(s[j]));
150                 ++j;
151             }
152
153             if (i == 0 && j == len)
154             {
155                 return s;
156             }
157             else
158             {
159                 return s.substr( i, j - i );
160             }
161         }
162
163         string
164         lstrip( const string& s )
165         {
166             return do_strip( s, LEFTSTRIP );
167         }
168
169         string
170         rstrip( const string& s )
171         {
172             return do_strip( s, RIGHTSTRIP );
173         }
174
175         string
176         strip( const string& s )
177         {
178             return do_strip( s, BOTHSTRIP );
179         }
180
181     } // end namespace strutils
182 } // end namespace simgear