1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 | #include "OgreStableHeaders.h"
|
---|
26 | #include "OgreString.h"
|
---|
27 |
|
---|
28 | #include "OgreStringVector.h"
|
---|
29 |
|
---|
30 | namespace Ogre {
|
---|
31 |
|
---|
32 | //-----------------------------------------------------------------------
|
---|
33 | const String StringUtil::BLANK = String("");
|
---|
34 | //-----------------------------------------------------------------------
|
---|
35 | void StringUtil::trim(String& str, bool left, bool right)
|
---|
36 | {
|
---|
37 | /*
|
---|
38 | size_t lspaces, rspaces, len = length(), i;
|
---|
39 |
|
---|
40 | lspaces = rspaces = 0;
|
---|
41 |
|
---|
42 | if( left )
|
---|
43 | {
|
---|
44 | // Find spaces / tabs on the left
|
---|
45 | for( i = 0;
|
---|
46 | i < len && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
|
---|
47 | ++lspaces, ++i );
|
---|
48 | }
|
---|
49 |
|
---|
50 | if( right && lspaces < len )
|
---|
51 | {
|
---|
52 | // Find spaces / tabs on the right
|
---|
53 | for( i = len - 1;
|
---|
54 | i >= 0 && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
|
---|
55 | rspaces++, i-- );
|
---|
56 | }
|
---|
57 |
|
---|
58 | *this = substr(lspaces, len-lspaces-rspaces);
|
---|
59 | */
|
---|
60 | static const String delims = " \t\r";
|
---|
61 | if(right)
|
---|
62 | str.erase(str.find_last_not_of(delims)+1); // trim right
|
---|
63 | if(left)
|
---|
64 | str.erase(0, str.find_first_not_of(delims)); // trim left
|
---|
65 | }
|
---|
66 |
|
---|
67 | //-----------------------------------------------------------------------
|
---|
68 | std::vector<String> StringUtil::split( const String& str, const String& delims, unsigned int maxSplits)
|
---|
69 | {
|
---|
70 | // static unsigned dl;
|
---|
71 | std::vector<String> ret;
|
---|
72 | unsigned int numSplits = 0;
|
---|
73 |
|
---|
74 | // Use STL methods
|
---|
75 | size_t start, pos;
|
---|
76 | start = 0;
|
---|
77 | do
|
---|
78 | {
|
---|
79 | pos = str.find_first_of(delims, start);
|
---|
80 | if (pos == start)
|
---|
81 | {
|
---|
82 | // Do nothing
|
---|
83 | start = pos + 1;
|
---|
84 | }
|
---|
85 | else if (pos == String::npos || (maxSplits && numSplits == maxSplits))
|
---|
86 | {
|
---|
87 | // Copy the rest of the string
|
---|
88 | ret.push_back( str.substr(start) );
|
---|
89 | break;
|
---|
90 | }
|
---|
91 | else
|
---|
92 | {
|
---|
93 | // Copy up to delimiter
|
---|
94 | ret.push_back( str.substr(start, pos - start) );
|
---|
95 | start = pos + 1;
|
---|
96 | }
|
---|
97 | // parse up to next real data
|
---|
98 | start = str.find_first_not_of(delims, start);
|
---|
99 | ++numSplits;
|
---|
100 |
|
---|
101 | } while (pos != String::npos);
|
---|
102 |
|
---|
103 |
|
---|
104 |
|
---|
105 | return ret;
|
---|
106 | }
|
---|
107 |
|
---|
108 | //-----------------------------------------------------------------------
|
---|
109 | void StringUtil::toLowerCase(String& str)
|
---|
110 | {
|
---|
111 | std::transform(
|
---|
112 | str.begin(),
|
---|
113 | str.end(),
|
---|
114 | str.begin(),
|
---|
115 | tolower);
|
---|
116 | }
|
---|
117 |
|
---|
118 | //-----------------------------------------------------------------------
|
---|
119 | void StringUtil::toUpperCase(String& str)
|
---|
120 | {
|
---|
121 | std::transform(
|
---|
122 | str.begin(),
|
---|
123 | str.end(),
|
---|
124 | str.begin(),
|
---|
125 | toupper);
|
---|
126 | }
|
---|
127 | //-----------------------------------------------------------------------
|
---|
128 | bool StringUtil::startsWith(const String& str, const String& pattern, bool lowerCase)
|
---|
129 | {
|
---|
130 | size_t thisLen = str.length();
|
---|
131 | size_t patternLen = pattern.length();
|
---|
132 | if (thisLen < patternLen || patternLen == 0)
|
---|
133 | return false;
|
---|
134 |
|
---|
135 | String startOfThis = str.substr(0, patternLen);
|
---|
136 | if (lowerCase)
|
---|
137 | StringUtil::toLowerCase(startOfThis);
|
---|
138 |
|
---|
139 | return (startOfThis == pattern);
|
---|
140 | }
|
---|
141 | //-----------------------------------------------------------------------
|
---|
142 | bool StringUtil::endsWith(const String& str, const String& pattern, bool lowerCase)
|
---|
143 | {
|
---|
144 | size_t thisLen = str.length();
|
---|
145 | size_t patternLen = pattern.length();
|
---|
146 | if (thisLen < patternLen || patternLen == 0)
|
---|
147 | return false;
|
---|
148 |
|
---|
149 | String endOfThis = str.substr(thisLen - patternLen, patternLen);
|
---|
150 | if (lowerCase)
|
---|
151 | StringUtil::toLowerCase(endOfThis);
|
---|
152 |
|
---|
153 | return (endOfThis == pattern);
|
---|
154 | }
|
---|
155 | //-----------------------------------------------------------------------
|
---|
156 | String StringUtil::standardisePath(const String& init)
|
---|
157 | {
|
---|
158 | String path = init;
|
---|
159 |
|
---|
160 | std::replace( path.begin(), path.end(), '\\', '/' );
|
---|
161 | if( path[path.length() - 1] != '/' )
|
---|
162 | path += '/';
|
---|
163 |
|
---|
164 | return path;
|
---|
165 | }
|
---|
166 | //-----------------------------------------------------------------------
|
---|
167 | void StringUtil::splitFilename(const String& qualifiedName,
|
---|
168 | String& outBasename, String& outPath)
|
---|
169 | {
|
---|
170 | String path = qualifiedName;
|
---|
171 | // Replace \ with / first
|
---|
172 | std::replace( path.begin(), path.end(), '\\', '/' );
|
---|
173 | // split based on final /
|
---|
174 | size_t i = path.find_last_of('/');
|
---|
175 |
|
---|
176 | if (i == String::npos)
|
---|
177 | {
|
---|
178 | outPath = "";
|
---|
179 | outBasename = qualifiedName;
|
---|
180 | }
|
---|
181 | else
|
---|
182 | {
|
---|
183 | outBasename = path.substr(i+1, path.size() - i - 1);
|
---|
184 | outPath = path.substr(0, i+1);
|
---|
185 | }
|
---|
186 |
|
---|
187 | }
|
---|
188 | //-----------------------------------------------------------------------
|
---|
189 | bool StringUtil::match(const String& str, const String& pattern, bool caseSensitive)
|
---|
190 | {
|
---|
191 | String tmpStr = str;
|
---|
192 | String tmpPattern = pattern;
|
---|
193 | if (!caseSensitive)
|
---|
194 | {
|
---|
195 | StringUtil::toLowerCase(tmpStr);
|
---|
196 | StringUtil::toLowerCase(tmpPattern);
|
---|
197 | }
|
---|
198 |
|
---|
199 | String::const_iterator strIt = tmpStr.begin();
|
---|
200 | String::const_iterator patIt = tmpPattern.begin();
|
---|
201 | String::const_iterator lastWildCardIt = tmpPattern.end();
|
---|
202 | while (strIt != tmpStr.end() && patIt != tmpPattern.end())
|
---|
203 | {
|
---|
204 | if (*patIt == '*')
|
---|
205 | {
|
---|
206 | lastWildCardIt = patIt;
|
---|
207 | // Skip over looking for next character
|
---|
208 | ++patIt;
|
---|
209 | if (patIt == tmpPattern.end())
|
---|
210 | {
|
---|
211 | // Skip right to the end since * matches the entire rest of the string
|
---|
212 | strIt = tmpStr.end();
|
---|
213 | }
|
---|
214 | else
|
---|
215 | {
|
---|
216 | // scan until we find next pattern character
|
---|
217 | while(strIt != tmpStr.end() && *strIt != *patIt)
|
---|
218 | ++strIt;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | else
|
---|
222 | {
|
---|
223 | if (*patIt != *strIt)
|
---|
224 | {
|
---|
225 | if (lastWildCardIt != tmpPattern.end())
|
---|
226 | {
|
---|
227 | // The last wildcard can match this incorrect sequence
|
---|
228 | // rewind pattern to wildcard and keep searching
|
---|
229 | patIt = lastWildCardIt;
|
---|
230 | lastWildCardIt = tmpPattern.end();
|
---|
231 | }
|
---|
232 | else
|
---|
233 | {
|
---|
234 | // no wildwards left
|
---|
235 | return false;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | else
|
---|
239 | {
|
---|
240 | ++patIt;
|
---|
241 | ++strIt;
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | }
|
---|
246 | // If we reached the end of both the pattern and the string, we succeeded
|
---|
247 | if (patIt == tmpPattern.end() && strIt == tmpStr.end())
|
---|
248 | {
|
---|
249 | return true;
|
---|
250 | }
|
---|
251 | else
|
---|
252 | {
|
---|
253 | return false;
|
---|
254 | }
|
---|
255 |
|
---|
256 | }
|
---|
257 |
|
---|
258 | }
|
---|