LCOV - code coverage report
Current view: top level - sal/rtl/source - string.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 111 128 86.7 %
Date: 2012-08-25 Functions: 9 10 90.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 51 74 68.9 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : #include "sal/config.h"
      30                 :            : 
      31                 :            : #if defined(_MSC_VER) && (_MSC_VER >= 1400)
      32                 :            : #pragma warning(disable:4738) // storing 32-bit float result in memory, possible loss of performance
      33                 :            : #endif
      34                 :            : 
      35                 :            : #include <cassert>
      36                 :            : #include <cstdlib>
      37                 :            : 
      38                 :            : #include <rtl/memory.h>
      39                 :            : #include <osl/interlck.h>
      40                 :            : #include <rtl/alloc.h>
      41                 :            : #include <osl/diagnose.h>
      42                 :            : #include <rtl/tencinfo.h>
      43                 :            : 
      44                 :            : #include "strimp.hxx"
      45                 :            : #include "surrogates.hxx"
      46                 :            : #include <rtl/string.h>
      47                 :            : 
      48                 :            : #include "rtl/math.h"
      49                 :            : #include "rtl/tencinfo.h"
      50                 :            : 
      51                 :            : /* ======================================================================= */
      52                 :            : 
      53                 :            : /* static data to be referenced by all empty strings
      54                 :            :  * the refCount is predefined to 1 and must never become 0 !
      55                 :            :  */
      56                 :            : static rtl_String const aImplEmpty_rtl_String =
      57                 :            : {
      58                 :            :     SAL_STRING_STATIC_FLAG|1,
      59                 :            :             /* sal_Int32    refCount;   */
      60                 :            :     0,      /* sal_Int32    length;     */
      61                 :            :     { 0 }   /* sal_Char     buffer[1];  */
      62                 :            : };
      63                 :            : 
      64                 :            : /* ======================================================================= */
      65                 :            : 
      66                 :            : #define IMPL_RTL_STRCODE            sal_Char
      67                 :            : #define IMPL_RTL_USTRCODE( c )      ((unsigned char)c)
      68                 :            : #define IMPL_RTL_STRNAME( n )       rtl_str_ ## n
      69                 :            : 
      70                 :            : #define IMPL_RTL_STRINGNAME( n )    rtl_string_ ## n
      71                 :            : #define IMPL_RTL_STRINGDATA         rtl_String
      72                 :            : #define IMPL_RTL_EMPTYSTRING        aImplEmpty_rtl_String
      73                 :            : 
      74                 :            : #undef RTL_LOG_STRING_NEW
      75                 :            : #define RTL_LOG_STRING_NEW(s)
      76                 :            : #undef RTL_LOG_STRING_DELETE
      77                 :            : #define RTL_LOG_STRING_DELETE(s)
      78                 :            : 
      79                 :            : /* ======================================================================= */
      80                 :            : 
      81                 :            : /* Include String/UString template code */
      82                 :            : 
      83                 :            : #include "strtmpl.cxx"
      84                 :            : 
      85                 :        580 : sal_Int32 SAL_CALL rtl_str_valueOfFloat(sal_Char * pStr, float f)
      86                 :            :     SAL_THROW_EXTERN_C()
      87                 :            : {
      88                 :        580 :     rtl_String * pResult = NULL;
      89                 :            :     sal_Int32 nLen;
      90                 :            :     rtl_math_doubleToString(
      91                 :            :         &pResult, 0, 0, f, rtl_math_StringFormat_G,
      92                 :            :         RTL_STR_MAX_VALUEOFFLOAT - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', 0, 0,
      93                 :        580 :         sal_True);
      94                 :        580 :     nLen = pResult->length;
      95                 :            :     OSL_ASSERT(nLen < RTL_STR_MAX_VALUEOFFLOAT);
      96                 :        580 :     memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Char));
      97                 :        580 :     rtl_string_release(pResult);
      98                 :        580 :     return nLen;
      99                 :            : }
     100                 :            : 
     101                 :       4230 : sal_Int32 SAL_CALL rtl_str_valueOfDouble(sal_Char * pStr, double d)
     102                 :            :     SAL_THROW_EXTERN_C()
     103                 :            : {
     104                 :       4230 :     rtl_String * pResult = NULL;
     105                 :            :     sal_Int32 nLen;
     106                 :            :     rtl_math_doubleToString(
     107                 :            :         &pResult, 0, 0, d, rtl_math_StringFormat_G,
     108                 :            :         RTL_STR_MAX_VALUEOFDOUBLE - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', 0,
     109                 :       4230 :         0, sal_True);
     110                 :       4230 :     nLen = pResult->length;
     111                 :            :     OSL_ASSERT(nLen < RTL_STR_MAX_VALUEOFDOUBLE);
     112                 :       4230 :     memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Char));
     113                 :       4230 :     rtl_string_release(pResult);
     114                 :       4230 :     return nLen;
     115                 :            : }
     116                 :            : 
     117                 :          0 : float SAL_CALL rtl_str_toFloat(sal_Char const * pStr) SAL_THROW_EXTERN_C()
     118                 :            : {
     119                 :          0 :     return (float) rtl_math_stringToDouble(pStr, pStr + rtl_str_getLength(pStr),
     120                 :          0 :                                            '.', 0, 0, 0);
     121                 :            : }
     122                 :            : 
     123                 :      34494 : double SAL_CALL rtl_str_toDouble(sal_Char const * pStr) SAL_THROW_EXTERN_C()
     124                 :            : {
     125                 :      34494 :     return rtl_math_stringToDouble(pStr, pStr + rtl_str_getLength(pStr), '.', 0,
     126                 :      34494 :                                    0, 0);
     127                 :            : }
     128                 :            : 
     129                 :            : /* ======================================================================= */
     130                 :            : 
     131                 :   12094683 : static int rtl_ImplGetFastUTF8ByteLen( const sal_Unicode* pStr, sal_Int32 nLen )
     132                 :            : {
     133                 :            :     int                 n;
     134                 :            :     sal_Unicode         c;
     135                 :            :     sal_uInt32          nUCS4Char;
     136                 :            :     const sal_Unicode*  pEndStr;
     137                 :            : 
     138                 :   12094683 :     n = 0;
     139                 :   12094683 :     pEndStr  = pStr+nLen;
     140         [ +  + ]:  315236632 :     while ( pStr < pEndStr )
     141                 :            :     {
     142                 :  303141949 :         c = *pStr;
     143                 :            : 
     144         [ +  + ]:  303141949 :         if ( c < 0x80 )
     145                 :  303125449 :             n++;
     146         [ +  + ]:      16500 :         else if ( c < 0x800 )
     147                 :       5478 :             n += 2;
     148                 :            :         else
     149                 :            :         {
     150 [ +  + ][ +  - ]:      11022 :             if ( !SAL_RTL_IS_HIGH_SURROGATE(c) )
     151                 :      11022 :                 n += 3;
     152                 :            :             else
     153                 :            :             {
     154                 :          0 :                 nUCS4Char = c;
     155                 :            : 
     156         [ #  # ]:          0 :                 if ( pStr+1 < pEndStr )
     157                 :            :                 {
     158                 :          0 :                     c = *(pStr+1);
     159 [ #  # ][ #  # ]:          0 :                     if ( SAL_RTL_IS_LOW_SURROGATE(c) )
     160                 :            :                     {
     161                 :          0 :                         nUCS4Char = SAL_RTL_COMBINE_SURROGATES(nUCS4Char, c);
     162                 :          0 :                         pStr++;
     163                 :            :                     }
     164                 :            :                 }
     165                 :            : 
     166         [ #  # ]:          0 :                 if ( nUCS4Char < 0x10000 )
     167                 :          0 :                     n += 3;
     168         [ #  # ]:          0 :                 else if ( nUCS4Char < 0x200000 )
     169                 :          0 :                     n += 4;
     170         [ #  # ]:          0 :                 else if ( nUCS4Char < 0x4000000 )
     171                 :          0 :                     n += 5;
     172                 :            :                 else
     173                 :          0 :                     n += 6;
     174                 :            :             }
     175                 :            :         }
     176                 :            : 
     177                 :  303141949 :         pStr++;
     178                 :            :     }
     179                 :            : 
     180                 :   12094683 :     return n;
     181                 :            : }
     182                 :            : 
     183                 :            : /* ----------------------------------------------------------------------- */
     184                 :            : 
     185                 :   13218116 : sal_Bool SAL_CALL rtl_impl_convertUStringToString(rtl_String ** pTarget,
     186                 :            :                                                   sal_Unicode const * pSource,
     187                 :            :                                                   sal_Int32 nLength,
     188                 :            :                                                   rtl_TextEncoding nEncoding,
     189                 :            :                                                   sal_uInt32 nFlags,
     190                 :            :                                                   sal_Bool bCheckErrors)
     191                 :            : {
     192                 :            :     OSL_ASSERT(pTarget != NULL
     193                 :            :                && (pSource != NULL || nLength == 0)
     194                 :            :                && nLength >= 0
     195                 :            :                && (nLength == 0 || rtl_isOctetTextEncoding(nEncoding)));
     196                 :            : 
     197         [ +  + ]:   13218116 :     if ( !nLength )
     198                 :     567693 :         rtl_string_new( pTarget );
     199                 :            :     else
     200                 :            :     {
     201                 :            :         rtl_String*                 pTemp;
     202                 :            :         rtl_UnicodeToTextConverter  hConverter;
     203                 :            :         sal_uInt32                  nInfo;
     204                 :            :         sal_Size                    nSrcChars;
     205                 :            :         sal_Size                    nDestBytes;
     206                 :            :         sal_Size                    nNewLen;
     207                 :            :         sal_Size                    nNotConvertedChars;
     208                 :            :         sal_Size                    nMaxCharLen;
     209                 :            : 
     210                 :            :         /* Optimization for UTF-8 - we try to calculate the exact length */
     211                 :            :         /* For all other encoding we try an good estimation */
     212         [ +  + ]:   12650423 :         if ( nEncoding == RTL_TEXTENCODING_UTF8 )
     213                 :            :         {
     214                 :   12094680 :             nNewLen = rtl_ImplGetFastUTF8ByteLen( pSource, nLength );
     215                 :            :             /* Includes the string only ASCII, then we could copy
     216                 :            :                the buffer faster */
     217         [ +  + ]:   12094682 :             if ( nNewLen == (sal_Size)nLength )
     218                 :            :             {
     219                 :            :                 IMPL_RTL_STRCODE* pBuffer;
     220         [ +  + ]:   12084235 :                 if ( *pTarget )
     221                 :    2863511 :                     IMPL_RTL_STRINGNAME( release )( *pTarget );
     222         [ +  - ]:   12084235 :                 *pTarget = IMPL_RTL_STRINGNAME( ImplAlloc )( nLength );
     223                 :            :                 OSL_ASSERT(*pTarget != NULL);
     224                 :   12084236 :                 pBuffer = (*pTarget)->buffer;
     225         [ +  + ]:  303122370 :                 do
     226                 :            :                 {
     227                 :            :                     /* Check ASCII range */
     228                 :            :                     OSL_ENSURE( *pSource <= 127,
     229                 :            :                                 "rtl_uString2String() - UTF8 test is encoding is wrong" );
     230                 :            : 
     231                 :  303122370 :                     *pBuffer = (IMPL_RTL_STRCODE)(unsigned char)*pSource;
     232                 :  303122370 :                     pBuffer++;
     233                 :  303122370 :                     pSource++;
     234                 :  303122370 :                     nLength--;
     235                 :            :                 }
     236                 :            :                 while ( nLength );
     237                 :   12084236 :                 return sal_True;
     238                 :            :             }
     239                 :            : 
     240                 :      10447 :             nMaxCharLen = 4;
     241                 :            :         }
     242                 :            :         else
     243                 :            :         {
     244                 :            :             rtl_TextEncodingInfo aTextEncInfo;
     245                 :     555743 :             aTextEncInfo.StructSize = sizeof( aTextEncInfo );
     246 [ +  - ][ +  + ]:     555743 :             if ( !rtl_getTextEncodingInfo( nEncoding, &aTextEncInfo ) )
     247                 :            :             {
     248                 :        540 :                 aTextEncInfo.AverageCharSize    = 1;
     249                 :        540 :                 aTextEncInfo.MaximumCharSize    = 8;
     250                 :            :             }
     251                 :            : 
     252                 :     555743 :             nNewLen = nLength*aTextEncInfo.AverageCharSize;
     253                 :     555743 :             nMaxCharLen = aTextEncInfo.MaximumCharSize;
     254                 :            :         }
     255                 :            : 
     256                 :     566190 :         nFlags |= RTL_UNICODETOTEXT_FLAGS_FLUSH;
     257         [ +  - ]:     566190 :         hConverter = rtl_createUnicodeToTextConverter( nEncoding );
     258                 :            : 
     259                 :         10 :         for (;;)
     260                 :            :         {
     261         [ +  - ]:     566201 :             pTemp = IMPL_RTL_STRINGNAME( ImplAlloc )( nNewLen );
     262                 :            :             OSL_ASSERT(pTemp != NULL);
     263                 :            :             nDestBytes = rtl_convertUnicodeToText( hConverter, 0,
     264                 :            :                                                    pSource, nLength,
     265                 :            :                                                    pTemp->buffer, nNewLen,
     266                 :            :                                                    nFlags,
     267         [ +  - ]:     566201 :                                                    &nInfo, &nSrcChars );
     268 [ +  + ][ +  + ]:     566201 :             if (bCheckErrors && (nInfo & RTL_UNICODETOTEXT_INFO_ERROR) != 0)
     269                 :            :             {
     270                 :         10 :                 rtl_freeMemory(pTemp);
     271         [ +  - ]:         10 :                 rtl_destroyUnicodeToTextConverter(hConverter);
     272                 :         10 :                 return sal_False;
     273                 :            :             }
     274                 :            : 
     275         [ +  + ]:     566191 :             if ((nInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL) == 0)
     276                 :     566181 :                 break;
     277                 :            : 
     278                 :            :             /* Buffer not big enough, try again with enough space */
     279                 :         10 :             rtl_freeMemory( pTemp );
     280                 :            : 
     281                 :            :             /* Try with the max. count of characters with
     282                 :            :                additional overhead for replacing functionality */
     283                 :         10 :             nNotConvertedChars = nLength-nSrcChars;
     284                 :         10 :             nNewLen = nDestBytes+(nNotConvertedChars*nMaxCharLen)+nNotConvertedChars+4;
     285                 :            :         }
     286                 :            : 
     287                 :            :         /* Set the buffer to the correct size or is there to
     288                 :            :            much overhead, reallocate to the correct size */
     289         [ +  + ]:     566181 :         if ( nNewLen > nDestBytes+8 )
     290                 :            :         {
     291         [ +  - ]:        110 :             rtl_String* pTemp2 = IMPL_RTL_STRINGNAME( ImplAlloc )( nDestBytes );
     292                 :            :             OSL_ASSERT(pTemp2 != NULL);
     293         [ +  + ]:       1320 :             rtl_str_ImplCopy( pTemp2->buffer, pTemp->buffer, nDestBytes );
     294                 :        110 :             rtl_freeMemory( pTemp );
     295                 :        110 :             pTemp = pTemp2;
     296                 :            :         }
     297                 :            :         else
     298                 :            :         {
     299                 :     566071 :             pTemp->length = nDestBytes;
     300                 :     566071 :             pTemp->buffer[nDestBytes] = 0;
     301                 :            :         }
     302                 :            : 
     303         [ +  - ]:     566181 :         rtl_destroyUnicodeToTextConverter( hConverter );
     304         [ +  + ]:     566181 :         if ( *pTarget )
     305                 :        715 :             IMPL_RTL_STRINGNAME( release )( *pTarget );
     306                 :     566181 :         *pTarget = pTemp;
     307                 :            : 
     308                 :            :         /* Results the conversion in an empty buffer -
     309                 :            :            create an empty string */
     310 [ +  - ][ +  + ]:     566181 :         if ( pTemp && !nDestBytes )
     311                 :   12650427 :             rtl_string_new( pTarget );
     312                 :            :     }
     313                 :   13218120 :     return sal_True;
     314                 :            : }
     315                 :            : 
     316                 :   10336003 : void SAL_CALL rtl_uString2String( rtl_String** ppThis,
     317                 :            :                                   const sal_Unicode* pUStr,
     318                 :            :                                   sal_Int32 nULen,
     319                 :            :                                   rtl_TextEncoding eTextEncoding,
     320                 :            :                                   sal_uInt32 nCvtFlags )
     321                 :            :     SAL_THROW_EXTERN_C()
     322                 :            : {
     323                 :            :     rtl_impl_convertUStringToString(ppThis, pUStr, nULen, eTextEncoding,
     324                 :   10336003 :                                     nCvtFlags, sal_False);
     325                 :   10336004 : }
     326                 :            : 
     327                 :    2882115 : sal_Bool SAL_CALL rtl_convertUStringToString(rtl_String ** pTarget,
     328                 :            :                                              sal_Unicode const * pSource,
     329                 :            :                                              sal_Int32 nLength,
     330                 :            :                                              rtl_TextEncoding nEncoding,
     331                 :            :                                              sal_uInt32 nFlags)
     332                 :            :     SAL_THROW_EXTERN_C()
     333                 :            : {
     334                 :            :     return rtl_impl_convertUStringToString(pTarget, pSource, nLength, nEncoding,
     335                 :    2882115 :                                            nFlags, sal_True);
     336                 :            : }
     337                 :            : 
     338                 :      54942 : void rtl_string_newReplaceFirst(
     339                 :            :     rtl_String ** newStr, rtl_String * str, char const * from,
     340                 :            :     sal_Int32 fromLength, char const * to, sal_Int32 toLength,
     341                 :            :     sal_Int32 * index) SAL_THROW_EXTERN_C()
     342                 :            : {
     343                 :            :     assert(str != 0);
     344                 :            :     assert(index != 0);
     345                 :            :     assert(*index >= 0 && *index <= str->length);
     346                 :            :     assert(fromLength >= 0);
     347                 :            :     assert(toLength >= 0);
     348                 :            :     sal_Int32 i = rtl_str_indexOfStr_WithLength(
     349                 :      54942 :         str->buffer + *index, str->length - *index, from, fromLength);
     350         [ +  + ]:      54942 :     if (i == -1) {
     351                 :      53360 :         rtl_string_assign(newStr, str);
     352                 :            :     } else {
     353                 :            :         assert(i <= str->length - *index);
     354                 :       1582 :         i += *index;
     355                 :            :         assert(fromLength <= str->length);
     356         [ -  + ]:       1582 :         if (str->length - fromLength > SAL_MAX_INT32 - toLength) {
     357                 :          0 :             std::abort();
     358                 :            :         }
     359                 :       1582 :         sal_Int32 n = str->length - fromLength + toLength;
     360                 :       1582 :         rtl_string_acquire(str); // in case *newStr == str
     361                 :       1582 :         rtl_string_new_WithLength(newStr, n);
     362         [ +  + ]:       1582 :         if (n != 0) {
     363                 :       1577 :             (*newStr)->length = n;
     364                 :            :             assert(i >= 0 && i < str->length);
     365                 :       1577 :             memcpy((*newStr)->buffer, str->buffer, i);
     366                 :       1577 :             memcpy((*newStr)->buffer + i, to, toLength);
     367                 :            :             memcpy(
     368                 :       1577 :                 (*newStr)->buffer + i + toLength, str->buffer + i + fromLength,
     369                 :       1577 :                 str->length - i - fromLength);
     370                 :            :         }
     371                 :       1582 :         rtl_string_release(str);
     372                 :            :     }
     373                 :      54942 :     *index = i;
     374                 :      54942 : }
     375                 :            : 
     376                 :      53350 : void rtl_string_newReplaceAll(
     377                 :            :     rtl_String ** newStr, rtl_String * str, char const * from,
     378                 :            :     sal_Int32 fromLength, char const * to, sal_Int32 toLength)
     379                 :            :     SAL_THROW_EXTERN_C()
     380                 :            : {
     381                 :      53350 :     rtl_string_assign(newStr, str);
     382                 :      54917 :     for (sal_Int32 i = 0;; i += toLength) {
     383                 :            :         rtl_string_newReplaceFirst(
     384                 :      54917 :             newStr, *newStr, from, fromLength, to, toLength, &i);
     385         [ +  + ]:      54917 :         if (i == -1) {
     386                 :      53350 :             break;
     387                 :            :         }
     388                 :            :     }
     389                 :      53350 : }
     390                 :            : 
     391                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10