xpcom/glue/nsStringAPI.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* vim:set ts=2 sw=2 et cindent: */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 /**
     7  * This header provides wrapper classes around the frozen string API
     8  * which are roughly equivalent to the internal string classes.
     9  */
    11 #ifdef MOZILLA_INTERNAL_API
    12 #error nsStringAPI.h is only usable from non-MOZILLA_INTERNAL_API code!
    13 #endif
    15 #ifndef nsStringAPI_h__
    16 #define nsStringAPI_h__
    18 #include "mozilla/Attributes.h"
    19 #include "mozilla/Char16.h"
    21 #include "nsXPCOMStrings.h"
    22 #include "nsISupportsImpl.h"
    23 #include "prlog.h"
    24 #include "nsTArray.h"
    26 /**
    27  * Comparison function for use with nsACString::Equals
    28  */
    29 NS_HIDDEN_(int32_t)
    30 CaseInsensitiveCompare(const char *a, const char *b,
    31                        uint32_t length);
    33 class nsAString
    34 {
    35 public:
    36   typedef char16_t  char_type;
    37   typedef nsAString  self_type;
    38   typedef uint32_t   size_type;
    39   typedef uint32_t   index_type;
    41   /**
    42    * Returns the length, beginning, and end of a string in one operation.
    43    */
    44   NS_HIDDEN_(uint32_t) BeginReading(const char_type **begin,
    45                                     const char_type **end = nullptr) const;
    47   NS_HIDDEN_(const char_type*) BeginReading() const;
    48   NS_HIDDEN_(const char_type*) EndReading() const;
    50   NS_HIDDEN_(char_type) CharAt(uint32_t aPos) const
    51   {
    52     NS_ASSERTION(aPos < Length(), "Out of bounds");
    53     return BeginReading()[aPos];
    54   }
    55   NS_HIDDEN_(char_type) operator [](uint32_t aPos) const
    56   {
    57     return CharAt(aPos);
    58   }
    59   NS_HIDDEN_(char_type) First() const
    60   {
    61     return CharAt(0);
    62   }
    63   NS_HIDDEN_(char_type) Last() const
    64   {
    65     const char_type* data;
    66     uint32_t dataLen = NS_StringGetData(*this, &data);
    67     return data[dataLen - 1];
    68   }
    70   /**
    71    * Get the length, begin writing, and optionally set the length of a
    72    * string all in one operation.
    73    *
    74    * @param   newSize Size the string to this length. Pass UINT32_MAX
    75    *                  to leave the length unchanged.
    76    * @return  The new length of the string, or 0 if resizing failed.
    77    */
    78   NS_HIDDEN_(uint32_t) BeginWriting(char_type **begin,
    79                                     char_type **end = nullptr,
    80                                     uint32_t newSize = UINT32_MAX);
    82   NS_HIDDEN_(char_type*) BeginWriting(uint32_t = UINT32_MAX);
    83   NS_HIDDEN_(char_type*) EndWriting();
    85   NS_HIDDEN_(bool) SetLength(uint32_t aLen);
    87   NS_HIDDEN_(size_type) Length() const
    88   {
    89     const char_type* data;
    90     return NS_StringGetData(*this, &data);
    91   }
    93   NS_HIDDEN_(bool) IsEmpty() const
    94   {
    95     return Length() == 0;
    96   }
    98   NS_HIDDEN_(void) SetIsVoid(bool val)
    99   {
   100     NS_StringSetIsVoid(*this, val);
   101   }
   102   NS_HIDDEN_(bool) IsVoid() const
   103   {
   104     return NS_StringGetIsVoid(*this);
   105   }
   107   NS_HIDDEN_(void) Assign(const self_type& aString)
   108   {
   109     NS_StringCopy(*this, aString);
   110   }
   111   NS_HIDDEN_(void) Assign(const char_type* aData, size_type aLength = UINT32_MAX)
   112   {
   113     NS_StringSetData(*this, aData, aLength);
   114   }
   115   NS_HIDDEN_(void) Assign(char_type aChar)
   116   {
   117     NS_StringSetData(*this, &aChar, 1);
   118   }
   119 #ifdef MOZ_USE_CHAR16_WRAPPER
   120   NS_HIDDEN_(void) Assign(char16ptr_t aData, size_type aLength = UINT32_MAX)
   121   {
   122     NS_StringSetData(*this, aData, aLength);
   123   }
   124 #endif
   126   NS_HIDDEN_(void) AssignLiteral(const char *aStr);
   127   NS_HIDDEN_(void) AssignASCII(const char *aStr) { AssignLiteral(aStr); }
   129   NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString);   return *this; }
   130   NS_HIDDEN_(self_type&) operator=(const char_type* aPtr)    { Assign(aPtr);      return *this; }
   131   NS_HIDDEN_(self_type&) operator=(char_type aChar)          { Assign(aChar);     return *this; }
   132 #ifdef MOZ_USE_CHAR16_WRAPPER
   133   NS_HIDDEN_(self_type&) operator=(char16ptr_t aPtr)         { Assign(aPtr);      return *this; }
   134 #endif
   136   NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) )
   137   {
   138     NS_StringSetDataRange(*this, cutStart, cutLength, data, length);
   139   }
   140   NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, char_type c )
   141   {
   142     Replace(cutStart, cutLength, &c, 1);
   143   }
   144   NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const self_type& readable )
   145   {
   146     const char_type* data;
   147     uint32_t dataLen = NS_StringGetData(readable, &data);
   148     NS_StringSetDataRange(*this, cutStart, cutLength, data, dataLen);
   149   }
   150   NS_HIDDEN_(void) SetCharAt( char_type c, index_type pos )
   151                       { Replace(pos, 1, &c, 1); }
   153   NS_HIDDEN_(void) Append( char_type c )                                                              { Replace(size_type(-1), 0, c); }
   154   NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) )                  { Replace(size_type(-1), 0, data, length); }
   155 #ifdef MOZ_USE_CHAR16_WRAPPER
   156   NS_HIDDEN_(void) Append( char16ptr_t data, size_type length = size_type(-1) )                       { Append(static_cast<const char16_t*>(data), length); }
   157 #endif
   158   NS_HIDDEN_(void) Append( const self_type& readable )                                                { Replace(size_type(-1), 0, readable); }
   159   NS_HIDDEN_(void) AppendLiteral( const char *aASCIIStr );
   160   NS_HIDDEN_(void) AppendASCII( const char *aASCIIStr )                                               { AppendLiteral(aASCIIStr); }
   162   NS_HIDDEN_(self_type&) operator+=( char_type c )                                                    { Append(c);        return *this; }
   163   NS_HIDDEN_(self_type&) operator+=( const char_type* data )                                          { Append(data);     return *this; }
   164   NS_HIDDEN_(self_type&) operator+=( const self_type& readable )                                      { Append(readable); return *this; }
   166   NS_HIDDEN_(void) Insert( char_type c, index_type pos )                                              { Replace(pos, 0, c); }
   167   NS_HIDDEN_(void) Insert( const char_type* data, index_type pos, size_type length = size_type(-1) )  { Replace(pos, 0, data, length); }
   168   NS_HIDDEN_(void) Insert( const self_type& readable, index_type pos )                                { Replace(pos, 0, readable); }
   170   NS_HIDDEN_(void) Cut( index_type cutStart, size_type cutLength )                                    { Replace(cutStart, cutLength, nullptr, 0); }
   172   NS_HIDDEN_(void) Truncate() { SetLength(0); }
   174   /**
   175    * Remove all occurences of characters in aSet from the string.
   176    */
   177   NS_HIDDEN_(void) StripChars(const char *aSet);
   179   /**
   180    * Strip whitespace characters from the string.
   181    */
   182   NS_HIDDEN_(void) StripWhitespace() { StripChars("\b\t\r\n "); }
   184   NS_HIDDEN_(void) Trim(const char *aSet, bool aLeading = true,
   185                         bool aTrailing = true);
   187   /**
   188    * Compare strings of characters. Return 0 if the characters are equal,
   189    */
   190   typedef int32_t (*ComparatorFunc)(const char_type *a,
   191                                     const char_type *b,
   192                                     uint32_t length);
   194   static NS_HIDDEN_(int32_t) DefaultComparator(const char_type *a,
   195                                                const char_type *b,
   196                                                uint32_t length);
   198   NS_HIDDEN_(int32_t) Compare( const char_type *other,
   199                                ComparatorFunc c = DefaultComparator ) const;
   201   NS_HIDDEN_(int32_t) Compare( const self_type &other,
   202                                ComparatorFunc c = DefaultComparator ) const;
   204   NS_HIDDEN_(bool) Equals( const char_type *other,
   205                              ComparatorFunc c = DefaultComparator ) const;
   207   NS_HIDDEN_(bool) Equals( const self_type &other,
   208                              ComparatorFunc c = DefaultComparator ) const;
   210   NS_HIDDEN_(bool) operator < (const self_type &other) const
   211   {
   212     return Compare(other) < 0;
   213   }
   214   NS_HIDDEN_(bool) operator < (const char_type *other) const
   215   {
   216     return Compare(other) < 0;
   217   }
   219   NS_HIDDEN_(bool) operator <= (const self_type &other) const
   220   {
   221     return Compare(other) <= 0;
   222   }
   223   NS_HIDDEN_(bool) operator <= (const char_type *other) const
   224   {
   225     return Compare(other) <= 0;
   226   }
   228   NS_HIDDEN_(bool) operator == (const self_type &other) const
   229   {
   230     return Equals(other);
   231   }
   232   NS_HIDDEN_(bool) operator == (const char_type *other) const
   233   {
   234     return Equals(other);
   235   }
   236 #ifdef MOZ_USE_CHAR16_WRAPPER
   237   NS_HIDDEN_(bool) operator == (char16ptr_t other) const
   238   {
   239     return Equals(other);
   240   }
   241 #endif
   243   NS_HIDDEN_(bool) operator >= (const self_type &other) const
   244   {
   245     return Compare(other) >= 0;
   246   }
   247   NS_HIDDEN_(bool) operator >= (const char_type *other) const
   248   {
   249     return Compare(other) >= 0;
   250   }
   252   NS_HIDDEN_(bool) operator > (const self_type &other) const
   253   {
   254     return Compare(other) > 0;
   255   }
   256   NS_HIDDEN_(bool) operator > (const char_type *other) const
   257   {
   258     return Compare(other) > 0;
   259   }
   261   NS_HIDDEN_(bool) operator != (const self_type &other) const
   262   {
   263     return !Equals(other);
   264   }
   265   NS_HIDDEN_(bool) operator != (const char_type *other) const
   266   {
   267     return !Equals(other);
   268   }
   270   NS_HIDDEN_(bool) EqualsLiteral(const char *aASCIIString) const;
   271   NS_HIDDEN_(bool) EqualsASCII(const char *aASCIIString) const
   272   {
   273     return EqualsLiteral(aASCIIString);
   274   }
   276   /**
   277    * Case-insensitive match this string to a lowercase ASCII string.
   278    */
   279   NS_HIDDEN_(bool) LowerCaseEqualsLiteral(const char *aASCIIString) const;
   281   /**
   282    * Find the first occurrence of aStr in this string.
   283    *
   284    * @return the offset of aStr, or -1 if not found
   285    */
   286   NS_HIDDEN_(int32_t) Find(const self_type& aStr,
   287                            ComparatorFunc c = DefaultComparator) const
   288   { return Find(aStr, 0, c); }
   290   /**
   291    * Find the first occurrence of aStr in this string, beginning at aOffset.
   292    *
   293    * @return the offset of aStr, or -1 if not found
   294    */
   295   NS_HIDDEN_(int32_t) Find(const self_type& aStr, uint32_t aOffset,
   296                            ComparatorFunc c = DefaultComparator) const;
   298   /**
   299    * Find an ASCII string within this string.
   300    *
   301    * @return the offset of aStr, or -1 if not found.
   302    */
   303   NS_HIDDEN_(int32_t) Find(const char *aStr, bool aIgnoreCase = false) const
   304   { return Find(aStr, 0, aIgnoreCase); }
   306   NS_HIDDEN_(int32_t) Find(const char *aStr, uint32_t aOffset, bool aIgnoreCase = false) const;
   308   /**
   309    * Find the last occurrence of aStr in this string.
   310    *
   311    * @return The offset of aStr from the beginning of the string,
   312    *         or -1 if not found.
   313    */
   314   NS_HIDDEN_(int32_t) RFind(const self_type& aStr,
   315                             ComparatorFunc c = DefaultComparator) const
   316   { return RFind(aStr, -1, c); }
   318   /**
   319    * Find the last occurrence of aStr in this string, beginning at aOffset.
   320    *
   321    * @param aOffset the offset from the beginning of the string to begin
   322    *        searching. If aOffset < 0, search from end of this string.
   323    * @return The offset of aStr from the beginning of the string,
   324    *         or -1 if not found.
   325    */
   326   NS_HIDDEN_(int32_t) RFind(const self_type& aStr, int32_t aOffset,
   327                             ComparatorFunc c = DefaultComparator) const;
   329   /**
   330    * Find the last occurrence of an ASCII string within this string.
   331    *
   332    * @return The offset of aStr from the beginning of the string,
   333    *         or -1 if not found.
   334    */
   335   NS_HIDDEN_(int32_t) RFind(const char *aStr, bool aIgnoreCase = false) const
   336   { return RFind(aStr, -1, aIgnoreCase); }
   338   /**
   339    * Find the last occurrence of an ASCII string beginning at aOffset.
   340    *
   341    * @param aOffset the offset from the beginning of the string to begin
   342    *        searching. If aOffset < 0, search from end of this string.
   343    * @return The offset of aStr from the beginning of the string,
   344    *         or -1 if not found.
   345    */
   346   NS_HIDDEN_(int32_t) RFind(const char *aStr, int32_t aOffset, bool aIgnoreCase) const;
   348   /**
   349    * Search for the offset of the first occurrence of a character in a
   350    * string.
   351    *
   352    * @param aOffset the offset from the beginning of the string to begin
   353    *        searching
   354    * @return The offset of the character from the beginning of the string,
   355    *         or -1 if not found.
   356    */
   357   NS_HIDDEN_(int32_t) FindChar(char_type aChar, uint32_t aOffset = 0) const;
   359   /**
   360    * Search for the offset of the last occurrence of a character in a
   361    * string.
   362    *
   363    * @return The offset of the character from the beginning of the string,
   364    *         or -1 if not found.
   365    */
   366   NS_HIDDEN_(int32_t) RFindChar(char_type aChar) const;
   368   /**
   369    * Append a string representation of a number.
   370    */
   371   NS_HIDDEN_(void) AppendInt(int aInt, int32_t aRadix = 10);
   373 #ifndef XPCOM_GLUE_AVOID_NSPR
   374   /**
   375    * Convert this string to an integer.
   376    *
   377    * @param aErrorCode pointer to contain result code.
   378    * @param aRadix must be 10 or 16
   379    */
   380   NS_HIDDEN_(int32_t) ToInteger(nsresult* aErrorCode,
   381                                 uint32_t aRadix = 10) const;
   382   /**
   383    * Convert this string to a 64-bit integer.
   384    *
   385    * @param aErrorCode pointer to contain result code.
   386    * @param aRadix must be 10 or 16
   387    */
   388   NS_HIDDEN_(int64_t) ToInteger64(nsresult* aErrorCode,
   389                                   uint32_t aRadix = 10) const;
   390 #endif // XPCOM_GLUE_AVOID_NSPR
   392 protected:
   393   // Prevent people from allocating a nsAString directly.
   394   ~nsAString() {}
   395 };
   397 class nsACString
   398 {
   399 public:
   400   typedef char       char_type;
   401   typedef nsACString self_type;
   402   typedef uint32_t   size_type;
   403   typedef uint32_t   index_type;
   405   /**
   406    * Returns the length, beginning, and end of a string in one operation.
   407    */
   408   NS_HIDDEN_(uint32_t) BeginReading(const char_type **begin,
   409                                     const char_type **end = nullptr) const;
   411   NS_HIDDEN_(const char_type*) BeginReading() const;
   412   NS_HIDDEN_(const char_type*) EndReading() const;
   414   NS_HIDDEN_(char_type) CharAt(uint32_t aPos) const
   415   {
   416     NS_ASSERTION(aPos < Length(), "Out of bounds");
   417     return BeginReading()[aPos];
   418   }
   419   NS_HIDDEN_(char_type) operator [](uint32_t aPos) const
   420   {
   421     return CharAt(aPos);
   422   }
   423   NS_HIDDEN_(char_type) First() const
   424   {
   425     return CharAt(0);
   426   }
   427   NS_HIDDEN_(char_type) Last() const
   428   {
   429     const char_type* data;
   430     uint32_t dataLen = NS_CStringGetData(*this, &data);
   431     return data[dataLen - 1];
   432   }
   434   /**
   435    * Get the length, begin writing, and optionally set the length of a
   436    * string all in one operation.
   437    *
   438    * @param   newSize Size the string to this length. Pass UINT32_MAX
   439    *                  to leave the length unchanged.
   440    * @return  The new length of the string, or 0 if resizing failed.
   441    */
   442   NS_HIDDEN_(uint32_t) BeginWriting(char_type **begin,
   443                                     char_type **end = nullptr,
   444                                     uint32_t newSize = UINT32_MAX);
   446   NS_HIDDEN_(char_type*) BeginWriting(uint32_t aLen = UINT32_MAX);
   447   NS_HIDDEN_(char_type*) EndWriting();
   449   NS_HIDDEN_(bool) SetLength(uint32_t aLen);
   451   NS_HIDDEN_(size_type) Length() const
   452   {
   453     const char_type* data;
   454     return NS_CStringGetData(*this, &data);
   455   }
   457   NS_HIDDEN_(bool) IsEmpty() const
   458   {
   459     return Length() == 0;
   460   }
   462   NS_HIDDEN_(void) SetIsVoid(bool val)
   463   {
   464     NS_CStringSetIsVoid(*this, val);
   465   }
   466   NS_HIDDEN_(bool) IsVoid() const
   467   {
   468     return NS_CStringGetIsVoid(*this);
   469   }
   471   NS_HIDDEN_(void) Assign(const self_type& aString)
   472   {
   473     NS_CStringCopy(*this, aString);
   474   }
   475   NS_HIDDEN_(void) Assign(const char_type* aData, size_type aLength = UINT32_MAX)
   476   {
   477     NS_CStringSetData(*this, aData, aLength);
   478   }
   479   NS_HIDDEN_(void) Assign(char_type aChar)
   480   {
   481     NS_CStringSetData(*this, &aChar, 1);
   482   }
   483   NS_HIDDEN_(void) AssignLiteral(const char_type *aData)
   484   {
   485     Assign(aData);
   486   }
   487   NS_HIDDEN_(void) AssignASCII(const char_type *aData)
   488   {
   489     Assign(aData);
   490   }
   492   NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString);   return *this; }
   493   NS_HIDDEN_(self_type&) operator=(const char_type* aPtr)    { Assign(aPtr);      return *this; }
   494   NS_HIDDEN_(self_type&) operator=(char_type aChar)          { Assign(aChar);     return *this; }
   496   NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) )
   497   {
   498     NS_CStringSetDataRange(*this, cutStart, cutLength, data, length);
   499   }
   500   NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, char_type c )
   501   {
   502     Replace(cutStart, cutLength, &c, 1);
   503   }
   504   NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const self_type& readable )
   505   {
   506     const char_type* data;
   507     uint32_t dataLen = NS_CStringGetData(readable, &data);
   508     NS_CStringSetDataRange(*this, cutStart, cutLength, data, dataLen);
   509   }
   510   NS_HIDDEN_(void) SetCharAt( char_type c, index_type pos )
   511                       { Replace(pos, 1, &c, 1); }
   513   NS_HIDDEN_(void) Append( char_type c )                                                              { Replace(size_type(-1), 0, c); }
   514   NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) )                  { Replace(size_type(-1), 0, data, length); }
   515   NS_HIDDEN_(void) Append( const self_type& readable )                                                { Replace(size_type(-1), 0, readable); }
   516   NS_HIDDEN_(void) AppendLiteral( const char *aASCIIStr )                                             { Append(aASCIIStr); }
   517   NS_HIDDEN_(void) AppendASCII( const char *aASCIIStr )                                               { Append(aASCIIStr); }
   519   NS_HIDDEN_(self_type&) operator+=( char_type c )                                                    { Append(c);        return *this; }
   520   NS_HIDDEN_(self_type&) operator+=( const char_type* data )                                          { Append(data);     return *this; }
   521   NS_HIDDEN_(self_type&) operator+=( const self_type& readable )                                      { Append(readable); return *this; }
   523   NS_HIDDEN_(void) Insert( char_type c, index_type pos )                                              { Replace(pos, 0, c); }
   524   NS_HIDDEN_(void) Insert( const char_type* data, index_type pos, size_type length = size_type(-1) )  { Replace(pos, 0, data, length); }
   525   NS_HIDDEN_(void) Insert( const self_type& readable, index_type pos )                                { Replace(pos, 0, readable); }
   527   NS_HIDDEN_(void) Cut( index_type cutStart, size_type cutLength )                                    { Replace(cutStart, cutLength, nullptr, 0); }
   529   NS_HIDDEN_(void) Truncate() { SetLength(0); }
   531   /**
   532    * Remove all occurences of characters in aSet from the string.
   533    */
   534   NS_HIDDEN_(void) StripChars(const char *aSet);
   536   /**
   537    * Strip whitespace characters from the string.
   538    */
   539   NS_HIDDEN_(void) StripWhitespace() { StripChars("\b\t\r\n "); }
   541   NS_HIDDEN_(void) Trim(const char *aSet, bool aLeading = true,
   542                         bool aTrailing = true);
   544   /**
   545    * Compare strings of characters. Return 0 if the characters are equal,
   546    */
   547   typedef int32_t (*ComparatorFunc)(const char_type *a,
   548                                     const char_type *b,
   549                                     uint32_t length);
   551   static NS_HIDDEN_(int32_t) DefaultComparator(const char_type *a,
   552                                                const char_type *b,
   553                                                uint32_t length);
   555   NS_HIDDEN_(int32_t) Compare( const char_type *other,
   556                                ComparatorFunc c = DefaultComparator ) const;
   558   NS_HIDDEN_(int32_t) Compare( const self_type &other,
   559                                ComparatorFunc c = DefaultComparator ) const;
   561   NS_HIDDEN_(bool) Equals( const char_type *other,
   562                              ComparatorFunc c = DefaultComparator ) const;
   564   NS_HIDDEN_(bool) Equals( const self_type &other,
   565                              ComparatorFunc c = DefaultComparator ) const;
   567   NS_HIDDEN_(bool) operator < (const self_type &other) const
   568   {
   569     return Compare(other) < 0;
   570   }
   571   NS_HIDDEN_(bool) operator < (const char_type *other) const
   572   {
   573     return Compare(other) < 0;
   574   }
   576   NS_HIDDEN_(bool) operator <= (const self_type &other) const
   577   {
   578     return Compare(other) <= 0;
   579   }
   580   NS_HIDDEN_(bool) operator <= (const char_type *other) const
   581   {
   582     return Compare(other) <= 0;
   583   }
   585   NS_HIDDEN_(bool) operator == (const self_type &other) const
   586   {
   587     return Equals(other);
   588   }
   589   NS_HIDDEN_(bool) operator == (const char_type *other) const
   590   {
   591     return Equals(other);
   592   }
   594   NS_HIDDEN_(bool) operator >= (const self_type &other) const
   595   {
   596     return Compare(other) >= 0;
   597   }
   598   NS_HIDDEN_(bool) operator >= (const char_type *other) const
   599   {
   600     return Compare(other) >= 0;
   601   }
   603   NS_HIDDEN_(bool) operator > (const self_type &other) const
   604   {
   605     return Compare(other) > 0;
   606   }
   607   NS_HIDDEN_(bool) operator > (const char_type *other) const
   608   {
   609     return Compare(other) > 0;
   610   }
   612   NS_HIDDEN_(bool) operator != (const self_type &other) const
   613   {
   614     return !Equals(other);
   615   }
   616   NS_HIDDEN_(bool) operator != (const char_type *other) const
   617   {
   618     return !Equals(other);
   619   }
   621   NS_HIDDEN_(bool) EqualsLiteral( const char_type *other ) const
   622   {
   623     return Equals(other);
   624   }
   625   NS_HIDDEN_(bool) EqualsASCII( const char_type *other ) const
   626   {
   627     return Equals(other);
   628   }
   630   /**
   631    * Case-insensitive match this string to a lowercase ASCII string.
   632    */
   633   NS_HIDDEN_(bool) LowerCaseEqualsLiteral(const char *aASCIIString) const
   634   {
   635     return Equals(aASCIIString, CaseInsensitiveCompare);
   636   }
   638   /**
   639    * Find the first occurrence of aStr in this string.
   640    *
   641    * @return the offset of aStr, or -1 if not found
   642    */
   643   NS_HIDDEN_(int32_t) Find(const self_type& aStr,
   644                            ComparatorFunc c = DefaultComparator) const
   645   { return Find(aStr, 0, c); }
   647   /**
   648    * Find the first occurrence of aStr in this string, beginning at aOffset.
   649    *
   650    * @return the offset of aStr, or -1 if not found
   651    */
   652   NS_HIDDEN_(int32_t) Find(const self_type& aStr, uint32_t aOffset,
   653                            ComparatorFunc c = DefaultComparator) const;
   655   /**
   656    * Find the first occurrence of aStr in this string.
   657    *
   658    * @return the offset of aStr, or -1 if not found
   659    */
   660   NS_HIDDEN_(int32_t) Find(const char_type *aStr,
   661                            ComparatorFunc c = DefaultComparator) const;
   663   NS_HIDDEN_(int32_t) Find(const char_type *aStr, uint32_t aLen,
   664                            ComparatorFunc c = DefaultComparator) const;
   666   /**
   667    * Find the last occurrence of aStr in this string.
   668    *
   669    * @return The offset of the character from the beginning of the string,
   670    *         or -1 if not found.
   671    */
   672   NS_HIDDEN_(int32_t) RFind(const self_type& aStr,
   673                             ComparatorFunc c = DefaultComparator) const
   674   { return RFind(aStr, -1, c); }
   676   /**
   677    * Find the last occurrence of aStr in this string, beginning at aOffset.
   678    *
   679    * @param aOffset the offset from the beginning of the string to begin
   680    *        searching. If aOffset < 0, search from end of this string.
   681    * @return The offset of aStr from the beginning of the string,
   682    *         or -1 if not found.
   683    */
   684   NS_HIDDEN_(int32_t) RFind(const self_type& aStr, int32_t aOffset,
   685                             ComparatorFunc c = DefaultComparator) const;
   687   /**
   688    * Find the last occurrence of aStr in this string.
   689    *
   690    * @return The offset of aStr from the beginning of the string,
   691    *         or -1 if not found.
   692    */
   693   NS_HIDDEN_(int32_t) RFind(const char_type *aStr,
   694                             ComparatorFunc c = DefaultComparator) const;
   696   /**
   697    * Find the last occurrence of an ASCII string in this string, 
   698    * beginning at aOffset.
   699    *
   700    * @param aLen is the length of aStr
   701    * @return The offset of aStr from the beginning of the string,
   702    *         or -1 if not found.
   703    */
   704   NS_HIDDEN_(int32_t) RFind(const char_type *aStr, int32_t aLen,
   705                             ComparatorFunc c = DefaultComparator) const;
   707   /**
   708    * Search for the offset of the first occurrence of a character in a
   709    * string.
   710    *
   711    * @param aOffset the offset from the beginning of the string to begin
   712    *        searching
   713    * @return The offset of the character from the beginning of the string,
   714    *         or -1 if not found.
   715    */
   716   NS_HIDDEN_(int32_t) FindChar(char_type aChar, uint32_t aOffset = 0) const;
   718   /**
   719    * Search for the offset of the last occurrence of a character in a
   720    * string.
   721    *
   722    * @return The offset of the character from the beginning of the string,
   723    *         or -1 if not found.
   724    */
   725   NS_HIDDEN_(int32_t) RFindChar(char_type aChar) const;
   727   /**
   728    * Append a string representation of a number.
   729    */
   730   NS_HIDDEN_(void) AppendInt(int aInt, int32_t aRadix = 10);
   732 #ifndef XPCOM_GLUE_AVOID_NSPR
   733   /**
   734    * Convert this string to an integer.
   735    *
   736    * @param aErrorCode pointer to contain result code.
   737    * @param aRadix must be 10 or 16
   738    */
   739   NS_HIDDEN_(int32_t) ToInteger(nsresult* aErrorCode,
   740                                 uint32_t aRadix = 10) const;
   741   /**
   742    * Convert this string to a 64-bit integer.
   743    *
   744    * @param aErrorCode pointer to contain result code.
   745    * @param aRadix must be 10 or 16
   746    */
   747   NS_HIDDEN_(int64_t) ToInteger64(nsresult* aErrorCode,
   748                                   uint32_t aRadix = 10) const;
   749 #endif // XPCOM_GLUE_AVOID_NSPR
   751 protected:
   752   // Prevent people from allocating a nsAString directly.
   753   ~nsACString() {}
   754 };
   756 /* ------------------------------------------------------------------------- */
   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  */
   765 class nsStringContainer : public nsAString,
   766                           private nsStringContainer_base
   767 {
   768 };
   770 class nsCStringContainer : public nsACString,
   771                            private nsStringContainer_base
   772 {
   773 };
   775 /**
   776  * The following classes are C++ helper classes that make the frozen string
   777  * API easier to use.
   778  */
   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
   796 /**
   797  * basic strings
   798  */
   800 class nsString : public nsStringContainer
   801 {
   802 public:
   803   typedef nsString         self_type;
   804   typedef nsAString        abstract_string_type;
   806   nsString()
   807   {
   808     NS_StringContainerInit(*this);
   809   }
   811   nsString(const self_type& aString)
   812   {
   813     NS_StringContainerInit(*this);
   814     NS_StringCopy(*this, aString);
   815   }
   817   explicit
   818   nsString(const abstract_string_type& aReadable)
   819   {
   820     NS_StringContainerInit(*this);
   821     NS_StringCopy(*this, aReadable);
   822   }
   824   explicit
   825   nsString(const char_type* aData, size_type aLength = UINT32_MAX)
   826   {
   827     NS_StringContainerInit2(*this, aData, aLength, 0);
   828   }
   830 #ifdef MOZ_USE_CHAR16_WRAPPER
   831   explicit
   832   nsString(char16ptr_t aData, size_type aLength = UINT32_MAX)
   833     : nsString(static_cast<const char16_t*>(aData), aLength) {}
   834 #endif
   836   ~nsString()
   837   {
   838     NS_StringContainerFinish(*this);
   839   }
   841   char16ptr_t get() const
   842   {
   843     return char16ptr_t(BeginReading());
   844   }
   846   self_type& operator=(const self_type& aString)              { Assign(aString);   return *this; }
   847   self_type& operator=(const abstract_string_type& aReadable) { Assign(aReadable); return *this; }
   848   self_type& operator=(const char_type* aPtr)                 { Assign(aPtr);      return *this; }
   849   self_type& operator=(char_type aChar)                       { Assign(aChar);     return *this; }
   851   void Adopt(const char_type *aData, size_type aLength = UINT32_MAX)
   852   {
   853     NS_StringContainerFinish(*this);
   854     NS_StringContainerInit2(*this, aData, aLength,
   855                             NS_STRING_CONTAINER_INIT_ADOPT);
   856   }
   858 protected:
   860   nsString(const char_type* aData, size_type aLength, uint32_t aFlags)
   861   {
   862     NS_StringContainerInit2(*this, aData, aLength, aFlags);
   863   }
   864 };
   866 class nsCString : public nsCStringContainer
   867 {
   868 public:
   869   typedef nsCString        self_type;
   870   typedef nsACString       abstract_string_type;
   872   nsCString()
   873   {
   874     NS_CStringContainerInit(*this);
   875   }
   877   nsCString(const self_type& aString)
   878   {
   879     NS_CStringContainerInit(*this);
   880     NS_CStringCopy(*this, aString);
   881   }
   883   explicit
   884   nsCString(const abstract_string_type& aReadable)
   885   {
   886     NS_CStringContainerInit(*this);
   887     NS_CStringCopy(*this, aReadable);
   888   }
   890   explicit
   891   nsCString(const char_type* aData, size_type aLength = UINT32_MAX)
   892   {
   893     NS_CStringContainerInit(*this);
   894     NS_CStringSetData(*this, aData, aLength);
   895   }
   897   ~nsCString()
   898   {
   899     NS_CStringContainerFinish(*this);
   900   }
   902   const char_type* get() const
   903   {
   904     return BeginReading();
   905   }
   907   self_type& operator=(const self_type& aString)              { Assign(aString);   return *this; }
   908   self_type& operator=(const abstract_string_type& aReadable) { Assign(aReadable); return *this; }
   909   self_type& operator=(const char_type* aPtr)                 { Assign(aPtr);      return *this; }
   910   self_type& operator=(char_type aChar)                       { Assign(aChar);     return *this; }
   912   void Adopt(const char_type *aData, size_type aLength = UINT32_MAX)
   913   {
   914     NS_CStringContainerFinish(*this);
   915     NS_CStringContainerInit2(*this, aData, aLength,
   916                              NS_CSTRING_CONTAINER_INIT_ADOPT);
   917   }
   919 protected:
   921   nsCString(const char_type* aData, size_type aLength, uint32_t aFlags)
   922   {
   923     NS_CStringContainerInit2(*this, aData, aLength, aFlags);
   924   }
   925 };
   928 /**
   929  * dependent strings
   930  */
   932 class nsDependentString : public nsString
   933 {
   934 public:
   935   typedef nsDependentString         self_type;
   937   nsDependentString() {}
   939   explicit
   940   nsDependentString(const char_type* aData, size_type aLength = UINT32_MAX)
   941     : nsString(aData, aLength, NS_CSTRING_CONTAINER_INIT_DEPEND)
   942   {}
   944 #ifdef MOZ_USE_CHAR16_WRAPPER
   945   explicit
   946   nsDependentString(char16ptr_t aData, size_type aLength = UINT32_MAX)
   947     : nsDependentString(static_cast<const char16_t*>(aData), aLength)
   948   {}
   949 #endif
   951   void Rebind(const char_type* aData, size_type aLength = UINT32_MAX)
   952   {
   953     NS_StringContainerFinish(*this);
   954     NS_StringContainerInit2(*this, aData, aLength,
   955                             NS_STRING_CONTAINER_INIT_DEPEND);
   956   }
   958 private:
   959   self_type& operator=(const self_type& aString) MOZ_DELETE;
   960 };
   962 class nsDependentCString : public nsCString
   963 {
   964 public:
   965   typedef nsDependentCString        self_type;
   967   nsDependentCString() {}
   969   explicit
   970   nsDependentCString(const char_type* aData, size_type aLength = UINT32_MAX)
   971     : nsCString(aData, aLength, NS_CSTRING_CONTAINER_INIT_DEPEND)
   972   {}
   974   void Rebind(const char_type* aData, size_type aLength = UINT32_MAX)
   975   {
   976     NS_CStringContainerFinish(*this);
   977     NS_CStringContainerInit2(*this, aData, aLength,
   978                              NS_CSTRING_CONTAINER_INIT_DEPEND);
   979   }
   981 private:
   982   self_type& operator=(const self_type& aString) MOZ_DELETE;
   983 };
   986 /**
   987  * conversion classes
   988  */
   990 inline void
   991 CopyUTF16toUTF8(const nsAString& aSource, nsACString& aDest)
   992 {
   993   NS_UTF16ToCString(aSource, NS_CSTRING_ENCODING_UTF8, aDest);
   994 }
   996 inline void
   997 CopyUTF8toUTF16(const nsACString& aSource, nsAString& aDest)
   998 {
   999   NS_CStringToUTF16(aSource, NS_CSTRING_ENCODING_UTF8, aDest);
  1002 inline void
  1003 LossyCopyUTF16toASCII(const nsAString& aSource, nsACString& aDest)
  1005   NS_UTF16ToCString(aSource, NS_CSTRING_ENCODING_ASCII, aDest);
  1008 inline void
  1009 CopyASCIItoUTF16(const nsACString& aSource, nsAString& aDest)
  1011   NS_CStringToUTF16(aSource, NS_CSTRING_ENCODING_ASCII, aDest);
  1014 NS_COM_GLUE char*
  1015 ToNewUTF8String(const nsAString& aSource);
  1017 class NS_ConvertASCIItoUTF16 : public nsString
  1019 public:
  1020   typedef NS_ConvertASCIItoUTF16    self_type;
  1022   explicit
  1023   NS_ConvertASCIItoUTF16(const nsACString& aStr)
  1025     NS_CStringToUTF16(aStr, NS_CSTRING_ENCODING_ASCII, *this);
  1028   explicit
  1029   NS_ConvertASCIItoUTF16(const char* aData, uint32_t aLength = UINT32_MAX)
  1031     NS_CStringToUTF16(nsDependentCString(aData, aLength),
  1032                       NS_CSTRING_ENCODING_ASCII, *this);
  1035 private:
  1036   self_type& operator=(const self_type& aString) MOZ_DELETE;
  1037 };
  1039 class NS_ConvertUTF8toUTF16 : public nsString
  1041 public:
  1042   typedef NS_ConvertUTF8toUTF16    self_type;
  1044   explicit
  1045   NS_ConvertUTF8toUTF16(const nsACString& aStr)
  1047     NS_CStringToUTF16(aStr, NS_CSTRING_ENCODING_UTF8, *this);
  1050   explicit
  1051   NS_ConvertUTF8toUTF16(const char* aData, uint32_t aLength = UINT32_MAX)
  1053     NS_CStringToUTF16(nsDependentCString(aData, aLength),
  1054                       NS_CSTRING_ENCODING_UTF8, *this);
  1057 private:
  1058   self_type& operator=(const self_type& aString) MOZ_DELETE;
  1059 };
  1061 class NS_ConvertUTF16toUTF8 : public nsCString
  1063 public:
  1064   typedef NS_ConvertUTF16toUTF8    self_type;
  1066   explicit
  1067   NS_ConvertUTF16toUTF8(const nsAString& aStr)
  1069     NS_UTF16ToCString(aStr, NS_CSTRING_ENCODING_UTF8, *this);
  1072   explicit
  1073   NS_ConvertUTF16toUTF8(const char16_t* aData, uint32_t aLength = UINT32_MAX)
  1075     NS_UTF16ToCString(nsDependentString(aData, aLength),
  1076                       NS_CSTRING_ENCODING_UTF8, *this);
  1079 private:
  1080   self_type& operator=(const self_type& aString) MOZ_DELETE;
  1081 };
  1083 class NS_LossyConvertUTF16toASCII : public nsCString
  1085 public:
  1086   typedef NS_LossyConvertUTF16toASCII    self_type;
  1088   explicit
  1089   NS_LossyConvertUTF16toASCII(const nsAString& aStr)
  1091     NS_UTF16ToCString(aStr, NS_CSTRING_ENCODING_ASCII, *this);
  1094   explicit
  1095   NS_LossyConvertUTF16toASCII(const char16_t* aData, uint32_t aLength = UINT32_MAX)
  1097     NS_UTF16ToCString(nsDependentString(aData, aLength),
  1098                       NS_CSTRING_ENCODING_ASCII, *this);
  1101 private:
  1102   self_type& operator=(const self_type& aString) MOZ_DELETE;
  1103 };
  1106 /**
  1107  * literal strings
  1108  */
  1109 static_assert(sizeof(char16_t) == 2, "size of char16_t must be 2");
  1110 static_assert(char16_t(-1) > char16_t(0), "char16_t must be unsigned");
  1112 #define NS_MULTILINE_LITERAL_STRING(s)          nsDependentString(reinterpret_cast<const nsAString::char_type*>(s), uint32_t((sizeof(s)/2)-1))
  1113 #define NS_MULTILINE_LITERAL_STRING_INIT(n,s)   n(reinterpret_cast<const nsAString::char_type*>(s), uint32_t((sizeof(s)/2)-1))
  1114 #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s)  const nsDependentString n(reinterpret_cast<const nsAString::char_type*>(s), uint32_t((sizeof(s)/2)-1))
  1115 typedef nsDependentString nsLiteralString;
  1117 /* Check that char16_t is unsigned */
  1118 static_assert(char16_t(-1) > char16_t(0), "char16_t is by definition an unsigned type");
  1120 #define NS_LITERAL_STRING(s)                      static_cast<const nsString&>(NS_MULTILINE_LITERAL_STRING(MOZ_UTF16(s)))
  1121 #define NS_LITERAL_STRING_INIT(n,s)               NS_MULTILINE_LITERAL_STRING_INIT(n, MOZ_UTF16(s))
  1122 #define NS_NAMED_LITERAL_STRING(n,s)              NS_NAMED_MULTILINE_LITERAL_STRING(n, MOZ_UTF16(s))
  1124 #define NS_LITERAL_CSTRING(s)                     static_cast<const nsDependentCString&>(nsDependentCString(s, uint32_t(sizeof(s)-1)))
  1125 #define NS_LITERAL_CSTRING_INIT(n,s)              n(s, uint32_t(sizeof(s)-1))
  1126 #define NS_NAMED_LITERAL_CSTRING(n,s)             const nsDependentCString n(s, uint32_t(sizeof(s)-1))
  1128 typedef nsDependentCString nsLiteralCString;
  1131 /**
  1132  * getter_Copies support
  1134  *    NS_IMETHOD GetBlah(char16_t**);
  1136  *    void some_function()
  1137  *    {
  1138  *      nsString blah;
  1139  *      GetBlah(getter_Copies(blah));
  1140  *      // ...
  1141  *    }
  1142  */
  1144 class nsGetterCopies
  1146 public:
  1147   typedef char16_t char_type;
  1149   nsGetterCopies(nsString& aStr)
  1150     : mString(aStr), mData(nullptr)
  1151   {}
  1153   ~nsGetterCopies()
  1155     mString.Adopt(mData);
  1158   operator char_type**()
  1160     return &mData;
  1163 private:
  1164   nsString&  mString;
  1165   char_type* mData;
  1166 };
  1168 inline nsGetterCopies
  1169 getter_Copies(nsString& aString)
  1171   return nsGetterCopies(aString);
  1174 class nsCGetterCopies
  1176 public:
  1177   typedef char char_type;
  1179   nsCGetterCopies(nsCString& aStr)
  1180     : mString(aStr), mData(nullptr)
  1181   {}
  1183   ~nsCGetterCopies()
  1185     mString.Adopt(mData);
  1188   operator char_type**()
  1190     return &mData;
  1193 private:
  1194   nsCString& mString;
  1195   char_type* mData;
  1196 };
  1198 inline nsCGetterCopies
  1199 getter_Copies(nsCString& aString)
  1201   return nsCGetterCopies(aString);
  1205 /**
  1206 * substrings
  1207 */
  1209 class NS_COM_GLUE nsDependentSubstring : public nsStringContainer
  1211 public:
  1212   typedef nsDependentSubstring self_type;
  1213   typedef nsAString            abstract_string_type;
  1215   ~nsDependentSubstring()
  1217     NS_StringContainerFinish(*this);
  1220   nsDependentSubstring()
  1222     NS_StringContainerInit(*this);
  1225   nsDependentSubstring(const char_type *aStart, uint32_t aLength)
  1227     NS_StringContainerInit2(*this, aStart, aLength,
  1228                             NS_STRING_CONTAINER_INIT_DEPEND |
  1229                             NS_STRING_CONTAINER_INIT_SUBSTRING);
  1232   nsDependentSubstring(const abstract_string_type& aStr,
  1233                        uint32_t aStartPos);
  1234   nsDependentSubstring(const abstract_string_type& aStr,
  1235                        uint32_t aStartPos, uint32_t aLength);
  1237   void Rebind(const char_type *aStart, uint32_t aLength)
  1239     NS_StringContainerFinish(*this);
  1240     NS_StringContainerInit2(*this, aStart, aLength,
  1241                             NS_STRING_CONTAINER_INIT_DEPEND |
  1242                             NS_STRING_CONTAINER_INIT_SUBSTRING);
  1245 private:
  1246   self_type& operator=(const self_type& aString) MOZ_DELETE;
  1247 };
  1249 class NS_COM_GLUE nsDependentCSubstring : public nsCStringContainer
  1251 public:
  1252   typedef nsDependentCSubstring self_type;
  1253   typedef nsACString            abstract_string_type;
  1255   ~nsDependentCSubstring()
  1257     NS_CStringContainerFinish(*this);
  1260   nsDependentCSubstring()
  1262     NS_CStringContainerInit(*this);
  1265   nsDependentCSubstring(const char_type *aStart, uint32_t aLength)
  1267     NS_CStringContainerInit2(*this, aStart, aLength,
  1268                              NS_CSTRING_CONTAINER_INIT_DEPEND |
  1269                              NS_CSTRING_CONTAINER_INIT_SUBSTRING);
  1272   nsDependentCSubstring(const abstract_string_type& aStr,
  1273                         uint32_t aStartPos);
  1274   nsDependentCSubstring(const abstract_string_type& aStr,
  1275                         uint32_t aStartPos, uint32_t aLength);
  1277   void Rebind(const char_type *aStart, uint32_t aLength)
  1279     NS_CStringContainerFinish(*this);
  1280     NS_CStringContainerInit2(*this, aStart, aLength,
  1281                              NS_CSTRING_CONTAINER_INIT_DEPEND |
  1282                              NS_CSTRING_CONTAINER_INIT_SUBSTRING);
  1285 private:
  1286   self_type& operator=(const self_type& aString) MOZ_DELETE;
  1287 };
  1290 /**
  1291  * Various nsDependentC?Substring constructor functions
  1292  */
  1294 // char16_t
  1295 inline const nsDependentSubstring
  1296 Substring( const nsAString& str, uint32_t startPos )
  1298   return nsDependentSubstring(str, startPos);
  1301 inline const nsDependentSubstring
  1302 Substring( const nsAString& str, uint32_t startPos, uint32_t length )
  1304   return nsDependentSubstring(str, startPos, length);
  1307 inline const nsDependentSubstring
  1308 Substring( const char16_t* start, const char16_t* end )
  1310   NS_ABORT_IF_FALSE(uint32_t(end - start) == uintptr_t(end - start), "string too long");
  1311   return nsDependentSubstring(start, uint32_t(end - start));
  1314 inline const nsDependentSubstring
  1315 Substring( const char16_t* start, uint32_t length )
  1317   return nsDependentSubstring(start, length);
  1320 inline const nsDependentSubstring
  1321 StringHead( const nsAString& str, uint32_t count )
  1323   return nsDependentSubstring(str, 0, count);
  1326 inline const nsDependentSubstring
  1327 StringTail( const nsAString& str, uint32_t count )
  1329   return nsDependentSubstring(str, str.Length() - count, count);
  1332 // char
  1333 inline const nsDependentCSubstring
  1334 Substring( const nsACString& str, uint32_t startPos )
  1336   return nsDependentCSubstring(str, startPos);
  1339 inline const nsDependentCSubstring
  1340 Substring( const nsACString& str, uint32_t startPos, uint32_t length )
  1342   return nsDependentCSubstring(str, startPos, length);
  1345 inline
  1346 const nsDependentCSubstring
  1347 Substring( const char* start, const char* end )
  1349   NS_ABORT_IF_FALSE(uint32_t(end - start) == uintptr_t(end - start), "string too long");
  1350   return nsDependentCSubstring(start, uint32_t(end - start));
  1353 inline
  1354 const nsDependentCSubstring
  1355 Substring( const char* start, uint32_t length )
  1357   return nsDependentCSubstring(start, length);
  1360 inline const nsDependentCSubstring
  1361 StringHead( const nsACString& str, uint32_t count )
  1363   return nsDependentCSubstring(str, 0, count);
  1366 inline const nsDependentCSubstring
  1367 StringTail( const nsACString& str, uint32_t count )
  1369   return nsDependentCSubstring(str, str.Length() - count, count);
  1373 inline bool
  1374 StringBeginsWith(const nsAString& aSource, const nsAString& aSubstring,
  1375                  nsAString::ComparatorFunc aComparator = nsAString::DefaultComparator)
  1377   return aSubstring.Length() <= aSource.Length() &&
  1378       StringHead(aSource, aSubstring.Length()).Equals(aSubstring, aComparator);
  1381 inline bool
  1382 StringEndsWith(const nsAString& aSource, const nsAString& aSubstring,
  1383                nsAString::ComparatorFunc aComparator = nsAString::DefaultComparator)
  1385   return aSubstring.Length() <= aSource.Length() &&
  1386       StringTail(aSource, aSubstring.Length()).Equals(aSubstring, aComparator);
  1389 inline bool
  1390 StringBeginsWith(const nsACString& aSource, const nsACString& aSubstring,
  1391                  nsACString::ComparatorFunc aComparator = nsACString::DefaultComparator)
  1393   return aSubstring.Length() <= aSource.Length() &&
  1394       StringHead(aSource, aSubstring.Length()).Equals(aSubstring, aComparator);
  1397 inline bool
  1398 StringEndsWith(const nsACString& aSource, const nsACString& aSubstring,
  1399                nsACString::ComparatorFunc aComparator = nsACString::DefaultComparator)
  1401   return aSubstring.Length() <= aSource.Length() &&
  1402       StringTail(aSource, aSubstring.Length()).Equals(aSubstring, aComparator);
  1405 /**
  1406  * Trim whitespace from the beginning and end of a string; then compress
  1407  * remaining runs of whitespace characters to a single space.
  1408  */
  1409 NS_HIDDEN_(void)
  1410 CompressWhitespace(nsAString& aString);
  1412 #define EmptyCString() nsCString()
  1413 #define EmptyString() nsString()
  1415 /**
  1416  * Convert an ASCII string to all upper/lowercase (a-z,A-Z only). As a bonus,
  1417  * returns the string length.
  1418  */
  1419 NS_HIDDEN_(uint32_t)
  1420 ToLowerCase(nsACString& aStr);
  1422 NS_HIDDEN_(uint32_t)
  1423 ToUpperCase(nsACString& aStr);
  1425 NS_HIDDEN_(uint32_t)
  1426 ToLowerCase(const nsACString& aSrc, nsACString& aDest);
  1428 NS_HIDDEN_(uint32_t)
  1429 ToUpperCase(const nsACString& aSrc, nsACString& aDest);
  1431 /**
  1432  * The following declarations are *deprecated*, and are included here only
  1433  * to make porting from existing code that doesn't use the frozen string API
  1434  * easier. They may disappear in the future.
  1435  */
  1437 inline char*
  1438 ToNewCString(const nsACString& aStr)
  1440   return NS_CStringCloneData(aStr);
  1443 inline char16_t*
  1444 ToNewUnicode(const nsAString& aStr)
  1446   return NS_StringCloneData(aStr);
  1449 typedef nsString PromiseFlatString;
  1450 typedef nsCString PromiseFlatCString;
  1452 typedef nsCString nsAutoCString;
  1453 typedef nsString nsAutoString;
  1455 NS_HIDDEN_(bool) ParseString(const nsACString& aAstring, char aDelimiter, 
  1456                                nsTArray<nsCString>& aArray);
  1458 #endif // nsStringAPI_h__

mercurial