Back to index

lightning-sunbird  0.9+nobinonly
nsReadableUtils.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Scott Collins <scc@mozilla.org> (original author)
00024  *   Johnny Stenbeck <jst@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef nsReadableUtils_h___
00041 #define nsReadableUtils_h___
00042 
00048 #ifndef nsAString_h___
00049 #include "nsAString.h"
00050 #endif
00051 
00052 inline size_t Distance( const nsReadingIterator<PRUnichar>& start, const nsReadingIterator<PRUnichar>& end )
00053   {
00054     return end.get() - start.get();
00055   }
00056 inline size_t Distance( const nsReadingIterator<char>& start, const nsReadingIterator<char>& end )
00057   {
00058     return end.get() - start.get();
00059   }
00060 
00061 NS_COM void LossyCopyUTF16toASCII( const nsAString& aSource, nsACString& aDest );
00062 NS_COM void CopyASCIItoUTF16( const nsACString& aSource, nsAString& aDest );
00063 
00064 NS_COM void LossyCopyUTF16toASCII( const PRUnichar* aSource, nsACString& aDest );
00065 NS_COM void CopyASCIItoUTF16( const char* aSource, nsAString& aDest );
00066 
00067 NS_COM void CopyUTF16toUTF8( const nsAString& aSource, nsACString& aDest );
00068 NS_COM void CopyUTF8toUTF16( const nsACString& aSource, nsAString& aDest );
00069 
00070 NS_COM void CopyUTF16toUTF8( const PRUnichar* aSource, nsACString& aDest );
00071 NS_COM void CopyUTF8toUTF16( const char* aSource, nsAString& aDest );
00072 
00073 NS_COM void LossyAppendUTF16toASCII( const nsAString& aSource, nsACString& aDest );
00074 NS_COM void AppendASCIItoUTF16( const nsACString& aSource, nsAString& aDest );
00075 
00076 NS_COM void LossyAppendUTF16toASCII( const PRUnichar* aSource, nsACString& aDest );
00077 NS_COM void AppendASCIItoUTF16( const char* aSource, nsAString& aDest );
00078 
00079 NS_COM void AppendUTF16toUTF8( const nsAString& aSource, nsACString& aDest );
00080 NS_COM void AppendUTF8toUTF16( const nsACString& aSource, nsAString& aDest );
00081 
00082 NS_COM void AppendUTF16toUTF8( const PRUnichar* aSource, nsACString& aDest );
00083 NS_COM void AppendUTF8toUTF16( const char* aSource, nsAString& aDest );
00084 
00085 // Backward compatibility
00086 inline 
00087 NS_COM void CopyUCS2toASCII( const nsAString& aSource, nsACString& aDest )
00088 { LossyCopyUTF16toASCII(aSource, aDest); }
00089 inline 
00090 NS_COM void CopyASCIItoUCS2( const nsACString& aSource, nsAString& aDest )
00091 { CopyASCIItoUTF16(aSource, aDest); }
00092 
00104 NS_COM char* ToNewCString( const nsAString& aSource );
00105 
00106 
00116 NS_COM char* ToNewCString( const nsACString& aSource );
00117 
00133 NS_COM char* ToNewUTF8String( const nsAString& aSource, PRUint32 *aUTF8Count = nsnull );
00134 
00135 
00148 NS_COM PRUnichar* ToNewUnicode( const nsAString& aSource );
00149 
00150 
00162 NS_COM PRUnichar* ToNewUnicode( const nsACString& aSource );
00163 
00179 NS_COM PRUnichar* UTF8ToNewUnicode( const nsACString& aSource, PRUint32 *aUTF16Count = nsnull );
00180 
00193 NS_COM PRUnichar* CopyUnicodeTo( const nsAString& aSource,
00194                                  PRUint32 aSrcOffset,
00195                                  PRUnichar* aDest,
00196                                  PRUint32 aLength );
00197 
00198 
00210 NS_COM void CopyUnicodeTo( const nsAString::const_iterator& aSrcStart,
00211                            const nsAString::const_iterator& aSrcEnd,
00212                            nsAString& aDest );
00213 
00224 NS_COM void AppendUnicodeTo( const nsAString::const_iterator& aSrcStart,
00225                              const nsAString::const_iterator& aSrcEnd,
00226                              nsAString& aDest );
00227 
00233 NS_COM PRBool IsASCII( const nsAString& aString );
00234 
00240 NS_COM PRBool IsASCII( const nsACString& aString );
00241 
00242 
00261 NS_COM PRBool IsUTF8( const nsACString& aString );
00262 
00263 
00267 NS_COM void ToUpperCase( nsACString& );
00268 
00269 NS_COM void ToLowerCase( nsACString& );
00270 
00271 NS_COM void ToUpperCase( nsCSubstring& );
00272 
00273 NS_COM void ToLowerCase( nsCSubstring& );
00274 
00278 NS_COM void ToUpperCase( const nsACString& aSource, nsACString& aDest );
00279 
00280 NS_COM void ToLowerCase( const nsACString& aSource, nsACString& aDest );
00281 
00292 NS_COM PRBool FindInReadable( const nsAString& aPattern, nsAString::const_iterator&, nsAString::const_iterator&, const nsStringComparator& = nsDefaultStringComparator() );
00293 NS_COM PRBool FindInReadable( const nsACString& aPattern, nsACString::const_iterator&, nsACString::const_iterator&, const nsCStringComparator& = nsDefaultCStringComparator() );
00294 
00295 /* sometimes we don't care about where the string was, just that we
00296  * found it or not */
00297 inline PRBool FindInReadable( const nsAString& aPattern, const nsAString& aSource, const nsStringComparator& compare = nsDefaultStringComparator() )
00298 {
00299   nsAString::const_iterator start, end;
00300   aSource.BeginReading(start);
00301   aSource.EndReading(end);
00302   return FindInReadable(aPattern, start, end, compare);
00303 }
00304 
00305 inline PRBool FindInReadable( const nsACString& aPattern, const nsACString& aSource, const nsCStringComparator& compare = nsDefaultCStringComparator() )
00306 {
00307   nsACString::const_iterator start, end;
00308   aSource.BeginReading(start);
00309   aSource.EndReading(end);
00310   return FindInReadable(aPattern, start, end, compare);
00311 }
00312 
00313 
00314 NS_COM PRBool CaseInsensitiveFindInReadable( const nsACString& aPattern, nsACString::const_iterator&, nsACString::const_iterator& );
00315 
00324 NS_COM PRBool RFindInReadable( const nsAString& aPattern, nsAString::const_iterator&, nsAString::const_iterator&, const nsStringComparator& = nsDefaultStringComparator() );
00325 NS_COM PRBool RFindInReadable( const nsACString& aPattern, nsACString::const_iterator&, nsACString::const_iterator&, const nsCStringComparator& = nsDefaultCStringComparator() );
00326 
00335 NS_COM PRBool FindCharInReadable( PRUnichar aChar, nsAString::const_iterator& aSearchStart, const nsAString::const_iterator& aSearchEnd );
00336 NS_COM PRBool FindCharInReadable( char aChar, nsACString::const_iterator& aSearchStart, const nsACString::const_iterator& aSearchEnd );
00337 
00341 NS_COM PRUint32 CountCharInReadable( const nsAString& aStr,
00342                                      PRUnichar aChar );
00343 NS_COM PRUint32 CountCharInReadable( const nsACString& aStr,
00344                                      char aChar );
00345 
00346 NS_COM PRBool
00347 StringBeginsWith( const nsAString& aSource, const nsAString& aSubstring,
00348                   const nsStringComparator& aComparator =
00349                                               nsDefaultStringComparator() );
00350 NS_COM PRBool
00351 StringBeginsWith( const nsACString& aSource, const nsACString& aSubstring,
00352                   const nsCStringComparator& aComparator =
00353                                                nsDefaultCStringComparator() );
00354 NS_COM PRBool
00355 StringEndsWith( const nsAString& aSource, const nsAString& aSubstring,
00356                 const nsStringComparator& aComparator =
00357                                             nsDefaultStringComparator() );
00358 NS_COM PRBool
00359 StringEndsWith( const nsACString& aSource, const nsACString& aSubstring,
00360                 const nsCStringComparator& aComparator =
00361                                              nsDefaultCStringComparator() );
00362 
00363 NS_COM const nsAFlatString& EmptyString();
00364 NS_COM const nsAFlatCString& EmptyCString();
00365 
00366 
00367 template<class T>
00368 inline PRBool EnsureStringLength(T& aStr, PRUint32 aLen)
00369 {
00370     aStr.SetLength(aLen);
00371     return (aStr.Length() == aLen);
00372 }
00373 
00374 NS_COM void
00375 AppendUCS4ToUTF16(const PRUint32 aSource, nsAString& aDest);
00376 
00377 #endif // !defined(nsReadableUtils_h___)