]> git.mxchange.org Git - simgear.git/blob - simgear/misc/strutils.cxx
Added a ptr() method to SGPropertyNode_ptr to get the raw pointer.
[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 Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA  02111-1307, USA.
21 //
22 // $Id$
23
24 #include <ctype.h>
25 #include "strutils.hxx"
26
27 namespace simgear {
28     namespace strutils {
29
30         /**
31          * 
32          */
33         static vector<string>
34         split_whitespace( const string& str, int maxsplit )
35         {
36             vector<string> result;
37             string::size_type len = str.length();
38             string::size_type i = 0;
39             string::size_type j;
40             int countsplit = 0;
41
42             while (i < len)
43             {
44                 while (i < len && isspace(str[i]))
45                 {
46                     ++i;
47                 }
48
49                 j = i;
50
51                 while (i < len && !isspace(str[i]))
52                 {
53                     ++i;
54                 }
55
56                 if (j < i)
57                 {
58                     result.push_back( str.substr(j, i-j) );
59                     ++countsplit;
60                     while (i < len && isspace(str[i]))
61                     {
62                         ++i;
63                     }
64
65                     if (maxsplit && (countsplit >= maxsplit) && i < len)
66                     {
67                         result.push_back( str.substr( i, len-i ) );
68                         i = len;
69                     }
70                 }
71             }
72
73             return result;
74         }
75
76         /**
77          * 
78          */
79         vector<string>
80         split( const string& str, const char* sep, int maxsplit )
81         {
82             if (sep == 0)
83                 return split_whitespace( str, maxsplit );
84
85             vector<string> result;
86             int n = strlen( sep );
87             if (n == 0)
88             {
89                 // Error: empty separator string
90                 return result;
91             }
92             const char* s = str.c_str();
93             string::size_type len = str.length();
94             string::size_type i = 0;
95             string::size_type j = 0;
96             int splitcount = 0;
97
98             while (i+n <= len)
99             {
100                 if (s[i] == sep[0] && (n == 1 || memcmp(s+i, sep, n) == 0))
101                 {
102                     result.push_back( str.substr(j,i-j) );
103                     i = j = i + n;
104                     ++splitcount;
105                     if (maxsplit && (splitcount >= maxsplit))
106                         break;
107                 }
108                 else
109                 {
110                     ++i;
111                 }
112             }
113
114             result.push_back( str.substr(j,len-j) );
115             return result;
116         }
117
118         /**
119          * The lstrip(), rstrip() and strip() functions are implemented
120          * in do_strip() which uses an additional parameter to indicate what
121          * type of strip should occur.
122          */
123         const int LEFTSTRIP = 0;
124         const int RIGHTSTRIP = 1;
125         const int BOTHSTRIP = 2;
126
127         static string
128         do_strip( const string& s, int striptype )
129         {
130             //     if (s.empty())
131             //      return s;
132
133             string::size_type len = s.length();
134             string::size_type i = 0;
135             if (striptype != RIGHTSTRIP)
136             {
137                 while (i < len && isspace(s[i]))
138                 {
139                     ++i;
140                 }
141             }
142
143             string::size_type j = len;
144             if (striptype != LEFTSTRIP)
145             {
146                 do
147                 {
148                     --j;
149                 }
150                 while (j >= 1 && isspace(s[j]));
151                 ++j;
152             }
153
154             if (i == 0 && j == len)
155             {
156                 return s;
157             }
158             else
159             {
160                 return s.substr( i, j - i );
161             }
162         }
163
164         string
165         lstrip( const string& s )
166         {
167             return do_strip( s, LEFTSTRIP );
168         }
169
170         string
171         rstrip( const string& s )
172         {
173             return do_strip( s, RIGHTSTRIP );
174         }
175
176         string
177         strip( const string& s )
178         {
179             return do_strip( s, BOTHSTRIP );
180         }
181
182     } // end namespace strutils
183 } // end namespace simgear