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 <darin@meer.net>
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 : #include "nsString.h"
39 : #include "nsCharTraits.h"
40 :
41 : #include "nsXPCOMStrings.h"
42 : #include "nsNativeCharsetUtils.h"
43 :
44 : /* ------------------------------------------------------------------------- */
45 :
46 : XPCOM_API(nsresult)
47 2734 : NS_StringContainerInit(nsStringContainer &aContainer)
48 : {
49 : NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsString),
50 : "nsStringContainer is not large enough");
51 :
52 : // use placement new to avoid heap allocating nsString object
53 2734 : new (&aContainer) nsString();
54 :
55 2734 : return NS_OK;
56 : }
57 :
58 : XPCOM_API(nsresult)
59 4 : NS_StringContainerInit2(nsStringContainer &aContainer,
60 : const PRUnichar *aData,
61 : PRUint32 aDataLength,
62 : PRUint32 aFlags)
63 : {
64 : NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsString),
65 : "nsStringContainer is not large enough");
66 :
67 4 : if (!aData)
68 : {
69 0 : new (&aContainer) nsString();
70 : }
71 : else
72 : {
73 4 : if (aDataLength == PR_UINT32_MAX)
74 : {
75 4 : NS_ENSURE_ARG(!(aFlags & NS_STRING_CONTAINER_INIT_SUBSTRING));
76 4 : aDataLength = nsCharTraits<PRUnichar>::length(aData);
77 : }
78 :
79 4 : if (aFlags & (NS_STRING_CONTAINER_INIT_DEPEND |
80 : NS_STRING_CONTAINER_INIT_ADOPT))
81 : {
82 : PRUint32 flags;
83 4 : if (aFlags & NS_STRING_CONTAINER_INIT_SUBSTRING)
84 0 : flags = nsSubstring::F_NONE;
85 : else
86 4 : flags = nsSubstring::F_TERMINATED;
87 :
88 4 : if (aFlags & NS_STRING_CONTAINER_INIT_ADOPT)
89 0 : flags |= nsSubstring::F_OWNED;
90 :
91 : new (&aContainer) nsSubstring(const_cast<PRUnichar *>(aData),
92 4 : aDataLength, flags);
93 : }
94 : else
95 : {
96 0 : new (&aContainer) nsString(aData, aDataLength);
97 : }
98 : }
99 :
100 4 : return NS_OK;
101 : }
102 :
103 : XPCOM_API(void)
104 2738 : NS_StringContainerFinish(nsStringContainer &aContainer)
105 : {
106 : // call the nsString dtor
107 2738 : reinterpret_cast<nsString *>(&aContainer)->~nsString();
108 2738 : }
109 :
110 : /* ------------------------------------------------------------------------- */
111 :
112 : XPCOM_API(PRUint32)
113 19 : NS_StringGetData(const nsAString &aStr, const PRUnichar **aData,
114 : bool *aTerminated)
115 : {
116 19 : if (aTerminated)
117 0 : *aTerminated = aStr.IsTerminated();
118 :
119 19 : nsAString::const_iterator begin;
120 19 : aStr.BeginReading(begin);
121 19 : *aData = begin.get();
122 19 : return begin.size_forward();
123 : }
124 :
125 : XPCOM_API(PRUint32)
126 0 : NS_StringGetMutableData(nsAString &aStr, PRUint32 aDataLength,
127 : PRUnichar **aData)
128 : {
129 0 : if (aDataLength != PR_UINT32_MAX) {
130 0 : aStr.SetLength(aDataLength);
131 0 : if (aStr.Length() != aDataLength) {
132 0 : *aData = nsnull;
133 0 : return 0;
134 : }
135 : }
136 :
137 0 : nsAString::iterator begin;
138 0 : aStr.BeginWriting(begin);
139 0 : *aData = begin.get();
140 0 : return begin.size_forward();
141 : }
142 :
143 : XPCOM_API(PRUnichar *)
144 220 : NS_StringCloneData(const nsAString &aStr)
145 : {
146 220 : return ToNewUnicode(aStr);
147 : }
148 :
149 : XPCOM_API(nsresult)
150 0 : NS_StringSetData(nsAString &aStr, const PRUnichar *aData, PRUint32 aDataLength)
151 : {
152 0 : aStr.Assign(aData, aDataLength);
153 0 : return NS_OK; // XXX report errors
154 : }
155 :
156 : XPCOM_API(nsresult)
157 1 : NS_StringSetDataRange(nsAString &aStr,
158 : PRUint32 aCutOffset, PRUint32 aCutLength,
159 : const PRUnichar *aData, PRUint32 aDataLength)
160 : {
161 1 : if (aCutOffset == PR_UINT32_MAX)
162 : {
163 : // append case
164 1 : if (aData)
165 1 : aStr.Append(aData, aDataLength);
166 1 : return NS_OK; // XXX report errors
167 : }
168 :
169 0 : if (aCutLength == PR_UINT32_MAX)
170 0 : aCutLength = aStr.Length() - aCutOffset;
171 :
172 0 : if (aData)
173 : {
174 0 : if (aDataLength == PR_UINT32_MAX)
175 0 : aStr.Replace(aCutOffset, aCutLength, nsDependentString(aData));
176 : else
177 0 : aStr.Replace(aCutOffset, aCutLength, Substring(aData, aDataLength));
178 : }
179 : else
180 0 : aStr.Cut(aCutOffset, aCutLength);
181 :
182 0 : return NS_OK; // XXX report errors
183 : }
184 :
185 : XPCOM_API(nsresult)
186 1370 : NS_StringCopy(nsAString &aDest, const nsAString &aSrc)
187 : {
188 1370 : aDest.Assign(aSrc);
189 1370 : return NS_OK; // XXX report errors
190 : }
191 :
192 : XPCOM_API(void)
193 0 : NS_StringSetIsVoid(nsAString &aStr, const bool aIsVoid)
194 : {
195 0 : aStr.SetIsVoid(aIsVoid);
196 0 : }
197 :
198 : XPCOM_API(bool)
199 0 : NS_StringGetIsVoid(const nsAString &aStr)
200 : {
201 0 : return aStr.IsVoid();
202 : }
203 :
204 : /* ------------------------------------------------------------------------- */
205 :
206 : XPCOM_API(nsresult)
207 12693 : NS_CStringContainerInit(nsCStringContainer &aContainer)
208 : {
209 : NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsCString),
210 : "nsCStringContainer is not large enough");
211 :
212 : // use placement new to avoid heap allocating nsCString object
213 12693 : new (&aContainer) nsCString();
214 :
215 12693 : return NS_OK;
216 : }
217 :
218 : XPCOM_API(nsresult)
219 5561 : NS_CStringContainerInit2(nsCStringContainer &aContainer,
220 : const char *aData,
221 : PRUint32 aDataLength,
222 : PRUint32 aFlags)
223 : {
224 : NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsCString),
225 : "nsStringContainer is not large enough");
226 :
227 5561 : if (!aData)
228 : {
229 21 : new (&aContainer) nsCString();
230 : }
231 : else
232 : {
233 5540 : if (aDataLength == PR_UINT32_MAX)
234 : {
235 27 : NS_ENSURE_ARG(!(aFlags & NS_CSTRING_CONTAINER_INIT_SUBSTRING));
236 27 : aDataLength = nsCharTraits<char>::length(aData);
237 : }
238 :
239 5540 : if (aFlags & (NS_CSTRING_CONTAINER_INIT_DEPEND |
240 : NS_CSTRING_CONTAINER_INIT_ADOPT))
241 : {
242 : PRUint32 flags;
243 5540 : if (aFlags & NS_CSTRING_CONTAINER_INIT_SUBSTRING)
244 1 : flags = nsCSubstring::F_NONE;
245 : else
246 5539 : flags = nsCSubstring::F_TERMINATED;
247 :
248 5540 : if (aFlags & NS_CSTRING_CONTAINER_INIT_ADOPT)
249 1 : flags |= nsCSubstring::F_OWNED;
250 :
251 : new (&aContainer) nsCSubstring(const_cast<char *>(aData),
252 5540 : aDataLength, flags);
253 : }
254 : else
255 : {
256 0 : new (&aContainer) nsCString(aData, aDataLength);
257 : }
258 : }
259 :
260 5561 : return NS_OK;
261 : }
262 :
263 : XPCOM_API(void)
264 18254 : NS_CStringContainerFinish(nsCStringContainer &aContainer)
265 : {
266 : // call the nsCString dtor
267 18254 : reinterpret_cast<nsCString *>(&aContainer)->~nsCString();
268 18254 : }
269 :
270 : /* ------------------------------------------------------------------------- */
271 :
272 : XPCOM_API(PRUint32)
273 14474 : NS_CStringGetData(const nsACString &aStr, const char **aData,
274 : bool *aTerminated)
275 : {
276 14474 : if (aTerminated)
277 0 : *aTerminated = aStr.IsTerminated();
278 :
279 14474 : nsACString::const_iterator begin;
280 14474 : aStr.BeginReading(begin);
281 14474 : *aData = begin.get();
282 14474 : return begin.size_forward();
283 : }
284 :
285 : XPCOM_API(PRUint32)
286 4114 : NS_CStringGetMutableData(nsACString &aStr, PRUint32 aDataLength, char **aData)
287 : {
288 4114 : if (aDataLength != PR_UINT32_MAX) {
289 2737 : aStr.SetLength(aDataLength);
290 2737 : if (aStr.Length() != aDataLength) {
291 0 : *aData = nsnull;
292 0 : return 0;
293 : }
294 : }
295 :
296 4114 : nsACString::iterator begin;
297 4114 : aStr.BeginWriting(begin);
298 4114 : *aData = begin.get();
299 4114 : return begin.size_forward();
300 : }
301 :
302 : XPCOM_API(char *)
303 8 : NS_CStringCloneData(const nsACString &aStr)
304 : {
305 8 : return ToNewCString(aStr);
306 : }
307 :
308 : XPCOM_API(nsresult)
309 7667 : NS_CStringSetData(nsACString &aStr, const char *aData, PRUint32 aDataLength)
310 : {
311 7667 : aStr.Assign(aData, aDataLength);
312 7667 : return NS_OK; // XXX report errors
313 : }
314 :
315 : XPCOM_API(nsresult)
316 1519 : NS_CStringSetDataRange(nsACString &aStr,
317 : PRUint32 aCutOffset, PRUint32 aCutLength,
318 : const char *aData, PRUint32 aDataLength)
319 : {
320 1519 : if (aCutOffset == PR_UINT32_MAX)
321 : {
322 : // append case
323 136 : if (aData)
324 136 : aStr.Append(aData, aDataLength);
325 136 : return NS_OK; // XXX report errors
326 : }
327 :
328 1383 : if (aCutLength == PR_UINT32_MAX)
329 0 : aCutLength = aStr.Length() - aCutOffset;
330 :
331 1383 : if (aData)
332 : {
333 1383 : if (aDataLength == PR_UINT32_MAX)
334 0 : aStr.Replace(aCutOffset, aCutLength, nsDependentCString(aData));
335 : else
336 1383 : aStr.Replace(aCutOffset, aCutLength, Substring(aData, aDataLength));
337 : }
338 : else
339 0 : aStr.Cut(aCutOffset, aCutLength);
340 :
341 1383 : return NS_OK; // XXX report errors
342 : }
343 :
344 : XPCOM_API(nsresult)
345 10959 : NS_CStringCopy(nsACString &aDest, const nsACString &aSrc)
346 : {
347 10959 : aDest.Assign(aSrc);
348 10959 : return NS_OK; // XXX report errors
349 : }
350 :
351 : XPCOM_API(void)
352 0 : NS_CStringSetIsVoid(nsACString &aStr, const bool aIsVoid)
353 : {
354 0 : aStr.SetIsVoid(aIsVoid);
355 0 : }
356 :
357 : XPCOM_API(bool)
358 0 : NS_CStringGetIsVoid(const nsACString &aStr)
359 : {
360 0 : return aStr.IsVoid();
361 : }
362 :
363 : /* ------------------------------------------------------------------------- */
364 :
365 : XPCOM_API(nsresult)
366 1368 : NS_CStringToUTF16(const nsACString &aSrc,
367 : nsCStringEncoding aSrcEncoding,
368 : nsAString &aDest)
369 : {
370 1368 : switch (aSrcEncoding)
371 : {
372 : case NS_CSTRING_ENCODING_ASCII:
373 0 : CopyASCIItoUTF16(aSrc, aDest);
374 0 : break;
375 : case NS_CSTRING_ENCODING_UTF8:
376 1368 : CopyUTF8toUTF16(aSrc, aDest);
377 1368 : break;
378 : case NS_CSTRING_ENCODING_NATIVE_FILESYSTEM:
379 0 : NS_CopyNativeToUnicode(aSrc, aDest);
380 0 : break;
381 : default:
382 0 : return NS_ERROR_NOT_IMPLEMENTED;
383 : }
384 :
385 1368 : return NS_OK; // XXX report errors
386 : }
387 :
388 : XPCOM_API(nsresult)
389 0 : NS_UTF16ToCString(const nsAString &aSrc,
390 : nsCStringEncoding aDestEncoding,
391 : nsACString &aDest)
392 : {
393 0 : switch (aDestEncoding)
394 : {
395 : case NS_CSTRING_ENCODING_ASCII:
396 0 : LossyCopyUTF16toASCII(aSrc, aDest);
397 0 : break;
398 : case NS_CSTRING_ENCODING_UTF8:
399 0 : CopyUTF16toUTF8(aSrc, aDest);
400 0 : break;
401 : case NS_CSTRING_ENCODING_NATIVE_FILESYSTEM:
402 0 : NS_CopyUnicodeToNative(aSrc, aDest);
403 0 : break;
404 : default:
405 0 : return NS_ERROR_NOT_IMPLEMENTED;
406 : }
407 :
408 0 : return NS_OK; // XXX report errors
409 : }
|