/src/ogre/OgreMain/src/OgreString.cpp
Line | Count | Source |
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-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #include "OgreStableHeaders.h" |
29 | | |
30 | | // A quick define to overcome different names for the same function |
31 | | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WINRT |
32 | | # define strnicmp _strnicmp |
33 | | #else |
34 | 0 | # define strnicmp strncasecmp |
35 | | #endif |
36 | | |
37 | | namespace Ogre { |
38 | | const String& StringUtil::BLANK = BLANKSTRING; |
39 | | |
40 | | //----------------------------------------------------------------------- |
41 | | void StringUtil::trim(String& str, bool left, bool right) |
42 | 3.02k | { |
43 | | /* |
44 | | size_t lspaces, rspaces, len = length(), i; |
45 | | |
46 | | lspaces = rspaces = 0; |
47 | | |
48 | | if( left ) |
49 | | { |
50 | | // Find spaces / tabs on the left |
51 | | for( i = 0; |
52 | | i < len && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r'); |
53 | | ++lspaces, ++i ); |
54 | | } |
55 | | |
56 | | if( right && lspaces < len ) |
57 | | { |
58 | | // Find spaces / tabs on the right |
59 | | for( i = len - 1; |
60 | | i >= 0 && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r'); |
61 | | rspaces++, i-- ); |
62 | | } |
63 | | |
64 | | *this = substr(lspaces, len-lspaces-rspaces); |
65 | | */ |
66 | 3.02k | static const String delims = " \t\r\n"; |
67 | 3.02k | if(right) |
68 | 3.02k | str.erase(str.find_last_not_of(delims)+1); // trim right |
69 | 3.02k | if(left) |
70 | 3.02k | str.erase(0, str.find_first_not_of(delims)); // trim left |
71 | 3.02k | } |
72 | | |
73 | | //----------------------------------------------------------------------- |
74 | | StringVector StringUtil::split( const String& str, const String& delims, unsigned int maxSplits, bool preserveDelims) |
75 | 3.02k | { |
76 | 3.02k | StringVector ret; |
77 | | // Pre-allocate some space for performance |
78 | 3.02k | ret.reserve(maxSplits ? maxSplits+1 : 10); // 10 is guessed capacity for most case |
79 | | |
80 | 3.02k | unsigned int numSplits = 0; |
81 | | |
82 | | // Use STL methods |
83 | 3.02k | size_t start, pos; |
84 | 3.02k | start = 0; |
85 | 3.02k | do |
86 | 33.2k | { |
87 | 33.2k | pos = str.find_first_of(delims, start); |
88 | 33.2k | if (pos == start) |
89 | 0 | { |
90 | | // Do nothing |
91 | 0 | start = pos + 1; |
92 | 0 | } |
93 | 33.2k | else if (pos == String::npos || (maxSplits && numSplits == maxSplits)) |
94 | 3.02k | { |
95 | | // Copy the rest of the string |
96 | 3.02k | ret.push_back( str.substr(start) ); |
97 | 3.02k | break; |
98 | 3.02k | } |
99 | 30.2k | else |
100 | 30.2k | { |
101 | | // Copy up to delimiter |
102 | 30.2k | ret.push_back( str.substr(start, pos - start) ); |
103 | | |
104 | 30.2k | if(preserveDelims) |
105 | 0 | { |
106 | | // Sometimes there could be more than one delimiter in a row. |
107 | | // Loop until we don't find any more delims |
108 | 0 | size_t delimStart = pos, delimPos; |
109 | 0 | delimPos = str.find_first_not_of(delims, delimStart); |
110 | 0 | if (delimPos == String::npos) |
111 | 0 | { |
112 | | // Copy the rest of the string |
113 | 0 | ret.push_back( str.substr(delimStart) ); |
114 | 0 | } |
115 | 0 | else |
116 | 0 | { |
117 | 0 | ret.push_back( str.substr(delimStart, delimPos - delimStart) ); |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | 30.2k | start = pos + 1; |
122 | 30.2k | } |
123 | | // parse up to next real data |
124 | 30.2k | start = str.find_first_not_of(delims, start); |
125 | 30.2k | ++numSplits; |
126 | | |
127 | 30.2k | } while (pos != String::npos); |
128 | | |
129 | | |
130 | | |
131 | 3.02k | return ret; |
132 | 3.02k | } |
133 | | //----------------------------------------------------------------------- |
134 | | StringVector StringUtil::tokenise( const String& str, const String& singleDelims, const String& doubleDelims, unsigned int maxSplits) |
135 | 0 | { |
136 | 0 | StringVector ret; |
137 | | // Pre-allocate some space for performance |
138 | 0 | ret.reserve(maxSplits ? maxSplits+1 : 10); // 10 is guessed capacity for most case |
139 | |
|
140 | 0 | unsigned int numSplits = 0; |
141 | 0 | String delims = singleDelims + doubleDelims; |
142 | | |
143 | | // Use STL methods |
144 | 0 | size_t start, pos; |
145 | 0 | char curDoubleDelim = 0; |
146 | 0 | start = 0; |
147 | 0 | do |
148 | 0 | { |
149 | 0 | if (curDoubleDelim != 0) |
150 | 0 | { |
151 | 0 | pos = str.find(curDoubleDelim, start); |
152 | 0 | } |
153 | 0 | else |
154 | 0 | { |
155 | 0 | pos = str.find_first_of(delims, start); |
156 | 0 | } |
157 | |
|
158 | 0 | if (pos == start) |
159 | 0 | { |
160 | 0 | char curDelim = str.at(pos); |
161 | 0 | if (doubleDelims.find_first_of(curDelim) != String::npos) |
162 | 0 | { |
163 | 0 | curDoubleDelim = curDelim; |
164 | 0 | } |
165 | | // Do nothing |
166 | 0 | start = pos + 1; |
167 | 0 | } |
168 | 0 | else if (pos == String::npos || (maxSplits && numSplits == maxSplits)) |
169 | 0 | { |
170 | 0 | if (curDoubleDelim != 0) |
171 | 0 | { |
172 | | //Missing closer. Warn or throw exception? |
173 | 0 | } |
174 | | // Copy the rest of the string |
175 | 0 | ret.push_back( str.substr(start) ); |
176 | 0 | break; |
177 | 0 | } |
178 | 0 | else |
179 | 0 | { |
180 | 0 | if (curDoubleDelim != 0) |
181 | 0 | { |
182 | 0 | curDoubleDelim = 0; |
183 | 0 | } |
184 | | |
185 | | // Copy up to delimiter |
186 | 0 | ret.push_back( str.substr(start, pos - start) ); |
187 | 0 | start = pos + 1; |
188 | 0 | } |
189 | 0 | if (curDoubleDelim == 0) |
190 | 0 | { |
191 | | // parse up to next real data |
192 | 0 | start = str.find_first_not_of(singleDelims, start); |
193 | 0 | } |
194 | | |
195 | 0 | ++numSplits; |
196 | |
|
197 | 0 | } while (start != String::npos); |
198 | |
|
199 | 0 | return ret; |
200 | 0 | } |
201 | | //----------------------------------------------------------------------- |
202 | | void StringUtil::toLowerCase(String& str) |
203 | 4.15k | { |
204 | 4.15k | std::transform( |
205 | 4.15k | str.begin(), |
206 | 4.15k | str.end(), |
207 | 4.15k | str.begin(), |
208 | 4.15k | tolower); |
209 | 4.15k | } |
210 | | |
211 | | //----------------------------------------------------------------------- |
212 | | void StringUtil::toUpperCase(String& str) |
213 | 0 | { |
214 | 0 | std::transform( |
215 | 0 | str.begin(), |
216 | 0 | str.end(), |
217 | 0 | str.begin(), |
218 | 0 | toupper); |
219 | 0 | } |
220 | | //----------------------------------------------------------------------- |
221 | | void StringUtil::toTitleCase(String& str) |
222 | 0 | { |
223 | 0 | String::iterator it = str.begin(); |
224 | 0 | *it = toupper(*it); |
225 | 0 | for (; it != str.end() - 1; it++) |
226 | 0 | { |
227 | 0 | if (*it == ' ') |
228 | 0 | { |
229 | 0 | *(it + 1) = toupper(*(it + 1)); |
230 | 0 | } |
231 | 0 | } |
232 | 0 | } |
233 | | //----------------------------------------------------------------------- |
234 | | bool StringUtil::startsWith(const String& str, const String& pattern, bool lowerCase) |
235 | 0 | { |
236 | 0 | if (pattern.empty()) |
237 | 0 | return false; |
238 | | |
239 | 0 | if (lowerCase) |
240 | 0 | { |
241 | 0 | return strnicmp(str.c_str(), pattern.c_str(), pattern.size()) == 0; |
242 | 0 | } |
243 | | |
244 | 0 | return strncmp(str.c_str(), pattern.c_str(), pattern.size()) == 0; |
245 | 0 | } |
246 | | //----------------------------------------------------------------------- |
247 | | bool StringUtil::endsWith(const String& str, const String& pattern, bool lowerCase) |
248 | 0 | { |
249 | 0 | if (pattern.empty()) |
250 | 0 | return false; |
251 | | |
252 | 0 | size_t offset = str.size() - pattern.size(); |
253 | |
|
254 | 0 | if (lowerCase) |
255 | 0 | { |
256 | 0 | return strnicmp(str.c_str() + offset, pattern.c_str(), pattern.size()) == 0; |
257 | 0 | } |
258 | | |
259 | 0 | return strncmp(str.c_str() + offset, pattern.c_str(), pattern.size()) == 0; |
260 | 0 | } |
261 | | //----------------------------------------------------------------------- |
262 | | String StringUtil::standardisePath(const String& init) |
263 | 0 | { |
264 | 0 | String path = init; |
265 | |
|
266 | 0 | std::replace( path.begin(), path.end(), '\\', '/' ); |
267 | 0 | if( path[path.length() - 1] != '/' ) |
268 | 0 | path += '/'; |
269 | |
|
270 | 0 | return path; |
271 | 0 | } |
272 | | //----------------------------------------------------------------------- |
273 | | String StringUtil::normalizeFilePath(const String& init, bool makeLowerCase) |
274 | 0 | { |
275 | 0 | const char* bufferSrc = init.c_str(); |
276 | 0 | int pathLen = (int)init.size(); |
277 | 0 | int indexSrc = 0; |
278 | 0 | int indexDst = 0; |
279 | 0 | int metaPathArea = 0; |
280 | |
|
281 | 0 | char reservedBuf[1024]; |
282 | 0 | char* bufferDst = reservedBuf; |
283 | 0 | bool isDestAllocated = false; |
284 | 0 | if (pathLen > 1023) |
285 | 0 | { |
286 | | //if source path is to long ensure we don't do a buffer overrun by allocating some |
287 | | //new memory |
288 | 0 | isDestAllocated = true; |
289 | 0 | bufferDst = new char[pathLen + 1]; |
290 | 0 | } |
291 | | |
292 | | //The outer loop loops over directories |
293 | 0 | while (indexSrc < pathLen) |
294 | 0 | { |
295 | 0 | if (indexSrc && ((bufferSrc[indexSrc] == '\\') || (bufferSrc[indexSrc] == '/'))) |
296 | 0 | { |
297 | | //check if we have a directory delimiter if so skip it (we should already |
298 | | //have written such a delimiter by this point |
299 | 0 | ++indexSrc; |
300 | 0 | continue; |
301 | 0 | } |
302 | 0 | else |
303 | 0 | { |
304 | | //check if there is a directory to skip of type ".\" |
305 | 0 | if ((bufferSrc[indexSrc] == '.') && |
306 | 0 | ((bufferSrc[indexSrc + 1] == '\\') || (bufferSrc[indexSrc + 1] == '/'))) |
307 | 0 | { |
308 | 0 | indexSrc += 2; |
309 | 0 | continue; |
310 | 0 | } |
311 | | |
312 | | //check if there is a directory to skip of type "..\" |
313 | 0 | else if ((bufferSrc[indexSrc] == '.') && (bufferSrc[indexSrc + 1] == '.') && |
314 | 0 | ((bufferSrc[indexSrc + 2] == '\\') || (bufferSrc[indexSrc + 2] == '/'))) |
315 | 0 | { |
316 | 0 | if (indexDst > metaPathArea) |
317 | 0 | { |
318 | | //skip a directory backward in the destination path |
319 | 0 | do { |
320 | 0 | --indexDst; |
321 | 0 | } |
322 | 0 | while ((indexDst > metaPathArea) && (bufferDst[indexDst - 1] != '/')); |
323 | 0 | indexSrc += 3; |
324 | 0 | continue; |
325 | 0 | } |
326 | 0 | else |
327 | 0 | { |
328 | | //we are about to write "..\" to the destination buffer |
329 | | //ensure we will not remove this in future "skip directories" |
330 | 0 | metaPathArea += 3; |
331 | 0 | } |
332 | 0 | } |
333 | 0 | } |
334 | | |
335 | | //transfer the current directory name from the source to the destination |
336 | 0 | while (indexSrc < pathLen) |
337 | 0 | { |
338 | 0 | char curChar = bufferSrc[indexSrc]; |
339 | 0 | if (makeLowerCase) curChar = tolower(curChar); |
340 | 0 | if ((curChar == '\\') || (curChar == '/')) curChar = '/'; |
341 | 0 | bufferDst[indexDst] = curChar; |
342 | 0 | ++indexDst; |
343 | 0 | ++indexSrc; |
344 | 0 | if (curChar == '/') break; |
345 | 0 | } |
346 | 0 | } |
347 | 0 | bufferDst[indexDst] = 0; |
348 | |
|
349 | 0 | String normalized(bufferDst); |
350 | 0 | if (isDestAllocated) |
351 | 0 | { |
352 | 0 | delete[] bufferDst; |
353 | 0 | } |
354 | |
|
355 | 0 | return normalized; |
356 | 0 | } |
357 | | //----------------------------------------------------------------------- |
358 | | void StringUtil::splitFilename(const String& qualifiedName, |
359 | | String& outBasename, String& outPath) |
360 | 0 | { |
361 | 0 | String path = qualifiedName; |
362 | | // Replace \ with / first |
363 | 0 | std::replace( path.begin(), path.end(), '\\', '/' ); |
364 | | // split based on final / |
365 | 0 | size_t i = path.find_last_of('/'); |
366 | |
|
367 | 0 | if (i == String::npos) |
368 | 0 | { |
369 | 0 | outPath.clear(); |
370 | 0 | outBasename = qualifiedName; |
371 | 0 | } |
372 | 0 | else |
373 | 0 | { |
374 | 0 | outBasename = path.substr(i+1, path.size() - i - 1); |
375 | 0 | outPath = path.substr(0, i+1); |
376 | 0 | } |
377 | |
|
378 | 0 | } |
379 | | //----------------------------------------------------------------------- |
380 | | void StringUtil::splitBaseFilename(const Ogre::String& fullName, |
381 | | Ogre::String& outBasename, Ogre::String& outExtention) |
382 | 1.14k | { |
383 | 1.14k | size_t i = fullName.find_last_of('.'); |
384 | 1.14k | if (i == Ogre::String::npos) |
385 | 0 | { |
386 | 0 | outExtention.clear(); |
387 | 0 | outBasename = fullName; |
388 | 0 | } |
389 | 1.14k | else |
390 | 1.14k | { |
391 | 1.14k | outExtention = fullName.substr(i+1); |
392 | 1.14k | outBasename = fullName.substr(0, i); |
393 | 1.14k | } |
394 | 1.14k | } |
395 | | // ---------------------------------------------------------------------------------------------------------------------------------------------- |
396 | | void StringUtil::splitFullFilename( const Ogre::String& qualifiedName, |
397 | | Ogre::String& outBasename, Ogre::String& outExtention, Ogre::String& outPath ) |
398 | 0 | { |
399 | 0 | Ogre::String fullName; |
400 | 0 | splitFilename( qualifiedName, fullName, outPath ); |
401 | 0 | splitBaseFilename( fullName, outBasename, outExtention ); |
402 | 0 | } |
403 | | //----------------------------------------------------------------------- |
404 | | bool StringUtil::match(const String& str, const String& pattern, bool caseSensitive) |
405 | 0 | { |
406 | 0 | String tmpStr = str; |
407 | 0 | String tmpPattern = pattern; |
408 | 0 | if (!caseSensitive) |
409 | 0 | { |
410 | 0 | StringUtil::toLowerCase(tmpStr); |
411 | 0 | StringUtil::toLowerCase(tmpPattern); |
412 | 0 | } |
413 | |
|
414 | 0 | String::const_iterator strIt = tmpStr.begin(); |
415 | 0 | String::const_iterator patIt = tmpPattern.begin(); |
416 | 0 | String::const_iterator lastWildCardIt = tmpPattern.end(); |
417 | 0 | while (strIt != tmpStr.end() && patIt != tmpPattern.end()) |
418 | 0 | { |
419 | 0 | if (*patIt == '*') |
420 | 0 | { |
421 | 0 | lastWildCardIt = patIt; |
422 | | // Skip over looking for next character |
423 | 0 | ++patIt; |
424 | 0 | if (patIt == tmpPattern.end()) |
425 | 0 | { |
426 | | // Skip right to the end since * matches the entire rest of the string |
427 | 0 | strIt = tmpStr.end(); |
428 | 0 | } |
429 | 0 | else |
430 | 0 | { |
431 | | // scan until we find next pattern character |
432 | 0 | while(strIt != tmpStr.end() && *strIt != *patIt) |
433 | 0 | ++strIt; |
434 | 0 | } |
435 | 0 | } |
436 | 0 | else |
437 | 0 | { |
438 | 0 | if (*patIt != *strIt) |
439 | 0 | { |
440 | 0 | if (lastWildCardIt != tmpPattern.end()) |
441 | 0 | { |
442 | | // The last wildcard can match this incorrect sequence |
443 | | // rewind pattern to wildcard and keep searching |
444 | 0 | patIt = lastWildCardIt; |
445 | 0 | lastWildCardIt = tmpPattern.end(); |
446 | 0 | } |
447 | 0 | else |
448 | 0 | { |
449 | | // no wildwards left |
450 | 0 | return false; |
451 | 0 | } |
452 | 0 | } |
453 | 0 | else |
454 | 0 | { |
455 | 0 | ++patIt; |
456 | 0 | ++strIt; |
457 | 0 | } |
458 | 0 | } |
459 | |
|
460 | 0 | } |
461 | | // If we reached the end of both the pattern and the string, we succeeded |
462 | 0 | if ((patIt == tmpPattern.end() || (*patIt == '*' && patIt + 1 == tmpPattern.end())) && strIt == tmpStr.end()) |
463 | 0 | { |
464 | 0 | return true; |
465 | 0 | } |
466 | 0 | else |
467 | 0 | { |
468 | 0 | return false; |
469 | 0 | } |
470 | |
|
471 | 0 | } |
472 | | //----------------------------------------------------------------------- |
473 | | const String StringUtil::replaceAll(const String& source, const String& replaceWhat, const String& replaceWithWhat) |
474 | 0 | { |
475 | 0 | String result = source; |
476 | 0 | String::size_type pos = 0; |
477 | 0 | while(1) |
478 | 0 | { |
479 | 0 | pos = result.find(replaceWhat,pos); |
480 | 0 | if (pos == String::npos) break; |
481 | 0 | result.replace(pos,replaceWhat.size(),replaceWithWhat); |
482 | 0 | pos += replaceWithWhat.size(); |
483 | 0 | } |
484 | 0 | return result; |
485 | 0 | } |
486 | | |
487 | | String StringUtil::format(const char* fmt, ...) |
488 | 0 | { |
489 | | // try to use a stack buffer and fall back to heap for large strings |
490 | 0 | char sbuf[1024]; |
491 | 0 | size_t bsize = sizeof(sbuf); |
492 | 0 | std::vector<char> hbuf; |
493 | 0 | char* pbuf = sbuf; |
494 | |
|
495 | 0 | while (true) |
496 | 0 | { |
497 | 0 | va_list va; |
498 | 0 | va_start(va, fmt); |
499 | 0 | int len = vsnprintf(pbuf, bsize, fmt, va); |
500 | 0 | va_end(va); |
501 | |
|
502 | 0 | OgreAssert(len >= 0, "Check format string for errors"); |
503 | 0 | if (size_t(len) >= bsize) |
504 | 0 | { |
505 | 0 | hbuf.resize(len + 1); |
506 | 0 | pbuf = hbuf.data(); |
507 | 0 | bsize = hbuf.size(); |
508 | 0 | continue; |
509 | 0 | } |
510 | 0 | pbuf[bsize - 1] = 0; |
511 | 0 | return String(pbuf, len); |
512 | 0 | } |
513 | 0 | } |
514 | | } |