PageRenderTime 118ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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