File: GulString.h

package info (click to toggle)
galeon 2.0.6-2.1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 19,156 kB
  • ctags: 9,680
  • sloc: ansic: 77,798; cpp: 13,928; sh: 9,000; xml: 5,761; makefile: 901
file content (243 lines) | stat: -rw-r--r-- 5,295 bytes parent folder | download | duplicates (4)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 *  Copyright (C) 2004 Crispin Flowerday
 *
 *  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, 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 with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* This provides string classes that have a more feature rich API
 * than the base mozilla nsEmbedString ones
 *
 * The GulString object contains UTF16 strings, and the GulCString
 * object contains UTF8 strings, you can assign one to the other to
 * get conversion.
 */

#ifndef MOZILLA_GUL_STRING_H
#define MOZILLA_GUL_STRING_H

#define MOZILLA_STRICT_API
#include <nsEmbedString.h>
#undef MOZILLA_STRICT_API
#include <glib/gstrfuncs.h>
#include <string.h>

class GulString;
class GulCString;
class GulDependentString;
class GulDependentCString;

class GulDependentCString : public nsEmbedCString
{
 public:
	GulDependentCString (const char * aString)
	{
		// Copy, although it is possible in trunk to do it
		// without the copy
		// https://bugzilla.mozilla.org/show_bug.cgi?id=264274
		Assign (aString);
	}
};

#ifndef NS_LITERAL_CSTRING
#define NS_LITERAL_CSTRING(a) GulDependentCString(a)
#endif


class GulDependentString : public nsEmbedString
{
 public:
	GulDependentString (const PRUnichar * aString)
	{
		// Copy, although it is possible in trunk to do it
		// without the copy
		// https://bugzilla.mozilla.org/show_bug.cgi?id=264274
		Assign (aString);
	}
};

class GulString : public nsEmbedString
{
 public:
	GulString() {}

	GulString (const PRUnichar * aString) 
	{
		Assign (aString);
	}

	GulString (const char * aString) // conversion
	{
		NS_CStringToUTF16 (GulDependentCString (aString),
				   NS_CSTRING_ENCODING_UTF8, *this);
	}
		
	GulString (const nsACString &aString) // conversion
	{
		NS_CStringToUTF16 (aString, NS_CSTRING_ENCODING_UTF8, *this);
	}

	PRBool IsEmpty() const
	{
		return (Length() == 0 ) ? PR_TRUE : PR_FALSE;
	}

	void ReplaceChar (PRUnichar what, PRUnichar with)
	{
		const PRUnichar *text = BeginReading();
		for (PRUint32 i = 0; i < Length(); i++)
		{
			if (text[i] == what)
			{
				Replace (i, 1, with);
			}
		}
	}

	PRBool Equals (const nsAString& aString) const
	{
		const PRUnichar *p1, *p2;
		const PRUint32 l1 = NS_StringGetData (*this, &p1);
		const PRUint32 l2 = NS_StringGetData (aString, &p2);

		if (l1 != l2 )
		{
			return PR_FALSE;
		}

		return memcmp (p1, p2, l1 * sizeof (PRUnichar)) == 0 ? PR_TRUE : PR_FALSE;
	}
	

	GulString &operator=(const PRUnichar * aString)
	{
		Assign (aString);
		return *this;
	}

	GulString &operator=(const nsAString &aString)
	{
		Assign (aString);
		return *this;
	}

	GulString &operator=(const nsACString& aString) // conversion
	{
		NS_CStringToUTF16 (aString, NS_CSTRING_ENCODING_UTF8, *this);
		return *this;
	}

};

#ifndef NS_LITERAL_STRING
#define NS_LITERAL_STRING(s)         GulString(s)
#endif

#ifndef NS_NAMED_LITERAL_STRING
#define NS_NAMED_LITERAL_STRING(v,s) GulString v(s);
#endif

class GulCString : public nsEmbedCString
{
 public:
	GulCString () {}

	GulCString (const char * aString)
	{
		Assign (aString);
	}

	GulCString (const nsACString & aString)
	{
		Assign (aString);
	}

	GulCString (const PRUnichar * aString) // conversion
	{
		NS_UTF16ToCString (GulDependentString (aString),
				   NS_CSTRING_ENCODING_UTF8, *this);
	}

	GulCString (const nsAString &aString) // conversion
	{
		NS_UTF16ToCString (aString, NS_CSTRING_ENCODING_UTF8, *this);
	}

	PRBool IsEmpty() const
	{
		return (Length() == 0 ) ? PR_TRUE : PR_FALSE;
	}

	PRBool Equals (const char * aString) const
	{
		PRUint32 l = Length();
		if (strlen (aString) != l)
		{
			return PR_FALSE;
		}

		return memcmp (get(), aString, l) == 0 ? PR_TRUE : PR_FALSE;
	}
	
	PRBool Equals (const nsACString& aString) const
	{
		const char *p1, *p2;
		const PRUint32 l1 = NS_CStringGetData (*this, &p1);
		const PRUint32 l2 = NS_CStringGetData (aString, &p2);

		if (l1 != l2 )
		{
			return PR_FALSE;
		}

		return memcmp (p1, p2, l1) == 0 ? PR_TRUE : PR_FALSE;
	}
	
	PRBool EqualsAsciiIgnoreCase (const char * aString) const
	{
		return g_ascii_strcasecmp (get(), aString) == 0 ? PR_TRUE : PR_FALSE;
	}
	
	PRBool StartsWith (const char * aString) const
	{
		const unsigned int len = strlen (aString);
		if (len > Length())
		{
			return PR_FALSE;
		}

		return memcmp (get(), aString, len) == 0 ? PR_TRUE : PR_FALSE;
	}

	GulCString &operator=(const char * aString)
	{
		Assign (aString);
		return *this;
	}

	GulCString &operator=(const nsAString& aString) // conversion
	{
		NS_UTF16ToCString (aString, NS_CSTRING_ENCODING_UTF8, *this);
		return *this;
	}

	GulCString &operator=(const PRUnichar * aString) // conversion
	{
		NS_UTF16ToCString (GulDependentString (aString), 
				   NS_CSTRING_ENCODING_UTF8, *this);
		return *this;
	}
};

#endif