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 <string.h>
30 : :
31 : : #include <osl/interlck.h>
32 : : #include <rtl/strbuf.hxx>
33 : : #include <rtl/memory.h>
34 : :
35 : : /*************************************************************************
36 : : * rtl_stringbuffer_newFromStr_WithLength
37 : : */
38 : 205738 : void SAL_CALL rtl_stringbuffer_newFromStr_WithLength( rtl_String ** newStr,
39 : : const sal_Char * value,
40 : : sal_Int32 count )
41 : : {
42 [ - + ]: 205738 : if (!value)
43 : : {
44 : 0 : rtl_string_new_WithLength( newStr, 16 );
45 : 0 : return;
46 : : }
47 : :
48 : 205738 : rtl_string_new_WithLength( newStr, count + 16 );
49 : 205738 : (*newStr)->length = count;
50 : 205738 : memcpy( (*newStr)->buffer, value, count );
51 : 205738 : return;
52 : : }
53 : :
54 : : /*************************************************************************
55 : : * rtl_stringbuffer_newFromStringBuffer
56 : : */
57 : 18109 : sal_Int32 SAL_CALL rtl_stringbuffer_newFromStringBuffer( rtl_String ** newStr,
58 : : sal_Int32 capacity,
59 : : rtl_String * oldStr )
60 : : {
61 : 18109 : sal_Int32 newCapacity = capacity;
62 : :
63 [ - + ]: 18109 : if (newCapacity < oldStr->length)
64 : 0 : newCapacity = oldStr->length;
65 : :
66 : 18109 : rtl_string_new_WithLength( newStr, newCapacity );
67 [ + + ]: 18109 : if (oldStr->length > 0) {
68 : 17674 : (*newStr)->length = oldStr->length;
69 : 17674 : memcpy( (*newStr)->buffer, oldStr->buffer, oldStr->length );
70 : : }
71 : 18109 : return newCapacity;
72 : : }
73 : :
74 : : /*************************************************************************
75 : : * rtl_stringbuffer_ensureCapacity
76 : : */
77 : 1202212 : void SAL_CALL rtl_stringbuffer_ensureCapacity
78 : : (rtl_String ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
79 : : {
80 [ + + ]: 1202212 : if (minimumCapacity > *capacity)
81 : : {
82 : 1202172 : rtl_String * pTmp = *This;
83 : 1202172 : rtl_String * pNew = NULL;
84 : 1202172 : *capacity = ((*This)->length + 1) * 2;
85 [ + + ]: 1202172 : if (minimumCapacity > *capacity)
86 : : /* still lower, set to the minimum capacity */
87 : 519301 : *capacity = minimumCapacity;
88 : :
89 : 1202172 : rtl_string_new_WithLength(&pNew, *capacity);
90 : 1202172 : pNew->length = (*This)->length;
91 : 1202172 : *This = pNew;
92 : :
93 : 1202172 : memcpy( (*This)->buffer, pTmp->buffer, pTmp->length );
94 : 1202172 : rtl_string_release( pTmp );
95 : : }
96 : 1202212 : }
97 : :
98 : : /*************************************************************************
99 : : * rtl_stringbuffer_insert
100 : : */
101 : 47379374 : void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This,
102 : : sal_Int32 * capacity,
103 : : sal_Int32 offset,
104 : : const sal_Char * str,
105 : : sal_Int32 len )
106 : : {
107 : : sal_Int32 nOldLen;
108 : : sal_Char * pBuf;
109 : : sal_Int32 n;
110 [ + + ]: 47379374 : if( len != 0 )
111 : : {
112 [ + + ]: 47071251 : if (*capacity < (*This)->length + len)
113 : 1187916 : rtl_stringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
114 : :
115 : : /*
116 : : if( len == 1 )
117 : : This->buffer
118 : : */
119 : 47071251 : nOldLen = (*This)->length;
120 : 47071251 : pBuf = (*This)->buffer;
121 : :
122 : : /* copy the tail */
123 : 47071251 : n = (nOldLen - offset);
124 [ + + ]: 47071251 : if( n == 1 )
125 : : /* optimized for 1 character */
126 : 13613 : pBuf[offset + len] = pBuf[offset];
127 [ + + ]: 47057638 : else if( n > 1 )
128 : 68325 : rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Char) );
129 : :
130 : : /* insert the new characters */
131 : 47071251 : n = len;
132 [ + + ]: 47071251 : if( len == 1 )
133 : : /* optimized for 1 character */
134 : 36466513 : pBuf[offset] = *str;
135 [ + - ]: 10604738 : else if( n > 1 )
136 : 10604738 : memcpy( pBuf + offset, str, len * sizeof(sal_Char) );
137 : 47071251 : (*This)->length = nOldLen + len;
138 : 47071251 : pBuf[ nOldLen + len ] = 0;
139 : : }
140 : 47379374 : }
141 : :
142 : : /*************************************************************************
143 : : * rtl_stringbuffer_remove
144 : : */
145 : 2027 : void SAL_CALL rtl_stringbuffer_remove( rtl_String ** This,
146 : : sal_Int32 start,
147 : : sal_Int32 len )
148 : : {
149 : : sal_Int32 nTailLen;
150 : : sal_Char * pBuf;
151 : :
152 [ + + ]: 2027 : if (len > (*This)->length - start)
153 : 10 : len = (*This)->length - start;
154 : :
155 : : //remove nothing
156 [ - + ]: 2027 : if (!len)
157 : 2027 : return;
158 : :
159 : 2027 : pBuf = (*This)->buffer;
160 : 2027 : nTailLen = (*This)->length - ( start + len );
161 : :
162 [ + + ]: 2027 : if (nTailLen)
163 : : {
164 : : /* move the tail */
165 : 5 : rtl_moveMemory(pBuf + start, pBuf + start + len, nTailLen * sizeof(sal_Char));
166 : : }
167 : :
168 : 2027 : (*This)->length-=len;
169 : 2027 : pBuf[ (*This)->length ] = 0;
170 : : }
171 : :
172 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|