vmime/src/utility/stringUtils.cpp

137 lines
3.3 KiB
C++
Raw Normal View History

2004-10-21 15:05:47 +00:00
//
2005-03-18 21:33:11 +00:00
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2005 Vincent Richard <vincent@vincent-richard.net>
2004-10-21 15:05:47 +00:00
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
2005-09-17 10:10:29 +00:00
// You should have received a copy of the GNU General Public License along along
// with this program; if not, write to the Free Software Foundation, Inc., Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA..
2004-10-21 15:05:47 +00:00
//
#include "vmime/utility/stringUtils.hpp"
#include "vmime/parserHelpers.hpp"
2004-10-21 15:05:47 +00:00
namespace vmime {
namespace utility {
2004-10-21 15:05:47 +00:00
const bool stringUtils::isStringEqualNoCase
(const string& s1, const char* s2, const string::size_type n)
{
// 'n' is the number of characters to compare
// 's2' must be in lowercase letters only
if (s1.length() < n)
return (false);
bool equal = true;
for (string::size_type i = 0 ; equal && i < n ; ++i)
equal = (std::tolower(s1[i], std::locale()) == s2[i]);
return (equal);
}
const bool stringUtils::isStringEqualNoCase(const string& s1, const string& s2)
{
if (s1.length() != s2.length())
return (false);
bool equal = true;
const string::const_iterator end = s1.end();
for (string::const_iterator i = s1.begin(), j = s2.begin(); i != end ; ++i, ++j)
equal = (std::tolower(*i, std::locale()) == std::tolower(*j, std::locale()));
return (equal);
}
const bool stringUtils::isStringEqualNoCase
(const string::const_iterator begin, const string::const_iterator end,
const char* s, const string::size_type n)
{
if (static_cast <string::size_type>(end - begin) < n)
2004-10-21 15:05:47 +00:00
return (false);
bool equal = true;
char* c = const_cast<char*>(s);
string::size_type r = n;
for (string::const_iterator i = begin ; equal && r && *c ; ++i, ++c, --r)
equal = (std::tolower(*i, std::locale()) == *c);
return (r == 0 && equal);
}
const string stringUtils::toLower(const string& str)
{
string out;
out.resize(str.size());
2004-10-21 15:05:47 +00:00
for (string::size_type i = 0, len = str.length() ; i < len ; ++i)
out[i] = std::tolower(str[i], std::locale());
2004-10-21 15:05:47 +00:00
return out;
2004-10-21 15:05:47 +00:00
}
2005-03-28 17:15:54 +00:00
const string stringUtils::toUpper(const string& str)
{
string out;
out.resize(str.size());
2005-03-28 17:15:54 +00:00
for (string::size_type i = 0, len = str.length() ; i < len ; ++i)
out[i] = std::toupper(str[i], std::locale());
2005-03-28 17:15:54 +00:00
return out;
2005-03-28 17:15:54 +00:00
}
2004-10-21 15:05:47 +00:00
const string stringUtils::trim(const string& str)
{
string::const_iterator b = str.begin();
string::const_iterator e = str.end();
if (b != e)
{
for ( ; b != e && parserHelpers::isSpace(*b) ; ++b);
for ( ; e != b && parserHelpers::isSpace(*(e - 1)) ; --e);
2004-10-21 15:05:47 +00:00
}
return (string(b, e));
}
const string::size_type stringUtils::countASCIIchars
(const string::const_iterator begin, const string::const_iterator end)
{
string::size_type count = 0;
for (string::const_iterator i = begin ; i != end ; ++i)
{
if (parserHelpers::isAscii(*i))
2004-10-21 15:05:47 +00:00
{
if (*i != '=' || *(i + 1) != '?') // To avoid bad behaviour...
++count;
}
}
return (count);
}
} // utility
2004-10-21 15:05:47 +00:00
} // vmime