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
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.
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 ***** */
39 #include "nsCharTraits.h"
41 #include "nsXPCOMStrings.h"
42 #include "nsNativeCharsetUtils.h"
44 /* ------------------------------------------------------------------------- */
47 NS_StringContainerInit(nsStringContainer
&aContainer
)
49 NS_ASSERTION(sizeof(nsStringContainer_base
) >= sizeof(nsString
),
50 "nsStringContainer is not large enough");
52 // use placement new to avoid heap allocating nsString object
53 new (&aContainer
) nsString();
59 NS_StringContainerInit2(nsStringContainer
&aContainer
,
60 const PRUnichar
*aData
,
64 NS_ASSERTION(sizeof(nsStringContainer_base
) >= sizeof(nsString
),
65 "nsStringContainer is not large enough");
69 new (&aContainer
) nsString();
73 if (aDataLength
== PR_UINT32_MAX
)
75 NS_ENSURE_ARG(!(aFlags
& NS_STRING_CONTAINER_INIT_SUBSTRING
));
76 aDataLength
= nsCharTraits
<PRUnichar
>::length(aData
);
79 if (aFlags
& (NS_STRING_CONTAINER_INIT_DEPEND
|
80 NS_STRING_CONTAINER_INIT_ADOPT
))
83 if (aFlags
& NS_STRING_CONTAINER_INIT_SUBSTRING
)
84 flags
= nsSubstring::F_NONE
;
86 flags
= nsSubstring::F_TERMINATED
;
88 if (aFlags
& NS_STRING_CONTAINER_INIT_ADOPT
)
89 flags
|= nsSubstring::F_OWNED
;
91 new (&aContainer
) nsSubstring(const_cast<PRUnichar
*>(aData
),
96 new (&aContainer
) nsString(aData
, aDataLength
);
104 NS_StringContainerFinish(nsStringContainer
&aContainer
)
106 // call the nsString dtor
107 reinterpret_cast<nsString
*>(&aContainer
)->~nsString();
110 /* ------------------------------------------------------------------------- */
113 NS_StringGetData(const nsAString
&aStr
, const PRUnichar
**aData
,
117 *aTerminated
= aStr
.IsTerminated();
119 nsAString::const_iterator begin
;
120 aStr
.BeginReading(begin
);
121 *aData
= begin
.get();
122 return begin
.size_forward();
126 NS_StringGetMutableData(nsAString
&aStr
, PRUint32 aDataLength
,
129 if (aDataLength
!= PR_UINT32_MAX
) {
130 aStr
.SetLength(aDataLength
);
131 if (aStr
.Length() != aDataLength
) {
137 nsAString::iterator begin
;
138 aStr
.BeginWriting(begin
);
139 *aData
= begin
.get();
140 return begin
.size_forward();
143 XPCOM_API(PRUnichar
*)
144 NS_StringCloneData(const nsAString
&aStr
)
146 return ToNewUnicode(aStr
);
150 NS_StringSetData(nsAString
&aStr
, const PRUnichar
*aData
, PRUint32 aDataLength
)
152 aStr
.Assign(aData
, aDataLength
);
153 return NS_OK
; // XXX report errors
157 NS_StringSetDataRange(nsAString
&aStr
,
158 PRUint32 aCutOffset
, PRUint32 aCutLength
,
159 const PRUnichar
*aData
, PRUint32 aDataLength
)
161 if (aCutOffset
== PR_UINT32_MAX
)
165 aStr
.Append(aData
, aDataLength
);
166 return NS_OK
; // XXX report errors
169 if (aCutLength
== PR_UINT32_MAX
)
170 aCutLength
= aStr
.Length() - aCutOffset
;
174 if (aDataLength
== PR_UINT32_MAX
)
175 aStr
.Replace(aCutOffset
, aCutLength
, nsDependentString(aData
));
177 aStr
.Replace(aCutOffset
, aCutLength
, Substring(aData
, aData
+ aDataLength
));
180 aStr
.Cut(aCutOffset
, aCutLength
);
182 return NS_OK
; // XXX report errors
186 NS_StringCopy(nsAString
&aDest
, const nsAString
&aSrc
)
189 return NS_OK
; // XXX report errors
193 NS_StringSetIsVoid(nsAString
&aStr
, const PRBool aIsVoid
)
195 aStr
.SetIsVoid(aIsVoid
);
199 NS_StringGetIsVoid(const nsAString
&aStr
)
201 return aStr
.IsVoid();
204 /* ------------------------------------------------------------------------- */
207 NS_CStringContainerInit(nsCStringContainer
&aContainer
)
209 NS_ASSERTION(sizeof(nsStringContainer_base
) >= sizeof(nsCString
),
210 "nsCStringContainer is not large enough");
212 // use placement new to avoid heap allocating nsCString object
213 new (&aContainer
) nsCString();
219 NS_CStringContainerInit2(nsCStringContainer
&aContainer
,
221 PRUint32 aDataLength
,
224 NS_ASSERTION(sizeof(nsStringContainer_base
) >= sizeof(nsCString
),
225 "nsStringContainer is not large enough");
229 new (&aContainer
) nsCString();
233 if (aDataLength
== PR_UINT32_MAX
)
235 NS_ENSURE_ARG(!(aFlags
& NS_CSTRING_CONTAINER_INIT_SUBSTRING
));
236 aDataLength
= nsCharTraits
<char>::length(aData
);
239 if (aFlags
& (NS_CSTRING_CONTAINER_INIT_DEPEND
|
240 NS_CSTRING_CONTAINER_INIT_ADOPT
))
243 if (aFlags
& NS_CSTRING_CONTAINER_INIT_SUBSTRING
)
244 flags
= nsCSubstring::F_NONE
;
246 flags
= nsCSubstring::F_TERMINATED
;
248 if (aFlags
& NS_CSTRING_CONTAINER_INIT_ADOPT
)
249 flags
|= nsCSubstring::F_OWNED
;
251 new (&aContainer
) nsCSubstring(const_cast<char *>(aData
),
256 new (&aContainer
) nsCString(aData
, aDataLength
);
264 NS_CStringContainerFinish(nsCStringContainer
&aContainer
)
266 // call the nsCString dtor
267 reinterpret_cast<nsCString
*>(&aContainer
)->~nsCString();
270 /* ------------------------------------------------------------------------- */
273 NS_CStringGetData(const nsACString
&aStr
, const char **aData
,
277 *aTerminated
= aStr
.IsTerminated();
279 nsACString::const_iterator begin
;
280 aStr
.BeginReading(begin
);
281 *aData
= begin
.get();
282 return begin
.size_forward();
286 NS_CStringGetMutableData(nsACString
&aStr
, PRUint32 aDataLength
, char **aData
)
288 if (aDataLength
!= PR_UINT32_MAX
) {
289 aStr
.SetLength(aDataLength
);
290 if (aStr
.Length() != aDataLength
) {
296 nsACString::iterator begin
;
297 aStr
.BeginWriting(begin
);
298 *aData
= begin
.get();
299 return begin
.size_forward();
303 NS_CStringCloneData(const nsACString
&aStr
)
305 return ToNewCString(aStr
);
309 NS_CStringSetData(nsACString
&aStr
, const char *aData
, PRUint32 aDataLength
)
311 aStr
.Assign(aData
, aDataLength
);
312 return NS_OK
; // XXX report errors
316 NS_CStringSetDataRange(nsACString
&aStr
,
317 PRUint32 aCutOffset
, PRUint32 aCutLength
,
318 const char *aData
, PRUint32 aDataLength
)
320 if (aCutOffset
== PR_UINT32_MAX
)
324 aStr
.Append(aData
, aDataLength
);
325 return NS_OK
; // XXX report errors
328 if (aCutLength
== PR_UINT32_MAX
)
329 aCutLength
= aStr
.Length() - aCutOffset
;
333 if (aDataLength
== PR_UINT32_MAX
)
334 aStr
.Replace(aCutOffset
, aCutLength
, nsDependentCString(aData
));
336 aStr
.Replace(aCutOffset
, aCutLength
, Substring(aData
, aData
+ aDataLength
));
339 aStr
.Cut(aCutOffset
, aCutLength
);
341 return NS_OK
; // XXX report errors
345 NS_CStringCopy(nsACString
&aDest
, const nsACString
&aSrc
)
348 return NS_OK
; // XXX report errors
352 NS_CStringSetIsVoid(nsACString
&aStr
, const PRBool aIsVoid
)
354 aStr
.SetIsVoid(aIsVoid
);
358 NS_CStringGetIsVoid(const nsACString
&aStr
)
360 return aStr
.IsVoid();
363 /* ------------------------------------------------------------------------- */
366 NS_CStringToUTF16(const nsACString
&aSrc
,
367 nsCStringEncoding aSrcEncoding
,
370 switch (aSrcEncoding
)
372 case NS_CSTRING_ENCODING_ASCII
:
373 CopyASCIItoUTF16(aSrc
, aDest
);
375 case NS_CSTRING_ENCODING_UTF8
:
376 CopyUTF8toUTF16(aSrc
, aDest
);
378 case NS_CSTRING_ENCODING_NATIVE_FILESYSTEM
:
379 NS_CopyNativeToUnicode(aSrc
, aDest
);
382 return NS_ERROR_NOT_IMPLEMENTED
;
385 return NS_OK
; // XXX report errors
389 NS_UTF16ToCString(const nsAString
&aSrc
,
390 nsCStringEncoding aDestEncoding
,
393 switch (aDestEncoding
)
395 case NS_CSTRING_ENCODING_ASCII
:
396 LossyCopyUTF16toASCII(aSrc
, aDest
);
398 case NS_CSTRING_ENCODING_UTF8
:
399 CopyUTF16toUTF8(aSrc
, aDest
);
401 case NS_CSTRING_ENCODING_NATIVE_FILESYSTEM
:
402 NS_CopyUnicodeToNative(aSrc
, aDest
);
405 return NS_ERROR_NOT_IMPLEMENTED
;
408 return NS_OK
; // XXX report errors