Back to index

lightning-sunbird  0.9+nobinonly
nsTextFragment.cpp
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 #include "nsTextFragment.h"
00038 #include "nsString.h"
00039 #include "nsCRT.h"
00040 #include "nsReadableUtils.h"
00041 #include "nsMemory.h"
00042 #include "nsBidiUtils.h"
00043 #include "nsUnicharUtils.h"
00044 
00045 // Static buffer used for newline fragments
00046 static unsigned char sNewLineCharacter = '\n';
00047 
00048 
00049 nsTextFragment::~nsTextFragment()
00050 {
00051   ReleaseText();
00052 }
00053 
00054 void
00055 nsTextFragment::ReleaseText()
00056 {
00057   if (mState.mLength && m1b && mState.mInHeap) {
00058     unsigned char *buf = NS_CONST_CAST(unsigned char *, m1b);
00059 
00060     nsMemory::Free(buf); // m1b == m2b as far as nsMemory is concerned
00061   }
00062 
00063   m1b = nsnull;
00064 
00065   // Set mState.mIs2b, mState.mInHeap, and mState.mLength = 0 with mAllBits;
00066   mAllBits = 0;
00067 }
00068 
00069 nsTextFragment::nsTextFragment(const nsTextFragment& aOther)
00070   : m1b(nsnull), mAllBits(0)
00071 {
00072   if (aOther.Is2b()) {
00073     SetTo(aOther.Get2b(), aOther.GetLength());
00074   } else {
00075     SetTo(aOther.Get1b(), aOther.GetLength());
00076   }
00077 }
00078 
00079 nsTextFragment::nsTextFragment(const char *aString)
00080   : m1b(nsnull), mAllBits(0)
00081 {
00082   SetTo(aString, strlen(aString));
00083 }
00084 
00085 nsTextFragment::nsTextFragment(const PRUnichar *aString)
00086   : m1b(nsnull), mAllBits(0)
00087 {
00088   SetTo(aString, nsCRT::strlen(aString));
00089 }
00090 
00091 nsTextFragment::nsTextFragment(const nsString& aString)
00092   : m1b(nsnull), mAllBits(0)
00093 {
00094   SetTo(aString.get(), aString.Length());
00095 }
00096 
00097 nsTextFragment&
00098 nsTextFragment::operator=(const nsTextFragment& aOther)
00099 {
00100   if (aOther.Is2b()) {
00101     SetTo(aOther.Get2b(), aOther.GetLength());
00102   } else {
00103     SetTo(aOther.Get1b(), aOther.GetLength());
00104   }
00105 
00106   if (aOther.mState.mIsBidi) {
00107     // Carry over BIDI state from aOther
00108     mState.mIsBidi = PR_TRUE;
00109   }
00110 
00111   return *this;
00112 }
00113 
00114 nsTextFragment&
00115 nsTextFragment::operator=(const char *aString)
00116 {
00117   SetTo(aString, strlen(aString));
00118 
00119   return *this;
00120 }
00121 
00122 nsTextFragment&
00123 nsTextFragment::operator=(const PRUnichar *aString)
00124 {
00125   SetTo(aString, nsCRT::strlen(aString));
00126 
00127   return *this;
00128 }
00129 
00130 nsTextFragment&
00131 nsTextFragment::operator=(const nsAString& aString)
00132 {
00133   ReleaseText();
00134 
00135   PRUint32 length = aString.Length();
00136 
00137   if (length > 0) {
00138     PRBool in_heap = PR_TRUE;
00139 
00140     if (IsASCII(aString)) {
00141       if (length == 1 && aString.First() == '\n') {
00142         m1b = &sNewLineCharacter;
00143 
00144         in_heap = PR_FALSE;
00145       } else {
00146         m1b = (unsigned char *)ToNewCString(aString);
00147       }
00148 
00149       mState.mIs2b = PR_FALSE;
00150     } else {
00151       m2b = ToNewUnicode(aString);
00152       mState.mIs2b = PR_TRUE;
00153     }
00154 
00155     mState.mInHeap = in_heap;
00156     mState.mLength = length;
00157   }
00158 
00159   return *this;
00160 }
00161 
00162 void
00163 nsTextFragment::SetTo(PRUnichar *aBuffer, PRInt32 aLength, PRBool aRelease)
00164 {
00165   ReleaseText();
00166 
00167   m2b = aBuffer;
00168   mState.mIs2b = PR_TRUE;
00169   mState.mInHeap = aRelease;
00170   mState.mLength = aLength;
00171 }
00172 
00173 void
00174 nsTextFragment::SetTo(const PRUnichar* aBuffer, PRInt32 aLength)
00175 {
00176   ReleaseText();
00177 
00178   if (aLength != 0) {
00179     // See if we need to store the data in ucs2 or not
00180     PRBool need2 = PR_FALSE;
00181     const PRUnichar *ucp = aBuffer;
00182     const PRUnichar *uend = aBuffer + aLength;
00183     while (ucp < uend) {
00184       PRUnichar ch = *ucp++;
00185       if (ch >> 8) {
00186         need2 = PR_TRUE;
00187         break;
00188       }
00189     }
00190 
00191     if (need2) {
00192       // Use ucs2 storage because we have to
00193       m2b = (const PRUnichar *)nsMemory::Clone(aBuffer,
00194                                                aLength * sizeof(PRUnichar));
00195 
00196       if (!m2b) {
00197         NS_ERROR("Failed to clone string buffer!");
00198 
00199         return;
00200       }
00201 
00202       // Setup our fields
00203       mState.mIs2b = PR_TRUE;
00204       mState.mInHeap = PR_TRUE;
00205       mState.mLength = aLength;
00206     } else {
00207       // Use 1 byte storage because we can
00208 
00209       PRBool in_heap = PR_TRUE;
00210 
00211       if (aLength == 1 && *aBuffer == '\n') {
00212         m1b = &sNewLineCharacter;
00213 
00214         in_heap = PR_FALSE;
00215       } else {
00216         unsigned char *nt =
00217           (unsigned char *)nsMemory::Alloc(aLength * sizeof(char));
00218 
00219         if (!nt) {
00220           NS_ERROR("Failed to allocate string buffer!");
00221 
00222           return;
00223         }
00224 
00225         // Copy data
00226         for (PRUint32 i = 0; i < (PRUint32)aLength; ++i) {
00227           nt[i] = (unsigned char)aBuffer[i];
00228         }
00229 
00230         m1b = nt;
00231       }
00232 
00233       // Setup our fields
00234       mState.mIs2b = PR_FALSE;
00235       mState.mInHeap = in_heap;
00236       mState.mLength = aLength;
00237     }
00238   }
00239 }
00240 
00241 void
00242 nsTextFragment::SetTo(const char *aBuffer, PRInt32 aLength)
00243 {
00244   ReleaseText();
00245   if (aLength != 0) {
00246     PRBool in_heap = PR_TRUE;
00247 
00248     if (aLength == 1 && *aBuffer == '\n') {
00249       m1b = &sNewLineCharacter;
00250 
00251       in_heap = PR_FALSE;
00252     } else {
00253       m1b = (unsigned char *)nsMemory::Clone(aBuffer, aLength * sizeof(char));
00254 
00255       if (!m1b) {
00256         NS_ERROR("Failed to allocate string buffer!");
00257 
00258         return;
00259       }
00260     }
00261 
00262     // Setup our fields
00263     mState.mIs2b = PR_FALSE;
00264     mState.mInHeap = in_heap;
00265     mState.mLength = aLength;
00266   }
00267 }
00268 
00269 void
00270 nsTextFragment::AppendTo(nsAString& aString) const
00271 {
00272   if (mState.mIs2b) {
00273     aString.Append(m2b, mState.mLength);
00274   } else {
00275     AppendASCIItoUTF16(Substring((char *)m1b, ((char *)m1b) + mState.mLength),
00276                        aString);
00277   }
00278 }
00279 
00280 void
00281 nsTextFragment::AppendTo(nsACString& aCString) const
00282 {
00283   if (mState.mIs2b) {
00284     LossyAppendUTF16toASCII(Substring((PRUnichar *)m2b,
00285                                       (PRUnichar *)m2b + mState.mLength),
00286                             aCString);
00287   } else {
00288     aCString.Append((char *)m1b, mState.mLength);
00289   }
00290 }
00291 
00292 void
00293 nsTextFragment::CopyTo(PRUnichar *aDest, PRInt32 aOffset, PRInt32 aCount)
00294 {
00295   NS_ASSERTION(aOffset >= 0, "Bad offset passed to nsTextFragment::CopyTo()!");
00296   NS_ASSERTION(aCount >= 0, "Bad count passed to nsTextFragment::CopyTo()!");
00297 
00298   if (aOffset < 0) {
00299     aOffset = 0;
00300   }
00301 
00302   if (aOffset + aCount > GetLength()) {
00303     aCount = mState.mLength - aOffset;
00304   }
00305 
00306   if (aCount != 0) {
00307     if (mState.mIs2b) {
00308       memcpy(aDest, m2b + aOffset, sizeof(PRUnichar) * aCount);
00309     } else {
00310       unsigned const char *cp = m1b + aOffset;
00311       unsigned const char *end = cp + aCount;
00312       while (cp < end) {
00313         *aDest++ = PRUnichar(*cp++);
00314       }
00315     }
00316   }
00317 }
00318 
00319 void
00320 nsTextFragment::CopyTo(char *aDest, PRInt32 aOffset, PRInt32 aCount)
00321 {
00322   NS_ASSERTION(aOffset >= 0, "Bad offset passed to nsTextFragment::CopyTo()!");
00323   NS_ASSERTION(aCount >= 0, "Bad count passed to nsTextFragment::CopyTo()!");
00324 
00325   if (aOffset < 0) {
00326     aOffset = 0;
00327   }
00328 
00329   if (aOffset + aCount > GetLength()) {
00330     aCount = mState.mLength - aOffset;
00331   }
00332 
00333   if (aCount != 0) {
00334     if (mState.mIs2b) {
00335       const PRUnichar *cp = m2b + aOffset;
00336       const PRUnichar *end = cp + aCount;
00337       while (cp < end) {
00338         *aDest++ = (char)(*cp++);
00339       }
00340     } else {
00341       memcpy(aDest, m1b + aOffset, sizeof(char) * aCount);
00342     }
00343   }
00344 }
00345 
00346 // To save time we only do this when we really want to know, not during
00347 // every allocation
00348 void
00349 nsTextFragment::SetBidiFlag()
00350 {
00351   if (mState.mIs2b && !mState.mIsBidi) {
00352     const PRUnichar* cp = m2b;
00353     const PRUnichar* end = cp + mState.mLength;
00354     while (cp < end) {
00355       PRUnichar ch1 = *cp++;
00356       PRUint32 utf32Char = ch1;
00357       if (IS_HIGH_SURROGATE(ch1) &&
00358           cp < end &&
00359           IS_LOW_SURROGATE(*cp)) {
00360         PRUnichar ch2 = *cp++;
00361         utf32Char = SURROGATE_TO_UCS4(ch1, ch2);
00362       }
00363       if (UTF32_CHAR_IS_BIDI(utf32Char) || IS_BIDI_CONTROL_CHAR(utf32Char)) {
00364         mState.mIsBidi = PR_TRUE;
00365         break;
00366       }
00367     }
00368   }
00369 }