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 | #include "nsString.h"
|
---|
39 | #include "nsCharTraits.h"
|
---|
40 |
|
---|
41 | #define NS_STRINGAPI_IMPL
|
---|
42 | #include "nsStringAPI.h"
|
---|
43 | #include "nsNativeCharsetUtils.h"
|
---|
44 |
|
---|
45 | /* ------------------------------------------------------------------------- */
|
---|
46 |
|
---|
47 | NS_STRINGAPI(nsresult)
|
---|
48 | NS_StringContainerInit(nsStringContainer &aContainer)
|
---|
49 | {
|
---|
50 | NS_ASSERTION(sizeof(nsStringContainer) >= sizeof(nsString),
|
---|
51 | "nsStringContainer is not large enough");
|
---|
52 |
|
---|
53 | // use placement new to avoid heap allocating nsString object
|
---|
54 | new (&aContainer) nsString();
|
---|
55 |
|
---|
56 | return NS_OK;
|
---|
57 | }
|
---|
58 |
|
---|
59 | NS_STRINGAPI(void)
|
---|
60 | NS_StringContainerFinish(nsStringContainer &aContainer)
|
---|
61 | {
|
---|
62 | // call the nsString dtor
|
---|
63 | NS_REINTERPRET_CAST(nsString *, &aContainer)->~nsString();
|
---|
64 | }
|
---|
65 |
|
---|
66 | /* ------------------------------------------------------------------------- */
|
---|
67 |
|
---|
68 | NS_STRINGAPI(PRUint32)
|
---|
69 | NS_StringGetData(const nsAString &aStr, const PRUnichar **aData,
|
---|
70 | PRBool *aTerminated)
|
---|
71 | {
|
---|
72 | if (aTerminated)
|
---|
73 | *aTerminated = aStr.IsTerminated();
|
---|
74 |
|
---|
75 | nsAString::const_iterator begin;
|
---|
76 | aStr.BeginReading(begin);
|
---|
77 | *aData = begin.get();
|
---|
78 | return begin.size_forward();
|
---|
79 | }
|
---|
80 |
|
---|
81 | NS_STRINGAPI(PRUnichar *)
|
---|
82 | NS_StringCloneData(const nsAString &aStr)
|
---|
83 | {
|
---|
84 | return ToNewUnicode(aStr);
|
---|
85 | }
|
---|
86 |
|
---|
87 | NS_STRINGAPI(nsresult)
|
---|
88 | NS_StringSetData(nsAString &aStr, const PRUnichar *aData, PRUint32 aDataLength)
|
---|
89 | {
|
---|
90 | aStr.Assign(aData, aDataLength);
|
---|
91 | return NS_OK; // XXX report errors
|
---|
92 | }
|
---|
93 |
|
---|
94 | NS_STRINGAPI(nsresult)
|
---|
95 | NS_StringSetDataRange(nsAString &aStr,
|
---|
96 | PRUint32 aCutOffset, PRUint32 aCutLength,
|
---|
97 | const PRUnichar *aData, PRUint32 aDataLength)
|
---|
98 | {
|
---|
99 | if (aCutOffset == PR_UINT32_MAX)
|
---|
100 | {
|
---|
101 | // append case
|
---|
102 | if (aData)
|
---|
103 | aStr.Append(aData, aDataLength);
|
---|
104 | return NS_OK; // XXX report errors
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (aCutLength == PR_UINT32_MAX)
|
---|
108 | aCutLength = aStr.Length() - aCutOffset;
|
---|
109 |
|
---|
110 | if (aData)
|
---|
111 | {
|
---|
112 | if (aDataLength == PR_UINT32_MAX)
|
---|
113 | aStr.Replace(aCutOffset, aCutLength, nsDependentString(aData));
|
---|
114 | else
|
---|
115 | aStr.Replace(aCutOffset, aCutLength, Substring(aData, aData + aDataLength));
|
---|
116 | }
|
---|
117 | else
|
---|
118 | aStr.Cut(aCutOffset, aCutLength);
|
---|
119 |
|
---|
120 | return NS_OK; // XXX report errors
|
---|
121 | }
|
---|
122 |
|
---|
123 | NS_STRINGAPI(nsresult)
|
---|
124 | NS_StringCopy(nsAString &aDest, const nsAString &aSrc)
|
---|
125 | {
|
---|
126 | aDest.Assign(aSrc);
|
---|
127 | return NS_OK; // XXX report errors
|
---|
128 | }
|
---|
129 |
|
---|
130 | /* ------------------------------------------------------------------------- */
|
---|
131 |
|
---|
132 | NS_STRINGAPI(nsresult)
|
---|
133 | NS_CStringContainerInit(nsCStringContainer &aContainer)
|
---|
134 | {
|
---|
135 | NS_ASSERTION(sizeof(nsCStringContainer) >= sizeof(nsCString),
|
---|
136 | "nsCStringContainer is not large enough");
|
---|
137 |
|
---|
138 | // use placement new to avoid heap allocating nsCString object
|
---|
139 | new (&aContainer) nsCString();
|
---|
140 |
|
---|
141 | return NS_OK;
|
---|
142 | }
|
---|
143 |
|
---|
144 | NS_STRINGAPI(void)
|
---|
145 | NS_CStringContainerFinish(nsCStringContainer &aContainer)
|
---|
146 | {
|
---|
147 | // call the nsCString dtor
|
---|
148 | NS_REINTERPRET_CAST(nsCString *, &aContainer)->~nsCString();
|
---|
149 | }
|
---|
150 |
|
---|
151 | /* ------------------------------------------------------------------------- */
|
---|
152 |
|
---|
153 | NS_STRINGAPI(PRUint32)
|
---|
154 | NS_CStringGetData(const nsACString &aStr, const char **aData,
|
---|
155 | PRBool *aTerminated)
|
---|
156 | {
|
---|
157 | if (aTerminated)
|
---|
158 | *aTerminated = aStr.IsTerminated();
|
---|
159 |
|
---|
160 | nsACString::const_iterator begin;
|
---|
161 | aStr.BeginReading(begin);
|
---|
162 | *aData = begin.get();
|
---|
163 | return begin.size_forward();
|
---|
164 | }
|
---|
165 |
|
---|
166 | NS_STRINGAPI(char *)
|
---|
167 | NS_CStringCloneData(const nsACString &aStr)
|
---|
168 | {
|
---|
169 | return ToNewCString(aStr);
|
---|
170 | }
|
---|
171 |
|
---|
172 | NS_STRINGAPI(nsresult)
|
---|
173 | NS_CStringSetData(nsACString &aStr, const char *aData, PRUint32 aDataLength)
|
---|
174 | {
|
---|
175 | aStr.Assign(aData, aDataLength);
|
---|
176 | return NS_OK; // XXX report errors
|
---|
177 | }
|
---|
178 |
|
---|
179 | NS_STRINGAPI(nsresult)
|
---|
180 | NS_CStringSetDataRange(nsACString &aStr,
|
---|
181 | PRUint32 aCutOffset, PRUint32 aCutLength,
|
---|
182 | const char *aData, PRUint32 aDataLength)
|
---|
183 | {
|
---|
184 | if (aCutOffset == PR_UINT32_MAX)
|
---|
185 | {
|
---|
186 | // append case
|
---|
187 | if (aData)
|
---|
188 | aStr.Append(aData, aDataLength);
|
---|
189 | return NS_OK; // XXX report errors
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (aCutLength == PR_UINT32_MAX)
|
---|
193 | aCutLength = aStr.Length() - aCutOffset;
|
---|
194 |
|
---|
195 | if (aData)
|
---|
196 | {
|
---|
197 | if (aDataLength == PR_UINT32_MAX)
|
---|
198 | aStr.Replace(aCutOffset, aCutLength, nsDependentCString(aData));
|
---|
199 | else
|
---|
200 | aStr.Replace(aCutOffset, aCutLength, Substring(aData, aData + aDataLength));
|
---|
201 | }
|
---|
202 | else
|
---|
203 | aStr.Cut(aCutOffset, aCutLength);
|
---|
204 |
|
---|
205 | return NS_OK; // XXX report errors
|
---|
206 | }
|
---|
207 |
|
---|
208 | NS_STRINGAPI(nsresult)
|
---|
209 | NS_CStringCopy(nsACString &aDest, const nsACString &aSrc)
|
---|
210 | {
|
---|
211 | aDest.Assign(aSrc);
|
---|
212 | return NS_OK; // XXX report errors
|
---|
213 | }
|
---|
214 |
|
---|
215 | /* ------------------------------------------------------------------------- */
|
---|
216 |
|
---|
217 | NS_STRINGAPI(nsresult)
|
---|
218 | NS_CStringToUTF16(const nsACString &aSrc,
|
---|
219 | nsCStringEncoding aSrcEncoding,
|
---|
220 | nsAString &aDest)
|
---|
221 | {
|
---|
222 | switch (aSrcEncoding)
|
---|
223 | {
|
---|
224 | case NS_CSTRING_ENCODING_ASCII:
|
---|
225 | CopyASCIItoUTF16(aSrc, aDest);
|
---|
226 | break;
|
---|
227 | case NS_CSTRING_ENCODING_UTF8:
|
---|
228 | CopyUTF8toUTF16(aSrc, aDest);
|
---|
229 | break;
|
---|
230 | case NS_CSTRING_ENCODING_NATIVE_FILESYSTEM:
|
---|
231 | NS_CopyNativeToUnicode(aSrc, aDest);
|
---|
232 | break;
|
---|
233 | default:
|
---|
234 | return NS_ERROR_NOT_IMPLEMENTED;
|
---|
235 | }
|
---|
236 |
|
---|
237 | return NS_OK; // XXX report errors
|
---|
238 | }
|
---|
239 |
|
---|
240 | NS_STRINGAPI(nsresult)
|
---|
241 | NS_UTF16ToCString(const nsAString &aSrc,
|
---|
242 | nsCStringEncoding aDestEncoding,
|
---|
243 | nsACString &aDest)
|
---|
244 | {
|
---|
245 | switch (aDestEncoding)
|
---|
246 | {
|
---|
247 | case NS_CSTRING_ENCODING_ASCII:
|
---|
248 | LossyCopyUTF16toASCII(aSrc, aDest);
|
---|
249 | break;
|
---|
250 | case NS_CSTRING_ENCODING_UTF8:
|
---|
251 | CopyUTF16toUTF8(aSrc, aDest);
|
---|
252 | break;
|
---|
253 | case NS_CSTRING_ENCODING_NATIVE_FILESYSTEM:
|
---|
254 | NS_CopyUnicodeToNative(aSrc, aDest);
|
---|
255 | break;
|
---|
256 | default:
|
---|
257 | return NS_ERROR_NOT_IMPLEMENTED;
|
---|
258 | }
|
---|
259 |
|
---|
260 | return NS_OK; // XXX report errors
|
---|
261 | }
|
---|