root/cafu/trunk/Libs/String.hpp

Revision 457, 3.3 KB (checked in by Carsten, 4 months ago)

Using UltraEdit's multi-line search-and-replace-in-files feature, replaced


^#ifndef _(CAFU|CF|CFS|CA)_(.*)_$
^#define _\1_\2_$

with

#ifndef CAFU_\2_INCLUDED
#define CAFU_\2_INCLUDED

and


^#ifndef _(.*)_HPP_$
^#define _\1_HPP_$

with

#ifndef CAFU_\1_HPP_INCLUDED
#define CAFU_\1_HPP_INCLUDED

Closes #91.

Line 
1/*
2=================================================================================
3This file is part of Cafu, the open-source game engine and graphics engine
4for multiplayer, cross-platform, real-time 3D action.
5Copyright (C) 2002-2012 Carsten Fuchs Software.
6
7Cafu is free software: you can redistribute it and/or modify it under the terms
8of the GNU General Public License as published by the Free Software Foundation,
9either version 3 of the License, or (at your option) any later version.
10
11Cafu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with Cafu. If not, see <http://www.gnu.org/licenses/>.
17
18For support and more information about Cafu, visit us at <http://www.cafu.de>.
19=================================================================================
20*/
21
22#ifndef CAFU_STRING_HPP_INCLUDED
23#define CAFU_STRING_HPP_INCLUDED
24
25#include <string>
26
27
28namespace cf
29{
30    /// The String namespace gathers auxiliary string functions that are not found in that standard library.
31    namespace String
32    {
33        /// Returns whether the given string \c String ends with the given \c Suffix.
34        inline bool EndsWith(const std::string& String, const std::string& Suffix)
35        {
36            const size_t StringLength=String.length();
37            const size_t SuffixLength=Suffix.length();
38
39            if (StringLength<SuffixLength) return false;
40
41            std::string StringSuffix=&(String.c_str()[StringLength-SuffixLength]);
42
43            return StringSuffix==Suffix;
44        }
45
46        /// Assumes that the given string \c s is a filename, removes the extension, if any, and returns the rest.
47        inline std::string StripExt(std::string s)
48        {
49            const size_t PosDot=s.find_last_of('.');
50            const size_t PosSep=s.find_last_of("/\\");
51
52            if (PosDot==std::string::npos) return s;                    // "." not found in s?
53            if (PosSep!=std::string::npos && PosDot<PosSep) return s;   // Last "." found before last "/"?
54
55            s.erase(PosDot);
56            return s;
57        }
58
59        /// Assumes that the given string \c s is a filename of pattern "path/filename.ext" and returns the path portion.
60        inline std::string GetPath(std::string s)
61        {
62            const size_t PosSep=s.find_last_of("/\\");
63
64            if (PosSep==std::string::npos) return "";   // "/" not found in s?
65
66            s.erase(PosSep);
67            return s;
68        }
69
70        /// Replaces in \c s all occurrences of \c search by \c replace, and returns the new string.
71        inline std::string Replace(std::string s, const std::string& search, const std::string& replace)
72        {
73            const size_t len_search =search.length(); if (len_search==0) return s;
74            const size_t len_replace=replace.length();
75
76            size_t pos=s.find(search);
77
78            while (pos!=std::string::npos)
79            {
80                s.replace(pos, len_search, replace);
81                pos=s.find(search, pos+len_replace);
82            }
83
84            return s;
85        }
86    }
87}
88
89#endif
Note: See TracBrowser for help on using the browser.