VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/string/public/nsReadableUtils.h@ 88292

最後變更 在這個檔案從88292是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 15.5 KB
 
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Scott Collins <[email protected]> (original author)
24 * Johnny Stenbeck <[email protected]>
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40#ifndef nsReadableUtils_h___
41#define nsReadableUtils_h___
42
43 /**
44 * I guess all the routines in this file are all mis-named.
45 * According to our conventions, they should be |NS_xxx|.
46 */
47
48#ifndef nsAString_h___
49#include "nsAString.h"
50#endif
51
52inline size_t Distance( const nsReadingIterator<PRUnichar>& start, const nsReadingIterator<PRUnichar>& end )
53 {
54 return end.get() - start.get();
55 }
56inline size_t Distance( const nsReadingIterator<char>& start, const nsReadingIterator<char>& end )
57 {
58 return end.get() - start.get();
59 }
60
61NS_COM void LossyCopyUTF16toASCII( const nsAString& aSource, nsACString& aDest );
62NS_COM void CopyASCIItoUTF16( const nsACString& aSource, nsAString& aDest );
63
64NS_COM void LossyCopyUTF16toASCII( const PRUnichar* aSource, nsACString& aDest );
65NS_COM void CopyASCIItoUTF16( const char* aSource, nsAString& aDest );
66
67NS_COM void CopyUTF16toUTF8( const nsAString& aSource, nsACString& aDest );
68NS_COM void CopyUTF8toUTF16( const nsACString& aSource, nsAString& aDest );
69
70NS_COM void CopyUTF16toUTF8( const PRUnichar* aSource, nsACString& aDest );
71NS_COM void CopyUTF8toUTF16( const char* aSource, nsAString& aDest );
72
73NS_COM void LossyAppendUTF16toASCII( const nsAString& aSource, nsACString& aDest );
74NS_COM void AppendASCIItoUTF16( const nsACString& aSource, nsAString& aDest );
75
76NS_COM void LossyAppendUTF16toASCII( const PRUnichar* aSource, nsACString& aDest );
77NS_COM void AppendASCIItoUTF16( const char* aSource, nsAString& aDest );
78
79NS_COM void AppendUTF16toUTF8( const nsAString& aSource, nsACString& aDest );
80NS_COM void AppendUTF8toUTF16( const nsACString& aSource, nsAString& aDest );
81
82NS_COM void AppendUTF16toUTF8( const PRUnichar* aSource, nsACString& aDest );
83NS_COM void AppendUTF8toUTF16( const char* aSource, nsAString& aDest );
84
85// Backward compatibility
86inline
87NS_COM void CopyUCS2toASCII( const nsAString& aSource, nsACString& aDest )
88{ LossyCopyUTF16toASCII(aSource, aDest); }
89inline
90NS_COM void CopyASCIItoUCS2( const nsACString& aSource, nsAString& aDest )
91{ CopyASCIItoUTF16(aSource, aDest); }
92
93 /**
94 * Returns a new |char| buffer containing a zero-terminated copy of |aSource|.
95 *
96 * Allocates and returns a new |char| buffer which you must free with |nsMemory::Free|.
97 * Performs a lossy encoding conversion by chopping 16-bit wide characters down to 8-bits wide while copying |aSource| to your new buffer.
98 * This conversion is not well defined; but it reproduces legacy string behavior.
99 * The new buffer is zero-terminated, but that may not help you if |aSource| contains embedded nulls.
100 *
101 * @param aSource a 16-bit wide string
102 * @return a new |char| buffer you must free with |nsMemory::Free|.
103 */
104NS_COM char* ToNewCString( const nsAString& aSource );
105
106
107 /**
108 * Returns a new |char| buffer containing a zero-terminated copy of |aSource|.
109 *
110 * Allocates and returns a new |char| buffer which you must free with |nsMemory::Free|.
111 * The new buffer is zero-terminated, but that may not help you if |aSource| contains embedded nulls.
112 *
113 * @param aSource an 8-bit wide string
114 * @return a new |char| buffer you must free with |nsMemory::Free|.
115 */
116NS_COM char* ToNewCString( const nsACString& aSource );
117
118 /**
119 * Returns a new |char| buffer containing a zero-terminated copy of |aSource|.
120 *
121 * Allocates and returns a new |char| buffer which you must free with
122 * |nsMemory::Free|.
123 * Performs an encoding conversion from a UTF-16 string to a UTF-8 string
124 * copying |aSource| to your new buffer.
125 * The new buffer is zero-terminated, but that may not help you if |aSource|
126 * contains embedded nulls.
127 *
128 * @param aSource a UTF-16 string (made of PRUnichar's)
129 * @param aUTF8Count the number of 8-bit units that was returned
130 * @return a new |char| buffer you must free with |nsMemory::Free|.
131 */
132
133NS_COM char* ToNewUTF8String( const nsAString& aSource, PRUint32 *aUTF8Count = nsnull );
134
135
136 /**
137 * Returns a new |PRUnichar| buffer containing a zero-terminated copy of
138 * |aSource|.
139 *
140 * Allocates and returns a new |PRUnichar| buffer which you must free with
141 * |nsMemory::Free|.
142 * The new buffer is zero-terminated, but that may not help you if |aSource|
143 * contains embedded nulls.
144 *
145 * @param aSource a UTF-16 string
146 * @return a new |PRUnichar| buffer you must free with |nsMemory::Free|.
147 */
148NS_COM PRUnichar* ToNewUnicode( const nsAString& aSource );
149
150
151 /**
152 * Returns a new |PRUnichar| buffer containing a zero-terminated copy of |aSource|.
153 *
154 * Allocates and returns a new |PRUnichar| buffer which you must free with |nsMemory::Free|.
155 * Performs an encoding conversion by 0-padding 8-bit wide characters up to 16-bits wide while copying |aSource| to your new buffer.
156 * This conversion is not well defined; but it reproduces legacy string behavior.
157 * The new buffer is zero-terminated, but that may not help you if |aSource| contains embedded nulls.
158 *
159 * @param aSource an 8-bit wide string (a C-string, NOT UTF-8)
160 * @return a new |PRUnichar| buffer you must free with |nsMemory::Free|.
161 */
162NS_COM PRUnichar* ToNewUnicode( const nsACString& aSource );
163
164 /**
165 * Returns a new |PRUnichar| buffer containing a zero-terminated copy
166 * of |aSource|.
167 *
168 * Allocates and returns a new |char| buffer which you must free with
169 * |nsMemory::Free|. Performs an encoding conversion from UTF-8 to UTF-16
170 * while copying |aSource| to your new buffer. This conversion is well defined
171 * for a valid UTF-8 string. The new buffer is zero-terminated, but that
172 * may not help you if |aSource| contains embedded nulls.
173 *
174 * @param aSource an 8-bit wide string, UTF-8 encoded
175 * @param aUTF16Count the number of 16-bit units that was returned
176 * @return a new |PRUnichar| buffer you must free with |nsMemory::Free|.
177 * (UTF-16 encoded)
178 */
179NS_COM PRUnichar* UTF8ToNewUnicode( const nsACString& aSource, PRUint32 *aUTF16Count = nsnull );
180
181 /**
182 * Copies |aLength| 16-bit code units from the start of |aSource| to the
183 * |PRUnichar| buffer |aDest|.
184 *
185 * After this operation |aDest| is not null terminated.
186 *
187 * @param aSource a UTF-16 string
188 * @param aSrcOffset start offset in the source string
189 * @param aDest a |PRUnichar| buffer
190 * @param aLength the number of 16-bit code units to copy
191 * @return pointer to destination buffer - identical to |aDest|
192 */
193NS_COM PRUnichar* CopyUnicodeTo( const nsAString& aSource,
194 PRUint32 aSrcOffset,
195 PRUnichar* aDest,
196 PRUint32 aLength );
197
198
199 /**
200 * Copies 16-bit characters between iterators |aSrcStart| and
201 * |aSrcEnd| to the writable string |aDest|. Similar to the
202 * |nsString::Mid| method.
203 *
204 * After this operation |aDest| is not null terminated.
205 *
206 * @param aSrcStart start source iterator
207 * @param aSrcEnd end source iterator
208 * @param aDest destination for the copy
209 */
210NS_COM void CopyUnicodeTo( const nsAString::const_iterator& aSrcStart,
211 const nsAString::const_iterator& aSrcEnd,
212 nsAString& aDest );
213
214 /**
215 * Appends 16-bit characters between iterators |aSrcStart| and
216 * |aSrcEnd| to the writable string |aDest|.
217 *
218 * After this operation |aDest| is not null terminated.
219 *
220 * @param aSrcStart start source iterator
221 * @param aSrcEnd end source iterator
222 * @param aDest destination for the copy
223 */
224NS_COM void AppendUnicodeTo( const nsAString::const_iterator& aSrcStart,
225 const nsAString::const_iterator& aSrcEnd,
226 nsAString& aDest );
227
228 /**
229 * Returns |PR_TRUE| if |aString| contains only ASCII characters, that is, characters in the range (0x00, 0x7F).
230 *
231 * @param aString a 16-bit wide string to scan
232 */
233NS_COM PRBool IsASCII( const nsAString& aString );
234
235 /**
236 * Returns |PR_TRUE| if |aString| contains only ASCII characters, that is, characters in the range (0x00, 0x7F).
237 *
238 * @param aString a 8-bit wide string to scan
239 */
240NS_COM PRBool IsASCII( const nsACString& aString );
241
242
243 /**
244 * Returns |PR_TRUE| if |aString| is a valid UTF-8 string.
245 * XXX This is not bullet-proof and nor an all-purpose UTF-8 validator.
246 * It is mainly written to replace and roughly equivalent to
247 *
248 * str.Equals(NS_ConvertUTF16toUTF8(NS_ConvertUTF8toUTF16(str)))
249 *
250 * (see bug 191541)
251 * As such, it does not check for non-UTF-8 7bit encodings such as
252 * ISO-2022-JP and HZ. However, it filters out UTF-8 representation
253 * of surrogate codepoints and non-characters ( 0xFFFE and 0xFFFF
254 * in planes 0 through 16.) as well as overlong UTF-8 sequences.
255 * Also note that it regards UTF-8 sequences corresponding to
256 * codepoints above 0x10FFFF as invalid in accordance with
257 * http://www.ietf.org/internet-drafts/draft-yergeau-rfc2279bis-04.txt
258 *
259 * @param aString an 8-bit wide string to scan
260 */
261NS_COM PRBool IsUTF8( const nsACString& aString );
262
263
264 /**
265 * Converts case in place in the argument string.
266 */
267NS_COM void ToUpperCase( nsACString& );
268
269NS_COM void ToLowerCase( nsACString& );
270
271NS_COM void ToUpperCase( nsCSubstring& );
272
273NS_COM void ToLowerCase( nsCSubstring& );
274
275 /**
276 * Converts case from string aSource to aDest.
277 */
278NS_COM void ToUpperCase( const nsACString& aSource, nsACString& aDest );
279
280NS_COM void ToLowerCase( const nsACString& aSource, nsACString& aDest );
281
282 /**
283 * Finds the leftmost occurance of |aPattern|, if any in the range |aSearchStart|..|aSearchEnd|.
284 *
285 * Returns |PR_TRUE| if a match was found, and adjusts |aSearchStart| and |aSearchEnd| to
286 * point to the match. If no match was found, returns |PR_FALSE| and makes |aSearchStart == aSearchEnd|.
287 *
288 * Currently, this is equivalent to the O(m*n) implementation previously on |ns[C]String|.
289 * If we need something faster, then we can implement that later.
290 */
291
292NS_COM PRBool FindInReadable( const nsAString& aPattern, nsAString::const_iterator&, nsAString::const_iterator&, const nsStringComparator& = nsDefaultStringComparator() );
293NS_COM PRBool FindInReadable( const nsACString& aPattern, nsACString::const_iterator&, nsACString::const_iterator&, const nsCStringComparator& = nsDefaultCStringComparator() );
294
295/* sometimes we don't care about where the string was, just that we
296 * found it or not */
297inline PRBool FindInReadable( const nsAString& aPattern, const nsAString& aSource, const nsStringComparator& compare = nsDefaultStringComparator() )
298{
299 nsAString::const_iterator start, end;
300 aSource.BeginReading(start);
301 aSource.EndReading(end);
302 return FindInReadable(aPattern, start, end, compare);
303}
304
305inline PRBool FindInReadable( const nsACString& aPattern, const nsACString& aSource, const nsCStringComparator& compare = nsDefaultCStringComparator() )
306{
307 nsACString::const_iterator start, end;
308 aSource.BeginReading(start);
309 aSource.EndReading(end);
310 return FindInReadable(aPattern, start, end, compare);
311}
312
313
314NS_COM PRBool CaseInsensitiveFindInReadable( const nsACString& aPattern, nsACString::const_iterator&, nsACString::const_iterator& );
315
316 /**
317 * Finds the rightmost occurance of |aPattern|
318 * Returns |PR_TRUE| if a match was found, and adjusts |aSearchStart| and |aSearchEnd| to
319 * point to the match. If no match was found, returns |PR_FALSE| and makes |aSearchStart == aSearchEnd|.
320 *
321 * Currently, this is equivalent to the O(m*n) implementation previously on |ns[C]String|.
322 * If we need something faster, then we can implement that later.
323 */
324NS_COM PRBool RFindInReadable( const nsAString& aPattern, nsAString::const_iterator&, nsAString::const_iterator&, const nsStringComparator& = nsDefaultStringComparator() );
325NS_COM PRBool RFindInReadable( const nsACString& aPattern, nsACString::const_iterator&, nsACString::const_iterator&, const nsCStringComparator& = nsDefaultCStringComparator() );
326
327 /**
328 * Finds the leftmost occurance of |aChar|, if any in the range
329 * |aSearchStart|..|aSearchEnd|.
330 *
331 * Returns |PR_TRUE| if a match was found, and adjusts |aSearchStart| to
332 * point to the match. If no match was found, returns |PR_FALSE| and
333 * makes |aSearchStart == aSearchEnd|.
334 */
335NS_COM PRBool FindCharInReadable( PRUnichar aChar, nsAString::const_iterator& aSearchStart, const nsAString::const_iterator& aSearchEnd );
336NS_COM PRBool FindCharInReadable( char aChar, nsACString::const_iterator& aSearchStart, const nsACString::const_iterator& aSearchEnd );
337
338 /**
339 * Finds the number of occurences of |aChar| in the string |aStr|
340 */
341NS_COM PRUint32 CountCharInReadable( const nsAString& aStr,
342 PRUnichar aChar );
343NS_COM PRUint32 CountCharInReadable( const nsACString& aStr,
344 char aChar );
345
346NS_COM PRBool
347StringBeginsWith( const nsAString& aSource, const nsAString& aSubstring,
348 const nsStringComparator& aComparator =
349 nsDefaultStringComparator() );
350NS_COM PRBool
351StringBeginsWith( const nsACString& aSource, const nsACString& aSubstring,
352 const nsCStringComparator& aComparator =
353 nsDefaultCStringComparator() );
354NS_COM PRBool
355StringEndsWith( const nsAString& aSource, const nsAString& aSubstring,
356 const nsStringComparator& aComparator =
357 nsDefaultStringComparator() );
358NS_COM PRBool
359StringEndsWith( const nsACString& aSource, const nsACString& aSubstring,
360 const nsCStringComparator& aComparator =
361 nsDefaultCStringComparator() );
362
363NS_COM PRUint32 HashString( const nsAString& aStr );
364NS_COM PRUint32 HashString( const nsACString& aStr );
365
366NS_COM const nsAFlatString& EmptyString();
367NS_COM const nsAFlatCString& EmptyCString();
368
369
370#endif // !defined(nsReadableUtils_h___)
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette