PageRenderTime 213ms CodeModel.GetById 61ms app.highlight 118ms RepoModel.GetById 22ms app.codeStats 1ms

/gecko_api/include/nsStringAPI.h

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