aboutsummaryrefslogtreecommitdiffstats
path: root/src/utility/stringUtils.cpp
blob: dab01f15db82fcd38ea159f88e5dcb72b55ba889 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2006 Vincent Richard <[email protected]>
//
// 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.
//
// 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..
//

#include "vmime/utility/stringUtils.hpp"
#include "vmime/parserHelpers.hpp"


namespace vmime {
namespace utility {


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);

	const std::ctype <unsigned char>& fac =
		std::use_facet <std::ctype <unsigned char> >(std::locale::classic());

	bool equal = true;

	for (string::size_type i = 0 ; equal && i < n ; ++i)
		equal = (fac.tolower(static_cast <unsigned char>(s1[i])) == s2[i]);

	return (equal);
}


const bool stringUtils::isStringEqualNoCase(const string& s1, const string& s2)
{
	if (s1.length() != s2.length())
		return (false);

	const std::ctype <unsigned char>& fac =
		std::use_facet <std::ctype <unsigned char> >(std::locale::classic());

	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 = (fac.tolower(static_cast <unsigned char>(*i)) == fac.tolower(static_cast <unsigned char>(*j)));

	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)
		return (false);

	const std::ctype <unsigned char>& fac =
		std::use_facet <std::ctype <unsigned char> >(std::locale::classic());

	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 = (fac.tolower(static_cast <unsigned char>(*i)) == static_cast <unsigned char>(*c));

	return (r == 0 && equal);
}


const string stringUtils::toLower(const string& str)
{
	const std::ctype <unsigned char>& fac =
		std::use_facet <std::ctype <unsigned char> >(std::locale::classic());

	string out;
	out.resize(str.size());

	for (string::size_type i = 0, len = str.length() ; i < len ; ++i)
		out[i] = fac.tolower(static_cast <unsigned char>(str[i]));

	return out;
}


const string stringUtils::toUpper(const string& str)
{
	const std::ctype <unsigned char>& fac =
		std::use_facet <std::ctype <unsigned char> >(std::locale::classic());

	string out;
	out.resize(str.size());

	for (string::size_type i = 0, len = str.length() ; i < len ; ++i)
		out[i] = fac.toupper(static_cast <unsigned char>(str[i]));

	return out;
}


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);
	}

	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))
		{
			if (*i != '=' || *(i + 1) != '?') // To avoid bad behaviour...
				++count;
		}
	}

	return (count);
}


} // utility
} // vmime