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