Back to index

lightning-sunbird  0.9+nobinonly
nsTAString.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* vim:set ts=2 sw=2 sts=2 et cindent: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla.
00017  *
00018  * The Initial Developer of the Original Code is IBM Corporation.
00019  * Portions created by IBM Corporation are Copyright (C) 2003
00020  * IBM Corporation. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Darin Fisher <darin@meer.net>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 
00054 nsTAString_CharT::~nsTAString_CharT()
00055   {
00056     if (mVTable == obsolete_string_type::sCanonicalVTable)
00057       AsSubstring()->Finalize();
00058     else
00059       AsObsoleteString()->~nsTObsoleteAString_CharT();
00060   }
00061 
00062 
00063 nsTAString_CharT::size_type
00064 nsTAString_CharT::Length() const
00065   {
00066     if (mVTable == obsolete_string_type::sCanonicalVTable)
00067       return AsSubstring()->Length();
00068 
00069     return AsObsoleteString()->Length();
00070   }
00071 
00072 PRBool
00073 nsTAString_CharT::Equals( const self_type& readable ) const
00074   {
00075     if (mVTable == obsolete_string_type::sCanonicalVTable)
00076       return AsSubstring()->Equals(readable);
00077 
00078     return ToSubstring().Equals(readable);
00079   }
00080 
00081 PRBool
00082 nsTAString_CharT::Equals( const self_type& readable, const comparator_type& comparator ) const
00083   {
00084     if (mVTable == obsolete_string_type::sCanonicalVTable)
00085       return AsSubstring()->Equals(readable, comparator);
00086 
00087     return ToSubstring().Equals(readable, comparator);
00088   }
00089 
00090 PRBool
00091 nsTAString_CharT::Equals( const char_type* data ) const
00092   {
00093     if (mVTable == obsolete_string_type::sCanonicalVTable)
00094       return AsSubstring()->Equals(data);
00095 
00096     return ToSubstring().Equals(data);
00097   }
00098 
00099 PRBool
00100 nsTAString_CharT::Equals( const char_type* data, const comparator_type& comparator ) const
00101   {
00102     if (mVTable == obsolete_string_type::sCanonicalVTable)
00103       return AsSubstring()->Equals(data, comparator);
00104 
00105     return ToSubstring().Equals(data, comparator);
00106   }
00107 
00108 PRBool
00109 nsTAString_CharT::EqualsASCII( const char* data, size_type len ) const
00110   {
00111     if (mVTable == obsolete_string_type::sCanonicalVTable)
00112       return AsSubstring()->EqualsASCII(data, len);
00113 
00114     return ToSubstring().EqualsASCII(data, len);
00115   }
00116 
00117 PRBool
00118 nsTAString_CharT::EqualsASCII( const char* data ) const
00119   {
00120     if (mVTable == obsolete_string_type::sCanonicalVTable)
00121       return AsSubstring()->EqualsASCII(data);
00122 
00123     return ToSubstring().EqualsASCII(data);
00124   }
00125 
00126 PRBool
00127 nsTAString_CharT::LowerCaseEqualsASCII( const char* data, size_type len ) const
00128   {
00129     if (mVTable == obsolete_string_type::sCanonicalVTable)
00130       return AsSubstring()->LowerCaseEqualsASCII(data, len);
00131 
00132     return ToSubstring().LowerCaseEqualsASCII(data, len);
00133   }
00134 
00135 PRBool
00136 nsTAString_CharT::LowerCaseEqualsASCII( const char* data ) const
00137   {
00138     if (mVTable == obsolete_string_type::sCanonicalVTable)
00139       return AsSubstring()->LowerCaseEqualsASCII(data);
00140 
00141     return ToSubstring().LowerCaseEqualsASCII(data);
00142   }
00143 
00144 PRBool
00145 nsTAString_CharT::IsVoid() const
00146   {
00147     if (mVTable == obsolete_string_type::sCanonicalVTable)
00148       return AsSubstring()->IsVoid();
00149 
00150     return AsObsoleteString()->IsVoid();
00151   }
00152 
00153 void
00154 nsTAString_CharT::SetIsVoid( PRBool val )
00155   {
00156     if (mVTable == obsolete_string_type::sCanonicalVTable)
00157       AsSubstring()->SetIsVoid(val);
00158     else
00159       AsObsoleteString()->SetIsVoid(val);
00160   }
00161 
00162 PRBool
00163 nsTAString_CharT::IsTerminated() const
00164   {
00165     if (mVTable == obsolete_string_type::sCanonicalVTable)
00166       return AsSubstring()->IsTerminated();
00167 
00168     return AsObsoleteString()->GetFlatBufferHandle() != nsnull;
00169   }
00170 
00171 CharT
00172 nsTAString_CharT::First() const
00173   {
00174     if (mVTable == obsolete_string_type::sCanonicalVTable)
00175       return AsSubstring()->First();
00176 
00177     return ToSubstring().First();
00178   }
00179 
00180 CharT
00181 nsTAString_CharT::Last() const
00182   {
00183     if (mVTable == obsolete_string_type::sCanonicalVTable)
00184       return AsSubstring()->Last();
00185 
00186     return ToSubstring().Last();
00187   }
00188 
00189 nsTAString_CharT::size_type
00190 nsTAString_CharT::CountChar( char_type c ) const
00191   {
00192     if (mVTable == obsolete_string_type::sCanonicalVTable)
00193       return AsSubstring()->CountChar(c);
00194 
00195     return ToSubstring().CountChar(c);
00196   }
00197 
00198 PRInt32
00199 nsTAString_CharT::FindChar( char_type c, index_type offset ) const
00200   {
00201     if (mVTable == obsolete_string_type::sCanonicalVTable)
00202       return AsSubstring()->FindChar(c, offset);
00203 
00204     return ToSubstring().FindChar(c, offset);
00205   }
00206 
00207 void
00208 nsTAString_CharT::SetCapacity( size_type size )
00209   {
00210     if (mVTable == obsolete_string_type::sCanonicalVTable)
00211       AsSubstring()->SetCapacity(size);
00212     else
00213       AsObsoleteString()->SetCapacity(size);
00214   }
00215 
00216 void
00217 nsTAString_CharT::SetLength( size_type size )
00218   {
00219     if (mVTable == obsolete_string_type::sCanonicalVTable)
00220       AsSubstring()->SetLength(size);
00221     else
00222       AsObsoleteString()->SetLength(size);
00223   }
00224 
00225 void
00226 nsTAString_CharT::Assign( const self_type& readable )
00227   {
00228     if (mVTable == obsolete_string_type::sCanonicalVTable)
00229       AsSubstring()->Assign(readable);
00230     else
00231       AsObsoleteString()->do_AssignFromReadable(readable);
00232   }
00233 
00234 void
00235 nsTAString_CharT::Assign( const substring_tuple_type& tuple )
00236   {
00237     if (mVTable == obsolete_string_type::sCanonicalVTable)
00238       AsSubstring()->Assign(tuple);
00239     else
00240       AsObsoleteString()->do_AssignFromReadable(nsTAutoString_CharT(tuple));
00241   }
00242 
00243 void
00244 nsTAString_CharT::Assign( const char_type* data )
00245   {
00246     // null check and SetLength(0) cases needed for backwards compat
00247 
00248     if (mVTable == obsolete_string_type::sCanonicalVTable)
00249       AsSubstring()->Assign(data);
00250     else if (data)
00251       AsObsoleteString()->do_AssignFromElementPtr(data);
00252     else
00253       AsObsoleteString()->SetLength(0);
00254   }
00255 
00256 void
00257 nsTAString_CharT::Assign( const char_type* data, size_type length )
00258   {
00259     if (mVTable == obsolete_string_type::sCanonicalVTable)
00260       AsSubstring()->Assign(data, length);
00261     else
00262       AsObsoleteString()->do_AssignFromElementPtrLength(data, length);
00263   }
00264 
00265 void
00266 nsTAString_CharT::AssignASCII( const char* data )
00267   {
00268     if (mVTable == obsolete_string_type::sCanonicalVTable)
00269       AsSubstring()->AssignASCII(data);
00270     else
00271       {
00272 #ifdef CharT_is_char
00273         AsObsoleteString()->do_AssignFromElementPtr(data);
00274 #else
00275         nsTAutoString_CharT temp;
00276         temp.AssignASCII(data);
00277         AsObsoleteString()->do_AssignFromReadable(temp);
00278 #endif
00279       }
00280   }
00281 
00282 void
00283 nsTAString_CharT::AssignASCII( const char* data, size_type length )
00284   {
00285     if (mVTable == obsolete_string_type::sCanonicalVTable)
00286       AsSubstring()->AssignASCII(data, length);
00287     else
00288       {
00289 #ifdef CharT_is_char
00290         AsObsoleteString()->do_AssignFromElementPtrLength(data, length);
00291 #else
00292         nsTAutoString_CharT temp;
00293         temp.AssignASCII(data, length);
00294         AsObsoleteString()->do_AssignFromReadable(temp);
00295 #endif
00296       }
00297   }
00298 
00299 void
00300 nsTAString_CharT::Assign( char_type c )
00301   {
00302     if (mVTable == obsolete_string_type::sCanonicalVTable)
00303       AsSubstring()->Assign(c);
00304     else
00305       AsObsoleteString()->do_AssignFromElement(c);
00306   }
00307 
00308 void
00309 nsTAString_CharT::Append( const self_type& readable )
00310   {
00311     if (mVTable == obsolete_string_type::sCanonicalVTable)
00312       AsSubstring()->Append(readable);
00313     else
00314       AsObsoleteString()->do_AppendFromReadable(readable);
00315   }
00316 
00317 void
00318 nsTAString_CharT::Append( const substring_tuple_type& tuple )
00319   {
00320     if (mVTable == obsolete_string_type::sCanonicalVTable)
00321       AsSubstring()->Append(tuple);
00322     else
00323       AsObsoleteString()->do_AppendFromReadable(nsTAutoString_CharT(tuple));
00324   }
00325 
00326 void
00327 nsTAString_CharT::Append( const char_type* data )
00328   {
00329     // null check case needed for backwards compat
00330 
00331     if (mVTable == obsolete_string_type::sCanonicalVTable)
00332       AsSubstring()->Append(data);
00333     else if (data)
00334       AsObsoleteString()->do_AppendFromElementPtr(data);
00335   }
00336 
00337 void
00338 nsTAString_CharT::Append( const char_type* data, size_type length )
00339   {
00340     if (mVTable == obsolete_string_type::sCanonicalVTable)
00341       AsSubstring()->Append(data, length);
00342     else
00343       AsObsoleteString()->do_AppendFromElementPtrLength(data, length);
00344   }
00345 
00346 void
00347 nsTAString_CharT::AppendASCII( const char* data )
00348   {
00349     if (mVTable == obsolete_string_type::sCanonicalVTable)
00350       AsSubstring()->AppendASCII(data);
00351     else
00352       {
00353 #ifdef CharT_is_char
00354         AsObsoleteString()->do_AppendFromElementPtr(data);
00355 #else
00356         nsTAutoString_CharT temp;
00357         temp.AssignASCII(data);
00358         AsObsoleteString()->do_AppendFromReadable(temp);
00359 #endif
00360       }
00361   }
00362 
00363 void
00364 nsTAString_CharT::AppendASCII( const char* data, size_type length )
00365   {
00366     if (mVTable == obsolete_string_type::sCanonicalVTable)
00367       AsSubstring()->AppendASCII(data, length);
00368     else
00369       {
00370 #ifdef CharT_is_char
00371         AsObsoleteString()->do_AppendFromElementPtrLength(data, length);
00372 #else
00373         nsTAutoString_CharT temp;
00374         temp.AssignASCII(data, length);
00375         AsObsoleteString()->do_AppendFromReadable(temp);
00376 #endif
00377       }
00378   }
00379 
00380 void
00381 nsTAString_CharT::Append( char_type c )
00382   {
00383     if (mVTable == obsolete_string_type::sCanonicalVTable)
00384       AsSubstring()->Append(c);
00385     else
00386       AsObsoleteString()->do_AppendFromElement(c);
00387   }
00388 
00389 void
00390 nsTAString_CharT::Insert( const self_type& readable, index_type pos )
00391   {
00392     if (mVTable == obsolete_string_type::sCanonicalVTable)
00393       AsSubstring()->Insert(readable, pos);
00394     else
00395       AsObsoleteString()->do_InsertFromReadable(readable, pos);
00396   }
00397 
00398 void
00399 nsTAString_CharT::Insert( const substring_tuple_type& tuple, index_type pos )
00400   {
00401     if (mVTable == obsolete_string_type::sCanonicalVTable)
00402       AsSubstring()->Insert(tuple, pos);
00403     else
00404       AsObsoleteString()->do_InsertFromReadable(nsTAutoString_CharT(tuple), pos);
00405   }
00406 
00407 void
00408 nsTAString_CharT::Insert( const char_type* data, index_type pos )
00409   {
00410     if (mVTable == obsolete_string_type::sCanonicalVTable)
00411       AsSubstring()->Insert(data, pos);
00412     else
00413       AsObsoleteString()->do_InsertFromElementPtr(data, pos);
00414   }
00415 
00416 void
00417 nsTAString_CharT::Insert( const char_type* data, index_type pos, size_type length )
00418   {
00419     if (mVTable == obsolete_string_type::sCanonicalVTable)
00420       AsSubstring()->Insert(data, pos, length);
00421     else
00422       AsObsoleteString()->do_InsertFromElementPtrLength(data, pos, length);
00423   }
00424 
00425 void
00426 nsTAString_CharT::Insert( char_type c, index_type pos )
00427   {
00428     if (mVTable == obsolete_string_type::sCanonicalVTable)
00429       AsSubstring()->Insert(c, pos);
00430     else
00431       AsObsoleteString()->do_InsertFromElement(c, pos);
00432   }
00433 
00434 void
00435 nsTAString_CharT::Cut( index_type cutStart, size_type cutLength )
00436   {
00437     if (mVTable == obsolete_string_type::sCanonicalVTable)
00438       AsSubstring()->Cut(cutStart, cutLength);
00439     else
00440       AsObsoleteString()->Cut(cutStart, cutLength);
00441   }
00442 
00443 void
00444 nsTAString_CharT::Replace( index_type cutStart, size_type cutLength, const self_type& readable )
00445   {
00446     if (mVTable == obsolete_string_type::sCanonicalVTable)
00447       AsSubstring()->Replace(cutStart, cutLength, readable);
00448     else
00449       AsObsoleteString()->do_ReplaceFromReadable(cutStart, cutLength, readable);
00450   }
00451 
00452 void
00453 nsTAString_CharT::Replace( index_type cutStart, size_type cutLength, const substring_tuple_type& tuple )
00454   {
00455     if (mVTable == obsolete_string_type::sCanonicalVTable)
00456       AsSubstring()->Replace(cutStart, cutLength, tuple);
00457     else
00458       AsObsoleteString()->do_ReplaceFromReadable(cutStart, cutLength, nsTAutoString_CharT(tuple));
00459   }
00460 
00461 nsTAString_CharT::size_type
00462 nsTAString_CharT::GetReadableBuffer( const char_type **data ) const
00463   {
00464     if (mVTable == obsolete_string_type::sCanonicalVTable)
00465       {
00466         const substring_type* str = AsSubstring();
00467         *data = str->mData;
00468         return str->mLength;
00469       }
00470 
00471     obsolete_string_type::const_fragment_type frag;
00472     AsObsoleteString()->GetReadableFragment(frag, obsolete_string_type::kFirstFragment, 0);
00473     *data = frag.mStart;
00474     return (frag.mEnd - frag.mStart);
00475   }
00476 
00477 nsTAString_CharT::size_type
00478 nsTAString_CharT::GetWritableBuffer(char_type **data)
00479   {
00480     if (mVTable == obsolete_string_type::sCanonicalVTable)
00481       {
00482         substring_type* str = AsSubstring();
00483         str->BeginWriting(*data);
00484         return str->Length();
00485       }
00486 
00487     obsolete_string_type::fragment_type frag;
00488     AsObsoleteString()->GetWritableFragment(frag, obsolete_string_type::kFirstFragment, 0);
00489     *data = frag.mStart;
00490     return (frag.mEnd - frag.mStart);
00491   }
00492 
00493 PRBool
00494 nsTAString_CharT::IsDependentOn(const char_type* start, const char_type *end) const
00495   {
00496       // this is an optimization...
00497     if (mVTable == obsolete_string_type::sCanonicalVTable)
00498       return AsSubstring()->IsDependentOn(start, end);
00499 
00500     return ToSubstring().IsDependentOn(start, end);
00501   }
00502 
00503 const nsTAString_CharT::substring_type
00504 nsTAString_CharT::ToSubstring() const
00505   {
00506     const char_type* data;
00507     size_type length = GetReadableBuffer(&data);
00508     return substring_type(NS_CONST_CAST(char_type*, data), length, 0);
00509   }