string.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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
19  for more details.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "string.H"
27 #include "stringOps.H"
28 
29 
30 /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
31 
32 const char* const Foam::string::typeName = "string";
33 int Foam::string::debug(Foam::debug::debugSwitch(string::typeName, 0));
35 
36 
37 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
38 
39 // Count and return the number of a given character in the string
41 {
42  size_type cCount = 0;
43 
44  for (const_iterator iter = begin(); iter != end(); ++iter)
45  {
46  if (*iter == c)
47  {
48  ++cCount;
49  }
50  }
51 
52  return cCount;
53 }
54 
55 
56 // Replace first occurence of sub-string oldStr with newStr
58 (
59  const string& oldStr,
60  const string& newStr,
61  size_type start
62 )
63 {
64  size_type newStart = start;
65 
66  if ((newStart = find(oldStr, newStart)) != npos)
67  {
68  std::string::replace(newStart, oldStr.size(), newStr);
69  }
70 
71  return *this;
72 }
73 
74 
75 // Replace all occurences of sub-string oldStr with newStr
77 (
78  const string& oldStr,
79  const string& newStr,
80  size_type start
81 )
82 {
83  if (oldStr.size())
84  {
85  size_type newStart = start;
86 
87  while ((newStart = find(oldStr, newStart)) != npos)
88  {
89  std::string::replace(newStart, oldStr.size(), newStr);
90  newStart += newStr.size();
91  }
92  }
93 
94  return *this;
95 }
96 
97 
98 Foam::string& Foam::string::expand(const bool allowEmpty)
99 {
100  stringOps::inplaceExpand(*this, allowEmpty);
101  return *this;
102 }
103 
104 
105 // Remove repeated characters returning true if string changed
106 bool Foam::string::removeRepeated(const char character)
107 {
108  bool changed = false;
109 
110  if (character && find(character) != npos)
111  {
112  string::size_type nChar=0;
113  iterator iter2 = begin();
114 
115  char prev = 0;
116 
117  for
118  (
119  string::const_iterator iter1 = iter2;
120  iter1 != end();
121  iter1++
122  )
123  {
124  char c = *iter1;
125 
126  if (prev == c && c == character)
127  {
128  changed = true;
129  }
130  else
131  {
132  *iter2 = prev = c;
133  ++iter2;
134  ++nChar;
135  }
136  }
137  resize(nChar);
138  }
139 
140  return changed;
141 }
142 
143 
144 // Return string with repeated characters removed
145 Foam::string Foam::string::removeRepeated(const char character) const
146 {
147  string str(*this);
148  str.removeRepeated(character);
149  return str;
150 }
151 
152 
153 // Remove trailing character returning true if string changed
154 bool Foam::string::removeTrailing(const char character)
155 {
156  bool changed = false;
157 
158  string::size_type nChar = size();
159  if (character && nChar > 1 && operator[](nChar-1) == character)
160  {
161  resize(nChar-1);
162  changed = true;
163  }
164 
165  return changed;
166 }
167 
168 
169 // Return string with trailing character removed
170 Foam::string Foam::string::removeTrailing(const char character) const
171 {
172  string str(*this);
173  str.removeTrailing(character);
174  return str;
175 }
176 
177 
178 // ************************************************************************* //
int debugSwitch(const char *name, const int defaultValue=0)
Lookup debug switch or add default value.
Definition: debug.C:165
string & expand(const bool allowEmpty=false)
Expand initial tildes and all occurences of environment variables.
Definition: string.C:98
static int debug
Definition: string.H:83
A class for handling character strings derived from std::string.
Definition: string.H:74
bool removeRepeated(const char)
Remove repeated characters returning true if string changed.
Definition: string.C:106
string & inplaceExpand(string &, const HashTable< string, word, string::hash > &mapping, const char sigil= '$')
Inplace expand occurences of variables according to the mapping.
Definition: stringOps.C:86
string & replaceAll(const string &oldStr, const string &newStr, size_type start=0)
Replace all occurences of sub-string oldStr with newStr.
Definition: string.C:77
static const string null
An empty string.
Definition: string.H:86
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
size_type count(const char) const
Count and return the number of a given character in the string.
Definition: string.C:40
bool removeTrailing(const char)
Remove trailing character returning true if string changed.
Definition: string.C:154
static const char *const typeName
Definition: string.H:82
string & replace(const string &oldStr, const string &newStr, size_type start=0)
Replace first occurence of sub-string oldStr with newStr.
Definition: string.C:58
const dimensionedScalar c
Speed of light in a vacuum.
triSurfaceToAgglom resize(surfacesMesh.size())