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