LCOV - code coverage report
Current view: top level - libreoffice/sal/rtl/source - ustrbuf.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 90 94 95.7 %
Date: 2012-12-17 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <string.h>
      21             : 
      22             : #include <osl/interlck.h>
      23             : 
      24             : #include <rtl/ustrbuf.hxx>
      25             : #include <strimp.hxx>
      26             : 
      27      557248 : void SAL_CALL rtl_uStringbuffer_newFromStr_WithLength( rtl_uString ** newStr,
      28             :                                                        const sal_Unicode * value,
      29             :                                                        sal_Int32 count)
      30             : {
      31      557248 :     if (!value)
      32             :     {
      33           0 :         rtl_uString_new_WithLength( newStr, 16 );
      34           0 :         return;
      35             :     }
      36             : 
      37      557248 :     rtl_uString_new_WithLength( newStr, count + 16 );
      38      557248 :     (*newStr)->length = count;
      39      557248 :     memcpy( (*newStr)->buffer, value, count * sizeof(sal_Unicode));
      40             :     RTL_LOG_STRING_NEW( *newStr );
      41      557248 :     return;
      42             : }
      43             : 
      44     2866081 : rtl_uString * SAL_CALL rtl_uStringBuffer_refReturn( rtl_uString * pThis )
      45             : {
      46             :     RTL_LOG_STRING_NEW( pThis );
      47     2866081 :     rtl_uString_acquire( pThis );
      48     2866081 :     return pThis;
      49             : }
      50             : 
      51     2518998 : rtl_uString * SAL_CALL rtl_uStringBuffer_makeStringAndClear( rtl_uString ** ppThis,
      52             :                                                              sal_Int32 *nCapacity )
      53             : {
      54             :     // avoid an un-necessary atomic ref/unref pair
      55     2518998 :     rtl_uString *pStr = *ppThis;
      56     2518998 :     *ppThis = NULL;
      57             : 
      58     2518998 :     rtl_uString_new (ppThis);
      59     2518998 :     *nCapacity = 0;
      60             : 
      61             :     RTL_LOG_STRING_NEW( pStr );
      62             : 
      63     2518998 :     return pStr;
      64             : }
      65             : 
      66      268217 : sal_Int32 SAL_CALL rtl_uStringbuffer_newFromStringBuffer( rtl_uString ** newStr,
      67             :                                                           sal_Int32 capacity,
      68             :                                                           rtl_uString * oldStr )
      69             : {
      70      268217 :     sal_Int32 newCapacity = capacity;
      71             : 
      72      268217 :     if (newCapacity < oldStr->length)
      73           0 :         newCapacity = oldStr->length;
      74             : 
      75      268217 :     rtl_uString_new_WithLength( newStr, newCapacity );
      76             : 
      77      268217 :     if (oldStr->length > 0) {
      78      183664 :         (*newStr)->length = oldStr->length;
      79      183664 :         memcpy( (*newStr)->buffer, oldStr->buffer, oldStr->length * sizeof(sal_Unicode));
      80             :     }
      81             :     RTL_LOG_STRING_NEW( *newStr );
      82      268217 :     return newCapacity;
      83             : }
      84             : 
      85      991191 : void SAL_CALL rtl_uStringbuffer_ensureCapacity
      86             :     (rtl_uString ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
      87             : {
      88      991191 :     if (minimumCapacity > *capacity)
      89             :     {
      90      990583 :         rtl_uString * pTmp = *This;
      91      990583 :         rtl_uString * pNew = NULL;
      92      990583 :         *capacity = ((*This)->length + 1) * 2;
      93      990583 :         if (minimumCapacity > *capacity)
      94             :             /* still lower, set to the minimum capacity */
      95      459654 :             *capacity = minimumCapacity;
      96             : 
      97      990583 :         rtl_uString_new_WithLength(&pNew, *capacity);
      98      990583 :         pNew->length = (*This)->length;
      99      990583 :         *This = pNew;
     100             : 
     101      990583 :         memcpy( (*This)->buffer, pTmp->buffer, pTmp->length * sizeof(sal_Unicode) );
     102             : 
     103             :         RTL_LOG_STRING_NEW( pTmp ); // with accurate contents
     104      990583 :         rtl_uString_release( pTmp );
     105             :     }
     106      991191 : }
     107             : 
     108    60773166 : void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This,
     109             :                                         sal_Int32 * capacity,
     110             :                                         sal_Int32 offset,
     111             :                                         const sal_Unicode * str,
     112             :                                         sal_Int32 len)
     113             : {
     114             :     sal_Int32 nOldLen;
     115             :     sal_Unicode * pBuf;
     116             :     sal_Int32 n;
     117    60773166 :     if( len != 0 )
     118             :     {
     119    60707316 :         if (*capacity < (*This)->length + len)
     120      865692 :             rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
     121             : 
     122             :         /*
     123             :         if( len == 1 )
     124             :             This->buffer
     125             :         */
     126    60707316 :         nOldLen = (*This)->length;
     127    60707316 :         pBuf = (*This)->buffer;
     128             : 
     129             :         /* copy the tail */
     130    60707316 :         n = (nOldLen - offset);
     131    60707316 :         if( n == 1 )
     132             :                             /* optimized for 1 character */
     133         404 :             pBuf[offset + len] = pBuf[offset];
     134    60706912 :         else if( n > 1 )
     135       18236 :             memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
     136             : 
     137             :         /* insert the new characters */
     138    60707316 :         if( len == 1 )
     139             :             /* optimized for 1 character */
     140    59204074 :             pBuf[offset] = *str;
     141     1503242 :         else if( len > 1 )
     142     1503242 :             memcpy( pBuf + offset, str, len * sizeof(sal_Unicode) );
     143    60707316 :         (*This)->length = nOldLen + len;
     144    60707316 :         pBuf[ nOldLen + len ] = 0;
     145             :     }
     146    60773166 : }
     147             : 
     148      554403 : void rtl_uStringbuffer_insertUtf32(
     149             :     rtl_uString ** pThis, sal_Int32 * capacity, sal_Int32 offset, sal_uInt32 c)
     150             :     SAL_THROW_EXTERN_C()
     151             : {
     152             :     sal_Unicode buf[2];
     153             :     sal_Int32 len;
     154             :     OSL_ASSERT(c <= 0x10FFFF && !(c >= 0xD800 && c <= 0xDFFF));
     155      554403 :     if (c <= 0xFFFF) {
     156      554399 :         buf[0] = (sal_Unicode) c;
     157      554399 :         len = 1;
     158             :     } else {
     159           4 :         c -= 0x10000;
     160           4 :         buf[0] = (sal_Unicode) ((c >> 10) | 0xD800);
     161           4 :         buf[1] = (sal_Unicode) ((c & 0x3FF) | 0xDC00);
     162           4 :         len = 2;
     163             :     }
     164      554403 :     rtl_uStringbuffer_insert(pThis, capacity, offset, buf, len);
     165      554403 : }
     166             : 
     167      762511 : void SAL_CALL rtl_uStringbuffer_insert_ascii(   /*inout*/rtl_uString ** This,
     168             :                                                 /*inout*/sal_Int32 * capacity,
     169             :                                                 sal_Int32 offset,
     170             :                                                 const sal_Char * str,
     171             :                                                 sal_Int32 len)
     172             : {
     173             :     sal_Int32 nOldLen;
     174             :     sal_Unicode * pBuf;
     175             :     sal_Int32 n;
     176      762511 :     if( len != 0 )
     177             :     {
     178      762509 :         if (*capacity < (*This)->length + len)
     179       22812 :             rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
     180             : 
     181      762509 :         nOldLen = (*This)->length;
     182      762509 :         pBuf = (*This)->buffer;
     183             : 
     184             :         /* copy the tail */
     185      762509 :         n = (nOldLen - offset);
     186      762509 :         if( n == 1 )
     187             :             /* optimized for 1 character */
     188          26 :             pBuf[offset + len] = pBuf[offset];
     189      762483 :         else if( n > 1 )
     190       15634 :             memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
     191             : 
     192             :         /* insert the new characters */
     193     4988036 :         for( n = 0; n < len; n++ )
     194             :         {
     195             :             /* Check ASCII range */
     196             :             OSL_ENSURE( (*str & 0x80) == 0, "Found ASCII char > 127");
     197             : 
     198     4225527 :             pBuf[offset + n] = (sal_Unicode)*(str++);
     199             :         }
     200             : 
     201      762509 :         (*This)->length = nOldLen + len;
     202      762509 :         pBuf[ nOldLen + len ] = 0;
     203             :     }
     204      762511 : }
     205             : 
     206             : /*************************************************************************
     207             :  *  rtl_uStringbuffer_remove
     208             :  */
     209       96074 : void SAL_CALL rtl_uStringbuffer_remove( rtl_uString ** This,
     210             :                                        sal_Int32 start,
     211             :                                        sal_Int32 len )
     212             : {
     213             :     sal_Int32 nTailLen;
     214             :     sal_Unicode * pBuf;
     215             : 
     216       96074 :     if (len > (*This)->length - start)
     217           0 :         len = (*This)->length - start;
     218             : 
     219             :     //remove nothing
     220       96074 :     if (!len)
     221       96074 :         return;
     222             : 
     223       96074 :     pBuf = (*This)->buffer;
     224       96074 :     nTailLen = (*This)->length - ( start + len );
     225             : 
     226       96074 :     if (nTailLen)
     227             :     {
     228             :         /* move the tail */
     229       15992 :         memmove(pBuf + start, pBuf + start + len, nTailLen * sizeof(sal_Unicode));
     230             :     }
     231             : 
     232       96074 :     (*This)->length-=len;
     233       96074 :     pBuf[ (*This)->length ] = 0;
     234             : }
     235             : 
     236             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10