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