1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */
|
---|
3 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
5 | *
|
---|
6 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
7 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
8 | * the License. You may obtain a copy of the License at
|
---|
9 | * http://www.mozilla.org/MPL/
|
---|
10 | *
|
---|
11 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
13 | * for the specific language governing rights and limitations under the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * The Original Code is Mozilla.
|
---|
17 | *
|
---|
18 | * The Initial Developer of the Original Code is IBM Corporation.
|
---|
19 | * Portions created by IBM Corporation are Copyright (C) 2003
|
---|
20 | * IBM Corporation. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | * Rick Gessner <rickg@netscape.com> (original author)
|
---|
24 | * Scott Collins <scc@mozilla.org>
|
---|
25 | * Darin Fisher <darin@meer.net>
|
---|
26 | *
|
---|
27 | * Alternatively, the contents of this file may be used under the terms of
|
---|
28 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
29 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
30 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
31 | * of those above. If you wish to allow use of your version of this file only
|
---|
32 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
33 | * use your version of this file under the terms of the MPL, indicate your
|
---|
34 | * decision by deleting the provisions above and replace them with the notice
|
---|
35 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
36 | * the provisions above, a recipient may use your version of this file under
|
---|
37 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
38 | *
|
---|
39 | * ***** END LICENSE BLOCK ***** */
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * This is the canonical null-terminated string class. All subclasses
|
---|
44 | * promise null-terminated storage. Instances of this class allocate
|
---|
45 | * strings on the heap.
|
---|
46 | *
|
---|
47 | * This class is also known as nsAFlat[C]String, where "flat" is used
|
---|
48 | * to denote a null-terminated string.
|
---|
49 | */
|
---|
50 | class nsTString_CharT : public nsTSubstring_CharT
|
---|
51 | {
|
---|
52 | public:
|
---|
53 |
|
---|
54 | typedef nsTString_CharT self_type;
|
---|
55 |
|
---|
56 | public:
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * constructors
|
---|
60 | */
|
---|
61 |
|
---|
62 | nsTString_CharT()
|
---|
63 | : substring_type() {}
|
---|
64 |
|
---|
65 | explicit
|
---|
66 | nsTString_CharT( char_type c )
|
---|
67 | : substring_type()
|
---|
68 | {
|
---|
69 | Assign(c);
|
---|
70 | }
|
---|
71 |
|
---|
72 | explicit
|
---|
73 | nsTString_CharT( const char_type* data, size_type length = size_type(-1) )
|
---|
74 | : substring_type()
|
---|
75 | {
|
---|
76 | Assign(data, length);
|
---|
77 | }
|
---|
78 |
|
---|
79 | nsTString_CharT( const self_type& str )
|
---|
80 | : substring_type()
|
---|
81 | {
|
---|
82 | Assign(str);
|
---|
83 | }
|
---|
84 |
|
---|
85 | nsTString_CharT( const substring_tuple_type& tuple )
|
---|
86 | : substring_type()
|
---|
87 | {
|
---|
88 | Assign(tuple);
|
---|
89 | }
|
---|
90 |
|
---|
91 | explicit
|
---|
92 | nsTString_CharT( const abstract_string_type& readable )
|
---|
93 | : substring_type()
|
---|
94 | {
|
---|
95 | Assign(readable);
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | // |operator=| does not inherit, so we must define our own
|
---|
100 | self_type& operator=( char_type c ) { Assign(c); return *this; }
|
---|
101 | self_type& operator=( const char_type* data ) { Assign(data); return *this; }
|
---|
102 | self_type& operator=( const self_type& str ) { Assign(str); return *this; }
|
---|
103 | self_type& operator=( const substring_type& str ) { Assign(str); return *this; }
|
---|
104 | self_type& operator=( const substring_tuple_type& tuple ) { Assign(tuple); return *this; }
|
---|
105 | self_type& operator=( const abstract_string_type& readable ) { Assign(readable); return *this; }
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * returns the null-terminated string
|
---|
110 | */
|
---|
111 |
|
---|
112 | const char_type* get() const
|
---|
113 | {
|
---|
114 | return mData;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * returns character at specified index.
|
---|
120 | *
|
---|
121 | * NOTE: unlike nsTSubstring::CharAt, this function allows you to index
|
---|
122 | * the null terminator character.
|
---|
123 | */
|
---|
124 |
|
---|
125 | char_type CharAt( index_type i ) const
|
---|
126 | {
|
---|
127 | NS_ASSERTION(i <= mLength, "index exceeds allowable range");
|
---|
128 | return mData[i];
|
---|
129 | }
|
---|
130 |
|
---|
131 | char_type operator[]( index_type i ) const
|
---|
132 | {
|
---|
133 | return CharAt(i);
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | #if MOZ_STRING_WITH_OBSOLETE_API
|
---|
138 |
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Search for the given substring within this string.
|
---|
142 | *
|
---|
143 | * @param aString is substring to be sought in this
|
---|
144 | * @param aIgnoreCase selects case sensitivity
|
---|
145 | * @param aOffset tells us where in this string to start searching
|
---|
146 | * @param aCount tells us how far from the offset we are to search. Use
|
---|
147 | * -1 to search the whole string.
|
---|
148 | * @return offset in string, or kNotFound
|
---|
149 | */
|
---|
150 |
|
---|
151 | NS_COM PRInt32 Find( const nsCString& aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
|
---|
152 | NS_COM PRInt32 Find( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
|
---|
153 |
|
---|
154 | #ifdef CharT_is_PRUnichar
|
---|
155 | NS_COM PRInt32 Find( const nsAFlatString& aString, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
|
---|
156 | NS_COM PRInt32 Find( const PRUnichar* aString, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
|
---|
157 | #endif
|
---|
158 |
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * This methods scans the string backwards, looking for the given string
|
---|
162 | *
|
---|
163 | * @param aString is substring to be sought in this
|
---|
164 | * @param aIgnoreCase tells us whether or not to do caseless compare
|
---|
165 | * @param aOffset tells us where in this string to start searching.
|
---|
166 | * Use -1 to search from the end of the string.
|
---|
167 | * @param aCount tells us how many iterations to make starting at the
|
---|
168 | * given offset.
|
---|
169 | * @return offset in string, or kNotFound
|
---|
170 | */
|
---|
171 |
|
---|
172 | NS_COM PRInt32 RFind( const nsCString& aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
|
---|
173 | NS_COM PRInt32 RFind( const char* aCString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
|
---|
174 |
|
---|
175 | #ifdef CharT_is_PRUnichar
|
---|
176 | NS_COM PRInt32 RFind( const nsAFlatString& aString, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
|
---|
177 | NS_COM PRInt32 RFind( const PRUnichar* aString, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
|
---|
178 | #endif
|
---|
179 |
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Search for given char within this string
|
---|
183 | *
|
---|
184 | * @param aChar is the character to search for
|
---|
185 | * @param aOffset tells us where in this strig to start searching
|
---|
186 | * @param aCount tells us how far from the offset we are to search.
|
---|
187 | * Use -1 to search the whole string.
|
---|
188 | * @return offset in string, or kNotFound
|
---|
189 | */
|
---|
190 |
|
---|
191 | // PRInt32 FindChar( PRUnichar aChar, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
|
---|
192 | NS_COM PRInt32 RFindChar( PRUnichar aChar, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * This method searches this string for the first character found in
|
---|
197 | * the given string.
|
---|
198 | *
|
---|
199 | * @param aString contains set of chars to be found
|
---|
200 | * @param aOffset tells us where in this string to start searching
|
---|
201 | * (counting from left)
|
---|
202 | * @return offset in string, or kNotFound
|
---|
203 | */
|
---|
204 |
|
---|
205 | NS_COM PRInt32 FindCharInSet( const char* aString, PRInt32 aOffset=0 ) const;
|
---|
206 | PRInt32 FindCharInSet( const self_type& aString, PRInt32 aOffset=0 ) const
|
---|
207 | {
|
---|
208 | return FindCharInSet(aString.get(), aOffset);
|
---|
209 | }
|
---|
210 |
|
---|
211 | #ifdef CharT_is_PRUnichar
|
---|
212 | NS_COM PRInt32 FindCharInSet( const PRUnichar* aString, PRInt32 aOffset=0 ) const;
|
---|
213 | #endif
|
---|
214 |
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * This method searches this string for the last character found in
|
---|
218 | * the given string.
|
---|
219 | *
|
---|
220 | * @param aString contains set of chars to be found
|
---|
221 | * @param aOffset tells us where in this string to start searching
|
---|
222 | * (counting from left)
|
---|
223 | * @return offset in string, or kNotFound
|
---|
224 | */
|
---|
225 |
|
---|
226 | NS_COM PRInt32 RFindCharInSet( const char_type* aString, PRInt32 aOffset=-1 ) const;
|
---|
227 | PRInt32 RFindCharInSet( const self_type& aString, PRInt32 aOffset=-1 ) const
|
---|
228 | {
|
---|
229 | return RFindCharInSet(aString.get(), aOffset);
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Compares a given string to this string.
|
---|
235 | *
|
---|
236 | * @param aString is the string to be compared
|
---|
237 | * @param aIgnoreCase tells us how to treat case
|
---|
238 | * @param aCount tells us how many chars to compare
|
---|
239 | * @return -1,0,1
|
---|
240 | */
|
---|
241 |
|
---|
242 | #ifdef CharT_is_char
|
---|
243 | NS_COM PRInt32 Compare( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aCount=-1 ) const;
|
---|
244 | #endif
|
---|
245 |
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Equality check between given string and this string.
|
---|
249 | *
|
---|
250 | * @param aString is the string to check
|
---|
251 | * @param aIgnoreCase tells us how to treat case
|
---|
252 | * @param aCount tells us how many chars to compare
|
---|
253 | * @return boolean
|
---|
254 | */
|
---|
255 | #ifdef CharT_is_char
|
---|
256 | PRBool EqualsIgnoreCase( const char* aString, PRInt32 aCount=-1 ) const {
|
---|
257 | return Compare(aString, PR_TRUE, aCount) == 0;
|
---|
258 | }
|
---|
259 | #else
|
---|
260 | NS_COM PRBool EqualsIgnoreCase( const char* aString, PRInt32 aCount=-1 ) const;
|
---|
261 |
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Copies data from internal buffer onto given char* buffer
|
---|
265 | *
|
---|
266 | * NOTE: This only copies as many chars as will fit in given buffer (clips)
|
---|
267 | * @param aBuf is the buffer where data is stored
|
---|
268 | * @param aBuflength is the max # of chars to move to buffer
|
---|
269 | * @param aOffset is the offset to copy from
|
---|
270 | * @return ptr to given buffer
|
---|
271 | */
|
---|
272 |
|
---|
273 | NS_COM char* ToCString( char* aBuf, PRUint32 aBufLength, PRUint32 aOffset=0 ) const;
|
---|
274 |
|
---|
275 | #endif // !CharT_is_PRUnichar
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Perform string to float conversion.
|
---|
279 | *
|
---|
280 | * @param aErrorCode will contain error if one occurs
|
---|
281 | * @return float rep of string value
|
---|
282 | */
|
---|
283 | NS_COM float ToFloat( PRInt32* aErrorCode ) const;
|
---|
284 |
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Perform string to int conversion.
|
---|
288 | * @param aErrorCode will contain error if one occurs
|
---|
289 | * @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you.
|
---|
290 | * @return int rep of string value, and possible (out) error code
|
---|
291 | */
|
---|
292 | NS_COM PRInt32 ToInteger( PRInt32* aErrorCode, PRUint32 aRadix=kRadix10 ) const;
|
---|
293 |
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * |Left|, |Mid|, and |Right| are annoying signatures that seem better almost
|
---|
297 | * any _other_ way than they are now. Consider these alternatives
|
---|
298 | *
|
---|
299 | * aWritable = aReadable.Left(17); // ...a member function that returns a |Substring|
|
---|
300 | * aWritable = Left(aReadable, 17); // ...a global function that returns a |Substring|
|
---|
301 | * Left(aReadable, 17, aWritable); // ...a global function that does the assignment
|
---|
302 | *
|
---|
303 | * as opposed to the current signature
|
---|
304 | *
|
---|
305 | * aReadable.Left(aWritable, 17); // ...a member function that does the assignment
|
---|
306 | *
|
---|
307 | * or maybe just stamping them out in favor of |Substring|, they are just duplicate functionality
|
---|
308 | *
|
---|
309 | * aWritable = Substring(aReadable, 0, 17);
|
---|
310 | */
|
---|
311 |
|
---|
312 | NS_COM size_type Mid( self_type& aResult, PRUint32 aStartPos, PRUint32 aCount ) const;
|
---|
313 |
|
---|
314 | size_type Left( self_type& aResult, size_type aCount ) const
|
---|
315 | {
|
---|
316 | return Mid(aResult, 0, aCount);
|
---|
317 | }
|
---|
318 |
|
---|
319 | size_type Right( self_type& aResult, size_type aCount ) const
|
---|
320 | {
|
---|
321 | aCount = NS_MIN(mLength, aCount);
|
---|
322 | return Mid(aResult, mLength - aCount, aCount);
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Set a char inside this string at given index
|
---|
328 | *
|
---|
329 | * @param aChar is the char you want to write into this string
|
---|
330 | * @param anIndex is the ofs where you want to write the given char
|
---|
331 | * @return TRUE if successful
|
---|
332 | */
|
---|
333 |
|
---|
334 | NS_COM PRBool SetCharAt( PRUnichar aChar, PRUint32 aIndex );
|
---|
335 |
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * These methods are used to remove all occurances of the
|
---|
339 | * characters found in aSet from this string.
|
---|
340 | *
|
---|
341 | * @param aSet -- characters to be cut from this
|
---|
342 | */
|
---|
343 | NS_COM void StripChars( const char* aSet );
|
---|
344 |
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * This method is used to remove all occurances of aChar from this
|
---|
348 | * string.
|
---|
349 | *
|
---|
350 | * @param aChar -- char to be stripped
|
---|
351 | * @param aOffset -- where in this string to start stripping chars
|
---|
352 | */
|
---|
353 |
|
---|
354 | NS_COM void StripChar( char_type aChar, PRInt32 aOffset=0 );
|
---|
355 |
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * This method strips whitespace throughout the string.
|
---|
359 | */
|
---|
360 | NS_COM void StripWhitespace();
|
---|
361 |
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * swaps occurence of 1 string for another
|
---|
365 | */
|
---|
366 |
|
---|
367 | NS_COM void ReplaceChar( char_type aOldChar, char_type aNewChar );
|
---|
368 | NS_COM void ReplaceChar( const char* aSet, char_type aNewChar );
|
---|
369 | NS_COM void ReplaceSubstring( const self_type& aTarget, const self_type& aNewValue);
|
---|
370 | NS_COM void ReplaceSubstring( const char_type* aTarget, const char_type* aNewValue);
|
---|
371 |
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * This method trims characters found in aTrimSet from
|
---|
375 | * either end of the underlying string.
|
---|
376 | *
|
---|
377 | * @param aSet -- contains chars to be trimmed from both ends
|
---|
378 | * @param aEliminateLeading
|
---|
379 | * @param aEliminateTrailing
|
---|
380 | * @param aIgnoreQuotes -- if true, causes surrounding quotes to be ignored
|
---|
381 | * @return this
|
---|
382 | */
|
---|
383 | NS_COM void Trim( const char* aSet, PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE, PRBool aIgnoreQuotes=PR_FALSE );
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * This method strips whitespace from string.
|
---|
387 | * You can control whether whitespace is yanked from start and end of
|
---|
388 | * string as well.
|
---|
389 | *
|
---|
390 | * @param aEliminateLeading controls stripping of leading ws
|
---|
391 | * @param aEliminateTrailing controls stripping of trailing ws
|
---|
392 | */
|
---|
393 | NS_COM void CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE );
|
---|
394 |
|
---|
395 |
|
---|
396 | /**
|
---|
397 | * assign/append/insert with _LOSSY_ conversion
|
---|
398 | */
|
---|
399 |
|
---|
400 | NS_COM void AssignWithConversion( const nsTAString_IncompatibleCharT& aString );
|
---|
401 | NS_COM void AssignWithConversion( const incompatible_char_type* aData, PRInt32 aLength=-1 );
|
---|
402 |
|
---|
403 | NS_COM void AppendWithConversion( const nsTAString_IncompatibleCharT& aString );
|
---|
404 | NS_COM void AppendWithConversion( const incompatible_char_type* aData, PRInt32 aLength=-1 );
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * Append the given integer to this string
|
---|
408 | */
|
---|
409 | NS_COM void AppendInt( PRInt32 aInteger, PRInt32 aRadix=kRadix10 ); //radix=8,10 or 16
|
---|
410 |
|
---|
411 | /**
|
---|
412 | * Append the given unsigned integer to this string
|
---|
413 | */
|
---|
414 | inline void AppendInt( PRUint32 aInteger, PRInt32 aRadix = kRadix10 )
|
---|
415 | {
|
---|
416 | AppendInt(PRInt32(aInteger), aRadix);
|
---|
417 | }
|
---|
418 |
|
---|
419 | /**
|
---|
420 | * Append the given 64-bit integer to this string.
|
---|
421 | * @param aInteger The integer to append
|
---|
422 | * @param aRadix The radix to use; can be 8, 10 or 16.
|
---|
423 | */
|
---|
424 | NS_COM void AppendInt( PRInt64 aInteger, PRInt32 aRadix=kRadix10 );
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Append the given float to this string
|
---|
428 | */
|
---|
429 |
|
---|
430 | NS_COM void AppendFloat( double aFloat );
|
---|
431 |
|
---|
432 | #endif // !MOZ_STRING_WITH_OBSOLETE_API
|
---|
433 |
|
---|
434 |
|
---|
435 | protected:
|
---|
436 |
|
---|
437 | explicit
|
---|
438 | nsTString_CharT( PRUint32 flags )
|
---|
439 | : substring_type(flags) {}
|
---|
440 |
|
---|
441 | // allow subclasses to initialize fields directly
|
---|
442 | nsTString_CharT( char_type* data, size_type length, PRUint32 flags )
|
---|
443 | : substring_type(data, length, flags) {}
|
---|
444 | };
|
---|
445 |
|
---|
446 |
|
---|
447 | class nsTFixedString_CharT : public nsTString_CharT
|
---|
448 | {
|
---|
449 | public:
|
---|
450 |
|
---|
451 | typedef nsTFixedString_CharT self_type;
|
---|
452 | typedef nsTFixedString_CharT fixed_string_type;
|
---|
453 |
|
---|
454 | public:
|
---|
455 |
|
---|
456 | /**
|
---|
457 | * @param data
|
---|
458 | * fixed-size buffer to be used by the string (the contents of
|
---|
459 | * this buffer may be modified by the string)
|
---|
460 | * @param storageSize
|
---|
461 | * the size of the fixed buffer
|
---|
462 | * @param length (optional)
|
---|
463 | * the length of the string already contained in the buffer
|
---|
464 | */
|
---|
465 |
|
---|
466 | nsTFixedString_CharT( char_type* data, size_type storageSize )
|
---|
467 | : string_type(data, char_traits::length(data), F_TERMINATED | F_FIXED | F_CLASS_FIXED)
|
---|
468 | , mFixedCapacity(storageSize - 1)
|
---|
469 | , mFixedBuf(data)
|
---|
470 | {}
|
---|
471 |
|
---|
472 | nsTFixedString_CharT( char_type* data, size_type storageSize, size_type length )
|
---|
473 | : string_type(data, length, F_TERMINATED | F_FIXED | F_CLASS_FIXED)
|
---|
474 | , mFixedCapacity(storageSize - 1)
|
---|
475 | , mFixedBuf(data)
|
---|
476 | {
|
---|
477 | // null-terminate
|
---|
478 | mFixedBuf[length] = char_type(0);
|
---|
479 | }
|
---|
480 |
|
---|
481 | // |operator=| does not inherit, so we must define our own
|
---|
482 | self_type& operator=( char_type c ) { Assign(c); return *this; }
|
---|
483 | self_type& operator=( const char_type* data ) { Assign(data); return *this; }
|
---|
484 | self_type& operator=( const substring_type& str ) { Assign(str); return *this; }
|
---|
485 | self_type& operator=( const substring_tuple_type& tuple ) { Assign(tuple); return *this; }
|
---|
486 | self_type& operator=( const abstract_string_type& readable ) { Assign(readable); return *this; }
|
---|
487 |
|
---|
488 | protected:
|
---|
489 |
|
---|
490 | friend class nsTSubstring_CharT;
|
---|
491 |
|
---|
492 | size_type mFixedCapacity;
|
---|
493 | char_type *mFixedBuf;
|
---|
494 | };
|
---|
495 |
|
---|
496 |
|
---|
497 | /**
|
---|
498 | * nsTAutoString_CharT
|
---|
499 | *
|
---|
500 | * Subclass of nsTString_CharT that adds support for stack-based string
|
---|
501 | * allocation. Do not allocate this class on the heap! ;-)
|
---|
502 | */
|
---|
503 | class nsTAutoString_CharT : public nsTFixedString_CharT
|
---|
504 | {
|
---|
505 | public:
|
---|
506 |
|
---|
507 | typedef nsTAutoString_CharT self_type;
|
---|
508 |
|
---|
509 | public:
|
---|
510 |
|
---|
511 | /**
|
---|
512 | * constructors
|
---|
513 | */
|
---|
514 |
|
---|
515 | nsTAutoString_CharT()
|
---|
516 | : fixed_string_type(mStorage, kDefaultStorageSize, 0)
|
---|
517 | {}
|
---|
518 |
|
---|
519 | explicit
|
---|
520 | nsTAutoString_CharT( char_type c )
|
---|
521 | : fixed_string_type(mStorage, kDefaultStorageSize, 0)
|
---|
522 | {
|
---|
523 | Assign(c);
|
---|
524 | }
|
---|
525 |
|
---|
526 | explicit
|
---|
527 | nsTAutoString_CharT( const char_type* data, size_type length = size_type(-1) )
|
---|
528 | : fixed_string_type(mStorage, kDefaultStorageSize, 0)
|
---|
529 | {
|
---|
530 | Assign(data, length);
|
---|
531 | }
|
---|
532 |
|
---|
533 | nsTAutoString_CharT( const self_type& str )
|
---|
534 | : fixed_string_type(mStorage, kDefaultStorageSize, 0)
|
---|
535 | {
|
---|
536 | Assign(str);
|
---|
537 | }
|
---|
538 |
|
---|
539 | explicit
|
---|
540 | nsTAutoString_CharT( const substring_type& str )
|
---|
541 | : fixed_string_type(mStorage, kDefaultStorageSize, 0)
|
---|
542 | {
|
---|
543 | Assign(str);
|
---|
544 | }
|
---|
545 |
|
---|
546 | nsTAutoString_CharT( const substring_tuple_type& tuple )
|
---|
547 | : fixed_string_type(mStorage, kDefaultStorageSize, 0)
|
---|
548 | {
|
---|
549 | Assign(tuple);
|
---|
550 | }
|
---|
551 |
|
---|
552 | explicit
|
---|
553 | nsTAutoString_CharT( const abstract_string_type& readable )
|
---|
554 | : fixed_string_type(mStorage, kDefaultStorageSize, 0)
|
---|
555 | {
|
---|
556 | Assign(readable);
|
---|
557 | }
|
---|
558 |
|
---|
559 | // |operator=| does not inherit, so we must define our own
|
---|
560 | self_type& operator=( char_type c ) { Assign(c); return *this; }
|
---|
561 | self_type& operator=( const char_type* data ) { Assign(data); return *this; }
|
---|
562 | self_type& operator=( const self_type& str ) { Assign(str); return *this; }
|
---|
563 | self_type& operator=( const substring_type& str ) { Assign(str); return *this; }
|
---|
564 | self_type& operator=( const substring_tuple_type& tuple ) { Assign(tuple); return *this; }
|
---|
565 | self_type& operator=( const abstract_string_type& readable ) { Assign(readable); return *this; }
|
---|
566 |
|
---|
567 | enum { kDefaultStorageSize = 64 };
|
---|
568 |
|
---|
569 | private:
|
---|
570 |
|
---|
571 | char_type mStorage[kDefaultStorageSize];
|
---|
572 | };
|
---|
573 |
|
---|
574 |
|
---|
575 | /**
|
---|
576 | * nsTXPIDLString extends nsTString such that:
|
---|
577 | *
|
---|
578 | * (1) mData can be null
|
---|
579 | * (2) objects of this type can be automatically cast to |const CharT*|
|
---|
580 | * (3) getter_Copies method is supported to adopt data
|
---|
581 | */
|
---|
582 | class nsTXPIDLString_CharT : public nsTString_CharT
|
---|
583 | {
|
---|
584 | public:
|
---|
585 |
|
---|
586 | typedef nsTXPIDLString_CharT self_type;
|
---|
587 |
|
---|
588 | public:
|
---|
589 |
|
---|
590 | nsTXPIDLString_CharT()
|
---|
591 | : string_type(NS_CONST_CAST(char_type*, char_traits::sEmptyBuffer), 0, F_TERMINATED | F_VOIDED) {}
|
---|
592 |
|
---|
593 | // copy-constructor required to avoid default
|
---|
594 | nsTXPIDLString_CharT( const self_type& str )
|
---|
595 | : string_type(NS_CONST_CAST(char_type*, char_traits::sEmptyBuffer), 0, F_TERMINATED | F_VOIDED)
|
---|
596 | {
|
---|
597 | Assign(str);
|
---|
598 | }
|
---|
599 |
|
---|
600 | // return nsnull if we are voided
|
---|
601 | const char_type* get() const
|
---|
602 | {
|
---|
603 | return (mFlags & F_VOIDED) ? nsnull : mData;
|
---|
604 | }
|
---|
605 |
|
---|
606 | // this case operator is the reason why this class cannot just be a
|
---|
607 | // typedef for nsTString
|
---|
608 | operator const char_type*() const
|
---|
609 | {
|
---|
610 | return get();
|
---|
611 | }
|
---|
612 |
|
---|
613 | // need this to diambiguous operator[int]
|
---|
614 | char_type operator[]( PRInt32 i ) const
|
---|
615 | {
|
---|
616 | return CharAt(index_type(i));
|
---|
617 | }
|
---|
618 |
|
---|
619 | // |operator=| does not inherit, so we must define our own
|
---|
620 | self_type& operator=( char_type c ) { Assign(c); return *this; }
|
---|
621 | self_type& operator=( const char_type* data ) { Assign(data); return *this; }
|
---|
622 | self_type& operator=( const self_type& str ) { Assign(str); return *this; }
|
---|
623 | self_type& operator=( const substring_type& str ) { Assign(str); return *this; }
|
---|
624 | self_type& operator=( const substring_tuple_type& tuple ) { Assign(tuple); return *this; }
|
---|
625 | self_type& operator=( const abstract_string_type& readable ) { Assign(readable); return *this; }
|
---|
626 | };
|
---|
627 |
|
---|
628 |
|
---|
629 | /**
|
---|
630 | * getter_Copies support for use with raw string out params:
|
---|
631 | *
|
---|
632 | * NS_IMETHOD GetBlah(char**);
|
---|
633 | *
|
---|
634 | * void some_function()
|
---|
635 | * {
|
---|
636 | * nsXPIDLCString blah;
|
---|
637 | * GetBlah(getter_Copies(blah));
|
---|
638 | * // ...
|
---|
639 | * }
|
---|
640 | */
|
---|
641 | class nsTGetterCopies_CharT
|
---|
642 | {
|
---|
643 | public:
|
---|
644 | typedef CharT char_type;
|
---|
645 |
|
---|
646 | nsTGetterCopies_CharT(nsTXPIDLString_CharT& str)
|
---|
647 | : mString(str), mData(nsnull) {}
|
---|
648 |
|
---|
649 | ~nsTGetterCopies_CharT()
|
---|
650 | {
|
---|
651 | mString.Adopt(mData); // OK if mData is null
|
---|
652 | }
|
---|
653 |
|
---|
654 | operator char_type**()
|
---|
655 | {
|
---|
656 | return &mData;
|
---|
657 | }
|
---|
658 |
|
---|
659 | private:
|
---|
660 | nsTXPIDLString_CharT& mString;
|
---|
661 | char_type* mData;
|
---|
662 | };
|
---|
663 |
|
---|
664 | inline
|
---|
665 | nsTGetterCopies_CharT
|
---|
666 | getter_Copies( nsTXPIDLString_CharT& aString )
|
---|
667 | {
|
---|
668 | return nsTGetterCopies_CharT(aString);
|
---|
669 | }
|
---|
670 |
|
---|
671 |
|
---|
672 | /**
|
---|
673 | * nsTAdoptingString extends nsTXPIDLString such that:
|
---|
674 | *
|
---|
675 | * (1) Adopt given string on construction or assignment, i.e. take
|
---|
676 | * the value of what's given, and make what's given forget its
|
---|
677 | * value. Note that this class violates constness in a few
|
---|
678 | * places. Be careful!
|
---|
679 | */
|
---|
680 | class nsTAdoptingString_CharT : public nsTXPIDLString_CharT
|
---|
681 | {
|
---|
682 | public:
|
---|
683 |
|
---|
684 | typedef nsTAdoptingString_CharT self_type;
|
---|
685 |
|
---|
686 | public:
|
---|
687 |
|
---|
688 | explicit nsTAdoptingString_CharT() {}
|
---|
689 | explicit nsTAdoptingString_CharT(char_type* str, size_type length = size_type(-1))
|
---|
690 | {
|
---|
691 | Adopt(str, length);
|
---|
692 | }
|
---|
693 |
|
---|
694 | // copy-constructor required to adopt on copy. Note that this
|
---|
695 | // will violate the constness of |str| in the operator=()
|
---|
696 | // call. |str| will be truncated as a side-effect of this
|
---|
697 | // constructor.
|
---|
698 | nsTAdoptingString_CharT( const self_type& str )
|
---|
699 | #ifdef VBOX /* bird: shut up annoying warnings */
|
---|
700 | : nsTXPIDLString_CharT()
|
---|
701 | #endif
|
---|
702 | {
|
---|
703 | *this = str;
|
---|
704 | }
|
---|
705 |
|
---|
706 | // |operator=| does not inherit, so we must define our own
|
---|
707 | self_type& operator=( const substring_type& str ) { Assign(str); return *this; }
|
---|
708 | self_type& operator=( const substring_tuple_type& tuple ) { Assign(tuple); return *this; }
|
---|
709 | self_type& operator=( const abstract_string_type& readable ) { Assign(readable); return *this; }
|
---|
710 |
|
---|
711 | // Adopt(), if possible, when assigning to a self_type&. Note
|
---|
712 | // that this violates the constness of str, str is always
|
---|
713 | // truncated when this operator is called.
|
---|
714 | NS_COM self_type& operator=( const self_type& str );
|
---|
715 |
|
---|
716 | private:
|
---|
717 | // NOT TO BE IMPLEMENTED.
|
---|
718 | self_type& operator=( const char_type* data );
|
---|
719 | self_type& operator=( char_type* data );
|
---|
720 | };
|
---|
721 |
|
---|