FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include <iostream>
00027 
00028 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00029 
00030 inline Foam::string::string()
00031 {}
00032 
00033 
00034 inline Foam::string::string(const std::string& str)
00035 :
00036     std::string(str)
00037 {}
00038 
00039 
00040 // Copy character array
00041 inline Foam::string::string(const char* str)
00042 :
00043     std::string(str)
00044 {}
00045 
00046 
00047 // Construct from a given number of characters in a character array
00048 inline Foam::string::string(const char* str, const size_type len)
00049 :
00050     std::string(str, len)
00051 {}
00052 
00053 
00054 // Construct from a single character
00055 inline Foam::string::string(const char c)
00056 :
00057     std::string(1, c)
00058 {}
00059 
00060 
00061 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00062 
00063 template<class String>
00064 inline bool Foam::string::valid(const string& str)
00065 {
00066     for (const_iterator iter = str.begin(); iter != str.end(); iter++)
00067     {
00068         if (!String::valid(*iter))
00069         {
00070             return false;
00071         }
00072     }
00073     return true;
00074 }
00075 
00076 
00077 template<class String>
00078 inline bool Foam::string::stripInvalid(string& str)
00079 {
00080     if (!valid<String>(str))
00081     {
00082         register size_type nValid = 0;
00083         iterator iter2 = str.begin();
00084 
00085         for
00086         (
00087             const_iterator iter1 = iter2;
00088             iter1 != const_cast<const string&>(str).end();
00089             iter1++
00090         )
00091         {
00092             register char c = *iter1;
00093 
00094             if (String::valid(c))
00095             {
00096                 *iter2 = c;
00097                 ++iter2;
00098                 ++nValid;
00099             }
00100         }
00101 
00102         str.resize(nValid);
00103 
00104         return true;
00105     }
00106 
00107     return false;
00108 }
00109 
00110 
00111 template<class String>
00112 inline bool Foam::string::meta(const string& str, const char quote)
00113 {
00114     int escaped = 0;
00115     for (const_iterator iter = str.begin(); iter != str.end(); iter++)
00116     {
00117         if (quote && *iter == quote)
00118         {
00119             escaped ^= 1;  // toggle state
00120         }
00121         else if (escaped)
00122         {
00123             escaped = false;
00124         }
00125         else if (String::meta(*iter))
00126         {
00127             return true;
00128         }
00129     }
00130     return false;
00131 }
00132 
00133 
00134 template<class String>
00135 inline Foam::string
00136 Foam::string::quotemeta(const string& str, const char quote)
00137 {
00138     if (!quote)
00139     {
00140         return str;
00141     }
00142 
00143     string sQuoted;
00144     sQuoted.reserve(2*str.length());
00145 
00146     int escaped = 0;
00147     for (const_iterator iter = str.begin(); iter != str.end(); iter++)
00148     {
00149         if (*iter == quote)
00150         {
00151             escaped ^= 1;  // toggle state
00152         }
00153         else if (escaped)
00154         {
00155             escaped = 0;
00156         }
00157         else if (String::meta(*iter))
00158         {
00159             sQuoted += quote;
00160         }
00161 
00162         sQuoted += *iter;
00163     }
00164 
00165     sQuoted.resize(sQuoted.length());
00166 
00167     return sQuoted;
00168 }
00169 
00170 
00171 template<class String>
00172 inline String Foam::string::validate(const string& str)
00173 {
00174     string ss = str;
00175     stripInvalid<String>(ss);
00176     return ss;
00177 }
00178 
00179 
00180 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00181 
00182 inline Foam::string Foam::string::operator()
00183 (
00184     const size_type i,
00185     const size_type n
00186 ) const
00187 {
00188     return substr(i, n);
00189 }
00190 
00191 
00192 inline Foam::string Foam::string::operator()(const size_type n) const
00193 {
00194     return substr(0, n);
00195 }
00196 
00197 
00198 inline unsigned Foam::string::hash::operator()
00199 (
00200     const string& key,
00201     unsigned seed
00202 ) const
00203 {
00204     return Hasher(key.data(), key.size(), seed);
00205 }
00206 
00207 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines