nsStringAPI.h 43 KB

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