OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

stringI.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include <iostream>
00028 
00029 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00030 
00031 inline Foam::string::string()
00032 {}
00033 
00034 
00035 inline Foam::string::string(const std::string& str)
00036 :
00037     std::string(str)
00038 {}
00039 
00040 
00041 // Copy character array
00042 inline Foam::string::string(const char* str)
00043 :
00044     std::string(str)
00045 {}
00046 
00047 
00048 // Construct from a given number of characters in a character array
00049 inline Foam::string::string(const char* str, const size_type len)
00050 :
00051     std::string(str, len)
00052 {}
00053 
00054 
00055 // Construct from a single character
00056 inline Foam::string::string(const char c)
00057 :
00058     std::string(1, c)
00059 {}
00060 
00061 
00062 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00063 
00064 template<class String>
00065 inline bool Foam::string::valid(const string& str)
00066 {
00067     for (const_iterator iter = str.begin(); iter != str.end(); iter++)
00068     {
00069         if (!String::valid(*iter))
00070         {
00071             return false;
00072         }
00073     }
00074     return true;
00075 }
00076 
00077 
00078 template<class String>
00079 inline bool Foam::string::stripInvalid(string& str)
00080 {
00081     if (!valid<String>(str))
00082     {
00083         register size_type nValid = 0;
00084         iterator iter2 = str.begin();
00085 
00086         for
00087         (
00088             const_iterator iter1 = iter2;
00089             iter1 != const_cast<const string&>(str).end();
00090             iter1++
00091         )
00092         {
00093             register char c = *iter1;
00094 
00095             if (String::valid(c))
00096             {
00097                 *iter2 = c;
00098                 ++iter2;
00099                 ++nValid;
00100             }
00101         }
00102 
00103         str.resize(nValid);
00104 
00105         return true;
00106     }
00107 
00108     return false;
00109 }
00110 
00111 
00112 template<class String>
00113 inline bool Foam::string::meta(const string& str, const char quote)
00114 {
00115     int escaped = 0;
00116     for (const_iterator iter = str.begin(); iter != str.end(); iter++)
00117     {
00118         if (quote && *iter == quote)
00119         {
00120             escaped ^= 1;  // toggle state
00121         }
00122         else if (escaped)
00123         {
00124             escaped = false;
00125         }
00126         else if (String::meta(*iter))
00127         {
00128             return true;
00129         }
00130     }
00131     return false;
00132 }
00133 
00134 
00135 template<class String>
00136 inline Foam::string
00137 Foam::string::quotemeta(const string& str, const char quote)
00138 {
00139     if (!quote)
00140     {
00141         return str;
00142     }
00143 
00144     string sQuoted;
00145     sQuoted.reserve(2*str.length());
00146 
00147     int escaped = 0;
00148     for (const_iterator iter = str.begin(); iter != str.end(); iter++)
00149     {
00150         if (*iter == quote)
00151         {
00152             escaped ^= 1;  // toggle state
00153         }
00154         else if (escaped)
00155         {
00156             escaped = 0;
00157         }
00158         else if (String::meta(*iter))
00159         {
00160             sQuoted += quote;
00161         }
00162 
00163         sQuoted += *iter;
00164     }
00165 
00166     sQuoted.resize(sQuoted.length());
00167 
00168     return sQuoted;
00169 }
00170 
00171 
00172 template<class String>
00173 inline String Foam::string::validate(const string& str)
00174 {
00175     string ss = str;
00176     stripInvalid<String>(ss);
00177     return ss;
00178 }
00179 
00180 
00181 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00182 
00183 inline Foam::string Foam::string::operator()
00184 (
00185     const size_type i,
00186     const size_type n
00187 ) const
00188 {
00189     return substr(i, n);
00190 }
00191 
00192 
00193 inline Foam::string Foam::string::operator()(const size_type n) const
00194 {
00195     return substr(0, n);
00196 }
00197 
00198 
00199 inline unsigned Foam::string::hash::operator()
00200 (
00201     const string& key,
00202     unsigned seed
00203 ) const
00204 {
00205     return Hasher(key.data(), key.size(), seed);
00206 }
00207 
00208 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd