1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
27 #include "stringOps.H"
30 /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
32 const char* const Foam::string::typeName = "string";
33 int Foam::string::debug(Foam::debug::debugSwitch(string::typeName, 0));
34 const Foam::string Foam::string::null;
37 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
39 // Count and return the number of a given character in the string
40 Foam::string::size_type Foam::string::count(const char c) const
42 register size_type cCount = 0;
44 for (const_iterator iter = begin(); iter != end(); ++iter)
56 // Replace first occurence of sub-string oldStr with newStr
57 Foam::string& Foam::string::replace
64 size_type newStart = start;
66 if ((newStart = find(oldStr, newStart)) != npos)
68 std::string::replace(newStart, oldStr.size(), newStr);
75 // Replace all occurences of sub-string oldStr with newStr
76 Foam::string& Foam::string::replaceAll
85 size_type newStart = start;
87 while ((newStart = find(oldStr, newStart)) != npos)
89 std::string::replace(newStart, oldStr.size(), newStr);
90 newStart += newStr.size();
98 Foam::string& Foam::string::expand(const bool allowEmpty)
100 stringOps::inplaceExpand(*this, allowEmpty);
105 // Remove repeated characters returning true if string changed
106 bool Foam::string::removeRepeated(const char character)
108 bool changed = false;
110 if (character && find(character) != npos)
112 register string::size_type nChar=0;
113 iterator iter2 = begin();
115 register char prev = 0;
119 string::const_iterator iter1 = iter2;
124 register char c = *iter1;
126 if (prev == c && c == character)
144 // Return string with repeated characters removed
145 Foam::string Foam::string::removeRepeated(const char character) const
148 str.removeRepeated(character);
153 // Remove trailing character returning true if string changed
154 bool Foam::string::removeTrailing(const char character)
156 bool changed = false;
158 string::size_type nChar = size();
159 if (character && nChar > 1 && operator[](nChar-1) == character)
169 // Return string with trailing character removed
170 Foam::string Foam::string::removeTrailing(const char character) const
173 str.removeTrailing(character);
178 // ************************************************************************* //