1 : /* vim:set ts=2 sw=2 et cindent: */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla.
16 : *
17 : * The Initial Developer of the Original Code is IBM Corporation.
18 : * Portions created by IBM Corporation are Copyright (C) 2003
19 : * IBM Corporation. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Darin Fisher <darin@meer.net>
23 : * Benjamin Smedberg <benjamin@smedbergs.us>
24 : * Ben Turner <mozilla@songbirdnest.com>
25 : * Prasad Sunkari <prasad@medhas.org>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : /**
42 : * This header provides wrapper classes around the frozen string API
43 : * which are roughly equivalent to the internal string classes.
44 : */
45 :
46 : #ifdef MOZILLA_INTERNAL_API
47 : #error nsStringAPI.h is only usable from non-MOZILLA_INTERNAL_API code!
48 : #endif
49 :
50 : #ifndef nsStringAPI_h__
51 : #define nsStringAPI_h__
52 :
53 : #include "mozilla/Attributes.h"
54 :
55 : #include "nsXPCOMStrings.h"
56 : #include "nsISupportsImpl.h"
57 : #include "prlog.h"
58 : #include "nsTArray.h"
59 :
60 : /**
61 : * Comparison function for use with nsACString::Equals
62 : */
63 : NS_HIDDEN_(PRInt32)
64 : CaseInsensitiveCompare(const char *a, const char *b,
65 : PRUint32 length);
66 :
67 : class nsAString
68 2859 : {
69 : public:
70 : typedef PRUnichar char_type;
71 : typedef nsAString self_type;
72 : typedef PRUint32 size_type;
73 : typedef PRUint32 index_type;
74 :
75 : /**
76 : * Returns the length, beginning, and end of a string in one operation.
77 : */
78 : NS_HIDDEN_(PRUint32) BeginReading(const char_type **begin,
79 : const char_type **end = nsnull) const;
80 :
81 : NS_HIDDEN_(const char_type*) BeginReading() const;
82 : NS_HIDDEN_(const char_type*) EndReading() const;
83 :
84 17 : NS_HIDDEN_(char_type) CharAt(PRUint32 aPos) const
85 : {
86 17 : NS_ASSERTION(aPos < Length(), "Out of bounds");
87 17 : return BeginReading()[aPos];
88 : }
89 11 : NS_HIDDEN_(char_type) operator [](PRUint32 aPos) const
90 : {
91 11 : return CharAt(aPos);
92 : }
93 1 : NS_HIDDEN_(char_type) First() const
94 : {
95 1 : return CharAt(0);
96 : }
97 : NS_HIDDEN_(char_type) Last() const
98 : {
99 : const char_type* data;
100 : PRUint32 dataLen = NS_StringGetData(*this, &data);
101 : return data[dataLen - 1];
102 : }
103 :
104 : /**
105 : * Get the length, begin writing, and optionally set the length of a
106 : * string all in one operation.
107 : *
108 : * @param newSize Size the string to this length. Pass PR_UINT32_MAX
109 : * to leave the length unchanged.
110 : * @return The new length of the string, or 0 if resizing failed.
111 : */
112 : NS_HIDDEN_(PRUint32) BeginWriting(char_type **begin,
113 : char_type **end = nsnull,
114 : PRUint32 newSize = PR_UINT32_MAX);
115 :
116 : NS_HIDDEN_(char_type*) BeginWriting(PRUint32 = PR_UINT32_MAX);
117 : NS_HIDDEN_(char_type*) EndWriting();
118 :
119 : NS_HIDDEN_(bool) SetLength(PRUint32 aLen);
120 :
121 58 : NS_HIDDEN_(size_type) Length() const
122 : {
123 : const char_type* data;
124 58 : return NS_StringGetData(*this, &data);
125 : }
126 :
127 3 : NS_HIDDEN_(bool) IsEmpty() const
128 : {
129 3 : return Length() == 0;
130 : }
131 :
132 4 : NS_HIDDEN_(void) SetIsVoid(bool val)
133 : {
134 4 : NS_StringSetIsVoid(*this, val);
135 4 : }
136 5 : NS_HIDDEN_(bool) IsVoid() const
137 : {
138 5 : return NS_StringGetIsVoid(*this);
139 : }
140 :
141 1392 : NS_HIDDEN_(void) Assign(const self_type& aString)
142 : {
143 1392 : NS_StringCopy(*this, aString);
144 1392 : }
145 2 : NS_HIDDEN_(void) Assign(const char_type* aData, size_type aLength = PR_UINT32_MAX)
146 : {
147 2 : NS_StringSetData(*this, aData, aLength);
148 2 : }
149 1 : NS_HIDDEN_(void) Assign(char_type aChar)
150 : {
151 1 : NS_StringSetData(*this, &aChar, 1);
152 1 : }
153 :
154 : NS_HIDDEN_(void) AssignLiteral(const char *aStr);
155 : NS_HIDDEN_(void) AssignASCII(const char *aStr) { AssignLiteral(aStr); }
156 :
157 1387 : NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString); return *this; }
158 : NS_HIDDEN_(self_type&) operator=(const char_type* aPtr) { Assign(aPtr); return *this; }
159 : NS_HIDDEN_(self_type&) operator=(char_type aChar) { Assign(aChar); return *this; }
160 :
161 80 : NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) )
162 : {
163 80 : NS_StringSetDataRange(*this, cutStart, cutLength, data, length);
164 80 : }
165 80 : NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, char_type c )
166 : {
167 80 : Replace(cutStart, cutLength, &c, 1);
168 80 : }
169 3 : NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const self_type& readable )
170 : {
171 : const char_type* data;
172 3 : PRUint32 dataLen = NS_StringGetData(readable, &data);
173 3 : NS_StringSetDataRange(*this, cutStart, cutLength, data, dataLen);
174 3 : }
175 : NS_HIDDEN_(void) SetCharAt( char_type c, index_type pos )
176 : { Replace(pos, 1, &c, 1); }
177 :
178 80 : NS_HIDDEN_(void) Append( char_type c ) { Replace(size_type(-1), 0, c); }
179 : NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) ) { Replace(size_type(-1), 0, data, length); }
180 3 : NS_HIDDEN_(void) Append( const self_type& readable ) { Replace(size_type(-1), 0, readable); }
181 : NS_HIDDEN_(void) AppendLiteral( const char *aASCIIStr );
182 : NS_HIDDEN_(void) AppendASCII( const char *aASCIIStr ) { AppendLiteral(aASCIIStr); }
183 :
184 : NS_HIDDEN_(self_type&) operator+=( char_type c ) { Append(c); return *this; }
185 : NS_HIDDEN_(self_type&) operator+=( const char_type* data ) { Append(data); return *this; }
186 : NS_HIDDEN_(self_type&) operator+=( const self_type& readable ) { Append(readable); return *this; }
187 :
188 : NS_HIDDEN_(void) Insert( char_type c, index_type pos ) { Replace(pos, 0, c); }
189 : NS_HIDDEN_(void) Insert( const char_type* data, index_type pos, size_type length = size_type(-1) ) { Replace(pos, 0, data, length); }
190 : NS_HIDDEN_(void) Insert( const self_type& readable, index_type pos ) { Replace(pos, 0, readable); }
191 :
192 : NS_HIDDEN_(void) Cut( index_type cutStart, size_type cutLength ) { Replace(cutStart, cutLength, nsnull, 0); }
193 :
194 1 : NS_HIDDEN_(void) Truncate() { SetLength(0); }
195 :
196 : /**
197 : * Remove all occurences of characters in aSet from the string.
198 : */
199 : NS_HIDDEN_(void) StripChars(const char *aSet);
200 :
201 : /**
202 : * Strip whitespace characters from the string.
203 : */
204 : NS_HIDDEN_(void) StripWhitespace() { StripChars("\b\t\r\n "); }
205 :
206 : NS_HIDDEN_(void) Trim(const char *aSet, bool aLeading = true,
207 : bool aTrailing = true);
208 :
209 : /**
210 : * Compare strings of characters. Return 0 if the characters are equal,
211 : */
212 : typedef PRInt32 (*ComparatorFunc)(const char_type *a,
213 : const char_type *b,
214 : PRUint32 length);
215 :
216 : static NS_HIDDEN_(PRInt32) DefaultComparator(const char_type *a,
217 : const char_type *b,
218 : PRUint32 length);
219 :
220 : NS_HIDDEN_(PRInt32) Compare( const char_type *other,
221 : ComparatorFunc c = DefaultComparator ) const;
222 :
223 : NS_HIDDEN_(PRInt32) Compare( const self_type &other,
224 : ComparatorFunc c = DefaultComparator ) const;
225 :
226 : NS_HIDDEN_(bool) Equals( const char_type *other,
227 : ComparatorFunc c = DefaultComparator ) const;
228 :
229 : NS_HIDDEN_(bool) Equals( const self_type &other,
230 : ComparatorFunc c = DefaultComparator ) const;
231 :
232 : NS_HIDDEN_(bool) operator < (const self_type &other) const
233 : {
234 : return Compare(other) < 0;
235 : }
236 : NS_HIDDEN_(bool) operator < (const char_type *other) const
237 : {
238 : return Compare(other) < 0;
239 : }
240 :
241 : NS_HIDDEN_(bool) operator <= (const self_type &other) const
242 : {
243 : return Compare(other) <= 0;
244 : }
245 : NS_HIDDEN_(bool) operator <= (const char_type *other) const
246 : {
247 : return Compare(other) <= 0;
248 : }
249 :
250 2 : NS_HIDDEN_(bool) operator == (const self_type &other) const
251 : {
252 2 : return Equals(other);
253 : }
254 : NS_HIDDEN_(bool) operator == (const char_type *other) const
255 : {
256 : return Equals(other);
257 : }
258 :
259 : NS_HIDDEN_(bool) operator >= (const self_type &other) const
260 : {
261 : return Compare(other) >= 0;
262 : }
263 : NS_HIDDEN_(bool) operator >= (const char_type *other) const
264 : {
265 : return Compare(other) >= 0;
266 : }
267 :
268 : NS_HIDDEN_(bool) operator > (const self_type &other) const
269 : {
270 : return Compare(other) > 0;
271 : }
272 : NS_HIDDEN_(bool) operator > (const char_type *other) const
273 : {
274 : return Compare(other) > 0;
275 : }
276 :
277 : NS_HIDDEN_(bool) operator != (const self_type &other) const
278 : {
279 : return !Equals(other);
280 : }
281 : NS_HIDDEN_(bool) operator != (const char_type *other) const
282 : {
283 : return !Equals(other);
284 : }
285 :
286 : NS_HIDDEN_(bool) EqualsLiteral(const char *aASCIIString) const;
287 : NS_HIDDEN_(bool) EqualsASCII(const char *aASCIIString) const
288 : {
289 : return EqualsLiteral(aASCIIString);
290 : }
291 :
292 : /**
293 : * Case-insensitive match this string to a lowercase ASCII string.
294 : */
295 : NS_HIDDEN_(bool) LowerCaseEqualsLiteral(const char *aASCIIString) const;
296 :
297 : /**
298 : * Find the first occurrence of aStr in this string.
299 : *
300 : * @return the offset of aStr, or -1 if not found
301 : */
302 2 : NS_HIDDEN_(PRInt32) Find(const self_type& aStr,
303 : ComparatorFunc c = DefaultComparator) const
304 2 : { return Find(aStr, 0, c); }
305 :
306 : /**
307 : * Find the first occurrence of aStr in this string, beginning at aOffset.
308 : *
309 : * @return the offset of aStr, or -1 if not found
310 : */
311 : NS_HIDDEN_(PRInt32) Find(const self_type& aStr, PRUint32 aOffset,
312 : ComparatorFunc c = DefaultComparator) const;
313 :
314 : /**
315 : * Find an ASCII string within this string.
316 : *
317 : * @return the offset of aStr, or -1 if not found.
318 : */
319 2 : NS_HIDDEN_(PRInt32) Find(const char *aStr, bool aIgnoreCase = false) const
320 2 : { return Find(aStr, 0, aIgnoreCase); }
321 :
322 : NS_HIDDEN_(PRInt32) Find(const char *aStr, PRUint32 aOffset, bool aIgnoreCase = false) const;
323 :
324 : /**
325 : * Find the last occurrence of aStr in this string.
326 : *
327 : * @return The offset of aStr from the beginning of the string,
328 : * or -1 if not found.
329 : */
330 2 : NS_HIDDEN_(PRInt32) RFind(const self_type& aStr,
331 : ComparatorFunc c = DefaultComparator) const
332 2 : { return RFind(aStr, -1, c); }
333 :
334 : /**
335 : * Find the last occurrence of aStr in this string, beginning at aOffset.
336 : *
337 : * @param aOffset the offset from the beginning of the string to begin
338 : * searching. If aOffset < 0, search from end of this string.
339 : * @return The offset of aStr from the beginning of the string,
340 : * or -1 if not found.
341 : */
342 : NS_HIDDEN_(PRInt32) RFind(const self_type& aStr, PRInt32 aOffset,
343 : ComparatorFunc c = DefaultComparator) const;
344 :
345 : /**
346 : * Find the last occurrence of an ASCII string within this string.
347 : *
348 : * @return The offset of aStr from the beginning of the string,
349 : * or -1 if not found.
350 : */
351 3 : NS_HIDDEN_(PRInt32) RFind(const char *aStr, bool aIgnoreCase = false) const
352 3 : { return RFind(aStr, -1, aIgnoreCase); }
353 :
354 : /**
355 : * Find the last occurrence of an ASCII string beginning at aOffset.
356 : *
357 : * @param aOffset the offset from the beginning of the string to begin
358 : * searching. If aOffset < 0, search from end of this string.
359 : * @return The offset of aStr from the beginning of the string,
360 : * or -1 if not found.
361 : */
362 : NS_HIDDEN_(PRInt32) RFind(const char *aStr, PRInt32 aOffset, bool aIgnoreCase) const;
363 :
364 : /**
365 : * Search for the offset of the first occurrence of a character in a
366 : * string.
367 : *
368 : * @param aOffset the offset from the beginning of the string to begin
369 : * searching
370 : * @return The offset of the character from the beginning of the string,
371 : * or -1 if not found.
372 : */
373 : NS_HIDDEN_(PRInt32) FindChar(char_type aChar, PRUint32 aOffset = 0) const;
374 :
375 : /**
376 : * Search for the offset of the last occurrence of a character in a
377 : * string.
378 : *
379 : * @return The offset of the character from the beginning of the string,
380 : * or -1 if not found.
381 : */
382 : NS_HIDDEN_(PRInt32) RFindChar(char_type aChar) const;
383 :
384 : /**
385 : * Append a string representation of a number.
386 : */
387 : NS_HIDDEN_(void) AppendInt(int aInt, PRInt32 aRadix = 10);
388 :
389 : #ifndef XPCOM_GLUE_AVOID_NSPR
390 : /**
391 : * Convert this string to an integer.
392 : *
393 : * @param aErrorCode pointer to contain result code.
394 : * @param aRadix must be 10 or 16
395 : */
396 : NS_HIDDEN_(PRInt32) ToInteger(nsresult* aErrorCode,
397 : PRUint32 aRadix = 10) const;
398 : #endif // XPCOM_GLUE_AVOID_NSPR
399 :
400 : protected:
401 : // Prevent people from allocating a nsAString directly.
402 2859 : ~nsAString() {}
403 : };
404 :
405 : class nsACString
406 19487 : {
407 : public:
408 : typedef char char_type;
409 : typedef nsACString self_type;
410 : typedef PRUint32 size_type;
411 : typedef PRUint32 index_type;
412 :
413 : /**
414 : * Returns the length, beginning, and end of a string in one operation.
415 : */
416 : NS_HIDDEN_(PRUint32) BeginReading(const char_type **begin,
417 : const char_type **end = nsnull) const;
418 :
419 : NS_HIDDEN_(const char_type*) BeginReading() const;
420 : NS_HIDDEN_(const char_type*) EndReading() const;
421 :
422 24 : NS_HIDDEN_(char_type) CharAt(PRUint32 aPos) const
423 : {
424 24 : NS_ASSERTION(aPos < Length(), "Out of bounds");
425 24 : return BeginReading()[aPos];
426 : }
427 : NS_HIDDEN_(char_type) operator [](PRUint32 aPos) const
428 : {
429 : return CharAt(aPos);
430 : }
431 : NS_HIDDEN_(char_type) First() const
432 : {
433 : return CharAt(0);
434 : }
435 : NS_HIDDEN_(char_type) Last() const
436 : {
437 : const char_type* data;
438 : PRUint32 dataLen = NS_CStringGetData(*this, &data);
439 : return data[dataLen - 1];
440 : }
441 :
442 : /**
443 : * Get the length, begin writing, and optionally set the length of a
444 : * string all in one operation.
445 : *
446 : * @param newSize Size the string to this length. Pass PR_UINT32_MAX
447 : * to leave the length unchanged.
448 : * @return The new length of the string, or 0 if resizing failed.
449 : */
450 : NS_HIDDEN_(PRUint32) BeginWriting(char_type **begin,
451 : char_type **end = nsnull,
452 : PRUint32 newSize = PR_UINT32_MAX);
453 :
454 : NS_HIDDEN_(char_type*) BeginWriting(PRUint32 aLen = PR_UINT32_MAX);
455 : NS_HIDDEN_(char_type*) EndWriting();
456 :
457 : NS_HIDDEN_(bool) SetLength(PRUint32 aLen);
458 :
459 2925 : NS_HIDDEN_(size_type) Length() const
460 : {
461 : const char_type* data;
462 2925 : return NS_CStringGetData(*this, &data);
463 : }
464 :
465 20 : NS_HIDDEN_(bool) IsEmpty() const
466 : {
467 20 : return Length() == 0;
468 : }
469 :
470 : NS_HIDDEN_(void) SetIsVoid(bool val)
471 : {
472 : NS_CStringSetIsVoid(*this, val);
473 : }
474 : NS_HIDDEN_(bool) IsVoid() const
475 : {
476 : return NS_CStringGetIsVoid(*this);
477 : }
478 :
479 192 : NS_HIDDEN_(void) Assign(const self_type& aString)
480 : {
481 192 : NS_CStringCopy(*this, aString);
482 192 : }
483 7349 : NS_HIDDEN_(void) Assign(const char_type* aData, size_type aLength = PR_UINT32_MAX)
484 : {
485 7349 : NS_CStringSetData(*this, aData, aLength);
486 7349 : }
487 12 : NS_HIDDEN_(void) Assign(char_type aChar)
488 : {
489 12 : NS_CStringSetData(*this, &aChar, 1);
490 12 : }
491 4 : NS_HIDDEN_(void) AssignLiteral(const char_type *aData)
492 : {
493 4 : Assign(aData);
494 4 : }
495 : NS_HIDDEN_(void) AssignASCII(const char_type *aData)
496 : {
497 : Assign(aData);
498 : }
499 :
500 80 : NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString); return *this; }
501 : NS_HIDDEN_(self_type&) operator=(const char_type* aPtr) { Assign(aPtr); return *this; }
502 : NS_HIDDEN_(self_type&) operator=(char_type aChar) { Assign(aChar); return *this; }
503 :
504 1725 : NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) )
505 : {
506 1725 : NS_CStringSetDataRange(*this, cutStart, cutLength, data, length);
507 1725 : }
508 1547 : NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, char_type c )
509 : {
510 1547 : Replace(cutStart, cutLength, &c, 1);
511 1547 : }
512 277 : NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const self_type& readable )
513 : {
514 : const char_type* data;
515 277 : PRUint32 dataLen = NS_CStringGetData(readable, &data);
516 277 : NS_CStringSetDataRange(*this, cutStart, cutLength, data, dataLen);
517 277 : }
518 : NS_HIDDEN_(void) SetCharAt( char_type c, index_type pos )
519 : { Replace(pos, 1, &c, 1); }
520 :
521 160 : NS_HIDDEN_(void) Append( char_type c ) { Replace(size_type(-1), 0, c); }
522 178 : NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) ) { Replace(size_type(-1), 0, data, length); }
523 259 : NS_HIDDEN_(void) Append( const self_type& readable ) { Replace(size_type(-1), 0, readable); }
524 0 : NS_HIDDEN_(void) AppendLiteral( const char *aASCIIStr ) { Append(aASCIIStr); }
525 : NS_HIDDEN_(void) AppendASCII( const char *aASCIIStr ) { Append(aASCIIStr); }
526 :
527 : NS_HIDDEN_(self_type&) operator+=( char_type c ) { Append(c); return *this; }
528 141 : NS_HIDDEN_(self_type&) operator+=( const char_type* data ) { Append(data); return *this; }
529 159 : NS_HIDDEN_(self_type&) operator+=( const self_type& readable ) { Append(readable); return *this; }
530 :
531 : NS_HIDDEN_(void) Insert( char_type c, index_type pos ) { Replace(pos, 0, c); }
532 : NS_HIDDEN_(void) Insert( const char_type* data, index_type pos, size_type length = size_type(-1) ) { Replace(pos, 0, data, length); }
533 : NS_HIDDEN_(void) Insert( const self_type& readable, index_type pos ) { Replace(pos, 0, readable); }
534 :
535 : NS_HIDDEN_(void) Cut( index_type cutStart, size_type cutLength ) { Replace(cutStart, cutLength, nsnull, 0); }
536 :
537 1 : NS_HIDDEN_(void) Truncate() { SetLength(0); }
538 :
539 : /**
540 : * Remove all occurences of characters in aSet from the string.
541 : */
542 : NS_HIDDEN_(void) StripChars(const char *aSet);
543 :
544 : /**
545 : * Strip whitespace characters from the string.
546 : */
547 0 : NS_HIDDEN_(void) StripWhitespace() { StripChars("\b\t\r\n "); }
548 :
549 : NS_HIDDEN_(void) Trim(const char *aSet, bool aLeading = true,
550 : bool aTrailing = true);
551 :
552 : /**
553 : * Compare strings of characters. Return 0 if the characters are equal,
554 : */
555 : typedef PRInt32 (*ComparatorFunc)(const char_type *a,
556 : const char_type *b,
557 : PRUint32 length);
558 :
559 : static NS_HIDDEN_(PRInt32) DefaultComparator(const char_type *a,
560 : const char_type *b,
561 : PRUint32 length);
562 :
563 : NS_HIDDEN_(PRInt32) Compare( const char_type *other,
564 : ComparatorFunc c = DefaultComparator ) const;
565 :
566 : NS_HIDDEN_(PRInt32) Compare( const self_type &other,
567 : ComparatorFunc c = DefaultComparator ) const;
568 :
569 : NS_HIDDEN_(bool) Equals( const char_type *other,
570 : ComparatorFunc c = DefaultComparator ) const;
571 :
572 : NS_HIDDEN_(bool) Equals( const self_type &other,
573 : ComparatorFunc c = DefaultComparator ) const;
574 :
575 151 : NS_HIDDEN_(bool) operator < (const self_type &other) const
576 : {
577 151 : return Compare(other) < 0;
578 : }
579 : NS_HIDDEN_(bool) operator < (const char_type *other) const
580 : {
581 : return Compare(other) < 0;
582 : }
583 :
584 : NS_HIDDEN_(bool) operator <= (const self_type &other) const
585 : {
586 : return Compare(other) <= 0;
587 : }
588 : NS_HIDDEN_(bool) operator <= (const char_type *other) const
589 : {
590 : return Compare(other) <= 0;
591 : }
592 :
593 318 : NS_HIDDEN_(bool) operator == (const self_type &other) const
594 : {
595 318 : return Equals(other);
596 : }
597 : NS_HIDDEN_(bool) operator == (const char_type *other) const
598 : {
599 : return Equals(other);
600 : }
601 :
602 : NS_HIDDEN_(bool) operator >= (const self_type &other) const
603 : {
604 : return Compare(other) >= 0;
605 : }
606 : NS_HIDDEN_(bool) operator >= (const char_type *other) const
607 : {
608 : return Compare(other) >= 0;
609 : }
610 :
611 : NS_HIDDEN_(bool) operator > (const self_type &other) const
612 : {
613 : return Compare(other) > 0;
614 : }
615 : NS_HIDDEN_(bool) operator > (const char_type *other) const
616 : {
617 : return Compare(other) > 0;
618 : }
619 :
620 60 : NS_HIDDEN_(bool) operator != (const self_type &other) const
621 : {
622 60 : return !Equals(other);
623 : }
624 : NS_HIDDEN_(bool) operator != (const char_type *other) const
625 : {
626 : return !Equals(other);
627 : }
628 :
629 4 : NS_HIDDEN_(bool) EqualsLiteral( const char_type *other ) const
630 : {
631 4 : return Equals(other);
632 : }
633 : NS_HIDDEN_(bool) EqualsASCII( const char_type *other ) const
634 : {
635 : return Equals(other);
636 : }
637 :
638 : /**
639 : * Case-insensitive match this string to a lowercase ASCII string.
640 : */
641 : NS_HIDDEN_(bool) LowerCaseEqualsLiteral(const char *aASCIIString) const
642 : {
643 : return Equals(aASCIIString, CaseInsensitiveCompare);
644 : }
645 :
646 : /**
647 : * Find the first occurrence of aStr in this string.
648 : *
649 : * @return the offset of aStr, or -1 if not found
650 : */
651 2 : NS_HIDDEN_(PRInt32) Find(const self_type& aStr,
652 : ComparatorFunc c = DefaultComparator) const
653 2 : { return Find(aStr, 0, c); }
654 :
655 : /**
656 : * Find the first occurrence of aStr in this string, beginning at aOffset.
657 : *
658 : * @return the offset of aStr, or -1 if not found
659 : */
660 : NS_HIDDEN_(PRInt32) Find(const self_type& aStr, PRUint32 aOffset,
661 : ComparatorFunc c = DefaultComparator) const;
662 :
663 : /**
664 : * Find the first occurrence of aStr in this string.
665 : *
666 : * @return the offset of aStr, or -1 if not found
667 : */
668 : NS_HIDDEN_(PRInt32) Find(const char_type *aStr,
669 : ComparatorFunc c = DefaultComparator) const;
670 :
671 : NS_HIDDEN_(PRInt32) Find(const char_type *aStr, PRUint32 aLen,
672 : ComparatorFunc c = DefaultComparator) const;
673 :
674 : /**
675 : * Find the last occurrence of aStr in this string.
676 : *
677 : * @return The offset of the character from the beginning of the string,
678 : * or -1 if not found.
679 : */
680 2 : NS_HIDDEN_(PRInt32) RFind(const self_type& aStr,
681 : ComparatorFunc c = DefaultComparator) const
682 2 : { return RFind(aStr, -1, c); }
683 :
684 : /**
685 : * Find the last occurrence of aStr in this string, beginning at aOffset.
686 : *
687 : * @param aOffset the offset from the beginning of the string to begin
688 : * searching. If aOffset < 0, search from end of this string.
689 : * @return The offset of aStr from the beginning of the string,
690 : * or -1 if not found.
691 : */
692 : NS_HIDDEN_(PRInt32) RFind(const self_type& aStr, PRInt32 aOffset,
693 : ComparatorFunc c = DefaultComparator) const;
694 :
695 : /**
696 : * Find the last occurrence of aStr in this string.
697 : *
698 : * @return The offset of aStr from the beginning of the string,
699 : * or -1 if not found.
700 : */
701 : NS_HIDDEN_(PRInt32) RFind(const char_type *aStr,
702 : ComparatorFunc c = DefaultComparator) const;
703 :
704 : /**
705 : * Find the last occurrence of an ASCII string in this string,
706 : * beginning at aOffset.
707 : *
708 : * @param aLen is the length of aStr
709 : * @return The offset of aStr from the beginning of the string,
710 : * or -1 if not found.
711 : */
712 : NS_HIDDEN_(PRInt32) RFind(const char_type *aStr, PRInt32 aLen,
713 : ComparatorFunc c = DefaultComparator) const;
714 :
715 : /**
716 : * Search for the offset of the first occurrence of a character in a
717 : * string.
718 : *
719 : * @param aOffset the offset from the beginning of the string to begin
720 : * searching
721 : * @return The offset of the character from the beginning of the string,
722 : * or -1 if not found.
723 : */
724 : NS_HIDDEN_(PRInt32) FindChar(char_type aChar, PRUint32 aOffset = 0) const;
725 :
726 : /**
727 : * Search for the offset of the last occurrence of a character in a
728 : * string.
729 : *
730 : * @return The offset of the character from the beginning of the string,
731 : * or -1 if not found.
732 : */
733 : NS_HIDDEN_(PRInt32) RFindChar(char_type aChar) const;
734 :
735 : /**
736 : * Append a string representation of a number.
737 : */
738 : NS_HIDDEN_(void) AppendInt(int aInt, PRInt32 aRadix = 10);
739 :
740 : #ifndef XPCOM_GLUE_AVOID_NSPR
741 : /**
742 : * Convert this string to an integer.
743 : *
744 : * @param aErrorCode pointer to contain result code.
745 : * @param aRadix must be 10 or 16
746 : */
747 : NS_HIDDEN_(PRInt32) ToInteger(nsresult* aErrorCode,
748 : PRUint32 aRadix = 10) const;
749 : #endif // XPCOM_GLUE_AVOID_NSPR
750 :
751 : protected:
752 : // Prevent people from allocating a nsAString directly.
753 19487 : ~nsACString() {}
754 : };
755 :
756 : /* ------------------------------------------------------------------------- */
757 :
758 : /**
759 : * Below we define nsStringContainer and nsCStringContainer. These classes
760 : * have unspecified structure. In most cases, your code should use
761 : * nsString/nsCString instead of these classes; if you prefer C-style
762 : * programming, then look no further.
763 : */
764 :
765 : class nsStringContainer : public nsAString,
766 : private nsStringContainer_base
767 5718 : {
768 : };
769 :
770 : class nsCStringContainer : public nsACString,
771 : private nsStringContainer_base
772 38974 : {
773 : };
774 :
775 : /**
776 : * The following classes are C++ helper classes that make the frozen string
777 : * API easier to use.
778 : */
779 :
780 : /**
781 : * Rename symbols to avoid conflicting with internal versions.
782 : */
783 : #define nsString nsString_external
784 : #define nsCString nsCString_external
785 : #define nsDependentString nsDependentString_external
786 : #define nsDependentCString nsDependentCString_external
787 : #define NS_ConvertASCIItoUTF16 NS_ConvertASCIItoUTF16_external
788 : #define NS_ConvertUTF8toUTF16 NS_ConvertUTF8toUTF16_external
789 : #define NS_ConvertUTF16toUTF8 NS_ConvertUTF16toUTF8_external
790 : #define NS_LossyConvertUTF16toASCII NS_LossyConvertUTF16toASCII_external
791 : #define nsGetterCopies nsGetterCopies_external
792 : #define nsCGetterCopies nsCGetterCopies_external
793 : #define nsDependentSubstring nsDependentSubstring_external
794 : #define nsDependentCSubstring nsDependentCSubstring_external
795 :
796 : /**
797 : * basic strings
798 : */
799 :
800 : class nsString : public nsStringContainer
801 : {
802 : public:
803 : typedef nsString self_type;
804 : typedef nsAString abstract_string_type;
805 :
806 2796 : nsString()
807 2796 : {
808 2796 : NS_StringContainerInit(*this);
809 2796 : }
810 :
811 3 : nsString(const self_type& aString)
812 3 : {
813 3 : NS_StringContainerInit(*this);
814 3 : NS_StringCopy(*this, aString);
815 3 : }
816 :
817 : explicit
818 0 : nsString(const abstract_string_type& aReadable)
819 0 : {
820 0 : NS_StringContainerInit(*this);
821 0 : NS_StringCopy(*this, aReadable);
822 0 : }
823 :
824 : explicit
825 1 : nsString(const char_type* aData, size_type aLength = PR_UINT32_MAX)
826 1 : {
827 1 : NS_StringContainerInit2(*this, aData, aLength, 0);
828 1 : }
829 :
830 2859 : ~nsString()
831 2859 : {
832 2859 : NS_StringContainerFinish(*this);
833 2859 : }
834 :
835 11 : const char_type* get() const
836 : {
837 11 : return BeginReading();
838 : }
839 :
840 0 : self_type& operator=(const self_type& aString) { Assign(aString); return *this; }
841 0 : self_type& operator=(const abstract_string_type& aReadable) { Assign(aReadable); return *this; }
842 : self_type& operator=(const char_type* aPtr) { Assign(aPtr); return *this; }
843 : self_type& operator=(char_type aChar) { Assign(aChar); return *this; }
844 :
845 0 : void Adopt(const char_type *aData, size_type aLength = PR_UINT32_MAX)
846 : {
847 0 : NS_StringContainerFinish(*this);
848 : NS_StringContainerInit2(*this, aData, aLength,
849 0 : NS_STRING_CONTAINER_INIT_ADOPT);
850 0 : }
851 :
852 : protected:
853 :
854 55 : nsString(const char_type* aData, size_type aLength, PRUint32 aFlags)
855 55 : {
856 55 : NS_StringContainerInit2(*this, aData, aLength, aFlags);
857 55 : }
858 : };
859 :
860 : class nsCString : public nsCStringContainer
861 : {
862 : public:
863 : typedef nsCString self_type;
864 : typedef nsACString abstract_string_type;
865 :
866 1693 : nsCString()
867 1693 : {
868 1693 : NS_CStringContainerInit(*this);
869 1693 : }
870 :
871 102 : nsCString(const self_type& aString)
872 102 : {
873 102 : NS_CStringContainerInit(*this);
874 102 : NS_CStringCopy(*this, aString);
875 102 : }
876 :
877 : explicit
878 10981 : nsCString(const abstract_string_type& aReadable)
879 10981 : {
880 10981 : NS_CStringContainerInit(*this);
881 10981 : NS_CStringCopy(*this, aReadable);
882 10981 : }
883 :
884 : explicit
885 386 : nsCString(const char_type* aData, size_type aLength = PR_UINT32_MAX)
886 386 : {
887 386 : NS_CStringContainerInit(*this);
888 386 : NS_CStringSetData(*this, aData, aLength);
889 386 : }
890 :
891 19486 : ~nsCString()
892 19486 : {
893 19486 : NS_CStringContainerFinish(*this);
894 19486 : }
895 :
896 11773 : const char_type* get() const
897 : {
898 11773 : return BeginReading();
899 : }
900 :
901 60 : self_type& operator=(const self_type& aString) { Assign(aString); return *this; }
902 48 : self_type& operator=(const abstract_string_type& aReadable) { Assign(aReadable); return *this; }
903 1 : self_type& operator=(const char_type* aPtr) { Assign(aPtr); return *this; }
904 : self_type& operator=(char_type aChar) { Assign(aChar); return *this; }
905 :
906 104 : void Adopt(const char_type *aData, size_type aLength = PR_UINT32_MAX)
907 : {
908 104 : NS_CStringContainerFinish(*this);
909 : NS_CStringContainerInit2(*this, aData, aLength,
910 104 : NS_CSTRING_CONTAINER_INIT_ADOPT);
911 104 : }
912 :
913 : protected:
914 :
915 6322 : nsCString(const char_type* aData, size_type aLength, PRUint32 aFlags)
916 6322 : {
917 6322 : NS_CStringContainerInit2(*this, aData, aLength, aFlags);
918 6322 : }
919 : };
920 :
921 :
922 : /**
923 : * dependent strings
924 : */
925 :
926 : class nsDependentString : public nsString
927 55 : {
928 : public:
929 : typedef nsDependentString self_type;
930 :
931 : nsDependentString() {}
932 :
933 : explicit
934 55 : nsDependentString(const char_type* aData, size_type aLength = PR_UINT32_MAX)
935 55 : : nsString(aData, aLength, NS_CSTRING_CONTAINER_INIT_DEPEND)
936 55 : {}
937 :
938 : void Rebind(const char_type* aData, size_type aLength = PR_UINT32_MAX)
939 : {
940 : NS_StringContainerFinish(*this);
941 : NS_StringContainerInit2(*this, aData, aLength,
942 : NS_STRING_CONTAINER_INIT_DEPEND);
943 : }
944 :
945 : private:
946 : self_type& operator=(const self_type& aString) MOZ_DELETE;
947 : };
948 :
949 : class nsDependentCString : public nsCString
950 6322 : {
951 : public:
952 : typedef nsDependentCString self_type;
953 :
954 : nsDependentCString() {}
955 :
956 : explicit
957 6322 : nsDependentCString(const char_type* aData, size_type aLength = PR_UINT32_MAX)
958 6322 : : nsCString(aData, aLength, NS_CSTRING_CONTAINER_INIT_DEPEND)
959 6322 : {}
960 :
961 : void Rebind(const char_type* aData, size_type aLength = PR_UINT32_MAX)
962 : {
963 : NS_CStringContainerFinish(*this);
964 : NS_CStringContainerInit2(*this, aData, aLength,
965 : NS_CSTRING_CONTAINER_INIT_DEPEND);
966 : }
967 :
968 : private:
969 : self_type& operator=(const self_type& aString) MOZ_DELETE;
970 : };
971 :
972 :
973 : /**
974 : * conversion classes
975 : */
976 :
977 : inline void
978 0 : CopyUTF16toUTF8(const nsAString& aSource, nsACString& aDest)
979 : {
980 0 : NS_UTF16ToCString(aSource, NS_CSTRING_ENCODING_UTF8, aDest);
981 0 : }
982 :
983 : inline void
984 : CopyUTF8toUTF16(const nsACString& aSource, nsAString& aDest)
985 : {
986 : NS_CStringToUTF16(aSource, NS_CSTRING_ENCODING_UTF8, aDest);
987 : }
988 :
989 : inline void
990 : LossyCopyUTF16toASCII(const nsAString& aSource, nsACString& aDest)
991 : {
992 : NS_UTF16ToCString(aSource, NS_CSTRING_ENCODING_ASCII, aDest);
993 : }
994 :
995 : inline void
996 : CopyASCIItoUTF16(const nsACString& aSource, nsAString& aDest)
997 : {
998 : NS_CStringToUTF16(aSource, NS_CSTRING_ENCODING_ASCII, aDest);
999 : }
1000 :
1001 : NS_COM_GLUE char*
1002 : ToNewUTF8String(const nsAString& aSource);
1003 :
1004 : class NS_ConvertASCIItoUTF16 : public nsString
1005 0 : {
1006 : public:
1007 : typedef NS_ConvertASCIItoUTF16 self_type;
1008 :
1009 : explicit
1010 : NS_ConvertASCIItoUTF16(const nsACString& aStr)
1011 : {
1012 : NS_CStringToUTF16(aStr, NS_CSTRING_ENCODING_ASCII, *this);
1013 : }
1014 :
1015 : explicit
1016 0 : NS_ConvertASCIItoUTF16(const char* aData, PRUint32 aLength = PR_UINT32_MAX)
1017 0 : {
1018 0 : NS_CStringToUTF16(nsDependentCString(aData, aLength),
1019 0 : NS_CSTRING_ENCODING_ASCII, *this);
1020 0 : }
1021 :
1022 : private:
1023 : self_type& operator=(const self_type& aString) MOZ_DELETE;
1024 : };
1025 :
1026 : class NS_ConvertUTF8toUTF16 : public nsString
1027 1391 : {
1028 : public:
1029 : typedef NS_ConvertUTF8toUTF16 self_type;
1030 :
1031 : explicit
1032 1388 : NS_ConvertUTF8toUTF16(const nsACString& aStr)
1033 1388 : {
1034 1388 : NS_CStringToUTF16(aStr, NS_CSTRING_ENCODING_UTF8, *this);
1035 1388 : }
1036 :
1037 : explicit
1038 3 : NS_ConvertUTF8toUTF16(const char* aData, PRUint32 aLength = PR_UINT32_MAX)
1039 3 : {
1040 3 : NS_CStringToUTF16(nsDependentCString(aData, aLength),
1041 3 : NS_CSTRING_ENCODING_UTF8, *this);
1042 3 : }
1043 :
1044 : private:
1045 : self_type& operator=(const self_type& aString) MOZ_DELETE;
1046 : };
1047 :
1048 : class NS_ConvertUTF16toUTF8 : public nsCString
1049 1 : {
1050 : public:
1051 : typedef NS_ConvertUTF16toUTF8 self_type;
1052 :
1053 : explicit
1054 1 : NS_ConvertUTF16toUTF8(const nsAString& aStr)
1055 1 : {
1056 1 : NS_UTF16ToCString(aStr, NS_CSTRING_ENCODING_UTF8, *this);
1057 1 : }
1058 :
1059 : explicit
1060 : NS_ConvertUTF16toUTF8(const PRUnichar* aData, PRUint32 aLength = PR_UINT32_MAX)
1061 : {
1062 : NS_UTF16ToCString(nsDependentString(aData, aLength),
1063 : NS_CSTRING_ENCODING_UTF8, *this);
1064 : }
1065 :
1066 : private:
1067 : self_type& operator=(const self_type& aString) MOZ_DELETE;
1068 : };
1069 :
1070 : class NS_LossyConvertUTF16toASCII : public nsCString
1071 1 : {
1072 : public:
1073 : typedef NS_LossyConvertUTF16toASCII self_type;
1074 :
1075 : explicit
1076 1 : NS_LossyConvertUTF16toASCII(const nsAString& aStr)
1077 1 : {
1078 1 : NS_UTF16ToCString(aStr, NS_CSTRING_ENCODING_ASCII, *this);
1079 1 : }
1080 :
1081 : explicit
1082 0 : NS_LossyConvertUTF16toASCII(const PRUnichar* aData, PRUint32 aLength = PR_UINT32_MAX)
1083 0 : {
1084 0 : NS_UTF16ToCString(nsDependentString(aData, aLength),
1085 0 : NS_CSTRING_ENCODING_ASCII, *this);
1086 0 : }
1087 :
1088 : private:
1089 : self_type& operator=(const self_type& aString) MOZ_DELETE;
1090 : };
1091 :
1092 :
1093 : /**
1094 : * literal strings
1095 : *
1096 : * NOTE: HAVE_CPP_2BYTE_WCHAR_T may be automatically defined for some platforms
1097 : * in nscore.h. On other platforms, it may be defined in xpcom-config.h.
1098 : * Under GCC, this define should only be set if compiling with -fshort-wchar.
1099 : */
1100 :
1101 : #if defined(HAVE_CPP_CHAR16_T) || defined(HAVE_CPP_2BYTE_WCHAR_T)
1102 : #if defined(HAVE_CPP_CHAR16_T)
1103 : PR_STATIC_ASSERT(sizeof(char16_t) == 2);
1104 : #define NS_LL(s) u##s
1105 : #else
1106 : PR_STATIC_ASSERT(sizeof(wchar_t) == 2);
1107 : #define NS_LL(s) L##s
1108 : #endif
1109 : #define NS_MULTILINE_LITERAL_STRING(s) nsDependentString(reinterpret_cast<const nsAString::char_type*>(s), PRUint32((sizeof(s)/2)-1))
1110 : #define NS_MULTILINE_LITERAL_STRING_INIT(n,s) n(reinterpret_cast<const nsAString::char_type*>(s), PRUint32((sizeof(s)/2)-1))
1111 : #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) const nsDependentString n(reinterpret_cast<const nsAString::char_type*>(s), PRUint32((sizeof(s)/2)-1))
1112 : typedef nsDependentString nsLiteralString;
1113 : #else
1114 : #define NS_LL(s) s
1115 : #define NS_MULTILINE_LITERAL_STRING(s) NS_ConvertASCIItoUTF16(s, PRUint32(sizeof(s)-1))
1116 : #define NS_MULTILINE_LITERAL_STRING_INIT(n,s) n(s, PRUint32(sizeof(s)-1))
1117 : #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) const NS_ConvertASCIItoUTF16 n(s, PRUint32(sizeof(s)-1))
1118 : typedef NS_ConvertASCIItoUTF16 nsLiteralString;
1119 : #endif
1120 :
1121 : /* Check that PRUnichar is unsigned */
1122 : PR_STATIC_ASSERT(PRUnichar(-1) > PRUnichar(0));
1123 :
1124 : /*
1125 : * Macro arguments used in concatenation or stringification won't be expanded.
1126 : * Therefore, in order for |NS_L(FOO)| to work as expected (which is to expand
1127 : * |FOO| before doing whatever |NS_L| needs to do to it) a helper macro needs
1128 : * to be inserted in between to allow the macro argument to expand.
1129 : * See "3.10.6 Separate Expansion of Macro Arguments" of the CPP manual for a
1130 : * more accurate and precise explanation.
1131 : */
1132 :
1133 : #define NS_L(s) NS_LL(s)
1134 :
1135 : #define NS_LITERAL_STRING(s) static_cast<const nsString&>(NS_MULTILINE_LITERAL_STRING(NS_LL(s)))
1136 : #define NS_LITERAL_STRING_INIT(n,s) NS_MULTILINE_LITERAL_STRING_INIT(n, NS_LL(s))
1137 : #define NS_NAMED_LITERAL_STRING(n,s) NS_NAMED_MULTILINE_LITERAL_STRING(n, NS_LL(s))
1138 :
1139 : #define NS_LITERAL_CSTRING(s) static_cast<const nsDependentCString&>(nsDependentCString(s, PRUint32(sizeof(s)-1)))
1140 : #define NS_LITERAL_CSTRING_INIT(n,s) n(s, PRUint32(sizeof(s)-1))
1141 : #define NS_NAMED_LITERAL_CSTRING(n,s) const nsDependentCString n(s, PRUint32(sizeof(s)-1))
1142 :
1143 : typedef nsDependentCString nsLiteralCString;
1144 :
1145 :
1146 : /**
1147 : * getter_Copies support
1148 : *
1149 : * NS_IMETHOD GetBlah(PRUnichar**);
1150 : *
1151 : * void some_function()
1152 : * {
1153 : * nsString blah;
1154 : * GetBlah(getter_Copies(blah));
1155 : * // ...
1156 : * }
1157 : */
1158 :
1159 : class nsGetterCopies
1160 : {
1161 : public:
1162 : typedef PRUnichar char_type;
1163 :
1164 0 : nsGetterCopies(nsString& aStr)
1165 0 : : mString(aStr), mData(nsnull)
1166 0 : {}
1167 :
1168 0 : ~nsGetterCopies()
1169 : {
1170 0 : mString.Adopt(mData);
1171 0 : }
1172 :
1173 0 : operator char_type**()
1174 : {
1175 0 : return &mData;
1176 : }
1177 :
1178 : private:
1179 : nsString& mString;
1180 : char_type* mData;
1181 : };
1182 :
1183 : inline nsGetterCopies
1184 0 : getter_Copies(nsString& aString)
1185 : {
1186 0 : return nsGetterCopies(aString);
1187 : }
1188 :
1189 : class nsCGetterCopies
1190 : {
1191 : public:
1192 : typedef char char_type;
1193 :
1194 104 : nsCGetterCopies(nsCString& aStr)
1195 104 : : mString(aStr), mData(nsnull)
1196 104 : {}
1197 :
1198 104 : ~nsCGetterCopies()
1199 : {
1200 104 : mString.Adopt(mData);
1201 104 : }
1202 :
1203 104 : operator char_type**()
1204 : {
1205 104 : return &mData;
1206 : }
1207 :
1208 : private:
1209 : nsCString& mString;
1210 : char_type* mData;
1211 : };
1212 :
1213 : inline nsCGetterCopies
1214 104 : getter_Copies(nsCString& aString)
1215 : {
1216 104 : return nsCGetterCopies(aString);
1217 : }
1218 :
1219 :
1220 : /**
1221 : * substrings
1222 : */
1223 :
1224 : class NS_COM_GLUE nsDependentSubstring : public nsStringContainer
1225 : {
1226 : public:
1227 : typedef nsDependentSubstring self_type;
1228 : typedef nsAString abstract_string_type;
1229 :
1230 : ~nsDependentSubstring()
1231 : {
1232 : NS_StringContainerFinish(*this);
1233 : }
1234 :
1235 : nsDependentSubstring()
1236 : {
1237 : NS_StringContainerInit(*this);
1238 : }
1239 :
1240 : nsDependentSubstring(const char_type *aStart, PRUint32 aLength)
1241 : {
1242 : NS_StringContainerInit2(*this, aStart, aLength,
1243 : NS_STRING_CONTAINER_INIT_DEPEND |
1244 : NS_STRING_CONTAINER_INIT_SUBSTRING);
1245 : }
1246 :
1247 : nsDependentSubstring(const abstract_string_type& aStr,
1248 : PRUint32 aStartPos);
1249 : nsDependentSubstring(const abstract_string_type& aStr,
1250 : PRUint32 aStartPos, PRUint32 aLength);
1251 :
1252 : void Rebind(const char_type *aStart, PRUint32 aLength)
1253 : {
1254 : NS_StringContainerFinish(*this);
1255 : NS_StringContainerInit2(*this, aStart, aLength,
1256 : NS_STRING_CONTAINER_INIT_DEPEND |
1257 : NS_STRING_CONTAINER_INIT_SUBSTRING);
1258 : }
1259 :
1260 : private:
1261 : self_type& operator=(const self_type& aString) MOZ_DELETE;
1262 : };
1263 :
1264 : class NS_COM_GLUE nsDependentCSubstring : public nsCStringContainer
1265 : {
1266 : public:
1267 : typedef nsDependentCSubstring self_type;
1268 : typedef nsACString abstract_string_type;
1269 :
1270 1 : ~nsDependentCSubstring()
1271 1 : {
1272 1 : NS_CStringContainerFinish(*this);
1273 1 : }
1274 :
1275 : nsDependentCSubstring()
1276 : {
1277 : NS_CStringContainerInit(*this);
1278 : }
1279 :
1280 1 : nsDependentCSubstring(const char_type *aStart, PRUint32 aLength)
1281 1 : {
1282 : NS_CStringContainerInit2(*this, aStart, aLength,
1283 : NS_CSTRING_CONTAINER_INIT_DEPEND |
1284 1 : NS_CSTRING_CONTAINER_INIT_SUBSTRING);
1285 1 : }
1286 :
1287 : nsDependentCSubstring(const abstract_string_type& aStr,
1288 : PRUint32 aStartPos);
1289 : nsDependentCSubstring(const abstract_string_type& aStr,
1290 : PRUint32 aStartPos, PRUint32 aLength);
1291 :
1292 : void Rebind(const char_type *aStart, PRUint32 aLength)
1293 : {
1294 : NS_CStringContainerFinish(*this);
1295 : NS_CStringContainerInit2(*this, aStart, aLength,
1296 : NS_CSTRING_CONTAINER_INIT_DEPEND |
1297 : NS_CSTRING_CONTAINER_INIT_SUBSTRING);
1298 : }
1299 :
1300 : private:
1301 : self_type& operator=(const self_type& aString) MOZ_DELETE;
1302 : };
1303 :
1304 :
1305 : /**
1306 : * Various nsDependentC?Substring constructor functions
1307 : */
1308 :
1309 : // PRUnichar
1310 : inline const nsDependentSubstring
1311 : Substring( const nsAString& str, PRUint32 startPos )
1312 : {
1313 : return nsDependentSubstring(str, startPos);
1314 : }
1315 :
1316 : inline const nsDependentSubstring
1317 : Substring( const nsAString& str, PRUint32 startPos, PRUint32 length )
1318 : {
1319 : return nsDependentSubstring(str, startPos, length);
1320 : }
1321 :
1322 : inline const nsDependentSubstring
1323 : Substring( const PRUnichar* start, const PRUnichar* end )
1324 : {
1325 : NS_ABORT_IF_FALSE(PRUint32(end - start) == end - start, "string too long");
1326 : return nsDependentSubstring(start, PRUint32(end - start));
1327 : }
1328 :
1329 : inline const nsDependentSubstring
1330 : Substring( const PRUnichar* start, PRUint32 length )
1331 : {
1332 : return nsDependentSubstring(start, length);
1333 : }
1334 :
1335 : inline const nsDependentSubstring
1336 : StringHead( const nsAString& str, PRUint32 count )
1337 : {
1338 : return nsDependentSubstring(str, 0, count);
1339 : }
1340 :
1341 : inline const nsDependentSubstring
1342 : StringTail( const nsAString& str, PRUint32 count )
1343 : {
1344 : return nsDependentSubstring(str, str.Length() - count, count);
1345 : }
1346 :
1347 : // char
1348 : inline const nsDependentCSubstring
1349 : Substring( const nsACString& str, PRUint32 startPos )
1350 : {
1351 : return nsDependentCSubstring(str, startPos);
1352 : }
1353 :
1354 : inline const nsDependentCSubstring
1355 : Substring( const nsACString& str, PRUint32 startPos, PRUint32 length )
1356 : {
1357 : return nsDependentCSubstring(str, startPos, length);
1358 : }
1359 :
1360 : inline
1361 : const nsDependentCSubstring
1362 : Substring( const char* start, const char* end )
1363 : {
1364 : NS_ABORT_IF_FALSE(PRUint32(end - start) == end - start, "string too long");
1365 : return nsDependentCSubstring(start, PRUint32(end - start));
1366 : }
1367 :
1368 : inline
1369 : const nsDependentCSubstring
1370 : Substring( const char* start, PRUint32 length )
1371 : {
1372 : return nsDependentCSubstring(start, length);
1373 : }
1374 :
1375 : inline const nsDependentCSubstring
1376 0 : StringHead( const nsACString& str, PRUint32 count )
1377 : {
1378 0 : return nsDependentCSubstring(str, 0, count);
1379 : }
1380 :
1381 : inline const nsDependentCSubstring
1382 : StringTail( const nsACString& str, PRUint32 count )
1383 : {
1384 : return nsDependentCSubstring(str, str.Length() - count, count);
1385 : }
1386 :
1387 :
1388 : inline bool
1389 : StringBeginsWith(const nsAString& aSource, const nsAString& aSubstring,
1390 : nsAString::ComparatorFunc aComparator = nsAString::DefaultComparator)
1391 : {
1392 : return aSubstring.Length() <= aSource.Length() &&
1393 : StringHead(aSource, aSubstring.Length()).Equals(aSubstring, aComparator);
1394 : }
1395 :
1396 : inline bool
1397 : StringEndsWith(const nsAString& aSource, const nsAString& aSubstring,
1398 : nsAString::ComparatorFunc aComparator = nsAString::DefaultComparator)
1399 : {
1400 : return aSubstring.Length() <= aSource.Length() &&
1401 : StringTail(aSource, aSubstring.Length()).Equals(aSubstring, aComparator);
1402 : }
1403 :
1404 : inline bool
1405 : StringBeginsWith(const nsACString& aSource, const nsACString& aSubstring,
1406 : nsACString::ComparatorFunc aComparator = nsACString::DefaultComparator)
1407 : {
1408 : return aSubstring.Length() <= aSource.Length() &&
1409 : StringHead(aSource, aSubstring.Length()).Equals(aSubstring, aComparator);
1410 : }
1411 :
1412 : inline bool
1413 : StringEndsWith(const nsACString& aSource, const nsACString& aSubstring,
1414 : nsACString::ComparatorFunc aComparator = nsACString::DefaultComparator)
1415 : {
1416 : return aSubstring.Length() <= aSource.Length() &&
1417 : StringTail(aSource, aSubstring.Length()).Equals(aSubstring, aComparator);
1418 : }
1419 :
1420 : /**
1421 : * Trim whitespace from the beginning and end of a string; then compress
1422 : * remaining runs of whitespace characters to a single space.
1423 : */
1424 : NS_HIDDEN_(void)
1425 : CompressWhitespace(nsAString& aString);
1426 :
1427 : #define EmptyCString() nsCString()
1428 : #define EmptyString() nsString()
1429 :
1430 : /**
1431 : * Convert an ASCII string to all upper/lowercase (a-z,A-Z only). As a bonus,
1432 : * returns the string length.
1433 : */
1434 : NS_HIDDEN_(PRUint32)
1435 : ToLowerCase(nsACString& aStr);
1436 :
1437 : NS_HIDDEN_(PRUint32)
1438 : ToUpperCase(nsACString& aStr);
1439 :
1440 : NS_HIDDEN_(PRUint32)
1441 : ToLowerCase(const nsACString& aSrc, nsACString& aDest);
1442 :
1443 : NS_HIDDEN_(PRUint32)
1444 : ToUpperCase(const nsACString& aSrc, nsACString& aDest);
1445 :
1446 : /**
1447 : * The following declarations are *deprecated*, and are included here only
1448 : * to make porting from existing code that doesn't use the frozen string API
1449 : * easier. They may disappear in the future.
1450 : */
1451 :
1452 : inline char*
1453 8 : ToNewCString(const nsACString& aStr)
1454 : {
1455 8 : return NS_CStringCloneData(aStr);
1456 : }
1457 :
1458 : inline PRUnichar*
1459 4 : ToNewUnicode(const nsAString& aStr)
1460 : {
1461 4 : return NS_StringCloneData(aStr);
1462 : }
1463 :
1464 : typedef nsString PromiseFlatString;
1465 : typedef nsCString PromiseFlatCString;
1466 :
1467 : typedef nsCString nsCAutoString;
1468 : typedef nsString nsAutoString;
1469 :
1470 : NS_HIDDEN_(bool) ParseString(const nsACString& aAstring, char aDelimiter,
1471 : nsTArray<nsCString>& aArray);
1472 :
1473 : #endif // nsStringAPI_h__
|