1 /* vim:set ts=2 sw=2 et cindent: */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is Mozilla.
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.
22 * Darin Fisher <darin@meer.net>
23 * Benjamin Smedberg <benjamin@smedbergs.us>
24 * Ben Turner <mozilla@songbirdnest.com>
25 * Prasad Sunkari <prasad@medhas.org>
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.
39 * ***** END LICENSE BLOCK ***** */
42 * This header provides wrapper classes around the frozen string API
43 * which are roughly equivalent to the internal string classes.
46 #ifdef MOZILLA_INTERNAL_API
47 #error nsStringAPI.h is only usable from non-MOZILLA_INTERNAL_API code!
50 #ifndef nsStringAPI_h__
51 #define nsStringAPI_h__
53 #include "nsXPCOMStrings.h"
54 #include "nsISupportsImpl.h"
60 typedef PRUnichar char_type
;
61 typedef nsAString self_type
;
62 typedef PRUint32 size_type
;
63 typedef PRUint32 index_type
;
66 * Returns the length, beginning, and end of a string in one operation.
68 NS_HIDDEN_(PRUint32
) BeginReading(const char_type
**begin
,
69 const char_type
**end
= nsnull
) const;
71 NS_HIDDEN_(const char_type
*) BeginReading() const;
72 NS_HIDDEN_(const char_type
*) EndReading() const;
74 NS_HIDDEN_(char_type
) CharAt(PRUint32 aPos
) const
76 NS_ASSERTION(aPos
< Length(), "Out of bounds");
77 return BeginReading()[aPos
];
79 NS_HIDDEN_(char_type
) operator [](PRUint32 aPos
) const
83 NS_HIDDEN_(char_type
) First() const
89 * Get the length, begin writing, and optionally set the length of a
90 * string all in one operation.
92 * @param newSize Size the string to this length. Pass PR_UINT32_MAX
93 * to leave the length unchanged.
94 * @return The new length of the string, or 0 if resizing failed.
96 NS_HIDDEN_(PRUint32
) BeginWriting(char_type
**begin
,
97 char_type
**end
= nsnull
,
98 PRUint32 newSize
= PR_UINT32_MAX
);
100 NS_HIDDEN_(char_type
*) BeginWriting(PRUint32
= PR_UINT32_MAX
);
101 NS_HIDDEN_(char_type
*) EndWriting();
103 NS_HIDDEN_(PRBool
) SetLength(PRUint32 aLen
);
105 NS_HIDDEN_(size_type
) Length() const
107 const char_type
* data
;
108 return NS_StringGetData(*this, &data
);
111 NS_HIDDEN_(PRBool
) IsEmpty() const
113 return Length() == 0;
116 NS_HIDDEN_(void) SetIsVoid(PRBool val
)
118 NS_StringSetIsVoid(*this, val
);
120 NS_HIDDEN_(PRBool
) IsVoid() const
122 return NS_StringGetIsVoid(*this);
125 NS_HIDDEN_(void) Assign(const self_type
& aString
)
127 NS_StringCopy(*this, aString
);
129 NS_HIDDEN_(void) Assign(const char_type
* aData
, size_type aLength
= PR_UINT32_MAX
)
131 NS_StringSetData(*this, aData
, aLength
);
133 NS_HIDDEN_(void) Assign(char_type aChar
)
135 NS_StringSetData(*this, &aChar
, 1);
138 NS_HIDDEN_(void) AssignLiteral(const char *aStr
);
140 NS_HIDDEN_(self_type
&) operator=(const self_type
& aString
) { Assign(aString
); return *this; }
141 NS_HIDDEN_(self_type
&) operator=(const char_type
* aPtr
) { Assign(aPtr
); return *this; }
142 NS_HIDDEN_(self_type
&) operator=(char_type aChar
) { Assign(aChar
); return *this; }
144 NS_HIDDEN_(void) Replace( index_type cutStart
, size_type cutLength
, const char_type
* data
, size_type length
= size_type(-1) )
146 NS_StringSetDataRange(*this, cutStart
, cutLength
, data
, length
);
148 NS_HIDDEN_(void) Replace( index_type cutStart
, size_type cutLength
, char_type c
)
150 Replace(cutStart
, cutLength
, &c
, 1);
152 NS_HIDDEN_(void) Replace( index_type cutStart
, size_type cutLength
, const self_type
& readable
)
154 const char_type
* data
;
155 PRUint32 dataLen
= NS_StringGetData(readable
, &data
);
156 NS_StringSetDataRange(*this, cutStart
, cutLength
, data
, dataLen
);
159 NS_HIDDEN_(void) Append( char_type c
) { Replace(size_type(-1), 0, c
); }
160 NS_HIDDEN_(void) Append( const char_type
* data
, size_type length
= size_type(-1) ) { Replace(size_type(-1), 0, data
, length
); }
161 NS_HIDDEN_(void) Append( const self_type
& readable
) { Replace(size_type(-1), 0, readable
); }
162 NS_HIDDEN_(void) AppendLiteral( const char *aASCIIStr
);
164 NS_HIDDEN_(self_type
&) operator+=( char_type c
) { Append(c
); return *this; }
165 NS_HIDDEN_(self_type
&) operator+=( const char_type
* data
) { Append(data
); return *this; }
166 NS_HIDDEN_(self_type
&) operator+=( const self_type
& readable
) { Append(readable
); return *this; }
168 NS_HIDDEN_(void) Insert( char_type c
, index_type pos
) { Replace(pos
, 0, c
); }
169 NS_HIDDEN_(void) Insert( const char_type
* data
, index_type pos
, size_type length
= size_type(-1) ) { Replace(pos
, 0, data
, length
); }
170 NS_HIDDEN_(void) Insert( const self_type
& readable
, index_type pos
) { Replace(pos
, 0, readable
); }
172 NS_HIDDEN_(void) Cut( index_type cutStart
, size_type cutLength
) { Replace(cutStart
, cutLength
, nsnull
, 0); }
174 NS_HIDDEN_(void) Truncate() { SetLength(0); }
177 * Remove all occurences of characters in aSet from the string.
179 NS_HIDDEN_(void) StripChars(const char *aSet
);
182 * Strip whitespace characters from the string.
184 NS_HIDDEN_(void) StripWhitespace() { StripChars(" \t\n\r"); }
186 NS_HIDDEN_(void) Trim(const char *aSet
, PRBool aLeading
= PR_TRUE
,
187 PRBool aTrailing
= PR_TRUE
);
190 * Compare strings of characters. Return 0 if the characters are equal,
192 typedef PRInt32 (*ComparatorFunc
)(const char_type
*a
,
196 static NS_HIDDEN_(PRInt32
) DefaultComparator(const char_type
*a
,
200 NS_HIDDEN_(PRInt32
) Compare( const char_type
*other
,
201 ComparatorFunc c
= DefaultComparator
) const;
203 NS_HIDDEN_(PRInt32
) Compare( const self_type
&other
,
204 ComparatorFunc c
= DefaultComparator
) const;
206 NS_HIDDEN_(PRBool
) Equals( const char_type
*other
,
207 ComparatorFunc c
= DefaultComparator
) const;
209 NS_HIDDEN_(PRBool
) Equals( const self_type
&other
,
210 ComparatorFunc c
= DefaultComparator
) const;
212 NS_HIDDEN_(PRBool
) operator < (const self_type
&other
) const
214 return Compare(other
) < 0;
216 NS_HIDDEN_(PRBool
) operator < (const char_type
*other
) const
218 return Compare(other
) < 0;
221 NS_HIDDEN_(PRBool
) operator <= (const self_type
&other
) const
223 return Compare(other
) <= 0;
225 NS_HIDDEN_(PRBool
) operator <= (const char_type
*other
) const
227 return Compare(other
) <= 0;
230 NS_HIDDEN_(PRBool
) operator == (const self_type
&other
) const
232 return Equals(other
);
234 NS_HIDDEN_(PRBool
) operator == (const char_type
*other
) const
236 return Equals(other
);
239 NS_HIDDEN_(PRBool
) operator >= (const self_type
&other
) const
241 return Compare(other
) >= 0;
243 NS_HIDDEN_(PRBool
) operator >= (const char_type
*other
) const
245 return Compare(other
) >= 0;
248 NS_HIDDEN_(PRBool
) operator > (const self_type
&other
) const
250 return Compare(other
) > 0;
252 NS_HIDDEN_(PRBool
) operator > (const char_type
*other
) const
254 return Compare(other
) > 0;
257 NS_HIDDEN_(PRBool
) operator != (const self_type
&other
) const
259 return !Equals(other
);
261 NS_HIDDEN_(PRBool
) operator != (const char_type
*other
) const
263 return !Equals(other
);
266 NS_HIDDEN_(PRBool
) EqualsLiteral(const char *aASCIIString
) const;
269 * Case-insensitive match this string to a lowercase ASCII string.
271 NS_HIDDEN_(PRBool
) LowerCaseEqualsLiteral(const char *aASCIIString
) const;
274 * Find the first occurrence of aStr in this string.
276 * @return the offset of aStr, or -1 if not found
278 NS_HIDDEN_(PRInt32
) Find(const self_type
& aStr
,
279 ComparatorFunc c
= DefaultComparator
) const
280 { return Find(aStr
, 0, c
); }
283 * Find the first occurrence of aStr in this string, beginning at aOffset.
285 * @return the offset of aStr, or -1 if not found
287 NS_HIDDEN_(PRInt32
) Find(const self_type
& aStr
, PRUint32 aOffset
,
288 ComparatorFunc c
= DefaultComparator
) const;
291 * Find an ASCII string within this string.
293 * @return the offset of aStr, or -1 if not found.
295 NS_HIDDEN_(PRInt32
) Find(const char *aStr
, PRBool aIgnoreCase
= PR_FALSE
) const
296 { return Find(aStr
, 0, aIgnoreCase
); }
298 NS_HIDDEN_(PRInt32
) Find(const char *aStr
, PRUint32 aOffset
, PRBool aIgnoreCase
= PR_FALSE
) const;
301 * Find the last occurrence of aStr in this string.
303 * @return The offset of aStr from the beginning of the string,
304 * or -1 if not found.
306 NS_HIDDEN_(PRInt32
) RFind(const self_type
& aStr
,
307 ComparatorFunc c
= DefaultComparator
) const
308 { return RFind(aStr
, -1, c
); }
311 * Find the last occurrence of aStr in this string, beginning at aOffset.
313 * @param aOffset the offset from the beginning of the string to begin
314 * searching. If aOffset < 0, search from end of this string.
315 * @return The offset of aStr from the beginning of the string,
316 * or -1 if not found.
318 NS_HIDDEN_(PRInt32
) RFind(const self_type
& aStr
, PRInt32 aOffset
,
319 ComparatorFunc c
= DefaultComparator
) const;
322 * Find the last occurrence of an ASCII string within this string.
324 * @return The offset of aStr from the beginning of the string,
325 * or -1 if not found.
327 NS_HIDDEN_(PRInt32
) RFind(const char *aStr
, PRBool aIgnoreCase
= PR_FALSE
) const
328 { return RFind(aStr
, -1, aIgnoreCase
); }
331 * Find the last occurrence of an ASCII string beginning at aOffset.
333 * @param aOffset the offset from the beginning of the string to begin
334 * searching. If aOffset < 0, search from end of this string.
335 * @return The offset of aStr from the beginning of the string,
336 * or -1 if not found.
338 NS_HIDDEN_(PRInt32
) RFind(const char *aStr
, PRInt32 aOffset
, PRBool aIgnoreCase
) const;
341 * Search for the offset of the first occurrence of a character in a
344 * @param aOffset the offset from the beginning of the string to begin
346 * @return The offset of the character from the beginning of the string,
347 * or -1 if not found.
349 NS_HIDDEN_(PRInt32
) FindChar(char_type aChar
, PRUint32 aOffset
= 0) const;
352 * Search for the offset of the last occurrence of a character in a
355 * @return The offset of the character from the beginning of the string,
356 * or -1 if not found.
358 NS_HIDDEN_(PRInt32
) RFindChar(char_type aChar
) const;
361 * Append a string representation of a number.
363 NS_HIDDEN_(void) AppendInt(int aInt
, PRInt32 aRadix
= 10);
365 #ifndef XPCOM_GLUE_AVOID_NSPR
367 * Convert this string to an integer.
369 * @param aErrorCode pointer to contain result code.
370 * @param aRadix must be 10 or 16
372 NS_HIDDEN_(PRInt32
) ToInteger(nsresult
* aErrorCode
,
373 PRUint32 aRadix
= 10) const;
374 #endif // XPCOM_GLUE_AVOID_NSPR
377 // Prevent people from allocating a nsAString directly.
384 typedef char char_type
;
385 typedef nsACString self_type
;
386 typedef PRUint32 size_type
;
387 typedef PRUint32 index_type
;
390 * Returns the length, beginning, and end of a string in one operation.
392 NS_HIDDEN_(PRUint32
) BeginReading(const char_type
**begin
,
393 const char_type
**end
= nsnull
) const;
395 NS_HIDDEN_(const char_type
*) BeginReading() const;
396 NS_HIDDEN_(const char_type
*) EndReading() const;
398 NS_HIDDEN_(char_type
) CharAt(PRUint32 aPos
) const
400 NS_ASSERTION(aPos
< Length(), "Out of bounds");
401 return BeginReading()[aPos
];
403 NS_HIDDEN_(char_type
) operator [](PRUint32 aPos
) const
407 NS_HIDDEN_(char_type
) First() const
413 * Get the length, begin writing, and optionally set the length of a
414 * string all in one operation.
416 * @param newSize Size the string to this length. Pass PR_UINT32_MAX
417 * to leave the length unchanged.
418 * @return The new length of the string, or 0 if resizing failed.
420 NS_HIDDEN_(PRUint32
) BeginWriting(char_type
**begin
,
421 char_type
**end
= nsnull
,
422 PRUint32 newSize
= PR_UINT32_MAX
);
424 NS_HIDDEN_(char_type
*) BeginWriting(PRUint32 aLen
= PR_UINT32_MAX
);
425 NS_HIDDEN_(char_type
*) EndWriting();
427 NS_HIDDEN_(PRBool
) SetLength(PRUint32 aLen
);
429 NS_HIDDEN_(size_type
) Length() const
431 const char_type
* data
;
432 return NS_CStringGetData(*this, &data
);
435 NS_HIDDEN_(PRBool
) IsEmpty() const
437 return Length() == 0;
440 NS_HIDDEN_(void) SetIsVoid(PRBool val
)
442 NS_CStringSetIsVoid(*this, val
);
444 NS_HIDDEN_(PRBool
) IsVoid() const
446 return NS_CStringGetIsVoid(*this);
449 NS_HIDDEN_(void) Assign(const self_type
& aString
)
451 NS_CStringCopy(*this, aString
);
453 NS_HIDDEN_(void) Assign(const char_type
* aData
, size_type aLength
= PR_UINT32_MAX
)
455 NS_CStringSetData(*this, aData
, aLength
);
457 NS_HIDDEN_(void) Assign(char_type aChar
)
459 NS_CStringSetData(*this, &aChar
, 1);
461 NS_HIDDEN_(void) AssignLiteral(const char_type
*aData
)
466 NS_HIDDEN_(self_type
&) operator=(const self_type
& aString
) { Assign(aString
); return *this; }
467 NS_HIDDEN_(self_type
&) operator=(const char_type
* aPtr
) { Assign(aPtr
); return *this; }
468 NS_HIDDEN_(self_type
&) operator=(char_type aChar
) { Assign(aChar
); return *this; }
470 NS_HIDDEN_(void) Replace( index_type cutStart
, size_type cutLength
, const char_type
* data
, size_type length
= size_type(-1) )
472 NS_CStringSetDataRange(*this, cutStart
, cutLength
, data
, length
);
474 NS_HIDDEN_(void) Replace( index_type cutStart
, size_type cutLength
, char_type c
)
476 Replace(cutStart
, cutLength
, &c
, 1);
478 NS_HIDDEN_(void) Replace( index_type cutStart
, size_type cutLength
, const self_type
& readable
)
480 const char_type
* data
;
481 PRUint32 dataLen
= NS_CStringGetData(readable
, &data
);
482 NS_CStringSetDataRange(*this, cutStart
, cutLength
, data
, dataLen
);
485 NS_HIDDEN_(void) Append( char_type c
) { Replace(size_type(-1), 0, c
); }
486 NS_HIDDEN_(void) Append( const char_type
* data
, size_type length
= size_type(-1) ) { Replace(size_type(-1), 0, data
, length
); }
487 NS_HIDDEN_(void) Append( const self_type
& readable
) { Replace(size_type(-1), 0, readable
); }
488 NS_HIDDEN_(void) AppendLiteral( const char *aASCIIStr
) { Append(aASCIIStr
); }
490 NS_HIDDEN_(self_type
&) operator+=( char_type c
) { Append(c
); return *this; }
491 NS_HIDDEN_(self_type
&) operator+=( const char_type
* data
) { Append(data
); return *this; }
492 NS_HIDDEN_(self_type
&) operator+=( const self_type
& readable
) { Append(readable
); return *this; }
494 NS_HIDDEN_(void) Insert( char_type c
, index_type pos
) { Replace(pos
, 0, c
); }
495 NS_HIDDEN_(void) Insert( const char_type
* data
, index_type pos
, size_type length
= size_type(-1) ) { Replace(pos
, 0, data
, length
); }
496 NS_HIDDEN_(void) Insert( const self_type
& readable
, index_type pos
) { Replace(pos
, 0, readable
); }
498 NS_HIDDEN_(void) Cut( index_type cutStart
, size_type cutLength
) { Replace(cutStart
, cutLength
, nsnull
, 0); }
500 NS_HIDDEN_(void) Truncate() { SetLength(0); }
503 * Remove all occurences of characters in aSet from the string.
505 NS_HIDDEN_(void) StripChars(const char *aSet
);
508 * Strip whitespace characters from the string.
510 NS_HIDDEN_(void) StripWhitespace() { StripChars(" \t\r\n"); }
512 NS_HIDDEN_(void) Trim(const char *aSet
, PRBool aLeading
= PR_TRUE
,
513 PRBool aTrailing
= PR_TRUE
);
516 * Compare strings of characters. Return 0 if the characters are equal,
518 typedef PRInt32 (*ComparatorFunc
)(const char_type
*a
,
522 static NS_HIDDEN_(PRInt32
) DefaultComparator(const char_type
*a
,
526 NS_HIDDEN_(PRInt32
) Compare( const char_type
*other
,
527 ComparatorFunc c
= DefaultComparator
) const;
529 NS_HIDDEN_(PRInt32
) Compare( const self_type
&other
,
530 ComparatorFunc c
= DefaultComparator
) const;
532 NS_HIDDEN_(PRBool
) Equals( const char_type
*other
,
533 ComparatorFunc c
= DefaultComparator
) const;
535 NS_HIDDEN_(PRBool
) Equals( const self_type
&other
,
536 ComparatorFunc c
= DefaultComparator
) const;
538 NS_HIDDEN_(PRBool
) operator < (const self_type
&other
) const
540 return Compare(other
) < 0;
542 NS_HIDDEN_(PRBool
) operator < (const char_type
*other
) const
544 return Compare(other
) < 0;
547 NS_HIDDEN_(PRBool
) operator <= (const self_type
&other
) const
549 return Compare(other
) <= 0;
551 NS_HIDDEN_(PRBool
) operator <= (const char_type
*other
) const
553 return Compare(other
) <= 0;
556 NS_HIDDEN_(PRBool
) operator == (const self_type
&other
) const
558 return Equals(other
);
560 NS_HIDDEN_(PRBool
) operator == (const char_type
*other
) const
562 return Equals(other
);
565 NS_HIDDEN_(PRBool
) operator >= (const self_type
&other
) const
567 return Compare(other
) >= 0;
569 NS_HIDDEN_(PRBool
) operator >= (const char_type
*other
) const
571 return Compare(other
) >= 0;
574 NS_HIDDEN_(PRBool
) operator > (const self_type
&other
) const
576 return Compare(other
) > 0;
578 NS_HIDDEN_(PRBool
) operator > (const char_type
*other
) const
580 return Compare(other
) > 0;
583 NS_HIDDEN_(PRBool
) operator != (const self_type
&other
) const
585 return !Equals(other
);
587 NS_HIDDEN_(PRBool
) operator != (const char_type
*other
) const
589 return !Equals(other
);
592 NS_HIDDEN_(PRBool
) EqualsLiteral( const char_type
*other
) const
594 return Equals(other
);
598 * Find the first occurrence of aStr in this string.
600 * @return the offset of aStr, or -1 if not found
602 NS_HIDDEN_(PRInt32
) Find(const self_type
& aStr
,
603 ComparatorFunc c
= DefaultComparator
) const
604 { return Find(aStr
, 0, c
); }
607 * Find the first occurrence of aStr in this string, beginning at aOffset.
609 * @return the offset of aStr, or -1 if not found
611 NS_HIDDEN_(PRInt32
) Find(const self_type
& aStr
, PRUint32 aOffset
,
612 ComparatorFunc c
= DefaultComparator
) const;
615 * Find the first occurrence of aStr in this string.
617 * @return the offset of aStr, or -1 if not found
619 NS_HIDDEN_(PRInt32
) Find(const char_type
*aStr
,
620 ComparatorFunc c
= DefaultComparator
) const;
622 NS_HIDDEN_(PRInt32
) Find(const char_type
*aStr
, PRUint32 aLen
,
623 ComparatorFunc c
= DefaultComparator
) const;
626 * Find the last occurrence of aStr in this string.
628 * @return The offset of the character from the beginning of the string,
629 * or -1 if not found.
631 NS_HIDDEN_(PRInt32
) RFind(const self_type
& aStr
,
632 ComparatorFunc c
= DefaultComparator
) const
633 { return RFind(aStr
, -1, c
); }
636 * Find the last occurrence of aStr in this string, beginning at aOffset.
638 * @param aOffset the offset from the beginning of the string to begin
639 * searching. If aOffset < 0, search from end of this string.
640 * @return The offset of aStr from the beginning of the string,
641 * or -1 if not found.
643 NS_HIDDEN_(PRInt32
) RFind(const self_type
& aStr
, PRInt32 aOffset
,
644 ComparatorFunc c
= DefaultComparator
) const;
647 * Find the last occurrence of aStr in this string.
649 * @return The offset of aStr from the beginning of the string,
650 * or -1 if not found.
652 NS_HIDDEN_(PRInt32
) RFind(const char_type
*aStr
,
653 ComparatorFunc c
= DefaultComparator
) const;
656 * Find the last occurrence of an ASCII string in this string,
657 * beginning at aOffset.
659 * @param aLen is the length of aStr
660 * @return The offset of aStr from the beginning of the string,
661 * or -1 if not found.
663 NS_HIDDEN_(PRInt32
) RFind(const char_type
*aStr
, PRInt32 aLen
,
664 ComparatorFunc c
= DefaultComparator
) const;
667 * Search for the offset of the first occurrence of a character in a
670 * @param aOffset the offset from the beginning of the string to begin
672 * @return The offset of the character from the beginning of the string,
673 * or -1 if not found.
675 NS_HIDDEN_(PRInt32
) FindChar(char_type aChar
, PRUint32 aOffset
= 0) const;
678 * Search for the offset of the last occurrence of a character in a
681 * @return The offset of the character from the beginning of the string,
682 * or -1 if not found.
684 NS_HIDDEN_(PRInt32
) RFindChar(char_type aChar
) const;
687 * Append a string representation of a number.
689 NS_HIDDEN_(void) AppendInt(int aInt
, PRInt32 aRadix
= 10);
691 #ifndef XPCOM_GLUE_AVOID_NSPR
693 * Convert this string to an integer.
695 * @param aErrorCode pointer to contain result code.
696 * @param aRadix must be 10 or 16
698 NS_HIDDEN_(PRInt32
) ToInteger(nsresult
* aErrorCode
,
699 PRUint32 aRadix
= 10) const;
700 #endif // XPCOM_GLUE_AVOID_NSPR
703 // Prevent people from allocating a nsAString directly.
707 /* ------------------------------------------------------------------------- */
710 * Below we define nsStringContainer and nsCStringContainer. These classes
711 * have unspecified structure. In most cases, your code should use
712 * nsString/nsCString instead of these classes; if you prefer C-style
713 * programming, then look no further.
716 class nsStringContainer
: public nsAString
,
717 private nsStringContainer_base
721 class nsCStringContainer
: public nsACString
,
722 private nsStringContainer_base
727 * The following classes are C++ helper classes that make the frozen string
732 * Rename symbols to avoid conflicting with internal versions.
734 #define nsString nsString_external
735 #define nsCString nsCString_external
736 #define nsDependentString nsDependentString_external
737 #define nsDependentCString nsDependentCString_external
738 #define NS_ConvertASCIItoUTF16 NS_ConvertASCIItoUTF16_external
739 #define NS_ConvertUTF8toUTF16 NS_ConvertUTF8toUTF16_external
740 #define NS_ConvertUTF16toUTF8 NS_ConvertUTF16toUTF8_external
741 #define NS_LossyConvertUTF16toASCII NS_LossyConvertUTF16toASCII_external
742 #define nsGetterCopies nsGetterCopies_external
743 #define nsCGetterCopies nsCGetterCopies_external
744 #define nsDependentSubstring nsDependentSubstring_external
745 #define nsDependentCSubstring nsDependentCSubstring_external
751 class nsString
: public nsStringContainer
754 typedef nsString self_type
;
755 typedef nsAString abstract_string_type
;
759 NS_StringContainerInit(*this);
762 nsString(const self_type
& aString
)
764 NS_StringContainerInit(*this);
765 NS_StringCopy(*this, aString
);
769 nsString(const abstract_string_type
& aReadable
)
771 NS_StringContainerInit(*this);
772 NS_StringCopy(*this, aReadable
);
776 nsString(const char_type
* aData
, size_type aLength
= PR_UINT32_MAX
)
778 NS_StringContainerInit2(*this, aData
, aLength
, 0);
783 NS_StringContainerFinish(*this);
786 const char_type
* get() const
788 return BeginReading();
791 self_type
& operator=(const self_type
& aString
) { Assign(aString
); return *this; }
792 self_type
& operator=(const abstract_string_type
& aReadable
) { Assign(aReadable
); return *this; }
793 self_type
& operator=(const char_type
* aPtr
) { Assign(aPtr
); return *this; }
794 self_type
& operator=(char_type aChar
) { Assign(aChar
); return *this; }
796 void Adopt(const char_type
*aData
, size_type aLength
= PR_UINT32_MAX
)
798 NS_StringContainerFinish(*this);
799 NS_StringContainerInit2(*this, aData
, aLength
,
800 NS_STRING_CONTAINER_INIT_ADOPT
);
805 nsString(const char_type
* aData
, size_type aLength
, PRUint32 aFlags
)
807 NS_StringContainerInit2(*this, aData
, aLength
, aFlags
);
811 class nsCString
: public nsCStringContainer
814 typedef nsCString self_type
;
815 typedef nsACString abstract_string_type
;
819 NS_CStringContainerInit(*this);
822 nsCString(const self_type
& aString
)
824 NS_CStringContainerInit(*this);
825 NS_CStringCopy(*this, aString
);
829 nsCString(const abstract_string_type
& aReadable
)
831 NS_CStringContainerInit(*this);
832 NS_CStringCopy(*this, aReadable
);
836 nsCString(const char_type
* aData
, size_type aLength
= PR_UINT32_MAX
)
838 NS_CStringContainerInit(*this);
839 NS_CStringSetData(*this, aData
, aLength
);
844 NS_CStringContainerFinish(*this);
847 const char_type
* get() const
849 return BeginReading();
852 self_type
& operator=(const self_type
& aString
) { Assign(aString
); return *this; }
853 self_type
& operator=(const abstract_string_type
& aReadable
) { Assign(aReadable
); return *this; }
854 self_type
& operator=(const char_type
* aPtr
) { Assign(aPtr
); return *this; }
855 self_type
& operator=(char_type aChar
) { Assign(aChar
); return *this; }
857 void Adopt(const char_type
*aData
, size_type aLength
= PR_UINT32_MAX
)
859 NS_CStringContainerFinish(*this);
860 NS_CStringContainerInit2(*this, aData
, aLength
,
861 NS_CSTRING_CONTAINER_INIT_ADOPT
);
866 nsCString(const char_type
* aData
, size_type aLength
, PRUint32 aFlags
)
868 NS_CStringContainerInit2(*this, aData
, aLength
, aFlags
);
877 class nsDependentString
: public nsString
880 typedef nsDependentString self_type
;
882 nsDependentString() {}
885 nsDependentString(const char_type
* aData
, size_type aLength
= PR_UINT32_MAX
)
886 : nsString(aData
, aLength
, NS_CSTRING_CONTAINER_INIT_DEPEND
)
889 void Rebind(const char_type
* aData
, size_type aLength
= PR_UINT32_MAX
)
891 NS_StringContainerFinish(*this);
892 NS_StringContainerInit2(*this, aData
, aLength
,
893 NS_STRING_CONTAINER_INIT_DEPEND
);
897 self_type
& operator=(const self_type
& aString
); // NOT IMPLEMENTED
900 class nsDependentCString
: public nsCString
903 typedef nsDependentCString self_type
;
905 nsDependentCString() {}
908 nsDependentCString(const char_type
* aData
, size_type aLength
= PR_UINT32_MAX
)
909 : nsCString(aData
, aLength
, NS_CSTRING_CONTAINER_INIT_DEPEND
)
912 void Rebind(const char_type
* aData
, size_type aLength
= PR_UINT32_MAX
)
914 NS_CStringContainerFinish(*this);
915 NS_CStringContainerInit2(*this, aData
, aLength
,
916 NS_CSTRING_CONTAINER_INIT_DEPEND
);
920 self_type
& operator=(const self_type
& aString
); // NOT IMPLEMENTED
929 CopyUTF16toUTF8(const nsAString
& aSource
, nsACString
& aDest
)
931 NS_UTF16ToCString(aSource
, NS_CSTRING_ENCODING_UTF8
, aDest
);
935 CopyUTF8toUTF16(const nsACString
& aSource
, nsAString
& aDest
)
937 NS_CStringToUTF16(aSource
, NS_CSTRING_ENCODING_UTF8
, aDest
);
941 LossyCopyUTF16toASCII(const nsAString
& aSource
, nsACString
& aDest
)
943 NS_UTF16ToCString(aSource
, NS_CSTRING_ENCODING_ASCII
, aDest
);
947 CopyASCIItoUTF16(const nsACString
& aSource
, nsAString
& aDest
)
949 NS_CStringToUTF16(aSource
, NS_CSTRING_ENCODING_ASCII
, aDest
);
953 ToNewUTF8String(const nsAString
& aSource
);
955 class NS_ConvertASCIItoUTF16
: public nsString
958 typedef NS_ConvertASCIItoUTF16 self_type
;
961 NS_ConvertASCIItoUTF16(const nsACString
& aStr
)
963 NS_CStringToUTF16(aStr
, NS_CSTRING_ENCODING_ASCII
, *this);
967 NS_ConvertASCIItoUTF16(const char* aData
, PRUint32 aLength
= PR_UINT32_MAX
)
969 NS_CStringToUTF16(nsDependentCString(aData
, aLength
),
970 NS_CSTRING_ENCODING_ASCII
, *this);
974 self_type
& operator=(const self_type
& aString
); // NOT IMPLEMENTED
977 class NS_ConvertUTF8toUTF16
: public nsString
980 typedef NS_ConvertUTF8toUTF16 self_type
;
983 NS_ConvertUTF8toUTF16(const nsACString
& aStr
)
985 NS_CStringToUTF16(aStr
, NS_CSTRING_ENCODING_UTF8
, *this);
989 NS_ConvertUTF8toUTF16(const char* aData
, PRUint32 aLength
= PR_UINT32_MAX
)
991 NS_CStringToUTF16(nsDependentCString(aData
, aLength
),
992 NS_CSTRING_ENCODING_UTF8
, *this);
996 self_type
& operator=(const self_type
& aString
); // NOT IMPLEMENTED
999 class NS_ConvertUTF16toUTF8
: public nsCString
1002 typedef NS_ConvertUTF16toUTF8 self_type
;
1005 NS_ConvertUTF16toUTF8(const nsAString
& aStr
)
1007 NS_UTF16ToCString(aStr
, NS_CSTRING_ENCODING_UTF8
, *this);
1011 NS_ConvertUTF16toUTF8(const PRUnichar
* aData
, PRUint32 aLength
= PR_UINT32_MAX
)
1013 NS_UTF16ToCString(nsDependentString(aData
, aLength
),
1014 NS_CSTRING_ENCODING_UTF8
, *this);
1018 self_type
& operator=(const self_type
& aString
); // NOT IMPLEMENTED
1021 class NS_LossyConvertUTF16toASCII
: public nsCString
1024 typedef NS_LossyConvertUTF16toASCII self_type
;
1027 NS_LossyConvertUTF16toASCII(const nsAString
& aStr
)
1029 NS_UTF16ToCString(aStr
, NS_CSTRING_ENCODING_ASCII
, *this);
1033 NS_LossyConvertUTF16toASCII(const PRUnichar
* aData
, PRUint32 aLength
= PR_UINT32_MAX
)
1035 NS_UTF16ToCString(nsDependentString(aData
, aLength
),
1036 NS_CSTRING_ENCODING_ASCII
, *this);
1040 self_type
& operator=(const self_type
& aString
); // NOT IMPLEMENTED
1047 * NOTE: HAVE_CPP_2BYTE_WCHAR_T may be automatically defined for some platforms
1048 * in nscore.h. On other platforms, it may be defined in xpcom-config.h.
1049 * Under GCC, this define should only be set if compiling with -fshort-wchar.
1052 #ifdef HAVE_CPP_2BYTE_WCHAR_T
1053 PR_STATIC_ASSERT(sizeof(wchar_t) == 2);
1054 #define NS_LL(s) L##s
1055 #define NS_MULTILINE_LITERAL_STRING(s) nsDependentString(reinterpret_cast<const nsAString::char_type*>(s), PRUint32((sizeof(s)/sizeof(wchar_t))-1))
1056 #define NS_MULTILINE_LITERAL_STRING_INIT(n,s) n(reinterpret_cast<const nsAString::char_type*>(s), PRUint32((sizeof(s)/sizeof(wchar_t))-1))
1057 #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) const nsDependentString n(reinterpret_cast<const nsAString::char_type*>(s), PRUint32((sizeof(s)/sizeof(wchar_t))-1))
1058 typedef nsDependentString nsLiteralString
;
1061 #define NS_MULTILINE_LITERAL_STRING(s) NS_ConvertASCIItoUTF16(s, PRUint32(sizeof(s)-1))
1062 #define NS_MULTILINE_LITERAL_STRING_INIT(n,s) n(s, PRUint32(sizeof(s)-1))
1063 #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) const NS_ConvertASCIItoUTF16 n(s, PRUint32(sizeof(s)-1))
1064 typedef NS_ConvertASCIItoUTF16 nsLiteralString
;
1067 /* Check that PRUnichar is unsigned */
1068 PR_STATIC_ASSERT(PRUnichar(-1) > PRUnichar(0));
1071 * Macro arguments used in concatenation or stringification won't be expanded.
1072 * Therefore, in order for |NS_L(FOO)| to work as expected (which is to expand
1073 * |FOO| before doing whatever |NS_L| needs to do to it) a helper macro needs
1074 * to be inserted in between to allow the macro argument to expand.
1075 * See "3.10.6 Separate Expansion of Macro Arguments" of the CPP manual for a
1076 * more accurate and precise explanation.
1079 #define NS_L(s) NS_LL(s)
1081 #define NS_LITERAL_STRING(s) static_cast<const nsString&>(NS_MULTILINE_LITERAL_STRING(NS_LL(s)))
1082 #define NS_LITERAL_STRING_INIT(n,s) NS_MULTILINE_LITERAL_STRING_INIT(n, NS_LL(s))
1083 #define NS_NAMED_LITERAL_STRING(n,s) NS_NAMED_MULTILINE_LITERAL_STRING(n, NS_LL(s))
1085 #define NS_LITERAL_CSTRING(s) static_cast<const nsDependentCString&>(nsDependentCString(s, PRUint32(sizeof(s)-1)))
1086 #define NS_LITERAL_CSTRING_INIT(n,s) n(s, PRUint32(sizeof(s)-1))
1087 #define NS_NAMED_LITERAL_CSTRING(n,s) const nsDependentCString n(s, PRUint32(sizeof(s)-1))
1089 typedef nsDependentCString nsLiteralCString
;
1093 * getter_Copies support
1095 * NS_IMETHOD GetBlah(PRUnichar**);
1097 * void some_function()
1100 * GetBlah(getter_Copies(blah));
1105 class nsGetterCopies
1108 typedef PRUnichar char_type
;
1110 nsGetterCopies(nsString
& aStr
)
1111 : mString(aStr
), mData(nsnull
)
1116 mString
.Adopt(mData
);
1119 operator char_type
**()
1129 inline nsGetterCopies
1130 getter_Copies(nsString
& aString
)
1132 return nsGetterCopies(aString
);
1135 class nsCGetterCopies
1138 typedef char char_type
;
1140 nsCGetterCopies(nsCString
& aStr
)
1141 : mString(aStr
), mData(nsnull
)
1146 mString
.Adopt(mData
);
1149 operator char_type
**()
1159 inline nsCGetterCopies
1160 getter_Copies(nsCString
& aString
)
1162 return nsCGetterCopies(aString
);
1170 class NS_COM_GLUE nsDependentSubstring
: public nsStringContainer
1173 typedef nsDependentSubstring self_type
;
1174 typedef nsAString abstract_string_type
;
1176 ~nsDependentSubstring()
1178 NS_StringContainerFinish(*this);
1181 nsDependentSubstring()
1183 NS_StringContainerInit(*this);
1186 nsDependentSubstring(const char_type
*aStart
, PRUint32 aLength
)
1188 NS_StringContainerInit2(*this, aStart
, aLength
,
1189 NS_STRING_CONTAINER_INIT_DEPEND
|
1190 NS_STRING_CONTAINER_INIT_SUBSTRING
);
1193 nsDependentSubstring(const abstract_string_type
& aStr
,
1194 PRUint32 aStartPos
);
1195 nsDependentSubstring(const abstract_string_type
& aStr
,
1196 PRUint32 aStartPos
, PRUint32 aLength
);
1198 void Rebind(const char_type
*aStart
, PRUint32 aLength
)
1200 NS_StringContainerFinish(*this);
1201 NS_StringContainerInit2(*this, aStart
, aLength
,
1202 NS_STRING_CONTAINER_INIT_DEPEND
|
1203 NS_STRING_CONTAINER_INIT_SUBSTRING
);
1207 self_type
& operator=(const self_type
& aString
); // NOT IMPLEMENTED
1210 class NS_COM_GLUE nsDependentCSubstring
: public nsCStringContainer
1213 typedef nsDependentCSubstring self_type
;
1214 typedef nsACString abstract_string_type
;
1216 ~nsDependentCSubstring()
1218 NS_CStringContainerFinish(*this);
1221 nsDependentCSubstring()
1223 NS_CStringContainerInit(*this);
1226 nsDependentCSubstring(const char_type
*aStart
, PRUint32 aLength
)
1228 NS_CStringContainerInit2(*this, aStart
, aLength
,
1229 NS_CSTRING_CONTAINER_INIT_DEPEND
|
1230 NS_CSTRING_CONTAINER_INIT_SUBSTRING
);
1233 nsDependentCSubstring(const abstract_string_type
& aStr
,
1234 PRUint32 aStartPos
);
1235 nsDependentCSubstring(const abstract_string_type
& aStr
,
1236 PRUint32 aStartPos
, PRUint32 aLength
);
1238 void Rebind(const char_type
*aStart
, PRUint32 aLength
)
1240 NS_CStringContainerFinish(*this);
1241 NS_CStringContainerInit2(*this, aStart
, aLength
,
1242 NS_CSTRING_CONTAINER_INIT_DEPEND
|
1243 NS_CSTRING_CONTAINER_INIT_SUBSTRING
);
1247 self_type
& operator=(const self_type
& aString
); // NOT IMPLEMENTED
1252 * Various nsDependentC?Substring constructor functions
1256 inline const nsDependentSubstring
1257 Substring( const nsAString
& str
, PRUint32 startPos
)
1259 return nsDependentSubstring(str
, startPos
);
1262 inline const nsDependentSubstring
1263 Substring( const nsAString
& str
, PRUint32 startPos
, PRUint32 length
)
1265 return nsDependentSubstring(str
, startPos
, length
);
1268 inline const nsDependentSubstring
1269 Substring( const PRUnichar
* start
, const PRUnichar
* end
)
1271 return nsDependentSubstring(start
, end
- start
);
1274 inline const nsDependentSubstring
1275 Substring( const PRUnichar
* start
, PRUint32 length
)
1277 return nsDependentSubstring(start
, length
);
1280 inline const nsDependentSubstring
1281 StringHead( const nsAString
& str
, PRUint32 count
)
1283 return nsDependentSubstring(str
, 0, count
);
1286 inline const nsDependentSubstring
1287 StringTail( const nsAString
& str
, PRUint32 count
)
1289 return nsDependentSubstring(str
, str
.Length() - count
, count
);
1293 inline const nsDependentCSubstring
1294 Substring( const nsACString
& str
, PRUint32 startPos
)
1296 return nsDependentCSubstring(str
, startPos
);
1299 inline const nsDependentCSubstring
1300 Substring( const nsACString
& str
, PRUint32 startPos
, PRUint32 length
)
1302 return nsDependentCSubstring(str
, startPos
, length
);
1306 const nsDependentCSubstring
1307 Substring( const char* start
, const char* end
)
1309 return nsDependentCSubstring(start
, end
- start
);
1313 const nsDependentCSubstring
1314 Substring( const char* start
, PRUint32 length
)
1316 return nsDependentCSubstring(start
, length
);
1319 inline const nsDependentCSubstring
1320 StringHead( const nsACString
& str
, PRUint32 count
)
1322 return nsDependentCSubstring(str
, 0, count
);
1325 inline const nsDependentCSubstring
1326 StringTail( const nsACString
& str
, PRUint32 count
)
1328 return nsDependentCSubstring(str
, str
.Length() - count
, count
);
1333 StringBeginsWith(const nsAString
& aSource
, const nsAString
& aSubstring
,
1334 nsAString::ComparatorFunc aComparator
= nsAString::DefaultComparator
)
1336 return aSubstring
.Length() <= aSource
.Length() &&
1337 StringHead(aSource
, aSubstring
.Length()).Equals(aSubstring
, aComparator
);
1341 StringEndsWith(const nsAString
& aSource
, const nsAString
& aSubstring
,
1342 nsAString::ComparatorFunc aComparator
= nsAString::DefaultComparator
)
1344 return aSubstring
.Length() <= aSource
.Length() &&
1345 StringTail(aSource
, aSubstring
.Length()).Equals(aSubstring
, aComparator
);
1349 StringBeginsWith(const nsACString
& aSource
, const nsACString
& aSubstring
,
1350 nsACString::ComparatorFunc aComparator
= nsACString::DefaultComparator
)
1352 return aSubstring
.Length() <= aSource
.Length() &&
1353 StringHead(aSource
, aSubstring
.Length()).Equals(aSubstring
, aComparator
);
1357 StringEndsWith(const nsACString
& aSource
, const nsACString
& aSubstring
,
1358 nsACString::ComparatorFunc aComparator
= nsACString::DefaultComparator
)
1360 return aSubstring
.Length() <= aSource
.Length() &&
1361 StringTail(aSource
, aSubstring
.Length()).Equals(aSubstring
, aComparator
);
1365 * Trim whitespace from the beginning and end of a string; then compress
1366 * remaining runs of whitespace characters to a single space.
1369 CompressWhitespace(nsAString
& aString
);
1371 #define EmptyCString() nsCString()
1372 #define EmptyString() nsString()
1375 * Convert an ASCII string to all upper/lowercase (a-z,A-Z only). As a bonus,
1376 * returns the string length.
1378 NS_HIDDEN_(PRUint32
)
1379 ToLowerCase(nsACString
& aStr
);
1381 NS_HIDDEN_(PRUint32
)
1382 ToUpperCase(nsACString
& aStr
);
1384 NS_HIDDEN_(PRUint32
)
1385 ToLowerCase(const nsACString
& aSrc
, nsACString
& aDest
);
1387 NS_HIDDEN_(PRUint32
)
1388 ToUpperCase(const nsACString
& aSrc
, nsACString
& aDest
);
1391 * Comparison function for use with nsACString::Equals
1394 CaseInsensitiveCompare(const char *a
, const char *b
,
1398 * The following declarations are *deprecated*, and are included here only
1399 * to make porting from existing code that doesn't use the frozen string API
1400 * easier. They may disappear in the future.
1404 ToNewCString(const nsACString
& aStr
)
1406 return NS_CStringCloneData(aStr
);
1410 ToNewUnicode(const nsAString
& aStr
)
1412 return NS_StringCloneData(aStr
);
1415 typedef nsString PromiseFlatString
;
1416 typedef nsCString PromiseFlatCString
;
1418 typedef nsCString nsCAutoString
;
1419 typedef nsString nsAutoString
;
1421 #endif // nsStringAPI_h__