Back to index

lightning-sunbird  0.9+nobinonly
nsTextFragment.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 Communicator client 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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #ifndef nsTextFragment_h___
00038 #define nsTextFragment_h___
00039 
00040 #include "nsAString.h"
00041 class nsString;
00042 class nsCString;
00043 
00044 // XXX should this normalize the code to keep a \u0000 at the end?
00045 
00046 // XXX nsTextFragmentPool?
00047 
00048 // XXX these need I18N spankage
00049 #define XP_IS_SPACE(_ch) \
00050   (((_ch) == ' ') || ((_ch) == '\t') || ((_ch) == '\n'))
00051 
00052 #define XP_IS_UPPERCASE(_ch) \
00053   (((_ch) >= 'A') && ((_ch) <= 'Z'))
00054 
00055 #define XP_IS_LOWERCASE(_ch) \
00056   (((_ch) >= 'a') && ((_ch) <= 'z'))
00057 
00058 #define XP_TO_LOWER(_ch) ((_ch) | 32)
00059 
00060 #define XP_TO_UPPER(_ch) ((_ch) & ~32)
00061 
00062 #define XP_IS_SPACE_W XP_IS_SPACE
00063 
00073 class nsTextFragment {
00074 public:
00078   nsTextFragment()
00079     : m1b(nsnull), mAllBits(0)
00080   {
00081   }
00082 
00083   ~nsTextFragment();
00084 
00089   nsTextFragment(const nsTextFragment& aOther);
00090 
00095   nsTextFragment(const char *aString);
00096 
00101   nsTextFragment(const PRUnichar *aString);
00102 
00107   nsTextFragment(const nsString& aString);
00108 
00113   nsTextFragment& operator=(const nsTextFragment& aOther);
00114 
00119   nsTextFragment& operator=(const char *aString);
00120 
00125   nsTextFragment& operator=(const PRUnichar *aString);
00126 
00131   nsTextFragment& operator=(const nsAString& aString);
00132 
00136   PRBool Is2b() const
00137   {
00138     return mState.mIs2b;
00139   }
00140 
00146   PRBool IsBidi() const
00147   {
00148     return mState.mIsBidi;
00149   }
00150 
00154   const PRUnichar *Get2b() const
00155   {
00156     NS_ASSERTION(Is2b(), "not 2b text"); 
00157     return m2b;
00158   }
00159 
00163   const char *Get1b() const
00164   {
00165     NS_ASSERTION(!Is2b(), "not 1b text"); 
00166     return (const char *)m1b;
00167   }
00168 
00173   PRInt32 GetLength() const
00174   {
00175     return PRInt32(mState.mLength);
00176   }
00177 
00184   void SetTo(PRUnichar *aBuffer, PRInt32 aLength, PRBool aRelease);
00185 
00191   void SetTo(const PRUnichar* aBuffer, PRInt32 aLength);
00192 
00198   void SetTo(const char *aBuffer, PRInt32 aLength);
00199 
00203   void AppendTo(nsAString& aString) const;
00204 
00209   void AppendTo(nsACString& aCString) const;
00210 
00217   void CopyTo(PRUnichar *aDest, PRInt32 aOffset, PRInt32 aCount);
00218 
00225   void CopyTo(char *aDest, PRInt32 aOffset, PRInt32 aCount);
00226 
00231   PRUnichar CharAt(PRInt32 aIndex) const
00232   {
00233     NS_ASSERTION(PRUint32(aIndex) < mState.mLength, "bad index");
00234     return mState.mIs2b ? m2b[aIndex] : PRUnichar(m1b[aIndex]);
00235   }
00236 
00241   void SetBidiFlag();
00242 
00243   struct FragmentBits {
00244     PRBool mInHeap : 1;
00245     PRBool mIs2b : 1;
00246     PRBool mIsBidi : 1;
00247     PRUint32 mLength : 29;
00248   };
00249 
00250 protected:
00251   void ReleaseText();
00252 
00253   union {
00254     const PRUnichar *m2b;
00255     const unsigned char *m1b;
00256   };
00257 
00258   union {
00259     PRUint32 mAllBits;
00260     FragmentBits mState;
00261   };
00262 };
00263 
00264 #endif /* nsTextFragment_h___ */
00265