OpenFOAM logo
Open Source CFD Toolkit

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-2005 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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00030 
00031 namespace Foam
00032 {
00033 
00034 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00035 
00036 inline string::string()
00037 {}
00038 
00039 
00040 inline string::string(const std::string& stdStr)
00041 :
00042     std::string(stdStr)
00043 {}
00044 
00045 
00046 // Copy character array
00047 inline string::string(const char* str)
00048 :
00049     std::string(str)
00050 {}
00051 
00052 
00053 // Construct from a given number of characters in a character array
00054 inline string::string(const char* str, const size_type len)
00055 :
00056     std::string(str, len)
00057 {}
00058 
00059 
00060 // Construct from a single character
00061 inline string::string(const char c)
00062 :
00063     std::string(1, c)
00064 {}
00065 
00066 
00067 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00068 
00069 template<class String>
00070 inline bool string::valid(const string& s)
00071 {
00072     bool iv = false;
00073 
00074     for (const_iterator iter = s.begin(); iter != s.end(); iter++)
00075     {
00076         if (!String::valid(*iter))
00077         {
00078             iv = true;
00079             break;
00080         }
00081     }
00082 
00083     return !iv;
00084 }
00085 
00086 
00087 template<class String>
00088 inline bool string::stripInvalid(string& s)
00089 {
00090     if (!valid<String>(s))
00091     {
00092         register size_type nValid=0;
00093         iterator iter2 = s.begin();
00094 
00095         for
00096         (
00097             const_iterator iter1 = iter2;
00098             iter1 != const_cast<const string&>(s).end();
00099             iter1++
00100         )
00101         {
00102             register char c = *iter1;
00103 
00104             if (String::valid(c))
00105             {
00106                 *iter2 = c;
00107                 ++iter2;
00108                 ++nValid;
00109             }
00110         }
00111     
00112         s.resize(nValid);
00113 
00114         return true;
00115     }
00116 
00117     return false;
00118 }
00119 
00120 
00121 template<class String>
00122 inline String string::validate(const string& s)
00123 {
00124     string ss = s;
00125     stripInvalid<String>(ss);
00126     return ss;
00127 }
00128 
00129 
00130 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00131 
00132 inline string string::operator()(const size_type i, const size_type n) const
00133 {
00134     return substr(i, n);
00135 }
00136 
00137 
00138 inline string string::operator()(const size_type n) const
00139 {
00140     return substr(0, n);
00141 }
00142 
00143 
00144 inline string::hash::hash()
00145 {}
00146 
00147 
00148 inline string::size_type string::hash::operator()(const string& key) const
00149 {
00150     register size_type hashVal = 0;
00151 
00152     for (string::const_iterator iter=key.begin(); iter!=key.end(); ++iter)
00153     {
00154         hashVal = hashVal<<1 ^ *iter;
00155     }
00156 
00157     return hashVal;
00158 }
00159 
00160 
00161 inline string::size_type string::hash::operator()
00162 (
00163     const string& key,
00164     const size_type tableSize
00165 ) const
00166 {
00167     return ::abs(operator()(key)) % tableSize;
00168 }
00169 
00170 
00171 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00172 
00173 } // End namespace Foam
00174 
00175 // ************************************************************************* //
For further information go to www.openfoam.org