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