initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / primitives / strings / string / stringI.H
blob3f7ae149fbe3ffaa4c1d95ae29659f7dd2bee038
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include <iostream>
29 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
31 inline Foam::string::string()
35 inline Foam::string::string(const std::string& stdStr)
37     std::string(stdStr)
41 // Copy character array
42 inline Foam::string::string(const char* str)
44     std::string(str)
48 // Construct from a given number of characters in a character array
49 inline Foam::string::string(const char* str, const size_type len)
51     std::string(str, len)
55 // Construct from a single character
56 inline Foam::string::string(const char c)
58     std::string(1, c)
62 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
64 template<class String>
65 inline bool Foam::string::valid(const string& s)
67     bool iv = false;
69     for (const_iterator iter = s.begin(); iter != s.end(); iter++)
70     {
71         if (!String::valid(*iter))
72         {
73             iv = true;
74             break;
75         }
76     }
78     return !iv;
82 template<class String>
83 inline bool Foam::string::stripInvalid(string& s)
85     if (!valid<String>(s))
86     {
87         register size_type nValid=0;
88         iterator iter2 = s.begin();
90         for
91         (
92             const_iterator iter1 = iter2;
93             iter1 != const_cast<const string&>(s).end();
94             iter1++
95         )
96         {
97             register char c = *iter1;
99             if (String::valid(c))
100             {
101                 *iter2 = c;
102                 ++iter2;
103                 ++nValid;
104             }
105         }
107         s.resize(nValid);
109         return true;
110     }
112     return false;
116 template<class String>
117 inline String Foam::string::validate(const string& s)
119     string ss = s;
120     stripInvalid<String>(ss);
121     return ss;
125 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
127 inline Foam::string Foam::string::operator()
129     const size_type i,
130     const size_type n
131 ) const
133     return substr(i, n);
137 inline Foam::string Foam::string::operator()(const size_type n) const
139     return substr(0, n);
143 inline Foam::string::hash::hash()
147 inline Foam::string::size_type Foam::string::hash::operator()
149     const string& key
150 ) const
152     register size_type hashVal = 0;
154     for (string::const_iterator iter=key.begin(); iter!=key.end(); ++iter)
155     {
156         hashVal = hashVal<<1 ^ *iter;
157     }
159     return hashVal;
163 inline Foam::string::size_type Foam::string::hash::operator()
165     const string& key,
166     const size_type tableSize
167 ) const
169     return ::abs(operator()(key)) % tableSize;
173 // ************************************************************************* //