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