LCOV - code coverage report
Current view: top level - libreoffice/sal/rtl/source - strbuf.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 57 60 95.0 %
Date: 2012-12-17 Functions: 5 5 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             : #include <rtl/strbuf.hxx>
      24             : 
      25             : /*************************************************************************
      26             :  *  rtl_stringbuffer_newFromStr_WithLength
      27             :  */
      28        8974 : void SAL_CALL rtl_stringbuffer_newFromStr_WithLength( rtl_String ** newStr,
      29             :                                                       const sal_Char * value,
      30             :                                                       sal_Int32 count )
      31             : {
      32        8974 :     if (!value)
      33             :     {
      34           0 :         rtl_string_new_WithLength( newStr, 16 );
      35           0 :         return;
      36             :     }
      37             : 
      38        8974 :     rtl_string_new_WithLength( newStr, count + 16 );
      39        8974 :     (*newStr)->length = count;
      40        8974 :     memcpy( (*newStr)->buffer, value, count );
      41        8974 :     return;
      42             : }
      43             : 
      44             : /*************************************************************************
      45             :  *  rtl_stringbuffer_newFromStringBuffer
      46             :  */
      47         756 : sal_Int32 SAL_CALL rtl_stringbuffer_newFromStringBuffer( rtl_String ** newStr,
      48             :                                                          sal_Int32 capacity,
      49             :                                                          rtl_String * oldStr )
      50             : {
      51         756 :     sal_Int32 newCapacity = capacity;
      52             : 
      53         756 :     if (newCapacity < oldStr->length)
      54           0 :         newCapacity = oldStr->length;
      55             : 
      56         756 :     rtl_string_new_WithLength( newStr, newCapacity );
      57         756 :     if (oldStr->length > 0) {
      58         522 :         (*newStr)->length = oldStr->length;
      59         522 :         memcpy( (*newStr)->buffer, oldStr->buffer, oldStr->length );
      60             :     }
      61         756 :     return newCapacity;
      62             : }
      63             : 
      64             : /*************************************************************************
      65             :  *  rtl_stringbuffer_ensureCapacity
      66             :  */
      67       73823 : void SAL_CALL rtl_stringbuffer_ensureCapacity
      68             :     (rtl_String ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
      69             : {
      70       73823 :     if (minimumCapacity > *capacity)
      71             :     {
      72       73805 :         rtl_String * pTmp = *This;
      73       73805 :         rtl_String * pNew = NULL;
      74       73805 :         *capacity = ((*This)->length + 1) * 2;
      75       73805 :         if (minimumCapacity > *capacity)
      76             :             /* still lower, set to the minimum capacity */
      77       38014 :             *capacity = minimumCapacity;
      78             : 
      79       73805 :         rtl_string_new_WithLength(&pNew, *capacity);
      80       73805 :         pNew->length = (*This)->length;
      81       73805 :         *This = pNew;
      82             : 
      83       73805 :         memcpy( (*This)->buffer, pTmp->buffer, pTmp->length );
      84       73805 :         rtl_string_release( pTmp );
      85             :     }
      86       73823 : }
      87             : 
      88             : /*************************************************************************
      89             :  *  rtl_stringbuffer_insert
      90             :  */
      91     3388894 : void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This,
      92             :                                        sal_Int32 * capacity,
      93             :                                        sal_Int32 offset,
      94             :                                        const sal_Char * str,
      95             :                                        sal_Int32 len )
      96             : {
      97             :     sal_Int32 nOldLen;
      98             :     sal_Char * pBuf;
      99             :     sal_Int32 n;
     100     3388894 :     if( len != 0 )
     101             :     {
     102     3385266 :         if (*capacity < (*This)->length + len)
     103       66599 :             rtl_stringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
     104             : 
     105             :         /*
     106             :         if( len == 1 )
     107             :             This->buffer
     108             :         */
     109     3385266 :         nOldLen = (*This)->length;
     110     3385266 :         pBuf = (*This)->buffer;
     111             : 
     112             :         /* copy the tail */
     113     3385266 :         n = (nOldLen - offset);
     114     3385266 :         if( n == 1 )
     115             :             /* optimized for 1 character */
     116        3614 :             pBuf[offset + len] = pBuf[offset];
     117     3381652 :         else if( n > 1 )
     118       21142 :             memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Char) );
     119             : 
     120             :         /* insert the new characters */
     121     3385266 :         n = len;
     122     3385266 :         if( len == 1 )
     123             :                             /* optimized for 1 character */
     124     2915862 :             pBuf[offset] = *str;
     125      469404 :         else if( n > 1 )
     126      469404 :             memcpy( pBuf + offset, str, len * sizeof(sal_Char) );
     127     3385266 :         (*This)->length = nOldLen + len;
     128     3385266 :         pBuf[ nOldLen + len ] = 0;
     129             :     }
     130     3388894 : }
     131             : 
     132             : /*************************************************************************
     133             :  *  rtl_stringbuffer_remove
     134             :  */
     135          10 : void SAL_CALL rtl_stringbuffer_remove( rtl_String ** This,
     136             :                                        sal_Int32 start,
     137             :                                        sal_Int32 len )
     138             : {
     139             :     sal_Int32 nTailLen;
     140             :     sal_Char * pBuf;
     141             : 
     142          10 :     if (len > (*This)->length - start)
     143           4 :         len = (*This)->length - start;
     144             : 
     145             :     //remove nothing
     146          10 :     if (!len)
     147          10 :         return;
     148             : 
     149          10 :     pBuf = (*This)->buffer;
     150          10 :     nTailLen = (*This)->length - ( start + len );
     151             : 
     152          10 :     if (nTailLen)
     153             :     {
     154             :         /* move the tail */
     155           2 :         memmove(pBuf + start, pBuf + start + len, nTailLen * sizeof(sal_Char));
     156             :     }
     157             : 
     158          10 :     (*This)->length-=len;
     159          10 :     pBuf[ (*This)->length ] = 0;
     160             : }
     161             : 
     162             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10