stringI.H
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 <iostream>
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
31 {}
32 
33 
34 inline Foam::string::string(const std::string& str)
35 :
36  std::string(str)
37 {}
38 
39 
40 // Copy character array
41 inline Foam::string::string(const char* str)
42 :
43  std::string(str)
44 {}
45 
46 
47 // Construct from a given number of characters in a character array
48 inline Foam::string::string(const char* str, const size_type len)
49 :
50  std::string(str, len)
51 {}
52 
53 
54 // Construct from a single character
55 inline Foam::string::string(const char c)
56 :
57  std::string(1, c)
58 {}
59 
60 
61 inline Foam::string::string(const size_type len, const char c)
62 :
63  std::string(len, c)
64 {}
65 
66 
67 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
68 
69 template<class String>
70 inline bool Foam::string::valid(const string& str)
71 {
72  for (const_iterator iter = str.begin(); iter != str.end(); ++iter)
73  {
74  if (!String::valid(*iter))
75  {
76  return false;
77  }
78  }
79  return true;
80 }
81 
82 
83 template<class String>
84 inline bool Foam::string::stripInvalid(string& str)
85 {
86  if (!valid<String>(str))
87  {
88  size_type nValid = 0;
89  iterator iter2 = str.begin();
90 
91  for
92  (
93  const_iterator iter1 = iter2;
94  iter1 != const_cast<const string&>(str).end();
95  iter1++
96  )
97  {
98  char c = *iter1;
99 
100  if (String::valid(c))
101  {
102  *iter2 = c;
103  ++iter2;
104  ++nValid;
105  }
106  }
107 
108  str.resize(nValid);
109 
110  return true;
111  }
112 
113  return false;
114 }
115 
116 
117 template<class String>
118 inline bool Foam::string::meta(const string& str, const char quote)
119 {
120  int escaped = 0;
121  for (const_iterator iter = str.begin(); iter != str.end(); ++iter)
122  {
123  if (quote && *iter == quote)
124  {
125  escaped ^= 1; // toggle state
126  }
127  else if (escaped)
128  {
129  escaped = false;
130  }
131  else if (String::meta(*iter))
132  {
133  return true;
134  }
135  }
136  return false;
137 }
138 
139 
140 template<class String>
141 inline Foam::string
142 Foam::string::quotemeta(const string& str, const char quote)
143 {
144  if (!quote)
145  {
146  return str;
147  }
148 
149  string sQuoted;
150  sQuoted.reserve(2*str.length());
151 
152  int escaped = 0;
153  for (const_iterator iter = str.begin(); iter != str.end(); ++iter)
154  {
155  if (*iter == quote)
156  {
157  escaped ^= 1; // toggle state
158  }
159  else if (escaped)
160  {
161  escaped = 0;
162  }
163  else if (String::meta(*iter))
164  {
165  sQuoted += quote;
166  }
167 
168  sQuoted += *iter;
169  }
170 
171  sQuoted.resize(sQuoted.length());
172 
173  return sQuoted;
174 }
175 
176 
177 template<class String>
178 inline String Foam::string::validate(const string& str)
179 {
180  string ss = str;
181  stripInvalid<String>(ss);
182  return ss;
183 }
184 
185 inline bool Foam::string::match(const std::string& str) const
186 {
187  // check as string
188  return (str == *this);
189 }
190 
191 
192 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
193 
194 inline Foam::string Foam::string::operator()
195 (
196  const size_type i,
197  const size_type n
198 ) const
199 {
200  return substr(i, n);
201 }
202 
203 
205 {
206  return substr(0, n);
207 }
208 
209 
210 inline unsigned Foam::string::hash::operator()
211 (
212  const string& key,
213  unsigned seed
214 ) const
215 {
216  return Hasher(key.data(), key.size(), seed);
217 }
218 
219 // ************************************************************************* //
static bool meta(const string &, const char quote='\\')
Does this string have particular meta-characters?
Definition: stringI.H:118
string operator()(const size_type i, const size_type n) const
Return the sub-string from the i-th character for n characters.
Definition: stringI.H:195
static String validate(const string &)
Return a valid String from the given string.
Definition: stringI.H:178
bool match(const std::string &) const
True when strings match literally.
Definition: stringI.H:185
static string quotemeta(const string &, const char quote='\\')
Return a String with quoted meta-characters from the given string.
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
string()
Construct null.
Definition: stringI.H:30
unsigned Hasher(const void *data, size_t len, unsigned seed=0)
Bob Jenkins&#39;s 96-bit mixer hashing function (lookup3)
Definition: Hasher.C:476
const dimensionedScalar c
Speed of light in a vacuum.
label n
static bool valid(const string &)
Is this string type valid?
Definition: stringI.H:70
static bool stripInvalid(string &)
Strip invalid characters from the given string.
Definition: stringI.H:84
A class for handling character strings derived from std::string.
Definition: string.H:74