FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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  register 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  register 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 
180 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
181 
182 inline Foam::string Foam::string::operator()
183 (
184  const size_type i,
185  const size_type n
186 ) const
187 {
188  return substr(i, n);
189 }
190 
191 
192 inline Foam::string Foam::string::operator()(const size_type n) const
193 {
194  return substr(0, n);
195 }
196 
197 
198 inline unsigned Foam::string::hash::operator()
199 (
200  const string& key,
201  unsigned seed
202 ) const
203 {
204  return Hasher(key.data(), key.size(), seed);
205 }
206 
207 // ************************ vim: set sw=4 sts=4 et: ************************ //