Bug 452317 - FeedConverter.js: QueryInterface should throw NS_ERROR_NO_INTERFACE...
[wine-gecko.git] / xpcom / string / public / nsXPCOMStrings.h
blob203a5b243c8b3e659c09246d9f98d87afd083f75
1 /* vim:set ts=2 sw=2 et cindent: */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
15 * The Original Code is Mozilla.
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.
21 * Contributor(s):
22 * Darin Fisher <darin@meer.net>
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.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsXPCOMStrings_h__
39 #define nsXPCOMStrings_h__
41 #include <string.h>
42 #include "nscore.h"
44 /**
45 * nsXPCOMStrings.h
47 * This file describes a minimal API for working with XPCOM's abstract
48 * string classes. It divorces the consumer from having any run-time
49 * dependency on the implementation details of the abstract string types.
52 // Map frozen functions to private symbol names if not using strict API.
53 #ifdef MOZILLA_INTERNAL_API
54 # define NS_StringContainerInit NS_StringContainerInit_P
55 # define NS_StringContainerInit2 NS_StringContainerInit2_P
56 # define NS_StringContainerFinish NS_StringContainerFinish_P
57 # define NS_StringGetData NS_StringGetData_P
58 # define NS_StringGetMutableData NS_StringGetMutableData_P
59 # define NS_StringCloneData NS_StringCloneData_P
60 # define NS_StringSetData NS_StringSetData_P
61 # define NS_StringSetDataRange NS_StringSetDataRange_P
62 # define NS_StringCopy NS_StringCopy_P
63 # define NS_StringSetIsVoid NS_StringSetIsVoid_P
64 # define NS_StringGetIsVoid NS_StringGetIsVoid_P
65 # define NS_CStringContainerInit NS_CStringContainerInit_P
66 # define NS_CStringContainerInit2 NS_CStringContainerInit2_P
67 # define NS_CStringContainerFinish NS_CStringContainerFinish_P
68 # define NS_CStringGetData NS_CStringGetData_P
69 # define NS_CStringGetMutableData NS_CStringGetMutableData_P
70 # define NS_CStringCloneData NS_CStringCloneData_P
71 # define NS_CStringSetData NS_CStringSetData_P
72 # define NS_CStringSetDataRange NS_CStringSetDataRange_P
73 # define NS_CStringCopy NS_CStringCopy_P
74 # define NS_CStringSetIsVoid NS_CStringSetIsVoid_P
75 # define NS_CStringGetIsVoid NS_CStringGetIsVoid_P
76 # define NS_CStringToUTF16 NS_CStringToUTF16_P
77 # define NS_UTF16ToCString NS_UTF16ToCString_P
78 #endif
80 #include "nscore.h"
82 /* The base string types */
83 class nsAString;
84 class nsACString;
86 /* ------------------------------------------------------------------------- */
88 /**
89 * nsStringContainer
91 * This is an opaque data type that is large enough to hold the canonical
92 * implementation of nsAString. The binary structure of this class is an
93 * implementation detail.
95 * The string data stored in a string container is always single fragment
96 * and may be null-terminated depending on how it is initialized.
98 * Typically, string containers are allocated on the stack for temporary
99 * use. However, they can also be malloc'd if necessary. In either case,
100 * a string container is not useful until it has been initialized with a
101 * call to NS_StringContainerInit. The following example shows how to use
102 * a string container to call a function that takes a |nsAString &| out-param.
104 * NS_METHOD GetBlah(nsAString &aBlah);
106 * nsresult MyCode()
108 * nsresult rv;
110 * nsStringContainer sc;
111 * rv = NS_StringContainerInit(sc);
112 * if (NS_FAILED(rv))
113 * return rv;
115 * rv = GetBlah(sc);
116 * if (NS_SUCCEEDED(rv))
118 * const PRUnichar *data;
119 * NS_StringGetData(sc, &data);
120 * //
121 * // |data| now points to the result of the GetBlah function
122 * //
125 * NS_StringContainerFinish(sc);
126 * return rv;
129 * The following example show how to use a string container to pass a string
130 * parameter to a function taking a |const nsAString &| in-param.
132 * NS_METHOD SetBlah(const nsAString &aBlah);
134 * nsresult MyCode()
136 * nsresult rv;
138 * nsStringContainer sc;
139 * rv = NS_StringContainerInit(sc);
140 * if (NS_FAILED(rv))
141 * return rv;
143 * const PRUnichar kData[] = {'x','y','z','\0'};
144 * rv = NS_StringSetData(sc, kData, sizeof(kData)/2 - 1);
145 * if (NS_SUCCEEDED(rv))
146 * rv = SetBlah(sc);
148 * NS_StringContainerFinish(sc);
149 * return rv;
152 class nsStringContainer;
154 struct nsStringContainer_base
156 private:
157 void *d1;
158 PRUint32 d2;
159 PRUint32 d3;
163 * Flags that may be OR'd together to pass to NS_StringContainerInit2:
165 enum {
166 /* Data passed into NS_StringContainerInit2 is not copied; instead, the
167 * string references the passed in data pointer directly. The caller must
168 * ensure that the data is valid for the lifetime of the string container.
169 * This flag should not be combined with NS_STRING_CONTAINER_INIT_ADOPT. */
170 NS_STRING_CONTAINER_INIT_DEPEND = (1 << 1),
172 /* Data passed into NS_StringContainerInit2 is not copied; instead, the
173 * string takes ownership over the data pointer. The caller must have
174 * allocated the data array using the XPCOM memory allocator (nsMemory).
175 * This flag should not be combined with NS_STRING_CONTAINER_INIT_DEPEND. */
176 NS_STRING_CONTAINER_INIT_ADOPT = (1 << 2),
178 /* Data passed into NS_StringContainerInit2 is a substring that is not
179 * null-terminated. */
180 NS_STRING_CONTAINER_INIT_SUBSTRING = (1 << 3)
184 * NS_StringContainerInit
186 * @param aContainer string container reference
187 * @return NS_OK if string container successfully initialized
189 * This function may allocate additional memory for aContainer. When
190 * aContainer is no longer needed, NS_StringContainerFinish should be called.
192 * @status FROZEN
194 XPCOM_API(nsresult)
195 NS_StringContainerInit(nsStringContainer &aContainer);
198 * NS_StringContainerInit2
200 * @param aContainer string container reference
201 * @param aData character buffer (may be null)
202 * @param aDataLength number of characters stored at aData (may pass
203 * PR_UINT32_MAX if aData is null-terminated)
204 * @param aFlags flags affecting how the string container is
205 * initialized. this parameter is ignored when aData
206 * is null. otherwise, if this parameter is 0, then
207 * aData is copied into the string.
209 * This function resembles NS_StringContainerInit but provides further
210 * options that permit more efficient memory usage. When aContainer is
211 * no longer needed, NS_StringContainerFinish should be called.
213 * NOTE: NS_StringContainerInit2(container, nsnull, 0, 0) is equivalent to
214 * NS_StringContainerInit(container).
216 * @status FROZEN
218 XPCOM_API(nsresult)
219 NS_StringContainerInit2
220 (nsStringContainer &aContainer, const PRUnichar *aData = nsnull,
221 PRUint32 aDataLength = PR_UINT32_MAX, PRUint32 aFlags = 0);
224 * NS_StringContainerFinish
226 * @param aContainer string container reference
228 * This function frees any memory owned by aContainer.
230 * @status FROZEN
232 XPCOM_API(void)
233 NS_StringContainerFinish(nsStringContainer &aContainer);
235 /* ------------------------------------------------------------------------- */
238 * NS_StringGetData
240 * This function returns a const character pointer to the string's internal
241 * buffer, the length of the string, and a boolean value indicating whether
242 * or not the buffer is null-terminated.
244 * @param aStr abstract string reference
245 * @param aData out param that will hold the address of aStr's
246 * internal buffer
247 * @param aTerminated if non-null, this out param will be set to indicate
248 * whether or not aStr's internal buffer is null-
249 * terminated
250 * @return length of aStr's internal buffer
252 * @status FROZEN
254 XPCOM_API(PRUint32)
255 NS_StringGetData
256 (const nsAString &aStr, const PRUnichar **aData,
257 PRBool *aTerminated = nsnull);
260 * NS_StringGetMutableData
262 * This function provides mutable access to a string's internal buffer. It
263 * returns a pointer to an array of characters that may be modified. The
264 * returned pointer remains valid until the string object is passed to some
265 * other string function.
267 * Optionally, this function may be used to resize the string's internal
268 * buffer. The aDataLength parameter specifies the requested length of the
269 * string's internal buffer. By passing some value other than PR_UINT32_MAX,
270 * the caller can request that the buffer be resized to the specified number of
271 * characters before returning. The caller is not responsible for writing a
272 * null-terminator.
274 * @param aStr abstract string reference
275 * @param aDataLength number of characters to resize the string's internal
276 * buffer to or PR_UINT32_MAX if no resizing is needed
277 * @param aData out param that upon return holds the address of aStr's
278 * internal buffer or null if the function failed
279 * @return number of characters or zero if the function failed
281 * This function does not necessarily null-terminate aStr after resizing its
282 * internal buffer. The behavior depends on the implementation of the abstract
283 * string, aStr. If aStr is a reference to a nsStringContainer, then its data
284 * will be null-terminated by this function.
286 * @status FROZEN
288 XPCOM_API(PRUint32)
289 NS_StringGetMutableData
290 (nsAString &aStr, PRUint32 aDataLength, PRUnichar **aData);
293 * NS_StringCloneData
295 * This function returns a null-terminated copy of the string's
296 * internal buffer.
298 * @param aStr abstract string reference
299 * @return null-terminated copy of the string's internal buffer
300 * (it must be free'd using using nsMemory::Free)
302 * @status FROZEN
304 XPCOM_API(PRUnichar *)
305 NS_StringCloneData
306 (const nsAString &aStr);
309 * NS_StringSetData
311 * This function copies aData into aStr.
313 * @param aStr abstract string reference
314 * @param aData character buffer
315 * @param aDataLength number of characters to copy from source string (pass
316 * PR_UINT32_MAX to copy until end of aData, designated by
317 * a null character)
318 * @return NS_OK if function succeeded
320 * This function does not necessarily null-terminate aStr after copying data
321 * from aData. The behavior depends on the implementation of the abstract
322 * string, aStr. If aStr is a reference to a nsStringContainer, then its data
323 * will be null-terminated by this function.
325 * @status FROZEN
327 XPCOM_API(nsresult)
328 NS_StringSetData
329 (nsAString &aStr, const PRUnichar *aData,
330 PRUint32 aDataLength = PR_UINT32_MAX);
333 * NS_StringSetDataRange
335 * This function copies aData into a section of aStr. As a result it can be
336 * used to insert new characters into the string.
338 * @param aStr abstract string reference
339 * @param aCutOffset starting index where the string's existing data
340 * is to be overwritten (pass PR_UINT32_MAX to cause
341 * aData to be appended to the end of aStr, in which
342 * case the value of aCutLength is ignored).
343 * @param aCutLength number of characters to overwrite starting at
344 * aCutOffset (pass PR_UINT32_MAX to overwrite until the
345 * end of aStr).
346 * @param aData character buffer (pass null to cause this function
347 * to simply remove the "cut" range)
348 * @param aDataLength number of characters to copy from source string (pass
349 * PR_UINT32_MAX to copy until end of aData, designated by
350 * a null character)
351 * @return NS_OK if function succeeded
353 * This function does not necessarily null-terminate aStr after copying data
354 * from aData. The behavior depends on the implementation of the abstract
355 * string, aStr. If aStr is a reference to a nsStringContainer, then its data
356 * will be null-terminated by this function.
358 * @status FROZEN
360 XPCOM_API(nsresult)
361 NS_StringSetDataRange
362 (nsAString &aStr, PRUint32 aCutOffset, PRUint32 aCutLength,
363 const PRUnichar *aData, PRUint32 aDataLength = PR_UINT32_MAX);
366 * NS_StringCopy
368 * This function makes aDestStr have the same value as aSrcStr. It is
369 * provided as an optimization.
371 * @param aDestStr abstract string reference to be modified
372 * @param aSrcStr abstract string reference containing source string
373 * @return NS_OK if function succeeded
375 * This function does not necessarily null-terminate aDestStr after copying
376 * data from aSrcStr. The behavior depends on the implementation of the
377 * abstract string, aDestStr. If aDestStr is a reference to a
378 * nsStringContainer, then its data will be null-terminated by this function.
380 * @status FROZEN
382 XPCOM_API(nsresult)
383 NS_StringCopy
384 (nsAString &aDestStr, const nsAString &aSrcStr);
387 * NS_StringAppendData
389 * This function appends data to the existing value of aStr.
391 * @param aStr abstract string reference to be modified
392 * @param aData character buffer
393 * @param aDataLength number of characters to append (pass PR_UINT32_MAX to
394 * append until a null-character is encountered)
395 * @return NS_OK if function succeeded
397 * This function does not necessarily null-terminate aStr upon completion.
398 * The behavior depends on the implementation of the abstract string, aStr.
399 * If aStr is a reference to a nsStringContainer, then its data will be null-
400 * terminated by this function.
402 inline NS_HIDDEN_(nsresult)
403 NS_StringAppendData(nsAString &aStr, const PRUnichar *aData,
404 PRUint32 aDataLength = PR_UINT32_MAX)
406 return NS_StringSetDataRange(aStr, PR_UINT32_MAX, 0, aData, aDataLength);
410 * NS_StringInsertData
412 * This function inserts data into the existing value of aStr at the specified
413 * offset.
415 * @param aStr abstract string reference to be modified
416 * @param aOffset specifies where in the string to insert aData
417 * @param aData character buffer
418 * @param aDataLength number of characters to append (pass PR_UINT32_MAX to
419 * append until a null-character is encountered)
420 * @return NS_OK if function succeeded
422 * This function does not necessarily null-terminate aStr upon completion.
423 * The behavior depends on the implementation of the abstract string, aStr.
424 * If aStr is a reference to a nsStringContainer, then its data will be null-
425 * terminated by this function.
427 inline NS_HIDDEN_(nsresult)
428 NS_StringInsertData(nsAString &aStr, PRUint32 aOffset, const PRUnichar *aData,
429 PRUint32 aDataLength = PR_UINT32_MAX)
431 return NS_StringSetDataRange(aStr, aOffset, 0, aData, aDataLength);
435 * NS_StringCutData
437 * This function shortens the existing value of aStr, by removing characters
438 * at the specified offset.
440 * @param aStr abstract string reference to be modified
441 * @param aCutOffset specifies where in the string to insert aData
442 * @param aCutLength number of characters to remove
443 * @return NS_OK if function succeeded
445 inline NS_HIDDEN_(nsresult)
446 NS_StringCutData(nsAString &aStr, PRUint32 aCutOffset, PRUint32 aCutLength)
448 return NS_StringSetDataRange(aStr, aCutOffset, aCutLength, nsnull, 0);
452 * NS_StringSetIsVoid
454 * This function marks a string as being a "void string". Any data in the
455 * string will be lost.
457 XPCOM_API(void)
458 NS_StringSetIsVoid(nsAString& aStr, const PRBool aIsVoid);
461 * NS_StringGetIsVoid
463 * This function provides a way to test if a string is a "void string", as
464 * marked by NS_StringSetIsVoid.
466 XPCOM_API(PRBool)
467 NS_StringGetIsVoid(const nsAString& aStr);
469 /* ------------------------------------------------------------------------- */
472 * nsCStringContainer
474 * This is an opaque data type that is large enough to hold the canonical
475 * implementation of nsACString. The binary structure of this class is an
476 * implementation detail.
478 * The string data stored in a string container is always single fragment
479 * and may be null-terminated depending on how it is initialized.
481 * @see nsStringContainer for use cases and further documentation.
483 class nsCStringContainer;
486 * Flags that may be OR'd together to pass to NS_StringContainerInit2:
488 enum {
489 /* Data passed into NS_CStringContainerInit2 is not copied; instead, the
490 * string references the passed in data pointer directly. The caller must
491 * ensure that the data is valid for the lifetime of the string container.
492 * This flag should not be combined with NS_CSTRING_CONTAINER_INIT_ADOPT. */
493 NS_CSTRING_CONTAINER_INIT_DEPEND = (1 << 1),
495 /* Data passed into NS_CStringContainerInit2 is not copied; instead, the
496 * string takes ownership over the data pointer. The caller must have
497 * allocated the data array using the XPCOM memory allocator (nsMemory).
498 * This flag should not be combined with NS_CSTRING_CONTAINER_INIT_DEPEND. */
499 NS_CSTRING_CONTAINER_INIT_ADOPT = (1 << 2),
501 /* Data passed into NS_CStringContainerInit2 is a substring that is not
502 * null-terminated. */
503 NS_CSTRING_CONTAINER_INIT_SUBSTRING = (1 << 3)
507 * NS_CStringContainerInit
509 * @param aContainer string container reference
510 * @return NS_OK if string container successfully initialized
512 * This function may allocate additional memory for aContainer. When
513 * aContainer is no longer needed, NS_CStringContainerFinish should be called.
515 * @status FROZEN
517 XPCOM_API(nsresult)
518 NS_CStringContainerInit(nsCStringContainer &aContainer);
521 * NS_CStringContainerInit2
523 * @param aContainer string container reference
524 * @param aData character buffer (may be null)
525 * @param aDataLength number of characters stored at aData (may pass
526 * PR_UINT32_MAX if aData is null-terminated)
527 * @param aFlags flags affecting how the string container is
528 * initialized. this parameter is ignored when aData
529 * is null. otherwise, if this parameter is 0, then
530 * aData is copied into the string.
532 * This function resembles NS_CStringContainerInit but provides further
533 * options that permit more efficient memory usage. When aContainer is
534 * no longer needed, NS_CStringContainerFinish should be called.
536 * NOTE: NS_CStringContainerInit2(container, nsnull, 0, 0) is equivalent to
537 * NS_CStringContainerInit(container).
539 * @status FROZEN
541 XPCOM_API(nsresult)
542 NS_CStringContainerInit2
543 (nsCStringContainer &aContainer, const char *aData = nsnull,
544 PRUint32 aDataLength = PR_UINT32_MAX, PRUint32 aFlags = 0);
547 * NS_CStringContainerFinish
549 * @param aContainer string container reference
551 * This function frees any memory owned by aContainer.
553 * @status FROZEN
555 XPCOM_API(void)
556 NS_CStringContainerFinish(nsCStringContainer &aContainer);
558 /* ------------------------------------------------------------------------- */
561 * NS_CStringGetData
563 * This function returns a const character pointer to the string's internal
564 * buffer, the length of the string, and a boolean value indicating whether
565 * or not the buffer is null-terminated.
567 * @param aStr abstract string reference
568 * @param aData out param that will hold the address of aStr's
569 * internal buffer
570 * @param aTerminated if non-null, this out param will be set to indicate
571 * whether or not aStr's internal buffer is null-
572 * terminated
573 * @return length of aStr's internal buffer
575 * @status FROZEN
577 XPCOM_API(PRUint32)
578 NS_CStringGetData
579 (const nsACString &aStr, const char **aData,
580 PRBool *aTerminated = nsnull);
583 * NS_CStringGetMutableData
585 * This function provides mutable access to a string's internal buffer. It
586 * returns a pointer to an array of characters that may be modified. The
587 * returned pointer remains valid until the string object is passed to some
588 * other string function.
590 * Optionally, this function may be used to resize the string's internal
591 * buffer. The aDataLength parameter specifies the requested length of the
592 * string's internal buffer. By passing some value other than PR_UINT32_MAX,
593 * the caller can request that the buffer be resized to the specified number of
594 * characters before returning. The caller is not responsible for writing a
595 * null-terminator.
597 * @param aStr abstract string reference
598 * @param aDataLength number of characters to resize the string's internal
599 * buffer to or PR_UINT32_MAX if no resizing is needed
600 * @param aData out param that upon return holds the address of aStr's
601 * internal buffer or null if the function failed
602 * @return number of characters or zero if the function failed
604 * This function does not necessarily null-terminate aStr after resizing its
605 * internal buffer. The behavior depends on the implementation of the abstract
606 * string, aStr. If aStr is a reference to a nsStringContainer, then its data
607 * will be null-terminated by this function.
609 * @status FROZEN
611 XPCOM_API(PRUint32)
612 NS_CStringGetMutableData
613 (nsACString &aStr, PRUint32 aDataLength, char **aData);
616 * NS_CStringCloneData
618 * This function returns a null-terminated copy of the string's
619 * internal buffer.
621 * @param aStr abstract string reference
622 * @return null-terminated copy of the string's internal buffer
623 * (it must be free'd using using nsMemory::Free)
625 * @status FROZEN
627 XPCOM_API(char *)
628 NS_CStringCloneData
629 (const nsACString &aStr);
632 * NS_CStringSetData
634 * This function copies aData into aStr.
636 * @param aStr abstract string reference
637 * @param aData character buffer
638 * @param aDataLength number of characters to copy from source string (pass
639 * PR_UINT32_MAX to copy until end of aData, designated by
640 * a null character)
641 * @return NS_OK if function succeeded
643 * This function does not necessarily null-terminate aStr after copying data
644 * from aData. The behavior depends on the implementation of the abstract
645 * string, aStr. If aStr is a reference to a nsStringContainer, then its data
646 * will be null-terminated by this function.
648 * @status FROZEN
650 XPCOM_API(nsresult)
651 NS_CStringSetData
652 (nsACString &aStr, const char *aData,
653 PRUint32 aDataLength = PR_UINT32_MAX);
656 * NS_CStringSetDataRange
658 * This function copies aData into a section of aStr. As a result it can be
659 * used to insert new characters into the string.
661 * @param aStr abstract string reference
662 * @param aCutOffset starting index where the string's existing data
663 * is to be overwritten (pass PR_UINT32_MAX to cause
664 * aData to be appended to the end of aStr, in which
665 * case the value of aCutLength is ignored).
666 * @param aCutLength number of characters to overwrite starting at
667 * aCutOffset (pass PR_UINT32_MAX to overwrite until the
668 * end of aStr).
669 * @param aData character buffer (pass null to cause this function
670 * to simply remove the "cut" range)
671 * @param aDataLength number of characters to copy from source string (pass
672 * PR_UINT32_MAX to copy until end of aData, designated by
673 * a null character)
674 * @return NS_OK if function succeeded
676 * This function does not necessarily null-terminate aStr after copying data
677 * from aData. The behavior depends on the implementation of the abstract
678 * string, aStr. If aStr is a reference to a nsStringContainer, then its data
679 * will be null-terminated by this function.
681 * @status FROZEN
683 XPCOM_API(nsresult)
684 NS_CStringSetDataRange
685 (nsACString &aStr, PRUint32 aCutOffset, PRUint32 aCutLength,
686 const char *aData, PRUint32 aDataLength = PR_UINT32_MAX);
689 * NS_CStringCopy
691 * This function makes aDestStr have the same value as aSrcStr. It is
692 * provided as an optimization.
694 * @param aDestStr abstract string reference to be modified
695 * @param aSrcStr abstract string reference containing source string
696 * @return NS_OK if function succeeded
698 * This function does not necessarily null-terminate aDestStr after copying
699 * data from aSrcStr. The behavior depends on the implementation of the
700 * abstract string, aDestStr. If aDestStr is a reference to a
701 * nsStringContainer, then its data will be null-terminated by this function.
703 * @status FROZEN
705 XPCOM_API(nsresult)
706 NS_CStringCopy
707 (nsACString &aDestStr, const nsACString &aSrcStr);
710 * NS_CStringAppendData
712 * This function appends data to the existing value of aStr.
714 * @param aStr abstract string reference to be modified
715 * @param aData character buffer
716 * @param aDataLength number of characters to append (pass PR_UINT32_MAX to
717 * append until a null-character is encountered)
718 * @return NS_OK if function succeeded
720 * This function does not necessarily null-terminate aStr upon completion.
721 * The behavior depends on the implementation of the abstract string, aStr.
722 * If aStr is a reference to a nsStringContainer, then its data will be null-
723 * terminated by this function.
725 inline NS_HIDDEN_(nsresult)
726 NS_CStringAppendData(nsACString &aStr, const char *aData,
727 PRUint32 aDataLength = PR_UINT32_MAX)
729 return NS_CStringSetDataRange(aStr, PR_UINT32_MAX, 0, aData, aDataLength);
733 * NS_CStringInsertData
735 * This function inserts data into the existing value of aStr at the specified
736 * offset.
738 * @param aStr abstract string reference to be modified
739 * @param aOffset specifies where in the string to insert aData
740 * @param aData character buffer
741 * @param aDataLength number of characters to append (pass PR_UINT32_MAX to
742 * append until a null-character is encountered)
743 * @return NS_OK if function succeeded
745 * This function does not necessarily null-terminate aStr upon completion.
746 * The behavior depends on the implementation of the abstract string, aStr.
747 * If aStr is a reference to a nsStringContainer, then its data will be null-
748 * terminated by this function.
750 inline NS_HIDDEN_(nsresult)
751 NS_CStringInsertData(nsACString &aStr, PRUint32 aOffset, const char *aData,
752 PRUint32 aDataLength = PR_UINT32_MAX)
754 return NS_CStringSetDataRange(aStr, aOffset, 0, aData, aDataLength);
758 * NS_CStringCutData
760 * This function shortens the existing value of aStr, by removing characters
761 * at the specified offset.
763 * @param aStr abstract string reference to be modified
764 * @param aCutOffset specifies where in the string to insert aData
765 * @param aCutLength number of characters to remove
766 * @return NS_OK if function succeeded
768 inline NS_HIDDEN_(nsresult)
769 NS_CStringCutData(nsACString &aStr, PRUint32 aCutOffset, PRUint32 aCutLength)
771 return NS_CStringSetDataRange(aStr, aCutOffset, aCutLength, nsnull, 0);
775 * NS_CStringSetIsVoid
777 * This function marks a string as being a "void string". Any data in the
778 * string will be lost.
780 XPCOM_API(void)
781 NS_CStringSetIsVoid(nsACString& aStr, const PRBool aIsVoid);
784 * NS_CStringGetIsVoid
786 * This function provides a way to test if a string is a "void string", as
787 * marked by NS_CStringSetIsVoid.
789 XPCOM_API(PRBool)
790 NS_CStringGetIsVoid(const nsACString& aStr);
792 /* ------------------------------------------------------------------------- */
795 * Encodings that can be used with the following conversion routines.
797 enum nsCStringEncoding {
798 /* Conversion between ASCII and UTF-16 assumes that all bytes in the source
799 * string are 7-bit ASCII and can be inflated to UTF-16 by inserting null
800 * bytes. Reverse conversion is done by truncating every other byte. The
801 * conversion may result in loss and/or corruption of information if the
802 * strings do not strictly contain ASCII data. */
803 NS_CSTRING_ENCODING_ASCII = 0,
805 /* Conversion between UTF-8 and UTF-16 is non-lossy. */
806 NS_CSTRING_ENCODING_UTF8 = 1,
808 /* Conversion from UTF-16 to the native filesystem charset may result in a
809 * loss of information. No attempt is made to protect against data loss in
810 * this case. The native filesystem charset applies to strings passed to
811 * the "Native" method variants on nsIFile and nsILocalFile. */
812 NS_CSTRING_ENCODING_NATIVE_FILESYSTEM = 2
816 * NS_CStringToUTF16
818 * This function converts the characters in a nsACString to an array of UTF-16
819 * characters, in the platform endianness. The result is stored in a nsAString
820 * object.
822 * @param aSource abstract string reference containing source string
823 * @param aSrcEncoding character encoding of the source string
824 * @param aDest abstract string reference to hold the result
826 * @status FROZEN
828 XPCOM_API(nsresult)
829 NS_CStringToUTF16(const nsACString &aSource, nsCStringEncoding aSrcEncoding,
830 nsAString &aDest);
833 * NS_UTF16ToCString
835 * This function converts the UTF-16 characters in a nsAString to a single-byte
836 * encoding. The result is stored in a nsACString object. In some cases this
837 * conversion may be lossy. In such cases, the conversion may succeed with a
838 * return code indicating loss of information. The exact behavior is not
839 * specified at this time.
841 * @param aSource abstract string reference containing source string
842 * @param aDestEncoding character encoding of the resulting string
843 * @param aDest abstract string reference to hold the result
845 * @status FROZEN
847 XPCOM_API(nsresult)
848 NS_UTF16ToCString(const nsAString &aSource, nsCStringEncoding aDestEncoding,
849 nsACString &aDest);
851 #endif // nsXPCOMStrings_h__