1 | /* vim:set ts=2 sw=2 et cindent: */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is Mozilla.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is IBM Corporation.
|
---|
18 | * Portions created by IBM Corporation are Copyright (C) 2003
|
---|
19 | * IBM Corporation. All Rights Reserved.
|
---|
20 | *
|
---|
21 | * Contributor(s):
|
---|
22 | * Darin Fisher <[email protected]>
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #ifndef nsStringAPI_h__
|
---|
39 | #define nsStringAPI_h__
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * nsStringAPI.h
|
---|
43 | *
|
---|
44 | * This file describes a minimal API for working with XPCOM's abstract
|
---|
45 | * string classes. It divorces the consumer from having any run-time
|
---|
46 | * dependency on the implementation details of the abstract string types.
|
---|
47 | */
|
---|
48 |
|
---|
49 | #include "nscore.h"
|
---|
50 |
|
---|
51 | #define NS_STRINGAPI(x) extern "C" NS_COM x
|
---|
52 |
|
---|
53 | /* The base string types */
|
---|
54 | class nsAString;
|
---|
55 | class nsACString;
|
---|
56 |
|
---|
57 | /* ------------------------------------------------------------------------- */
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * nsStringContainer
|
---|
61 | *
|
---|
62 | * This is an opaque data type that is large enough to hold the canonical
|
---|
63 | * implementation of nsAString. The binary structure of this class is an
|
---|
64 | * implementation detail.
|
---|
65 | *
|
---|
66 | * The string data stored in a string container is always single fragment
|
---|
67 | * and null-terminated.
|
---|
68 | *
|
---|
69 | * Typically, string containers are allocated on the stack for temporary
|
---|
70 | * use. However, they can also be malloc'd if necessary. In either case,
|
---|
71 | * a string container is not useful until it has been initialized with a
|
---|
72 | * call to NS_StringContainerInit. The following example shows how to use
|
---|
73 | * a string container to call a function that takes a |nsAString &| out-param.
|
---|
74 | *
|
---|
75 | * NS_METHOD GetBlah(nsAString &aBlah);
|
---|
76 | *
|
---|
77 | * nsresult MyCode()
|
---|
78 | * {
|
---|
79 | * nsresult rv;
|
---|
80 | *
|
---|
81 | * nsStringContainer sc;
|
---|
82 | * rv = NS_StringContainerInit(sc);
|
---|
83 | * if (NS_FAILED(rv))
|
---|
84 | * return rv;
|
---|
85 | *
|
---|
86 | * rv = GetBlah(sc);
|
---|
87 | * if (NS_SUCCEEDED(rv))
|
---|
88 | * {
|
---|
89 | * const PRUnichar *data;
|
---|
90 | * NS_StringGetData(sc, &data);
|
---|
91 | * //
|
---|
92 | * // |data| now points to the result of the GetBlah function
|
---|
93 | * //
|
---|
94 | * }
|
---|
95 | *
|
---|
96 | * NS_StringContainerFinish(sc);
|
---|
97 | * return rv;
|
---|
98 | * }
|
---|
99 | *
|
---|
100 | * The following example show how to use a string container to pass a string
|
---|
101 | * parameter to a function taking a |const nsAString &| in-param.
|
---|
102 | *
|
---|
103 | * NS_METHOD SetBlah(const nsAString &aBlah);
|
---|
104 | *
|
---|
105 | * nsresult MyCode()
|
---|
106 | * {
|
---|
107 | * nsresult rv;
|
---|
108 | *
|
---|
109 | * nsStringContainer sc;
|
---|
110 | * rv = NS_StringContainerInit(sc);
|
---|
111 | * if (NS_FAILED(rv))
|
---|
112 | * return rv;
|
---|
113 | *
|
---|
114 | * const PRUnichar kData[] = {'x','y','z','\0'};
|
---|
115 | * rv = NS_StringSetData(sc, kData, sizeof(kData)/2 - 1);
|
---|
116 | * if (NS_SUCCEEDED(rv))
|
---|
117 | * rv = SetBlah(sc);
|
---|
118 | *
|
---|
119 | * NS_StringContainerFinish(sc);
|
---|
120 | * return rv;
|
---|
121 | * }
|
---|
122 | */
|
---|
123 | class nsStringContainer;
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * NS_StringContainerInit
|
---|
127 | *
|
---|
128 | * @param aContainer string container reference
|
---|
129 | * @return NS_OK if string container successfully initialized
|
---|
130 | *
|
---|
131 | * This function may allocate additional memory for aContainer. When
|
---|
132 | * aContainer is no longer needed, NS_StringContainerFinish should be called.
|
---|
133 | *
|
---|
134 | * @status FROZEN
|
---|
135 | */
|
---|
136 | NS_STRINGAPI(nsresult)
|
---|
137 | NS_StringContainerInit(nsStringContainer &aContainer);
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * NS_StringContainerFinish
|
---|
141 | *
|
---|
142 | * @param aContainer string container reference
|
---|
143 | *
|
---|
144 | * This function frees any memory owned by aContainer.
|
---|
145 | *
|
---|
146 | * @status FROZEN
|
---|
147 | */
|
---|
148 | NS_STRINGAPI(void)
|
---|
149 | NS_StringContainerFinish(nsStringContainer &aContainer);
|
---|
150 |
|
---|
151 | /* ------------------------------------------------------------------------- */
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * NS_StringGetData
|
---|
155 | *
|
---|
156 | * This function returns a const character pointer to the string's internal
|
---|
157 | * buffer, the length of the string, and a boolean value indicating whether
|
---|
158 | * or not the buffer is null-terminated.
|
---|
159 | *
|
---|
160 | * @param aStr abstract string reference
|
---|
161 | * @param aData out param that will hold the address of aStr's
|
---|
162 | * internal buffer
|
---|
163 | * @param aTerminated if non-null, this out param will be set to indicate
|
---|
164 | * whether or not aStr's internal buffer is null-
|
---|
165 | * terminated
|
---|
166 | * @return length of aStr's internal buffer
|
---|
167 | *
|
---|
168 | * @status FROZEN
|
---|
169 | */
|
---|
170 | NS_STRINGAPI(PRUint32)
|
---|
171 | NS_StringGetData
|
---|
172 | (const nsAString &aStr, const PRUnichar **aData,
|
---|
173 | PRBool *aTerminated = nsnull);
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * NS_StringCloneData
|
---|
177 | *
|
---|
178 | * This function returns a null-terminated copy of the string's
|
---|
179 | * internal buffer.
|
---|
180 | *
|
---|
181 | * @param aStr abstract string reference
|
---|
182 | * @return null-terminated copy of the string's internal buffer
|
---|
183 | * (it must be free'd using using nsMemory::Free)
|
---|
184 | *
|
---|
185 | * @status FROZEN
|
---|
186 | */
|
---|
187 | NS_STRINGAPI(PRUnichar *)
|
---|
188 | NS_StringCloneData
|
---|
189 | (const nsAString &aStr);
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * NS_StringSetData
|
---|
193 | *
|
---|
194 | * This function copies aData into aStr.
|
---|
195 | *
|
---|
196 | * @param aStr abstract string reference
|
---|
197 | * @param aData character buffer
|
---|
198 | * @param aDataLength number of characters to copy from source string (pass
|
---|
199 | * PR_UINT32_MAX to copy until end of aData, designated by
|
---|
200 | * a null character)
|
---|
201 | * @return NS_OK if function succeeded
|
---|
202 | *
|
---|
203 | * This function does not necessarily null-terminate aStr after copying data
|
---|
204 | * from aData. The behavior depends on the implementation of the abstract
|
---|
205 | * string, aStr. If aStr is a reference to a nsStringContainer, then its data
|
---|
206 | * will be null-terminated by this function.
|
---|
207 | *
|
---|
208 | * @status FROZEN
|
---|
209 | */
|
---|
210 | NS_STRINGAPI(nsresult)
|
---|
211 | NS_StringSetData
|
---|
212 | (nsAString &aStr, const PRUnichar *aData,
|
---|
213 | PRUint32 aDataLength = PR_UINT32_MAX);
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * NS_StringSetDataRange
|
---|
217 | *
|
---|
218 | * This function copies aData into a section of aStr. As a result it can be
|
---|
219 | * used to insert new characters into the string.
|
---|
220 | *
|
---|
221 | * @param aStr abstract string reference
|
---|
222 | * @param aCutOffset starting index where the string's existing data
|
---|
223 | * is to be overwritten (pass PR_UINT32_MAX to cause
|
---|
224 | * aData to be appended to the end of aStr, in which
|
---|
225 | * case the value of aCutLength is ignored).
|
---|
226 | * @param aCutLength number of characters to overwrite starting at
|
---|
227 | * aCutOffset (pass PR_UINT32_MAX to overwrite until the
|
---|
228 | * end of aStr).
|
---|
229 | * @param aData character buffer (pass null to cause this function
|
---|
230 | * to simply remove the "cut" range)
|
---|
231 | * @param aDataLength number of characters to copy from source string (pass
|
---|
232 | * PR_UINT32_MAX to copy until end of aData, designated by
|
---|
233 | * a null character)
|
---|
234 | * @return NS_OK if function succeeded
|
---|
235 | *
|
---|
236 | * This function does not necessarily null-terminate aStr after copying data
|
---|
237 | * from aData. The behavior depends on the implementation of the abstract
|
---|
238 | * string, aStr. If aStr is a reference to a nsStringContainer, then its data
|
---|
239 | * will be null-terminated by this function.
|
---|
240 | *
|
---|
241 | * @status FROZEN
|
---|
242 | */
|
---|
243 | NS_STRINGAPI(nsresult)
|
---|
244 | NS_StringSetDataRange
|
---|
245 | (nsAString &aStr, PRUint32 aCutOffset, PRUint32 aCutLength,
|
---|
246 | const PRUnichar *aData, PRUint32 aDataLength = PR_UINT32_MAX);
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * NS_StringCopy
|
---|
250 | *
|
---|
251 | * This function makes aDestStr have the same value as aSrcStr. It is
|
---|
252 | * provided as an optimization.
|
---|
253 | *
|
---|
254 | * @param aDestStr abstract string reference to be modified
|
---|
255 | * @param aSrcStr abstract string reference containing source string
|
---|
256 | * @return NS_OK if function succeeded
|
---|
257 | *
|
---|
258 | * This function does not necessarily null-terminate aDestStr after copying
|
---|
259 | * data from aSrcStr. The behavior depends on the implementation of the
|
---|
260 | * abstract string, aDestStr. If aDestStr is a reference to a
|
---|
261 | * nsStringContainer, then its data will be null-terminated by this function.
|
---|
262 | *
|
---|
263 | * @status FROZEN
|
---|
264 | */
|
---|
265 | NS_STRINGAPI(nsresult)
|
---|
266 | NS_StringCopy
|
---|
267 | (nsAString &aDestStr, const nsAString &aSrcStr);
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * NS_StringAppendData
|
---|
271 | *
|
---|
272 | * This function appends data to the existing value of aStr.
|
---|
273 | *
|
---|
274 | * @param aStr abstract string reference to be modified
|
---|
275 | * @param aData character buffer
|
---|
276 | * @param aDataLength number of characters to append (pass PR_UINT32_MAX to
|
---|
277 | * append until a null-character is encountered)
|
---|
278 | * @return NS_OK if function succeeded
|
---|
279 | *
|
---|
280 | * This function does not necessarily null-terminate aStr upon completion.
|
---|
281 | * The behavior depends on the implementation of the abstract string, aStr.
|
---|
282 | * If aStr is a reference to a nsStringContainer, then its data will be null-
|
---|
283 | * terminated by this function.
|
---|
284 | */
|
---|
285 | inline NS_HIDDEN_(nsresult)
|
---|
286 | NS_StringAppendData(nsAString &aStr, const PRUnichar *aData,
|
---|
287 | PRUint32 aDataLength = PR_UINT32_MAX)
|
---|
288 | {
|
---|
289 | return NS_StringSetDataRange(aStr, PR_UINT32_MAX, 0, aData, aDataLength);
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * NS_StringInsertData
|
---|
294 | *
|
---|
295 | * This function inserts data into the existing value of aStr at the specified
|
---|
296 | * offset.
|
---|
297 | *
|
---|
298 | * @param aStr abstract string reference to be modified
|
---|
299 | * @param aOffset specifies where in the string to insert aData
|
---|
300 | * @param aData character buffer
|
---|
301 | * @param aDataLength number of characters to append (pass PR_UINT32_MAX to
|
---|
302 | * append until a null-character is encountered)
|
---|
303 | * @return NS_OK if function succeeded
|
---|
304 | *
|
---|
305 | * This function does not necessarily null-terminate aStr upon completion.
|
---|
306 | * The behavior depends on the implementation of the abstract string, aStr.
|
---|
307 | * If aStr is a reference to a nsStringContainer, then its data will be null-
|
---|
308 | * terminated by this function.
|
---|
309 | */
|
---|
310 | inline NS_HIDDEN_(nsresult)
|
---|
311 | NS_StringInsertData(nsAString &aStr, PRUint32 aOffset, const PRUnichar *aData,
|
---|
312 | PRUint32 aDataLength = PR_UINT32_MAX)
|
---|
313 | {
|
---|
314 | return NS_StringSetDataRange(aStr, aOffset, 0, aData, aDataLength);
|
---|
315 | }
|
---|
316 |
|
---|
317 | /**
|
---|
318 | * NS_StringCutData
|
---|
319 | *
|
---|
320 | * This function shortens the existing value of aStr, by removing characters
|
---|
321 | * at the specified offset.
|
---|
322 | *
|
---|
323 | * @param aStr abstract string reference to be modified
|
---|
324 | * @param aCutOffset specifies where in the string to insert aData
|
---|
325 | * @param aCutLength number of characters to remove
|
---|
326 | * @return NS_OK if function succeeded
|
---|
327 | */
|
---|
328 | inline NS_HIDDEN_(nsresult)
|
---|
329 | NS_StringCutData(nsAString &aStr, PRUint32 aCutOffset, PRUint32 aCutLength)
|
---|
330 | {
|
---|
331 | return NS_StringSetDataRange(aStr, aCutOffset, aCutLength, nsnull, 0);
|
---|
332 | }
|
---|
333 |
|
---|
334 | /* ------------------------------------------------------------------------- */
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * nsCStringContainer
|
---|
338 | *
|
---|
339 | * This is an opaque data type that is large enough to hold the canonical
|
---|
340 | * implementation of nsACString. The binary structure of this class is an
|
---|
341 | * implementation detail.
|
---|
342 | *
|
---|
343 | * The string data stored in a string container is always single fragment
|
---|
344 | * and null-terminated.
|
---|
345 | *
|
---|
346 | * @see nsStringContainer for use cases and further documentation.
|
---|
347 | */
|
---|
348 | class nsCStringContainer;
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * NS_CStringContainerInit
|
---|
352 | *
|
---|
353 | * @param aContainer string container reference
|
---|
354 | * @return NS_OK if string container successfully initialized
|
---|
355 | *
|
---|
356 | * This function may allocate additional memory for aContainer. When
|
---|
357 | * aContainer is no longer needed, NS_CStringContainerFinish should be called.
|
---|
358 | *
|
---|
359 | * @status FROZEN
|
---|
360 | */
|
---|
361 | NS_STRINGAPI(nsresult)
|
---|
362 | NS_CStringContainerInit(nsCStringContainer &aContainer);
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * NS_CStringContainerFinish
|
---|
366 | *
|
---|
367 | * @param aContainer string container reference
|
---|
368 | *
|
---|
369 | * This function frees any memory owned by aContainer.
|
---|
370 | *
|
---|
371 | * @status FROZEN
|
---|
372 | */
|
---|
373 | NS_STRINGAPI(void)
|
---|
374 | NS_CStringContainerFinish(nsCStringContainer &aContainer);
|
---|
375 |
|
---|
376 | /* ------------------------------------------------------------------------- */
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * NS_CStringGetData
|
---|
380 | *
|
---|
381 | * This function returns a const character pointer to the string's internal
|
---|
382 | * buffer, the length of the string, and a boolean value indicating whether
|
---|
383 | * or not the buffer is null-terminated.
|
---|
384 | *
|
---|
385 | * @param aStr abstract string reference
|
---|
386 | * @param aData out param that will hold the address of aStr's
|
---|
387 | * internal buffer
|
---|
388 | * @param aTerminated if non-null, this out param will be set to indicate
|
---|
389 | * whether or not aStr's internal buffer is null-
|
---|
390 | * terminated
|
---|
391 | * @return length of aStr's internal buffer
|
---|
392 | *
|
---|
393 | * @status FROZEN
|
---|
394 | */
|
---|
395 | NS_STRINGAPI(PRUint32)
|
---|
396 | NS_CStringGetData
|
---|
397 | (const nsACString &aStr, const char **aData,
|
---|
398 | PRBool *aTerminated = nsnull);
|
---|
399 |
|
---|
400 | /**
|
---|
401 | * NS_CStringCloneData
|
---|
402 | *
|
---|
403 | * This function returns a null-terminated copy of the string's
|
---|
404 | * internal buffer.
|
---|
405 | *
|
---|
406 | * @param aStr abstract string reference
|
---|
407 | * @return null-terminated copy of the string's internal buffer
|
---|
408 | * (it must be free'd using using nsMemory::Free)
|
---|
409 | *
|
---|
410 | * @status FROZEN
|
---|
411 | */
|
---|
412 | NS_STRINGAPI(char *)
|
---|
413 | NS_CStringCloneData
|
---|
414 | (const nsACString &aStr);
|
---|
415 |
|
---|
416 | /**
|
---|
417 | * NS_CStringSetData
|
---|
418 | *
|
---|
419 | * This function copies aData into aStr.
|
---|
420 | *
|
---|
421 | * @param aStr abstract string reference
|
---|
422 | * @param aData character buffer
|
---|
423 | * @param aDataLength number of characters to copy from source string (pass
|
---|
424 | * PR_UINT32_MAX to copy until end of aData, designated by
|
---|
425 | * a null character)
|
---|
426 | * @return NS_OK if function succeeded
|
---|
427 | *
|
---|
428 | * This function does not necessarily null-terminate aStr after copying data
|
---|
429 | * from aData. The behavior depends on the implementation of the abstract
|
---|
430 | * string, aStr. If aStr is a reference to a nsStringContainer, then its data
|
---|
431 | * will be null-terminated by this function.
|
---|
432 | *
|
---|
433 | * @status FROZEN
|
---|
434 | */
|
---|
435 | NS_STRINGAPI(nsresult)
|
---|
436 | NS_CStringSetData
|
---|
437 | (nsACString &aStr, const char *aData,
|
---|
438 | PRUint32 aDataLength = PR_UINT32_MAX);
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * NS_CStringSetDataRange
|
---|
442 | *
|
---|
443 | * This function copies aData into a section of aStr. As a result it can be
|
---|
444 | * used to insert new characters into the string.
|
---|
445 | *
|
---|
446 | * @param aStr abstract string reference
|
---|
447 | * @param aCutOffset starting index where the string's existing data
|
---|
448 | * is to be overwritten (pass PR_UINT32_MAX to cause
|
---|
449 | * aData to be appended to the end of aStr, in which
|
---|
450 | * case the value of aCutLength is ignored).
|
---|
451 | * @param aCutLength number of characters to overwrite starting at
|
---|
452 | * aCutOffset (pass PR_UINT32_MAX to overwrite until the
|
---|
453 | * end of aStr).
|
---|
454 | * @param aData character buffer (pass null to cause this function
|
---|
455 | * to simply remove the "cut" range)
|
---|
456 | * @param aDataLength number of characters to copy from source string (pass
|
---|
457 | * PR_UINT32_MAX to copy until end of aData, designated by
|
---|
458 | * a null character)
|
---|
459 | * @return NS_OK if function succeeded
|
---|
460 | *
|
---|
461 | * This function does not necessarily null-terminate aStr after copying data
|
---|
462 | * from aData. The behavior depends on the implementation of the abstract
|
---|
463 | * string, aStr. If aStr is a reference to a nsStringContainer, then its data
|
---|
464 | * will be null-terminated by this function.
|
---|
465 | *
|
---|
466 | * @status FROZEN
|
---|
467 | */
|
---|
468 | NS_STRINGAPI(nsresult)
|
---|
469 | NS_CStringSetDataRange
|
---|
470 | (nsACString &aStr, PRUint32 aCutOffset, PRUint32 aCutLength,
|
---|
471 | const char *aData, PRUint32 aDataLength = PR_UINT32_MAX);
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * NS_CStringCopy
|
---|
475 | *
|
---|
476 | * This function makes aDestStr have the same value as aSrcStr. It is
|
---|
477 | * provided as an optimization.
|
---|
478 | *
|
---|
479 | * @param aDestStr abstract string reference to be modified
|
---|
480 | * @param aSrcStr abstract string reference containing source string
|
---|
481 | * @return NS_OK if function succeeded
|
---|
482 | *
|
---|
483 | * This function does not necessarily null-terminate aDestStr after copying
|
---|
484 | * data from aSrcStr. The behavior depends on the implementation of the
|
---|
485 | * abstract string, aDestStr. If aDestStr is a reference to a
|
---|
486 | * nsStringContainer, then its data will be null-terminated by this function.
|
---|
487 | *
|
---|
488 | * @status FROZEN
|
---|
489 | */
|
---|
490 | NS_STRINGAPI(nsresult)
|
---|
491 | NS_CStringCopy
|
---|
492 | (nsACString &aDestStr, const nsACString &aSrcStr);
|
---|
493 |
|
---|
494 | /**
|
---|
495 | * NS_CStringAppendData
|
---|
496 | *
|
---|
497 | * This function appends data to the existing value of aStr.
|
---|
498 | *
|
---|
499 | * @param aStr abstract string reference to be modified
|
---|
500 | * @param aData character buffer
|
---|
501 | * @param aDataLength number of characters to append (pass PR_UINT32_MAX to
|
---|
502 | * append until a null-character is encountered)
|
---|
503 | * @return NS_OK if function succeeded
|
---|
504 | *
|
---|
505 | * This function does not necessarily null-terminate aStr upon completion.
|
---|
506 | * The behavior depends on the implementation of the abstract string, aStr.
|
---|
507 | * If aStr is a reference to a nsStringContainer, then its data will be null-
|
---|
508 | * terminated by this function.
|
---|
509 | */
|
---|
510 | inline NS_HIDDEN_(nsresult)
|
---|
511 | NS_CStringAppendData(nsACString &aStr, const char *aData,
|
---|
512 | PRUint32 aDataLength = PR_UINT32_MAX)
|
---|
513 | {
|
---|
514 | return NS_CStringSetDataRange(aStr, PR_UINT32_MAX, 0, aData, aDataLength);
|
---|
515 | }
|
---|
516 |
|
---|
517 | /**
|
---|
518 | * NS_CStringInsertData
|
---|
519 | *
|
---|
520 | * This function inserts data into the existing value of aStr at the specified
|
---|
521 | * offset.
|
---|
522 | *
|
---|
523 | * @param aStr abstract string reference to be modified
|
---|
524 | * @param aOffset specifies where in the string to insert aData
|
---|
525 | * @param aData character buffer
|
---|
526 | * @param aDataLength number of characters to append (pass PR_UINT32_MAX to
|
---|
527 | * append until a null-character is encountered)
|
---|
528 | * @return NS_OK if function succeeded
|
---|
529 | *
|
---|
530 | * This function does not necessarily null-terminate aStr upon completion.
|
---|
531 | * The behavior depends on the implementation of the abstract string, aStr.
|
---|
532 | * If aStr is a reference to a nsStringContainer, then its data will be null-
|
---|
533 | * terminated by this function.
|
---|
534 | */
|
---|
535 | inline NS_HIDDEN_(nsresult)
|
---|
536 | NS_CStringInsertData(nsACString &aStr, PRUint32 aOffset, const char *aData,
|
---|
537 | PRUint32 aDataLength = PR_UINT32_MAX)
|
---|
538 | {
|
---|
539 | return NS_CStringSetDataRange(aStr, aOffset, 0, aData, aDataLength);
|
---|
540 | }
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * NS_CStringCutData
|
---|
544 | *
|
---|
545 | * This function shortens the existing value of aStr, by removing characters
|
---|
546 | * at the specified offset.
|
---|
547 | *
|
---|
548 | * @param aStr abstract string reference to be modified
|
---|
549 | * @param aCutOffset specifies where in the string to insert aData
|
---|
550 | * @param aCutLength number of characters to remove
|
---|
551 | * @return NS_OK if function succeeded
|
---|
552 | */
|
---|
553 | inline NS_HIDDEN_(nsresult)
|
---|
554 | NS_CStringCutData(nsACString &aStr, PRUint32 aCutOffset, PRUint32 aCutLength)
|
---|
555 | {
|
---|
556 | return NS_CStringSetDataRange(aStr, aCutOffset, aCutLength, nsnull, 0);
|
---|
557 | }
|
---|
558 |
|
---|
559 | /* ------------------------------------------------------------------------- */
|
---|
560 |
|
---|
561 | /**
|
---|
562 | * Encodings that can be used with the following conversion routines.
|
---|
563 | */
|
---|
564 | enum nsCStringEncoding {
|
---|
565 | /* Conversion between ASCII and UTF-16 assumes that all bytes in the source
|
---|
566 | * string are 7-bit ASCII and can be inflated to UTF-16 by inserting null
|
---|
567 | * bytes. Reverse conversion is done by truncating every other byte. The
|
---|
568 | * conversion may result in loss and/or corruption of information if the
|
---|
569 | * strings do not strictly contain ASCII data. */
|
---|
570 | NS_CSTRING_ENCODING_ASCII = 0,
|
---|
571 |
|
---|
572 | /* Conversion between UTF-8 and UTF-16 is non-lossy. */
|
---|
573 | NS_CSTRING_ENCODING_UTF8 = 1,
|
---|
574 |
|
---|
575 | /* Conversion from UTF-16 to the native filesystem charset may result in a
|
---|
576 | * loss of information. No attempt is made to protect against data loss in
|
---|
577 | * this case. The native filesystem charset applies to strings passed to
|
---|
578 | * the "Native" method variants on nsIFile and nsILocalFile. */
|
---|
579 | NS_CSTRING_ENCODING_NATIVE_FILESYSTEM = 2
|
---|
580 | };
|
---|
581 |
|
---|
582 | /**
|
---|
583 | * NS_CStringToUTF16
|
---|
584 | *
|
---|
585 | * This function converts the characters in a nsACString to an array of UTF-16
|
---|
586 | * characters, in the platform endianness. The result is stored in a nsAString
|
---|
587 | * object.
|
---|
588 | *
|
---|
589 | * @param aSource abstract string reference containing source string
|
---|
590 | * @param aSrcEncoding character encoding of the source string
|
---|
591 | * @param aDest abstract string reference to hold the result
|
---|
592 | *
|
---|
593 | * @status FROZEN
|
---|
594 | */
|
---|
595 | NS_STRINGAPI(nsresult)
|
---|
596 | NS_CStringToUTF16(const nsACString &aSource, nsCStringEncoding aSrcEncoding,
|
---|
597 | nsAString &aDest);
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * NS_UTF16ToCString
|
---|
601 | *
|
---|
602 | * This function converts the UTF-16 characters in a nsAString to a single-byte
|
---|
603 | * encoding. The result is stored in a nsACString object. In some cases this
|
---|
604 | * conversion may be lossy. In such cases, the conversion may succeed with a
|
---|
605 | * return code indicating loss of information. The exact behavior is not
|
---|
606 | * specified at this time.
|
---|
607 | *
|
---|
608 | * @param aSource abstract string reference containing source string
|
---|
609 | * @param aDestEncoding character encoding of the resulting string
|
---|
610 | * @param aDest abstract string reference to hold the result
|
---|
611 | *
|
---|
612 | * @status FROZEN
|
---|
613 | */
|
---|
614 | NS_STRINGAPI(nsresult)
|
---|
615 | NS_UTF16ToCString(const nsAString &aSource, nsCStringEncoding aDestEncoding,
|
---|
616 | nsACString &aDest);
|
---|
617 |
|
---|
618 | /* ------------------------------------------------------------------------- */
|
---|
619 |
|
---|
620 | /**
|
---|
621 | * Below we define nsAString and nsACString. The "_external" suffix is an
|
---|
622 | * implementation detail. nsAString_external is the name of the external
|
---|
623 | * representation of nsAString from the point of view of the Mozilla codebase.
|
---|
624 | * To a user of this API, nsAString_external is exactly nsAString.
|
---|
625 | *
|
---|
626 | * These classes should be treated as abstract classes with unspecified
|
---|
627 | * structure. The inline methods are provided as helper functions around the
|
---|
628 | * C-style API provided above.
|
---|
629 | *
|
---|
630 | * Do not try to mix these definitions of nsAString and nsACString with the
|
---|
631 | * internal definition of these classes from nsAString.h in the Mozilla tree.
|
---|
632 | */
|
---|
633 |
|
---|
634 | #ifndef NS_STRINGAPI_IMPL
|
---|
635 | #define nsAString_external nsAString
|
---|
636 | #define nsACString_external nsACString
|
---|
637 | #endif
|
---|
638 |
|
---|
639 | class nsAString_external
|
---|
640 | {
|
---|
641 | #ifndef NS_STRINGAPI_IMPL
|
---|
642 |
|
---|
643 | public:
|
---|
644 | typedef PRUnichar char_type;
|
---|
645 | typedef nsAString_external self_type;
|
---|
646 | typedef PRUint32 size_type;
|
---|
647 | typedef PRUint32 index_type;
|
---|
648 |
|
---|
649 | NS_HIDDEN_(const char_type*) BeginReading() const
|
---|
650 | {
|
---|
651 | const char_type *data;
|
---|
652 | NS_StringGetData(*this, &data);
|
---|
653 | return data;
|
---|
654 | }
|
---|
655 |
|
---|
656 | NS_HIDDEN_(const char_type*) EndReading() const
|
---|
657 | {
|
---|
658 | const char_type *data;
|
---|
659 | PRUint32 len = NS_StringGetData(*this, &data);
|
---|
660 | return data + len;
|
---|
661 | }
|
---|
662 |
|
---|
663 | NS_HIDDEN_(size_type) Length() const
|
---|
664 | {
|
---|
665 | const char_type* data;
|
---|
666 | return NS_StringGetData(*this, &data);
|
---|
667 | }
|
---|
668 |
|
---|
669 | NS_HIDDEN_(void) Assign(const self_type& aString)
|
---|
670 | {
|
---|
671 | NS_StringCopy(*this, aString);
|
---|
672 | }
|
---|
673 | NS_HIDDEN_(void) Assign(const char_type* aData, size_type aLength = PR_UINT32_MAX)
|
---|
674 | {
|
---|
675 | NS_StringSetData(*this, aData, aLength);
|
---|
676 | }
|
---|
677 | NS_HIDDEN_(void) Assign(char_type aChar)
|
---|
678 | {
|
---|
679 | NS_StringSetData(*this, &aChar, 1);
|
---|
680 | }
|
---|
681 |
|
---|
682 | NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString); return *this; }
|
---|
683 | NS_HIDDEN_(self_type&) operator=(const char_type* aPtr) { Assign(aPtr); return *this; }
|
---|
684 | NS_HIDDEN_(self_type&) operator=(char_type aChar) { Assign(aChar); return *this; }
|
---|
685 |
|
---|
686 | NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) )
|
---|
687 | {
|
---|
688 | NS_StringSetDataRange(*this, cutStart, cutLength, data, length);
|
---|
689 | }
|
---|
690 | NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, char_type c )
|
---|
691 | {
|
---|
692 | Replace(cutStart, cutLength, &c, 1);
|
---|
693 | }
|
---|
694 | NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const self_type& readable )
|
---|
695 | {
|
---|
696 | const char_type* data;
|
---|
697 | PRUint32 dataLen = NS_StringGetData(readable, &data);
|
---|
698 | NS_StringSetDataRange(*this, cutStart, cutLength, data, dataLen);
|
---|
699 | }
|
---|
700 |
|
---|
701 | NS_HIDDEN_(void) Append( char_type c ) { Replace(size_type(-1), 0, c); }
|
---|
702 | NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) ) { Replace(size_type(-1), 0, data, length); }
|
---|
703 | NS_HIDDEN_(void) Append( const self_type& readable ) { Replace(size_type(-1), 0, readable); }
|
---|
704 |
|
---|
705 | NS_HIDDEN_(self_type&) operator+=( char_type c ) { Append(c); return *this; }
|
---|
706 | NS_HIDDEN_(self_type&) operator+=( const char_type* data ) { Append(data); return *this; }
|
---|
707 | NS_HIDDEN_(self_type&) operator+=( const self_type& readable ) { Append(readable); return *this; }
|
---|
708 |
|
---|
709 | NS_HIDDEN_(void) Insert( char_type c, index_type pos ) { Replace(pos, 0, c); }
|
---|
710 | NS_HIDDEN_(void) Insert( const char_type* data, index_type pos, size_type length = size_type(-1) ) { Replace(pos, 0, data, length); }
|
---|
711 | NS_HIDDEN_(void) Insert( const self_type& readable, index_type pos ) { Replace(pos, 0, readable); }
|
---|
712 |
|
---|
713 | NS_HIDDEN_(void) Cut( index_type cutStart, size_type cutLength ) { Replace(cutStart, cutLength, nsnull, 0); }
|
---|
714 |
|
---|
715 | #endif // NS_STRINGAPI_IMPL
|
---|
716 |
|
---|
717 | private:
|
---|
718 | void *v;
|
---|
719 | };
|
---|
720 |
|
---|
721 | class nsACString_external
|
---|
722 | {
|
---|
723 | #ifndef NS_STRINGAPI_IMPL
|
---|
724 |
|
---|
725 | public:
|
---|
726 | typedef char char_type;
|
---|
727 | typedef nsACString_external self_type;
|
---|
728 | typedef PRUint32 size_type;
|
---|
729 | typedef PRUint32 index_type;
|
---|
730 |
|
---|
731 | NS_HIDDEN_(const char_type*) BeginReading() const
|
---|
732 | {
|
---|
733 | const char_type *data;
|
---|
734 | NS_CStringGetData(*this, &data);
|
---|
735 | return data;
|
---|
736 | }
|
---|
737 |
|
---|
738 | NS_HIDDEN_(const char_type*) EndReading() const
|
---|
739 | {
|
---|
740 | const char_type *data;
|
---|
741 | PRUint32 len = NS_CStringGetData(*this, &data);
|
---|
742 | return data + len;
|
---|
743 | }
|
---|
744 |
|
---|
745 | NS_HIDDEN_(size_type) Length() const
|
---|
746 | {
|
---|
747 | const char_type* data;
|
---|
748 | return NS_CStringGetData(*this, &data);
|
---|
749 | }
|
---|
750 |
|
---|
751 | NS_HIDDEN_(void) Assign(const self_type& aString)
|
---|
752 | {
|
---|
753 | NS_CStringCopy(*this, aString);
|
---|
754 | }
|
---|
755 | NS_HIDDEN_(void) Assign(const char_type* aData, size_type aLength = PR_UINT32_MAX)
|
---|
756 | {
|
---|
757 | NS_CStringSetData(*this, aData, aLength);
|
---|
758 | }
|
---|
759 | NS_HIDDEN_(void) Assign(char_type aChar)
|
---|
760 | {
|
---|
761 | NS_CStringSetData(*this, &aChar, 1);
|
---|
762 | }
|
---|
763 |
|
---|
764 | NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString); return *this; }
|
---|
765 | NS_HIDDEN_(self_type&) operator=(const char_type* aPtr) { Assign(aPtr); return *this; }
|
---|
766 | NS_HIDDEN_(self_type&) operator=(char_type aChar) { Assign(aChar); return *this; }
|
---|
767 |
|
---|
768 | NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) )
|
---|
769 | {
|
---|
770 | NS_CStringSetDataRange(*this, cutStart, cutLength, data, length);
|
---|
771 | }
|
---|
772 | NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, char_type c )
|
---|
773 | {
|
---|
774 | Replace(cutStart, cutLength, &c, 1);
|
---|
775 | }
|
---|
776 | NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const self_type& readable )
|
---|
777 | {
|
---|
778 | const char_type* data;
|
---|
779 | PRUint32 dataLen = NS_CStringGetData(readable, &data);
|
---|
780 | NS_CStringSetDataRange(*this, cutStart, cutLength, data, dataLen);
|
---|
781 | }
|
---|
782 |
|
---|
783 | NS_HIDDEN_(void) Append( char_type c ) { Replace(size_type(-1), 0, c); }
|
---|
784 | NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) ) { Replace(size_type(-1), 0, data, length); }
|
---|
785 | NS_HIDDEN_(void) Append( const self_type& readable ) { Replace(size_type(-1), 0, readable); }
|
---|
786 |
|
---|
787 | NS_HIDDEN_(self_type&) operator+=( char_type c ) { Append(c); return *this; }
|
---|
788 | NS_HIDDEN_(self_type&) operator+=( const char_type* data ) { Append(data); return *this; }
|
---|
789 | NS_HIDDEN_(self_type&) operator+=( const self_type& readable ) { Append(readable); return *this; }
|
---|
790 |
|
---|
791 | NS_HIDDEN_(void) Insert( char_type c, index_type pos ) { Replace(pos, 0, c); }
|
---|
792 | NS_HIDDEN_(void) Insert( const char_type* data, index_type pos, size_type length = size_type(-1) ) { Replace(pos, 0, data, length); }
|
---|
793 | NS_HIDDEN_(void) Insert( const self_type& readable, index_type pos ) { Replace(pos, 0, readable); }
|
---|
794 |
|
---|
795 | NS_HIDDEN_(void) Cut( index_type cutStart, size_type cutLength ) { Replace(cutStart, cutLength, nsnull, 0); }
|
---|
796 |
|
---|
797 | #endif // NS_STRINGAPI_IMPL
|
---|
798 |
|
---|
799 | private:
|
---|
800 | void *v;
|
---|
801 | };
|
---|
802 |
|
---|
803 | /* ------------------------------------------------------------------------- */
|
---|
804 |
|
---|
805 | /**
|
---|
806 | * Below we define nsStringContainer and nsCStringContainer. These classes
|
---|
807 | * have unspecified structure. In most cases, your code should use
|
---|
808 | * nsEmbedString instead of these classes; however, if you prefer C-style
|
---|
809 | * programming, then look no further...
|
---|
810 | */
|
---|
811 |
|
---|
812 | class nsStringContainer : public nsAString_external
|
---|
813 | {
|
---|
814 | private:
|
---|
815 | void *d1;
|
---|
816 | PRUint32 d2;
|
---|
817 | void *d3;
|
---|
818 |
|
---|
819 | public:
|
---|
820 | nsStringContainer() {} // MSVC6 needs this
|
---|
821 | };
|
---|
822 |
|
---|
823 | class nsCStringContainer : public nsACString_external
|
---|
824 | {
|
---|
825 | private:
|
---|
826 | void *d1;
|
---|
827 | PRUint32 d2;
|
---|
828 | void *d3;
|
---|
829 |
|
---|
830 | public:
|
---|
831 | nsCStringContainer() {} // MSVC6 needs this
|
---|
832 | };
|
---|
833 |
|
---|
834 | #endif // nsStringAPI_h__
|
---|