LCOV - code coverage report
Current view: directory - xpcom/build - nsXPCOMStrings.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 148 114 77.0 %
Date: 2012-06-02 Functions: 24 22 91.7 %

       1                 : /* vim:set ts=2 sw=2 et cindent: */
       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.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is IBM Corporation.
      18                 :  * Portions created by IBM Corporation are Copyright (C) 2003
      19                 :  * IBM Corporation.  All Rights Reserved.
      20                 :  *
      21                 :  * Contributor(s):
      22                 :  *   Darin Fisher <darin@meer.net>
      23                 :  *
      24                 :  * Alternatively, the contents of this file may be used under the terms of
      25                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      26                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      28                 :  * of those above. If you wish to allow use of your version of this file only
      29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      30                 :  * use your version of this file under the terms of the MPL, indicate your
      31                 :  * decision by deleting the provisions above and replace them with the notice
      32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      33                 :  * the provisions above, a recipient may use your version of this file under
      34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      35                 :  *
      36                 :  * ***** END LICENSE BLOCK ***** */
      37                 : 
      38                 : #include "nsString.h"
      39                 : #include "nsCharTraits.h"
      40                 : 
      41                 : #include "nsXPCOMStrings.h"
      42                 : #include "nsNativeCharsetUtils.h"
      43                 : 
      44                 : /* ------------------------------------------------------------------------- */
      45                 : 
      46                 : XPCOM_API(nsresult)
      47            2803 : NS_StringContainerInit(nsStringContainer &aContainer)
      48                 : {
      49                 :   NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsString),
      50                 :       "nsStringContainer is not large enough");
      51                 : 
      52                 :   // use placement new to avoid heap allocating nsString object
      53            2803 :   new (&aContainer) nsString();
      54                 : 
      55            2803 :   return NS_OK;
      56                 : }
      57                 : 
      58                 : XPCOM_API(nsresult)
      59              56 : NS_StringContainerInit2(nsStringContainer &aContainer,
      60                 :                         const PRUnichar   *aData,
      61                 :                         PRUint32           aDataLength,
      62                 :                         PRUint32           aFlags)
      63                 : {
      64                 :   NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsString),
      65                 :       "nsStringContainer is not large enough");
      66                 : 
      67              56 :   if (!aData)
      68                 :   {
      69               0 :     new (&aContainer) nsString();
      70                 :   }
      71                 :   else
      72                 :   {
      73              56 :     if (aDataLength == PR_UINT32_MAX)
      74                 :     {
      75               5 :       NS_ENSURE_ARG(!(aFlags & NS_STRING_CONTAINER_INIT_SUBSTRING));
      76               5 :       aDataLength = nsCharTraits<PRUnichar>::length(aData);
      77                 :     }
      78                 : 
      79              56 :     if (aFlags & (NS_STRING_CONTAINER_INIT_DEPEND |
      80                 :                   NS_STRING_CONTAINER_INIT_ADOPT))
      81                 :     {
      82                 :       PRUint32 flags;
      83              55 :       if (aFlags & NS_STRING_CONTAINER_INIT_SUBSTRING)
      84               0 :         flags = nsSubstring::F_NONE;
      85                 :       else
      86              55 :         flags = nsSubstring::F_TERMINATED;
      87                 : 
      88              55 :       if (aFlags & NS_STRING_CONTAINER_INIT_ADOPT)
      89               0 :         flags |= nsSubstring::F_OWNED;
      90                 : 
      91                 :       new (&aContainer) nsSubstring(const_cast<PRUnichar *>(aData),
      92              55 :                                     aDataLength, flags);
      93                 :     }
      94                 :     else
      95                 :     {
      96               1 :       new (&aContainer) nsString(aData, aDataLength);
      97                 :     }
      98                 :   }
      99                 : 
     100              56 :   return NS_OK;
     101                 : }
     102                 : 
     103                 : XPCOM_API(void)
     104            2859 : NS_StringContainerFinish(nsStringContainer &aContainer)
     105                 : {
     106                 :   // call the nsString dtor
     107            2859 :   reinterpret_cast<nsString *>(&aContainer)->~nsString();
     108            2859 : }
     109                 : 
     110                 : /* ------------------------------------------------------------------------- */
     111                 : 
     112                 : XPCOM_API(PRUint32)
     113             171 : NS_StringGetData(const nsAString &aStr, const PRUnichar **aData,
     114                 :                  bool *aTerminated)
     115                 : {
     116             171 :   if (aTerminated)
     117               0 :     *aTerminated = aStr.IsTerminated();
     118                 : 
     119             171 :   nsAString::const_iterator begin;
     120             171 :   aStr.BeginReading(begin);
     121             171 :   *aData = begin.get();
     122             171 :   return begin.size_forward();
     123                 : }
     124                 : 
     125                 : XPCOM_API(PRUint32)
     126              30 : NS_StringGetMutableData(nsAString &aStr, PRUint32 aDataLength,
     127                 :                         PRUnichar **aData)
     128                 : {
     129              30 :   if (aDataLength != PR_UINT32_MAX) {
     130              24 :     aStr.SetLength(aDataLength);
     131              24 :     if (aStr.Length() != aDataLength) {
     132               0 :       *aData = nsnull;
     133               0 :       return 0;
     134                 :     }
     135                 :   }
     136                 : 
     137              30 :   nsAString::iterator begin;
     138              30 :   aStr.BeginWriting(begin);
     139              30 :   *aData = begin.get();
     140              30 :   return begin.size_forward();
     141                 : }
     142                 : 
     143                 : XPCOM_API(PRUnichar *)
     144             238 : NS_StringCloneData(const nsAString &aStr)
     145                 : {
     146             238 :   return ToNewUnicode(aStr);
     147                 : }
     148                 : 
     149                 : XPCOM_API(nsresult)
     150               3 : NS_StringSetData(nsAString &aStr, const PRUnichar *aData, PRUint32 aDataLength)
     151                 : {
     152               3 :   aStr.Assign(aData, aDataLength);
     153               3 :   return NS_OK; // XXX report errors
     154                 : }
     155                 : 
     156                 : XPCOM_API(nsresult)
     157              83 : NS_StringSetDataRange(nsAString &aStr,
     158                 :                       PRUint32 aCutOffset, PRUint32 aCutLength,
     159                 :                       const PRUnichar *aData, PRUint32 aDataLength)
     160                 : {
     161              83 :   if (aCutOffset == PR_UINT32_MAX)
     162                 :   {
     163                 :     // append case
     164              83 :     if (aData)
     165              83 :       aStr.Append(aData, aDataLength);
     166              83 :     return NS_OK; // XXX report errors
     167                 :   }
     168                 : 
     169               0 :   if (aCutLength == PR_UINT32_MAX)
     170               0 :     aCutLength = aStr.Length() - aCutOffset;
     171                 : 
     172               0 :   if (aData)
     173                 :   {
     174               0 :     if (aDataLength == PR_UINT32_MAX)
     175               0 :       aStr.Replace(aCutOffset, aCutLength, nsDependentString(aData));
     176                 :     else
     177               0 :       aStr.Replace(aCutOffset, aCutLength, Substring(aData, aDataLength));
     178                 :   }
     179                 :   else
     180               0 :     aStr.Cut(aCutOffset, aCutLength);
     181                 : 
     182               0 :   return NS_OK; // XXX report errors
     183                 : }
     184                 : 
     185                 : XPCOM_API(nsresult)
     186            1395 : NS_StringCopy(nsAString &aDest, const nsAString &aSrc)
     187                 : {
     188            1395 :   aDest.Assign(aSrc);
     189            1395 :   return NS_OK; // XXX report errors
     190                 : }
     191                 : 
     192                 : XPCOM_API(void)
     193               4 : NS_StringSetIsVoid(nsAString &aStr, const bool aIsVoid)
     194                 : {
     195               4 :   aStr.SetIsVoid(aIsVoid);
     196               4 : }
     197                 : 
     198                 : XPCOM_API(bool)
     199               5 : NS_StringGetIsVoid(const nsAString &aStr)
     200                 : {
     201               5 :   return aStr.IsVoid();
     202                 : }
     203                 : 
     204                 : /* ------------------------------------------------------------------------- */
     205                 : 
     206                 : XPCOM_API(nsresult)
     207           13162 : NS_CStringContainerInit(nsCStringContainer &aContainer)
     208                 : {
     209                 :   NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsCString),
     210                 :       "nsCStringContainer is not large enough");
     211                 : 
     212                 :   // use placement new to avoid heap allocating nsCString object
     213           13162 :   new (&aContainer) nsCString();
     214                 : 
     215           13162 :   return NS_OK;
     216                 : }
     217                 : 
     218                 : XPCOM_API(nsresult)
     219            6429 : NS_CStringContainerInit2(nsCStringContainer &aContainer,
     220                 :                          const char         *aData,
     221                 :                          PRUint32            aDataLength,
     222                 :                          PRUint32            aFlags)
     223                 : {
     224                 :   NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsCString),
     225                 :       "nsStringContainer is not large enough");
     226                 : 
     227            6429 :   if (!aData)
     228                 :   {
     229              62 :     new (&aContainer) nsCString();
     230                 :   }
     231                 :   else
     232                 :   {
     233            6367 :     if (aDataLength == PR_UINT32_MAX)
     234                 :     {
     235             443 :       NS_ENSURE_ARG(!(aFlags & NS_CSTRING_CONTAINER_INIT_SUBSTRING));
     236             443 :       aDataLength = nsCharTraits<char>::length(aData);
     237                 :     }
     238                 : 
     239            6367 :     if (aFlags & (NS_CSTRING_CONTAINER_INIT_DEPEND |
     240                 :                   NS_CSTRING_CONTAINER_INIT_ADOPT))
     241                 :     {
     242                 :       PRUint32 flags;
     243            6367 :       if (aFlags & NS_CSTRING_CONTAINER_INIT_SUBSTRING)
     244               1 :         flags = nsCSubstring::F_NONE;
     245                 :       else
     246            6366 :         flags = nsCSubstring::F_TERMINATED;
     247                 : 
     248            6367 :       if (aFlags & NS_CSTRING_CONTAINER_INIT_ADOPT)
     249              42 :         flags |= nsCSubstring::F_OWNED;
     250                 : 
     251                 :       new (&aContainer) nsCSubstring(const_cast<char *>(aData),
     252            6367 :                                      aDataLength, flags);
     253                 :     }
     254                 :     else
     255                 :     {
     256               0 :       new (&aContainer) nsCString(aData, aDataLength);
     257                 :     }
     258                 :   }
     259                 : 
     260            6429 :   return NS_OK;
     261                 : }
     262                 : 
     263                 : XPCOM_API(void)
     264           19591 : NS_CStringContainerFinish(nsCStringContainer &aContainer)
     265                 : {
     266                 :   // call the nsCString dtor
     267           19591 :   reinterpret_cast<nsCString *>(&aContainer)->~nsCString();
     268           19591 : }
     269                 : 
     270                 : /* ------------------------------------------------------------------------- */
     271                 : 
     272                 : XPCOM_API(PRUint32)
     273           16503 : NS_CStringGetData(const nsACString &aStr, const char **aData,
     274                 :                   bool *aTerminated)
     275                 : {
     276           16503 :   if (aTerminated)
     277               0 :     *aTerminated = aStr.IsTerminated();
     278                 : 
     279           16503 :   nsACString::const_iterator begin;
     280           16503 :   aStr.BeginReading(begin);
     281           16503 :   *aData = begin.get();
     282           16503 :   return begin.size_forward();
     283                 : }
     284                 : 
     285                 : XPCOM_API(PRUint32)
     286            4180 : NS_CStringGetMutableData(nsACString &aStr, PRUint32 aDataLength, char **aData)
     287                 : {
     288            4180 :   if (aDataLength != PR_UINT32_MAX) {
     289            2781 :     aStr.SetLength(aDataLength);
     290            2781 :     if (aStr.Length() != aDataLength) {
     291               0 :       *aData = nsnull;
     292               0 :       return 0;
     293                 :     }
     294                 :   }
     295                 : 
     296            4180 :   nsACString::iterator begin;
     297            4180 :   aStr.BeginWriting(begin);
     298            4180 :   *aData = begin.get();
     299            4180 :   return begin.size_forward();
     300                 : }
     301                 : 
     302                 : XPCOM_API(char *)
     303               8 : NS_CStringCloneData(const nsACString &aStr)
     304                 : {
     305               8 :   return ToNewCString(aStr);
     306                 : }
     307                 : 
     308                 : XPCOM_API(nsresult)
     309            7747 : NS_CStringSetData(nsACString &aStr, const char *aData, PRUint32 aDataLength)
     310                 : {
     311            7747 :   aStr.Assign(aData, aDataLength);
     312            7747 :   return NS_OK; // XXX report errors
     313                 : }
     314                 : 
     315                 : XPCOM_API(nsresult)
     316            2084 : NS_CStringSetDataRange(nsACString &aStr,
     317                 :                        PRUint32 aCutOffset, PRUint32 aCutLength,
     318                 :                        const char *aData, PRUint32 aDataLength)
     319                 : {
     320            2084 :   if (aCutOffset == PR_UINT32_MAX)
     321                 :   {
     322                 :     // append case
     323             679 :     if (aData)
     324             679 :       aStr.Append(aData, aDataLength);
     325             679 :     return NS_OK; // XXX report errors
     326                 :   }
     327                 : 
     328            1405 :   if (aCutLength == PR_UINT32_MAX)
     329               0 :     aCutLength = aStr.Length() - aCutOffset;
     330                 : 
     331            1405 :   if (aData)
     332                 :   {
     333            1405 :     if (aDataLength == PR_UINT32_MAX)
     334               0 :       aStr.Replace(aCutOffset, aCutLength, nsDependentCString(aData));
     335                 :     else
     336            1405 :       aStr.Replace(aCutOffset, aCutLength, Substring(aData, aDataLength));
     337                 :   }
     338                 :   else
     339               0 :     aStr.Cut(aCutOffset, aCutLength);
     340                 : 
     341            1405 :   return NS_OK; // XXX report errors
     342                 : }
     343                 : 
     344                 : XPCOM_API(nsresult)
     345           11275 : NS_CStringCopy(nsACString &aDest, const nsACString &aSrc)
     346                 : {
     347           11275 :   aDest.Assign(aSrc);
     348           11275 :   return NS_OK; // XXX report errors
     349                 : }
     350                 : 
     351                 : XPCOM_API(void)
     352               0 : NS_CStringSetIsVoid(nsACString &aStr, const bool aIsVoid)
     353                 : {
     354               0 :   aStr.SetIsVoid(aIsVoid);
     355               0 : }
     356                 : 
     357                 : XPCOM_API(bool)
     358               0 : NS_CStringGetIsVoid(const nsACString &aStr)
     359                 : {
     360               0 :   return aStr.IsVoid();
     361                 : }
     362                 : 
     363                 : /* ------------------------------------------------------------------------- */
     364                 : 
     365                 : XPCOM_API(nsresult)
     366            1393 : NS_CStringToUTF16(const nsACString &aSrc,
     367                 :                   nsCStringEncoding aSrcEncoding,
     368                 :                   nsAString &aDest)
     369                 : {
     370            1393 :   switch (aSrcEncoding)
     371                 :   {
     372                 :     case NS_CSTRING_ENCODING_ASCII:
     373               0 :       CopyASCIItoUTF16(aSrc, aDest);
     374               0 :       break;
     375                 :     case NS_CSTRING_ENCODING_UTF8:
     376            1393 :       CopyUTF8toUTF16(aSrc, aDest);
     377            1393 :       break;
     378                 :     case NS_CSTRING_ENCODING_NATIVE_FILESYSTEM:
     379               0 :       NS_CopyNativeToUnicode(aSrc, aDest);
     380               0 :       break;
     381                 :     default:
     382               0 :       return NS_ERROR_NOT_IMPLEMENTED;
     383                 :   }
     384                 : 
     385            1393 :   return NS_OK; // XXX report errors
     386                 : }
     387                 : 
     388                 : XPCOM_API(nsresult)
     389               2 : NS_UTF16ToCString(const nsAString &aSrc,
     390                 :                   nsCStringEncoding aDestEncoding,
     391                 :                   nsACString &aDest)
     392                 : {
     393               2 :   switch (aDestEncoding)
     394                 :   {
     395                 :     case NS_CSTRING_ENCODING_ASCII:
     396               1 :       LossyCopyUTF16toASCII(aSrc, aDest);
     397               1 :       break;
     398                 :     case NS_CSTRING_ENCODING_UTF8:
     399               1 :       CopyUTF16toUTF8(aSrc, aDest);
     400               1 :       break;
     401                 :     case NS_CSTRING_ENCODING_NATIVE_FILESYSTEM:
     402               0 :       NS_CopyUnicodeToNative(aSrc, aDest);
     403               0 :       break;
     404                 :     default:
     405               0 :       return NS_ERROR_NOT_IMPLEMENTED;
     406                 :   }
     407                 : 
     408               2 :   return NS_OK; // XXX report errors
     409                 : }

Generated by: LCOV version 1.7