OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen 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-2007 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& stdStr)
00036 :
00037     std::string(stdStr)
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& s)
00066 {
00067     bool iv = false;
00068 
00069     for (const_iterator iter = s.begin(); iter != s.end(); iter++)
00070     {
00071         if (!String::valid(*iter))
00072         {
00073             iv = true;
00074             break;
00075         }
00076     }
00077 
00078     return !iv;
00079 }
00080 
00081 
00082 template<class String>
00083 inline bool Foam::string::stripInvalid(string& s)
00084 {
00085     if (!valid<String>(s))
00086     {
00087         register size_type nValid=0;
00088         iterator iter2 = s.begin();
00089 
00090         for
00091         (
00092             const_iterator iter1 = iter2;
00093             iter1 != const_cast<const string&>(s).end();
00094             iter1++
00095         )
00096         {
00097             register char c = *iter1;
00098 
00099             if (String::valid(c))
00100             {
00101                 *iter2 = c;
00102                 ++iter2;
00103                 ++nValid;
00104             }
00105         }
00106     
00107         s.resize(nValid);
00108 
00109         return true;
00110     }
00111 
00112     return false;
00113 }
00114 
00115 
00116 template<class String>
00117 inline String Foam::string::validate(const string& s)
00118 {
00119     string ss = s;
00120     stripInvalid<String>(ss);
00121     return ss;
00122 }
00123 
00124 
00125 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00126 
00127 inline Foam::string Foam::string::operator()
00128 (
00129     const size_type i,
00130     const size_type n
00131 ) const
00132 {
00133     return substr(i, n);
00134 }
00135 
00136 
00137 inline Foam::string Foam::string::operator()(const size_type n) const
00138 {
00139     return substr(0, n);
00140 }
00141 
00142 
00143 inline Foam::string::hash::hash()
00144 {}
00145 
00146 
00147 inline Foam::string::size_type Foam::string::hash::operator()
00148 (
00149     const string& key
00150 ) const
00151 {
00152     register size_type hashVal = 0;
00153 
00154     for (string::const_iterator iter=key.begin(); iter!=key.end(); ++iter)
00155     {
00156         hashVal = hashVal<<1 ^ *iter;
00157     }
00158 
00159     return hashVal;
00160 }
00161 
00162 
00163 inline Foam::string::size_type Foam::string::hash::operator()
00164 (
00165     const string& key,
00166     const size_type tableSize
00167 ) const
00168 {
00169     return ::abs(operator()(key)) % tableSize;
00170 }
00171 
00172 
00173 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd