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 : #if USE_SDT_PROBES
28 : #define RTL_LOG_STRING_BITS 16
29 : #endif
30 :
31 675 : void SAL_CALL rtl_uStringbuffer_newFromStr_WithLength( rtl_uString ** newStr,
32 : const sal_Unicode * value,
33 : sal_Int32 count)
34 : {
35 675 : if (!value)
36 : {
37 0 : rtl_uString_new_WithLength( newStr, 16 );
38 0 : return;
39 : }
40 :
41 675 : rtl_uString_new_WithLength( newStr, count + 16 );
42 675 : (*newStr)->length = count;
43 675 : memcpy( (*newStr)->buffer, value, count * sizeof(sal_Unicode));
44 : RTL_LOG_STRING_NEW( *newStr );
45 675 : return;
46 : }
47 :
48 0 : rtl_uString * SAL_CALL rtl_uStringBuffer_refReturn( rtl_uString * pThis )
49 : {
50 : RTL_LOG_STRING_NEW( pThis );
51 0 : rtl_uString_acquire( pThis );
52 0 : return pThis;
53 : }
54 :
55 7951911 : rtl_uString * SAL_CALL rtl_uStringBuffer_makeStringAndClear( rtl_uString ** ppThis,
56 : sal_Int32 *nCapacity )
57 : {
58 : // avoid an un-necessary atomic ref/unref pair
59 7951911 : rtl_uString *pStr = *ppThis;
60 7951911 : *ppThis = NULL;
61 :
62 7951911 : rtl_uString_new (ppThis);
63 7951911 : *nCapacity = 0;
64 :
65 : RTL_LOG_STRING_NEW( pStr );
66 :
67 7951911 : return pStr;
68 : }
69 :
70 0 : sal_Int32 SAL_CALL rtl_uStringbuffer_newFromStringBuffer( rtl_uString ** newStr,
71 : sal_Int32 capacity,
72 : rtl_uString * oldStr )
73 : {
74 0 : sal_Int32 newCapacity = capacity;
75 :
76 0 : if (newCapacity < oldStr->length)
77 0 : newCapacity = oldStr->length;
78 :
79 0 : rtl_uString_new_WithLength( newStr, newCapacity );
80 :
81 0 : if (oldStr->length > 0) {
82 0 : (*newStr)->length = oldStr->length;
83 0 : memcpy( (*newStr)->buffer, oldStr->buffer, oldStr->length * sizeof(sal_Unicode));
84 : }
85 : RTL_LOG_STRING_NEW( *newStr );
86 0 : return newCapacity;
87 : }
88 :
89 3869109 : void SAL_CALL rtl_uStringbuffer_ensureCapacity
90 : (rtl_uString ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
91 : {
92 3869109 : if (minimumCapacity > *capacity)
93 : {
94 3869108 : rtl_uString * pTmp = *This;
95 3869108 : rtl_uString * pNew = NULL;
96 3869108 : *capacity = ((*This)->length + 1) * 2;
97 3869108 : if (minimumCapacity > *capacity)
98 : /* still lower, set to the minimum capacity */
99 1742996 : *capacity = minimumCapacity;
100 :
101 3869108 : rtl_uString_new_WithLength(&pNew, *capacity);
102 3869108 : pNew->length = (*This)->length;
103 3869108 : *This = pNew;
104 :
105 3869108 : memcpy( (*This)->buffer, pTmp->buffer, pTmp->length * sizeof(sal_Unicode) );
106 :
107 : RTL_LOG_STRING_NEW( pTmp ); // with accurate contents
108 3869108 : rtl_uString_release( pTmp );
109 : }
110 3869109 : }
111 :
112 256989531 : void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This,
113 : sal_Int32 * capacity,
114 : sal_Int32 offset,
115 : const sal_Unicode * str,
116 : sal_Int32 len)
117 : {
118 : sal_Int32 nOldLen;
119 : sal_Unicode * pBuf;
120 : sal_Int32 n;
121 256989531 : if( len != 0 )
122 : {
123 256946995 : if (*capacity < (*This)->length + len)
124 3869107 : rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
125 :
126 : /*
127 : if( len == 1 )
128 : This->buffer
129 : */
130 256946995 : nOldLen = (*This)->length;
131 256946995 : pBuf = (*This)->buffer;
132 :
133 : /* copy the tail */
134 256946995 : n = (nOldLen - offset);
135 256946995 : if( n == 1 )
136 : /* optimized for 1 character */
137 0 : pBuf[offset + len] = pBuf[offset];
138 256946995 : else if( n > 1 )
139 0 : memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
140 :
141 : /* insert the new characters */
142 256946995 : if( len == 1 )
143 : /* optimized for 1 character */
144 254012609 : pBuf[offset] = *str;
145 2934386 : else if( len > 1 )
146 2934386 : memcpy( pBuf + offset, str, len * sizeof(sal_Unicode) );
147 256946995 : (*This)->length = nOldLen + len;
148 256946995 : pBuf[ nOldLen + len ] = 0;
149 : }
150 256989531 : }
151 :
152 0 : void rtl_uStringbuffer_insertUtf32(
153 : rtl_uString ** pThis, sal_Int32 * capacity, sal_Int32 offset, sal_uInt32 c)
154 : SAL_THROW_EXTERN_C()
155 : {
156 : sal_Unicode buf[2];
157 : sal_Int32 len;
158 : OSL_ASSERT(c <= 0x10FFFF && !(c >= 0xD800 && c <= 0xDFFF));
159 0 : if (c <= 0xFFFF) {
160 0 : buf[0] = (sal_Unicode) c;
161 0 : len = 1;
162 : } else {
163 0 : c -= 0x10000;
164 0 : buf[0] = (sal_Unicode) ((c >> 10) | 0xD800);
165 0 : buf[1] = (sal_Unicode) ((c & 0x3FF) | 0xDC00);
166 0 : len = 2;
167 : }
168 0 : rtl_uStringbuffer_insert(pThis, capacity, offset, buf, len);
169 0 : }
170 :
171 807842 : void SAL_CALL rtl_uStringbuffer_insert_ascii( /*inout*/rtl_uString ** This,
172 : /*inout*/sal_Int32 * capacity,
173 : sal_Int32 offset,
174 : const sal_Char * str,
175 : sal_Int32 len)
176 : {
177 : sal_Int32 nOldLen;
178 : sal_Unicode * pBuf;
179 : sal_Int32 n;
180 807842 : if( len != 0 )
181 : {
182 807842 : if (*capacity < (*This)->length + len)
183 0 : rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
184 :
185 807842 : nOldLen = (*This)->length;
186 807842 : pBuf = (*This)->buffer;
187 :
188 : /* copy the tail */
189 807842 : n = (nOldLen - offset);
190 807842 : if( n == 1 )
191 : /* optimized for 1 character */
192 0 : pBuf[offset + len] = pBuf[offset];
193 807842 : else if( n > 1 )
194 0 : memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
195 :
196 : /* insert the new characters */
197 5016301 : for( n = 0; n < len; n++ )
198 : {
199 : /* Check ASCII range */
200 : OSL_ENSURE( (*str & 0x80) == 0, "Found ASCII char > 127");
201 :
202 4208459 : pBuf[offset + n] = (sal_Unicode)*(str++);
203 : }
204 :
205 807842 : (*This)->length = nOldLen + len;
206 807842 : pBuf[ nOldLen + len ] = 0;
207 : }
208 807842 : }
209 :
210 : /*************************************************************************
211 : * rtl_uStringbuffer_remove
212 : */
213 0 : void SAL_CALL rtl_uStringbuffer_remove( rtl_uString ** This,
214 : sal_Int32 start,
215 : sal_Int32 len )
216 : {
217 : sal_Int32 nTailLen;
218 : sal_Unicode * pBuf;
219 :
220 0 : if (len > (*This)->length - start)
221 0 : len = (*This)->length - start;
222 :
223 : //remove nothing
224 0 : if (!len)
225 0 : return;
226 :
227 0 : pBuf = (*This)->buffer;
228 0 : nTailLen = (*This)->length - ( start + len );
229 :
230 0 : if (nTailLen)
231 : {
232 : /* move the tail */
233 0 : memmove(pBuf + start, pBuf + start + len, nTailLen * sizeof(sal_Unicode));
234 : }
235 :
236 0 : (*This)->length-=len;
237 0 : pBuf[ (*This)->length ] = 0;
238 : }
239 :
240 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|