Bug 572417 - Release mouse capture in flash subclass after mouse events get delivered...
[mozilla-central.git] / xpcom / string / public / nsTString.h
blob07c3c002007181380836be3266fdf1cf594119f5
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla.
18 * The Initial Developer of the Original Code is IBM Corporation.
19 * Portions created by IBM Corporation are Copyright (C) 2003
20 * IBM Corporation. All Rights Reserved.
22 * Contributor(s):
23 * Rick Gessner <rickg@netscape.com> (original author)
24 * Scott Collins <scc@mozilla.org>
25 * Darin Fisher <darin@meer.net>
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 /**
43 * This is the canonical null-terminated string class. All subclasses
44 * promise null-terminated storage. Instances of this class allocate
45 * strings on the heap.
47 * NAMES:
48 * nsString for wide characters
49 * nsCString for narrow characters
51 * This class is also known as nsAFlat[C]String, where "flat" is used
52 * to denote a null-terminated string.
54 class nsTString_CharT : public nsTSubstring_CharT
56 public:
58 typedef nsTString_CharT self_type;
60 public:
62 /**
63 * constructors
66 nsTString_CharT()
67 : substring_type() {}
69 explicit
70 nsTString_CharT( char_type c )
71 : substring_type()
73 Assign(c);
76 explicit
77 nsTString_CharT( const char_type* data, size_type length = size_type(-1) )
78 : substring_type()
80 Assign(data, length);
83 nsTString_CharT( const self_type& str )
84 : substring_type()
86 Assign(str);
89 nsTString_CharT( const substring_tuple_type& tuple )
90 : substring_type()
92 Assign(tuple);
95 explicit
96 nsTString_CharT( const substring_type& readable )
97 : substring_type()
99 Assign(readable);
103 // |operator=| does not inherit, so we must define our own
104 self_type& operator=( char_type c ) { Assign(c); return *this; }
105 self_type& operator=( const char_type* data ) { Assign(data); return *this; }
106 self_type& operator=( const self_type& str ) { Assign(str); return *this; }
107 self_type& operator=( const substring_type& str ) { Assign(str); return *this; }
108 self_type& operator=( const substring_tuple_type& tuple ) { Assign(tuple); return *this; }
111 * returns the null-terminated string
114 const char_type* get() const
116 return mData;
121 * returns character at specified index.
123 * NOTE: unlike nsTSubstring::CharAt, this function allows you to index
124 * the null terminator character.
127 char_type CharAt( index_type i ) const
129 NS_ASSERTION(i <= mLength, "index exceeds allowable range");
130 return mData[i];
133 char_type operator[]( index_type i ) const
135 return CharAt(i);
139 #if MOZ_STRING_WITH_OBSOLETE_API
143 * Search for the given substring within this string.
145 * @param aString is substring to be sought in this
146 * @param aIgnoreCase selects case sensitivity
147 * @param aOffset tells us where in this string to start searching
148 * @param aCount tells us how far from the offset we are to search. Use
149 * -1 to search the whole string.
150 * @return offset in string, or kNotFound
153 NS_COM PRInt32 Find( const nsCString& aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
154 NS_COM PRInt32 Find( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
156 #ifdef CharT_is_PRUnichar
157 NS_COM PRInt32 Find( const nsAFlatString& aString, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
158 NS_COM PRInt32 Find( const PRUnichar* aString, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
159 #endif
163 * This methods scans the string backwards, looking for the given string
165 * @param aString is substring to be sought in this
166 * @param aIgnoreCase tells us whether or not to do caseless compare
167 * @param aOffset tells us where in this string to start searching.
168 * Use -1 to search from the end of the string.
169 * @param aCount tells us how many iterations to make starting at the
170 * given offset.
171 * @return offset in string, or kNotFound
174 NS_COM PRInt32 RFind( const nsCString& aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
175 NS_COM PRInt32 RFind( const char* aCString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
177 #ifdef CharT_is_PRUnichar
178 NS_COM PRInt32 RFind( const nsAFlatString& aString, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
179 NS_COM PRInt32 RFind( const PRUnichar* aString, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
180 #endif
184 * Search for given char within this string
186 * @param aChar is the character to search for
187 * @param aOffset tells us where in this string to start searching
188 * @param aCount tells us how far from the offset we are to search.
189 * Use -1 to search the whole string.
190 * @return offset in string, or kNotFound
193 // PRInt32 FindChar( PRUnichar aChar, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
194 NS_COM PRInt32 RFindChar( PRUnichar aChar, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
198 * This method searches this string for the first character found in
199 * the given string.
201 * @param aString contains set of chars to be found
202 * @param aOffset tells us where in this string to start searching
203 * (counting from left)
204 * @return offset in string, or kNotFound
207 NS_COM PRInt32 FindCharInSet( const char* aString, PRInt32 aOffset=0 ) const;
208 PRInt32 FindCharInSet( const self_type& aString, PRInt32 aOffset=0 ) const
210 return FindCharInSet(aString.get(), aOffset);
213 #ifdef CharT_is_PRUnichar
214 NS_COM PRInt32 FindCharInSet( const PRUnichar* aString, PRInt32 aOffset=0 ) const;
215 #endif
219 * This method searches this string for the last character found in
220 * the given string.
222 * @param aString contains set of chars to be found
223 * @param aOffset tells us where in this string to start searching
224 * (counting from left)
225 * @return offset in string, or kNotFound
228 NS_COM PRInt32 RFindCharInSet( const char_type* aString, PRInt32 aOffset=-1 ) const;
229 PRInt32 RFindCharInSet( const self_type& aString, PRInt32 aOffset=-1 ) const
231 return RFindCharInSet(aString.get(), aOffset);
236 * Compares a given string to this string.
238 * @param aString is the string to be compared
239 * @param aIgnoreCase tells us how to treat case
240 * @param aCount tells us how many chars to compare
241 * @return -1,0,1
244 #ifdef CharT_is_char
245 NS_COM PRInt32 Compare( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aCount=-1 ) const;
246 #endif
250 * Equality check between given string and this string.
252 * @param aString is the string to check
253 * @param aIgnoreCase tells us how to treat case
254 * @param aCount tells us how many chars to compare
255 * @return boolean
257 #ifdef CharT_is_char
258 PRBool EqualsIgnoreCase( const char* aString, PRInt32 aCount=-1 ) const {
259 return Compare(aString, PR_TRUE, aCount) == 0;
261 #else
262 NS_COM PRBool EqualsIgnoreCase( const char* aString, PRInt32 aCount=-1 ) const;
265 #endif // !CharT_is_PRUnichar
268 * Perform string to float conversion.
270 * @param aErrorCode will contain error if one occurs
271 * @return float rep of string value
273 NS_COM float ToFloat( PRInt32* aErrorCode ) const;
277 * Perform string to int conversion.
278 * @param aErrorCode will contain error if one occurs
279 * @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you.
280 * @return int rep of string value, and possible (out) error code
282 NS_COM PRInt32 ToInteger( PRInt32* aErrorCode, PRUint32 aRadix=kRadix10 ) const;
283 PRInt32 ToInteger( nsresult* aErrorCode, PRUint32 aRadix=kRadix10 ) const {
284 return ToInteger(reinterpret_cast<PRInt32*>(aErrorCode), aRadix);
288 * |Left|, |Mid|, and |Right| are annoying signatures that seem better almost
289 * any _other_ way than they are now. Consider these alternatives
291 * aWritable = aReadable.Left(17); // ...a member function that returns a |Substring|
292 * aWritable = Left(aReadable, 17); // ...a global function that returns a |Substring|
293 * Left(aReadable, 17, aWritable); // ...a global function that does the assignment
295 * as opposed to the current signature
297 * aReadable.Left(aWritable, 17); // ...a member function that does the assignment
299 * or maybe just stamping them out in favor of |Substring|, they are just duplicate functionality
301 * aWritable = Substring(aReadable, 0, 17);
304 NS_COM size_type Mid( self_type& aResult, PRUint32 aStartPos, PRUint32 aCount ) const;
306 size_type Left( self_type& aResult, size_type aCount ) const
308 return Mid(aResult, 0, aCount);
311 size_type Right( self_type& aResult, size_type aCount ) const
313 aCount = NS_MIN(mLength, aCount);
314 return Mid(aResult, mLength - aCount, aCount);
319 * Set a char inside this string at given index
321 * @param aChar is the char you want to write into this string
322 * @param anIndex is the ofs where you want to write the given char
323 * @return TRUE if successful
326 NS_COM PRBool SetCharAt( PRUnichar aChar, PRUint32 aIndex );
330 * These methods are used to remove all occurrences of the
331 * characters found in aSet from this string.
333 * @param aSet -- characters to be cut from this
335 NS_COM void StripChars( const char* aSet );
339 * This method strips whitespace throughout the string.
341 NS_COM void StripWhitespace();
345 * swaps occurence of 1 string for another
348 NS_COM void ReplaceChar( char_type aOldChar, char_type aNewChar );
349 NS_COM void ReplaceChar( const char* aSet, char_type aNewChar );
350 NS_COM void ReplaceSubstring( const self_type& aTarget, const self_type& aNewValue);
351 NS_COM void ReplaceSubstring( const char_type* aTarget, const char_type* aNewValue);
355 * This method trims characters found in aTrimSet from
356 * either end of the underlying string.
358 * @param aSet -- contains chars to be trimmed from both ends
359 * @param aEliminateLeading
360 * @param aEliminateTrailing
361 * @param aIgnoreQuotes -- if true, causes surrounding quotes to be ignored
362 * @return this
364 NS_COM void Trim( const char* aSet, PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE, PRBool aIgnoreQuotes=PR_FALSE );
367 * This method strips whitespace from string.
368 * You can control whether whitespace is yanked from start and end of
369 * string as well.
371 * @param aEliminateLeading controls stripping of leading ws
372 * @param aEliminateTrailing controls stripping of trailing ws
374 NS_COM void CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE );
378 * assign/append/insert with _LOSSY_ conversion
381 NS_COM void AssignWithConversion( const nsTAString_IncompatibleCharT& aString );
382 NS_COM void AssignWithConversion( const incompatible_char_type* aData, PRInt32 aLength=-1 );
384 NS_COM void AppendWithConversion( const nsTAString_IncompatibleCharT& aString );
385 NS_COM void AppendWithConversion( const incompatible_char_type* aData, PRInt32 aLength=-1 );
387 #endif // !MOZ_STRING_WITH_OBSOLETE_API
390 protected:
392 explicit
393 nsTString_CharT( PRUint32 flags )
394 : substring_type(flags) {}
396 // allow subclasses to initialize fields directly
397 nsTString_CharT( char_type* data, size_type length, PRUint32 flags )
398 : substring_type(data, length, flags) {}
402 class nsTFixedString_CharT : public nsTString_CharT
404 public:
406 typedef nsTFixedString_CharT self_type;
407 typedef nsTFixedString_CharT fixed_string_type;
409 public:
412 * @param data
413 * fixed-size buffer to be used by the string (the contents of
414 * this buffer may be modified by the string)
415 * @param storageSize
416 * the size of the fixed buffer
417 * @param length (optional)
418 * the length of the string already contained in the buffer
421 nsTFixedString_CharT( char_type* data, size_type storageSize )
422 : string_type(data, PRUint32(char_traits::length(data)), F_TERMINATED | F_FIXED | F_CLASS_FIXED)
423 , mFixedCapacity(storageSize - 1)
424 , mFixedBuf(data)
427 nsTFixedString_CharT( char_type* data, size_type storageSize, size_type length )
428 : string_type(data, length, F_TERMINATED | F_FIXED | F_CLASS_FIXED)
429 , mFixedCapacity(storageSize - 1)
430 , mFixedBuf(data)
432 // null-terminate
433 mFixedBuf[length] = char_type(0);
436 // |operator=| does not inherit, so we must define our own
437 self_type& operator=( char_type c ) { Assign(c); return *this; }
438 self_type& operator=( const char_type* data ) { Assign(data); return *this; }
439 self_type& operator=( const substring_type& str ) { Assign(str); return *this; }
440 self_type& operator=( const substring_tuple_type& tuple ) { Assign(tuple); return *this; }
442 protected:
444 friend class nsTSubstring_CharT;
446 size_type mFixedCapacity;
447 char_type *mFixedBuf;
452 * nsTAutoString_CharT
454 * Subclass of nsTString_CharT that adds support for stack-based string
455 * allocation. It is normally not a good idea to use this class on the
456 * heap, because it will allocate space which may be wasted if the string
457 * it contains is significantly smaller or any larger than 64 characters.
459 * NAMES:
460 * nsAutoString for wide characters
461 * nsCAutoString for narrow characters
463 class NS_STACK_CLASS nsTAutoString_CharT : public nsTFixedString_CharT
465 public:
467 typedef nsTAutoString_CharT self_type;
469 public:
472 * constructors
475 nsTAutoString_CharT()
476 : fixed_string_type(mStorage, kDefaultStorageSize, 0)
479 explicit
480 nsTAutoString_CharT( char_type c )
481 : fixed_string_type(mStorage, kDefaultStorageSize, 0)
483 Assign(c);
486 explicit
487 nsTAutoString_CharT( const char_type* data, size_type length = size_type(-1) )
488 : fixed_string_type(mStorage, kDefaultStorageSize, 0)
490 Assign(data, length);
493 nsTAutoString_CharT( const self_type& str )
494 : fixed_string_type(mStorage, kDefaultStorageSize, 0)
496 Assign(str);
499 explicit
500 nsTAutoString_CharT( const substring_type& str )
501 : fixed_string_type(mStorage, kDefaultStorageSize, 0)
503 Assign(str);
506 nsTAutoString_CharT( const substring_tuple_type& tuple )
507 : fixed_string_type(mStorage, kDefaultStorageSize, 0)
509 Assign(tuple);
512 // |operator=| does not inherit, so we must define our own
513 self_type& operator=( char_type c ) { Assign(c); return *this; }
514 self_type& operator=( const char_type* data ) { Assign(data); return *this; }
515 self_type& operator=( const self_type& str ) { Assign(str); return *this; }
516 self_type& operator=( const substring_type& str ) { Assign(str); return *this; }
517 self_type& operator=( const substring_tuple_type& tuple ) { Assign(tuple); return *this; }
519 enum { kDefaultStorageSize = 64 };
521 private:
523 char_type mStorage[kDefaultStorageSize];
528 // nsAutoString stores pointers into itself which are invalidated when an
529 // nsTArray is resized, so nsTArray must not be instantiated with nsAutoString
530 // elements!
532 template<class E> class nsTArrayElementTraits;
533 template<>
534 class nsTArrayElementTraits<nsTAutoString_CharT> {
535 public:
536 template<class A> struct Dont_Instantiate_nsTArray_of;
537 template<class A> struct Instead_Use_nsTArray_of;
539 static Dont_Instantiate_nsTArray_of<nsTAutoString_CharT> *
540 Construct(Instead_Use_nsTArray_of<nsTString_CharT> *e) {
541 return 0;
543 template<class A>
544 static Dont_Instantiate_nsTArray_of<nsTAutoString_CharT> *
545 Construct(Instead_Use_nsTArray_of<nsTString_CharT> *e,
546 const A &arg) {
547 return 0;
549 static Dont_Instantiate_nsTArray_of<nsTAutoString_CharT> *
550 Destruct(Instead_Use_nsTArray_of<nsTString_CharT> *e) {
551 return 0;
556 * nsTXPIDLString extends nsTString such that:
558 * (1) mData can be null
559 * (2) objects of this type can be automatically cast to |const CharT*|
560 * (3) getter_Copies method is supported to adopt data allocated with
561 * NS_Alloc, such as "out string" parameters in XPIDL.
563 * NAMES:
564 * nsXPIDLString for wide characters
565 * nsXPIDLCString for narrow characters
567 class nsTXPIDLString_CharT : public nsTString_CharT
569 public:
571 typedef nsTXPIDLString_CharT self_type;
573 public:
575 nsTXPIDLString_CharT()
576 : string_type(char_traits::sEmptyBuffer, 0, F_TERMINATED | F_VOIDED) {}
578 // copy-constructor required to avoid default
579 nsTXPIDLString_CharT( const self_type& str )
580 : string_type(char_traits::sEmptyBuffer, 0, F_TERMINATED | F_VOIDED)
582 Assign(str);
585 // return nsnull if we are voided
586 const char_type* get() const
588 return (mFlags & F_VOIDED) ? nsnull : mData;
591 // this case operator is the reason why this class cannot just be a
592 // typedef for nsTString
593 operator const char_type*() const
595 return get();
598 // need this to diambiguous operator[int]
599 char_type operator[]( PRInt32 i ) const
601 return CharAt(index_type(i));
604 // |operator=| does not inherit, so we must define our own
605 self_type& operator=( char_type c ) { Assign(c); return *this; }
606 self_type& operator=( const char_type* data ) { Assign(data); return *this; }
607 self_type& operator=( const self_type& str ) { Assign(str); return *this; }
608 self_type& operator=( const substring_type& str ) { Assign(str); return *this; }
609 self_type& operator=( const substring_tuple_type& tuple ) { Assign(tuple); return *this; }
614 * getter_Copies support for use with raw string out params:
616 * NS_IMETHOD GetBlah(char**);
618 * void some_function()
620 * nsXPIDLCString blah;
621 * GetBlah(getter_Copies(blah));
622 * // ...
625 class NS_STACK_CLASS nsTGetterCopies_CharT
627 public:
628 typedef CharT char_type;
630 nsTGetterCopies_CharT(nsTSubstring_CharT& str)
631 : mString(str), mData(nsnull) {}
633 ~nsTGetterCopies_CharT()
635 mString.Adopt(mData); // OK if mData is null
638 operator char_type**()
640 return &mData;
643 private:
644 nsTSubstring_CharT& mString;
645 char_type* mData;
648 inline
649 nsTGetterCopies_CharT
650 getter_Copies( nsTSubstring_CharT& aString )
652 return nsTGetterCopies_CharT(aString);
657 * nsTAdoptingString extends nsTXPIDLString such that:
659 * (1) Adopt given string on construction or assignment, i.e. take
660 * the value of what's given, and make what's given forget its
661 * value. Note that this class violates constness in a few
662 * places. Be careful!
664 class nsTAdoptingString_CharT : public nsTXPIDLString_CharT
666 public:
668 typedef nsTAdoptingString_CharT self_type;
670 public:
672 explicit nsTAdoptingString_CharT() {}
673 explicit nsTAdoptingString_CharT(char_type* str, size_type length = size_type(-1))
675 Adopt(str, length);
678 // copy-constructor required to adopt on copy. Note that this
679 // will violate the constness of |str| in the operator=()
680 // call. |str| will be truncated as a side-effect of this
681 // constructor.
682 nsTAdoptingString_CharT( const self_type& str )
684 *this = str;
687 // |operator=| does not inherit, so we must define our own
688 self_type& operator=( const substring_type& str ) { Assign(str); return *this; }
689 self_type& operator=( const substring_tuple_type& tuple ) { Assign(tuple); return *this; }
691 // Adopt(), if possible, when assigning to a self_type&. Note
692 // that this violates the constness of str, str is always
693 // truncated when this operator is called.
694 NS_COM self_type& operator=( const self_type& str );
696 private:
697 // NOT TO BE IMPLEMENTED.
698 self_type& operator=( const char_type* data );
699 self_type& operator=( char_type* data );